blob: dd5a827f9cb0eab7be01a1449d1bf007e766ba5f [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 Berg2740f0c2014-09-03 15:24:58 +03005 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg55682962007-09-20 13:09:35 -04006 */
7
8#include <linux/if.h>
9#include <linux/module.h>
10#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040012#include <linux/list.h>
13#include <linux/if_ether.h>
14#include <linux/ieee80211.h>
15#include <linux/nl80211.h>
16#include <linux/rtnetlink.h>
17#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010018#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020019#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040020#include <net/genetlink.h>
21#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020022#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010023#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040024#include "core.h"
25#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070026#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030027#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040028
Jouni Malinen5fb628e2011-08-10 23:54:35 +030029static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
30 struct genl_info *info,
31 struct cfg80211_crypto_settings *settings,
32 int cipher_limit);
33
Johannes Bergf84f7712013-11-14 17:14:45 +010034static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020035 struct genl_info *info);
Johannes Bergf84f7712013-11-14 17:14:45 +010036static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020037 struct genl_info *info);
38
Johannes Berg55682962007-09-20 13:09:35 -040039/* the netlink family */
40static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070041 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
42 .name = NL80211_GENL_NAME, /* have users key off the name instead */
43 .hdrsize = 0, /* no private header */
44 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040045 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020046 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020047 .pre_doit = nl80211_pre_doit,
48 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040049};
50
Johannes Berg2a94fe42013-11-19 15:19:39 +010051/* multicast groups */
52enum nl80211_multicast_groups {
53 NL80211_MCGRP_CONFIG,
54 NL80211_MCGRP_SCAN,
55 NL80211_MCGRP_REGULATORY,
56 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010057 NL80211_MCGRP_VENDOR,
Johannes Berg2a94fe42013-11-19 15:19:39 +010058 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
59};
60
61static const struct genl_multicast_group nl80211_mcgrps[] = {
62 [NL80211_MCGRP_CONFIG] = { .name = "config", },
63 [NL80211_MCGRP_SCAN] = { .name = "scan", },
64 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
65 [NL80211_MCGRP_MLME] = { .name = "mlme", },
Johannes Berg567ffc32013-12-18 14:43:31 +010066 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
Johannes Berg2a94fe42013-11-19 15:19:39 +010067#ifdef CONFIG_NL80211_TESTMODE
68 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
69#endif
70};
71
Johannes Berg89a54e42012-06-15 14:33:17 +020072/* returns ERR_PTR values */
73static struct wireless_dev *
74__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040075{
Johannes Berg89a54e42012-06-15 14:33:17 +020076 struct cfg80211_registered_device *rdev;
77 struct wireless_dev *result = NULL;
78 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
79 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
80 u64 wdev_id;
81 int wiphy_idx = -1;
82 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040083
Johannes Berg5fe231e2013-05-08 21:45:15 +020084 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040085
Johannes Berg89a54e42012-06-15 14:33:17 +020086 if (!have_ifidx && !have_wdev_id)
87 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040088
Johannes Berg89a54e42012-06-15 14:33:17 +020089 if (have_ifidx)
90 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
91 if (have_wdev_id) {
92 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
93 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040094 }
95
Johannes Berg89a54e42012-06-15 14:33:17 +020096 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
97 struct wireless_dev *wdev;
98
99 if (wiphy_net(&rdev->wiphy) != netns)
100 continue;
101
102 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
103 continue;
104
Johannes Berg89a54e42012-06-15 14:33:17 +0200105 list_for_each_entry(wdev, &rdev->wdev_list, list) {
106 if (have_ifidx && wdev->netdev &&
107 wdev->netdev->ifindex == ifidx) {
108 result = wdev;
109 break;
110 }
111 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
112 result = wdev;
113 break;
114 }
115 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200116
117 if (result)
118 break;
119 }
120
121 if (result)
122 return result;
123 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400124}
125
Johannes Berga9455402012-06-15 13:32:49 +0200126static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200127__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200128{
Johannes Berg7fee4772012-06-15 14:09:58 +0200129 struct cfg80211_registered_device *rdev = NULL, *tmp;
130 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200131
Johannes Berg5fe231e2013-05-08 21:45:15 +0200132 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200133
Johannes Berg878d9ec2012-06-15 14:18:32 +0200134 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200135 !attrs[NL80211_ATTR_IFINDEX] &&
136 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200137 return ERR_PTR(-EINVAL);
138
Johannes Berg878d9ec2012-06-15 14:18:32 +0200139 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200140 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200141 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200142
Johannes Berg89a54e42012-06-15 14:33:17 +0200143 if (attrs[NL80211_ATTR_WDEV]) {
144 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
145 struct wireless_dev *wdev;
146 bool found = false;
147
148 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
149 if (tmp) {
150 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200151 list_for_each_entry(wdev, &tmp->wdev_list, list) {
152 if (wdev->identifier != (u32)wdev_id)
153 continue;
154 found = true;
155 break;
156 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200157
158 if (!found)
159 tmp = NULL;
160
161 if (rdev && tmp != rdev)
162 return ERR_PTR(-EINVAL);
163 rdev = tmp;
164 }
165 }
166
Johannes Berg878d9ec2012-06-15 14:18:32 +0200167 if (attrs[NL80211_ATTR_IFINDEX]) {
168 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Ying Xue7f2b8562014-01-15 10:23:45 +0800169 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200170 if (netdev) {
171 if (netdev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800172 tmp = wiphy_to_rdev(
173 netdev->ieee80211_ptr->wiphy);
Johannes Berg7fee4772012-06-15 14:09:58 +0200174 else
175 tmp = NULL;
176
Johannes Berg7fee4772012-06-15 14:09:58 +0200177 /* not wireless device -- return error */
178 if (!tmp)
179 return ERR_PTR(-EINVAL);
180
181 /* mismatch -- return error */
182 if (rdev && tmp != rdev)
183 return ERR_PTR(-EINVAL);
184
185 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200186 }
Johannes Berga9455402012-06-15 13:32:49 +0200187 }
188
Johannes Berg4f7eff12012-06-15 14:14:22 +0200189 if (!rdev)
190 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200191
Johannes Berg4f7eff12012-06-15 14:14:22 +0200192 if (netns != wiphy_net(&rdev->wiphy))
193 return ERR_PTR(-ENODEV);
194
195 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200196}
197
198/*
199 * This function returns a pointer to the driver
200 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200201 *
202 * The result of this can be a PTR_ERR and hence must
203 * be checked with IS_ERR() for errors.
204 */
205static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200206cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200207{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200208 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209}
210
Johannes Berg55682962007-09-20 13:09:35 -0400211/* policy for the attributes */
Luciano Coelho8cd4d452014-09-17 11:55:28 +0300212static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
Johannes Berg55682962007-09-20 13:09:35 -0400213 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
214 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700215 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200216 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100217
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200218 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530219 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100220 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
222 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
223
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200224 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
226 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
227 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100228 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +0200229 [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400230
231 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
232 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
233 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100234
Eliad Pellere007b852011-11-24 18:13:56 +0200235 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
236 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Johannes Bergb9454e82009-07-08 13:29:08 +0200238 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100239 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
240 .len = WLAN_MAX_KEY_LEN },
241 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
242 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
243 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200244 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200245 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100246
247 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
248 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
249 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
251 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100253 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
255 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
256 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
257 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100258 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100259 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200260 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800262 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100263 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300264
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700265 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
266 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
267
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300268 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
269 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
270 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200271 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
272 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100273 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300274
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800275 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700276 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700277
Johannes Berg6c739412011-11-03 09:27:01 +0100278 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200279
280 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
281 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
282 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100283 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
284 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200285
286 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
287 .len = IEEE80211_MAX_SSID_LEN },
288 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
289 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200290 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300291 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300292 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300293 [NL80211_ATTR_STA_FLAGS2] = {
294 .len = sizeof(struct nl80211_sta_flag_update),
295 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300296 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300297 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
298 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200299 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
300 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
301 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200302 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100303 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100304 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
305 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100306 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
307 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200308 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200309 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
310 .len = IEEE80211_MAX_DATA_LEN },
311 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200312 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200313 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300314 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200315 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300316 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
317 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200318 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900319 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100321 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100322 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100323 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200324 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700325 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300326 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200327 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200328 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300329 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300330 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
332 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
333 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530334 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300335 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530336 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300337 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
339 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
340 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
341 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Arik Nemtsov31fa97c2014-06-11 17:18:21 +0300342 [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100343 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200344 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
345 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700346 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800347 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
348 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
349 .len = NL80211_HT_CAPABILITY_LEN
350 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100351 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530352 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530353 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200354 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700355 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300356 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000357 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700358 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100359 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
360 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530361 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
362 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200363 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
364 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100365 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100366 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
367 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
368 .len = NL80211_VHT_CAPABILITY_LEN,
369 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200370 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
371 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
372 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300373 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200374 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
375 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
376 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +0300377 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
378 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530379 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
380 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200381 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100382 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100383 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
384 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
385 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800386 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
387 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200388 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
389 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530390 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Johannes Berg78f22b62014-03-24 17:57:27 +0100391 [NL80211_ATTR_IFACE_SOCKET_OWNER] = { .type = NLA_FLAG },
Andrei Otcheretianski34d22ce2014-05-09 14:11:44 +0300392 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
Assaf Kraussbab5ab72014-09-03 15:25:01 +0300393 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
Johannes Berg960d01a2014-09-09 22:55:35 +0300394 [NL80211_ATTR_TSID] = { .type = NLA_U8 },
395 [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
396 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
Eliad Peller18998c32014-09-10 14:07:34 +0300397 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
Johannes Bergad2b26a2014-06-12 21:39:05 +0200398 [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
Johannes Berg55682962007-09-20 13:09:35 -0400399};
400
Johannes Berge31b8212010-10-05 19:39:30 +0200401/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000402static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200403 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200404 [NL80211_KEY_IDX] = { .type = NLA_U8 },
405 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200406 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200407 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
408 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200409 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100410 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
411};
412
413/* policy for the key default flags */
414static const struct nla_policy
415nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
416 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
417 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200418};
419
Johannes Bergff1b6e62011-05-04 15:37:28 +0200420/* policy for WoWLAN attributes */
421static const struct nla_policy
422nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
423 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
424 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
425 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
426 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200427 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
428 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
429 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
430 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100431 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
Luciano Coelho8cd4d452014-09-17 11:55:28 +0300432 [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100433};
434
435static const struct nla_policy
436nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
437 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
438 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
439 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
440 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
441 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
442 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
443 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
444 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
445 },
446 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
447 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
448 },
449 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
450 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
451 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200452};
453
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700454/* policy for coalesce rule attributes */
455static const struct nla_policy
456nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
457 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
458 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
459 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
460};
461
Johannes Berge5497d72011-07-05 16:35:40 +0200462/* policy for GTK rekey offload attributes */
463static const struct nla_policy
464nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
465 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
466 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
467 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
468};
469
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300470static const struct nla_policy
471nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200472 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300473 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700474 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300475};
476
Johannes Berg97990a02013-04-19 01:02:55 +0200477static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
478 struct netlink_callback *cb,
479 struct cfg80211_registered_device **rdev,
480 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100481{
Johannes Berg67748892010-10-04 21:14:06 +0200482 int err;
483
Johannes Berg67748892010-10-04 21:14:06 +0200484 rtnl_lock();
485
Johannes Berg97990a02013-04-19 01:02:55 +0200486 if (!cb->args[0]) {
487 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
488 nl80211_fam.attrbuf, nl80211_fam.maxattr,
489 nl80211_policy);
490 if (err)
491 goto out_unlock;
492
493 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
494 nl80211_fam.attrbuf);
495 if (IS_ERR(*wdev)) {
496 err = PTR_ERR(*wdev);
497 goto out_unlock;
498 }
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800499 *rdev = wiphy_to_rdev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200500 /* 0 is the first index - add 1 to parse only once */
501 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200502 cb->args[1] = (*wdev)->identifier;
503 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200504 /* subtract the 1 again here */
505 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200506 struct wireless_dev *tmp;
507
508 if (!wiphy) {
509 err = -ENODEV;
510 goto out_unlock;
511 }
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800512 *rdev = wiphy_to_rdev(wiphy);
Johannes Berg97990a02013-04-19 01:02:55 +0200513 *wdev = NULL;
514
Johannes Berg97990a02013-04-19 01:02:55 +0200515 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
516 if (tmp->identifier == cb->args[1]) {
517 *wdev = tmp;
518 break;
519 }
520 }
Johannes Berg97990a02013-04-19 01:02:55 +0200521
522 if (!*wdev) {
523 err = -ENODEV;
524 goto out_unlock;
525 }
Johannes Berg67748892010-10-04 21:14:06 +0200526 }
527
Johannes Berg67748892010-10-04 21:14:06 +0200528 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200529 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200530 rtnl_unlock();
531 return err;
532}
533
Johannes Berg97990a02013-04-19 01:02:55 +0200534static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200535{
Johannes Berg67748892010-10-04 21:14:06 +0200536 rtnl_unlock();
537}
538
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100539/* IE validation */
540static bool is_valid_ie_attr(const struct nlattr *attr)
541{
542 const u8 *pos;
543 int len;
544
545 if (!attr)
546 return true;
547
548 pos = nla_data(attr);
549 len = nla_len(attr);
550
551 while (len) {
552 u8 elemlen;
553
554 if (len < 2)
555 return false;
556 len -= 2;
557
558 elemlen = pos[1];
559 if (elemlen > len)
560 return false;
561
562 len -= elemlen;
563 pos += 2 + elemlen;
564 }
565
566 return true;
567}
568
Johannes Berg55682962007-09-20 13:09:35 -0400569/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000570static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400571 int flags, u8 cmd)
572{
573 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000574 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400575}
576
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400577static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100578 struct ieee80211_channel *chan,
579 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400580{
Rostislav Lisovyea077c12014-04-15 14:37:55 +0200581 /* Some channels must be completely excluded from the
582 * list to protect old user-space tools from breaking
583 */
584 if (!large && chan->flags &
585 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
586 return 0;
587
David S. Miller9360ffd2012-03-29 04:41:26 -0400588 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
589 chan->center_freq))
590 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400591
David S. Miller9360ffd2012-03-29 04:41:26 -0400592 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
593 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
594 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200595 if (chan->flags & IEEE80211_CHAN_NO_IR) {
596 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
597 goto nla_put_failure;
598 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
599 goto nla_put_failure;
600 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100601 if (chan->flags & IEEE80211_CHAN_RADAR) {
602 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
603 goto nla_put_failure;
604 if (large) {
605 u32 time;
606
607 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
608
609 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
610 chan->dfs_state))
611 goto nla_put_failure;
612 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
613 time))
614 goto nla_put_failure;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100615 if (nla_put_u32(msg,
616 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
617 chan->dfs_cac_ms))
618 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100619 }
620 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400621
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100622 if (large) {
623 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
624 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
625 goto nla_put_failure;
626 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
627 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
628 goto nla_put_failure;
629 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
630 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
631 goto nla_put_failure;
632 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
633 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
634 goto nla_put_failure;
David Spinadel570dbde2014-02-23 09:12:59 +0200635 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
636 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
637 goto nla_put_failure;
638 if ((chan->flags & IEEE80211_CHAN_GO_CONCURRENT) &&
639 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_GO_CONCURRENT))
640 goto nla_put_failure;
Rostislav Lisovyea077c12014-04-15 14:37:55 +0200641 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
642 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
643 goto nla_put_failure;
644 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
645 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
646 goto nla_put_failure;
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100647 }
648
David S. Miller9360ffd2012-03-29 04:41:26 -0400649 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
650 DBM_TO_MBM(chan->max_power)))
651 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400652
653 return 0;
654
655 nla_put_failure:
656 return -ENOBUFS;
657}
658
Johannes Berg55682962007-09-20 13:09:35 -0400659/* netlink command implementations */
660
Johannes Bergb9454e82009-07-08 13:29:08 +0200661struct key_parse {
662 struct key_params p;
663 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200664 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200665 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100666 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200667};
668
669static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
670{
671 struct nlattr *tb[NL80211_KEY_MAX + 1];
672 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
673 nl80211_key_policy);
674 if (err)
675 return err;
676
677 k->def = !!tb[NL80211_KEY_DEFAULT];
678 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
679
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100680 if (k->def) {
681 k->def_uni = true;
682 k->def_multi = true;
683 }
684 if (k->defmgmt)
685 k->def_multi = true;
686
Johannes Bergb9454e82009-07-08 13:29:08 +0200687 if (tb[NL80211_KEY_IDX])
688 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
689
690 if (tb[NL80211_KEY_DATA]) {
691 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
692 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
693 }
694
695 if (tb[NL80211_KEY_SEQ]) {
696 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
697 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
698 }
699
700 if (tb[NL80211_KEY_CIPHER])
701 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
702
Johannes Berge31b8212010-10-05 19:39:30 +0200703 if (tb[NL80211_KEY_TYPE]) {
704 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
705 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
706 return -EINVAL;
707 }
708
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100709 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
710 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100711 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
712 tb[NL80211_KEY_DEFAULT_TYPES],
713 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (err)
715 return err;
716
717 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
718 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
719 }
720
Johannes Bergb9454e82009-07-08 13:29:08 +0200721 return 0;
722}
723
724static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
725{
726 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
727 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
728 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
729 }
730
731 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
732 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
733 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
734 }
735
736 if (info->attrs[NL80211_ATTR_KEY_IDX])
737 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
738
739 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
740 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
741
742 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
743 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
744
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100745 if (k->def) {
746 k->def_uni = true;
747 k->def_multi = true;
748 }
749 if (k->defmgmt)
750 k->def_multi = true;
751
Johannes Berge31b8212010-10-05 19:39:30 +0200752 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
753 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
754 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
755 return -EINVAL;
756 }
757
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100758 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
759 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
760 int err = nla_parse_nested(
761 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
762 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
763 nl80211_key_default_policy);
764 if (err)
765 return err;
766
767 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
768 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
769 }
770
Johannes Bergb9454e82009-07-08 13:29:08 +0200771 return 0;
772}
773
774static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
775{
776 int err;
777
778 memset(k, 0, sizeof(*k));
779 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200780 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200781
782 if (info->attrs[NL80211_ATTR_KEY])
783 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
784 else
785 err = nl80211_parse_key_old(info, k);
786
787 if (err)
788 return err;
789
790 if (k->def && k->defmgmt)
791 return -EINVAL;
792
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100793 if (k->defmgmt) {
794 if (k->def_uni || !k->def_multi)
795 return -EINVAL;
796 }
797
Johannes Bergb9454e82009-07-08 13:29:08 +0200798 if (k->idx != -1) {
799 if (k->defmgmt) {
800 if (k->idx < 4 || k->idx > 5)
801 return -EINVAL;
802 } else if (k->def) {
803 if (k->idx < 0 || k->idx > 3)
804 return -EINVAL;
805 } else {
806 if (k->idx < 0 || k->idx > 5)
807 return -EINVAL;
808 }
809 }
810
811 return 0;
812}
813
Johannes Bergfffd0932009-07-08 14:22:54 +0200814static struct cfg80211_cached_keys *
815nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530816 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200817{
818 struct key_parse parse;
819 struct nlattr *key;
820 struct cfg80211_cached_keys *result;
821 int rem, err, def = 0;
822
823 result = kzalloc(sizeof(*result), GFP_KERNEL);
824 if (!result)
825 return ERR_PTR(-ENOMEM);
826
827 result->def = -1;
828 result->defmgmt = -1;
829
830 nla_for_each_nested(key, keys, rem) {
831 memset(&parse, 0, sizeof(parse));
832 parse.idx = -1;
833
834 err = nl80211_parse_key_new(key, &parse);
835 if (err)
836 goto error;
837 err = -EINVAL;
838 if (!parse.p.key)
839 goto error;
840 if (parse.idx < 0 || parse.idx > 4)
841 goto error;
842 if (parse.def) {
843 if (def)
844 goto error;
845 def = 1;
846 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100847 if (!parse.def_uni || !parse.def_multi)
848 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 } else if (parse.defmgmt)
850 goto error;
851 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200852 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 if (err)
854 goto error;
855 result->params[parse.idx].cipher = parse.p.cipher;
856 result->params[parse.idx].key_len = parse.p.key_len;
857 result->params[parse.idx].key = result->data[parse.idx];
858 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530859
860 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
861 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
862 if (no_ht)
863 *no_ht = true;
864 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200865 }
866
867 return result;
868 error:
869 kfree(result);
870 return ERR_PTR(err);
871}
872
873static int nl80211_key_allowed(struct wireless_dev *wdev)
874{
875 ASSERT_WDEV_LOCK(wdev);
876
Johannes Bergfffd0932009-07-08 14:22:54 +0200877 switch (wdev->iftype) {
878 case NL80211_IFTYPE_AP:
879 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200880 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700881 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200882 break;
883 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200884 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200885 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200886 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200887 return -ENOLINK;
888 break;
Johannes Bergde4fcba2014-10-31 14:16:12 +0100889 case NL80211_IFTYPE_UNSPECIFIED:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100890 case NL80211_IFTYPE_OCB:
Johannes Bergde4fcba2014-10-31 14:16:12 +0100891 case NL80211_IFTYPE_MONITOR:
892 case NL80211_IFTYPE_P2P_DEVICE:
893 case NL80211_IFTYPE_WDS:
894 case NUM_NL80211_IFTYPES:
Johannes Bergfffd0932009-07-08 14:22:54 +0200895 return -EINVAL;
896 }
897
898 return 0;
899}
900
Jouni Malinen664834d2014-01-15 00:01:44 +0200901static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
902 struct nlattr *tb)
903{
904 struct ieee80211_channel *chan;
905
906 if (tb == NULL)
907 return NULL;
908 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
909 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
910 return NULL;
911 return chan;
912}
913
Johannes Berg7527a782011-05-13 10:58:57 +0200914static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
915{
916 struct nlattr *nl_modes = nla_nest_start(msg, attr);
917 int i;
918
919 if (!nl_modes)
920 goto nla_put_failure;
921
922 i = 0;
923 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400924 if ((ifmodes & 1) && nla_put_flag(msg, i))
925 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200926 ifmodes >>= 1;
927 i++;
928 }
929
930 nla_nest_end(msg, nl_modes);
931 return 0;
932
933nla_put_failure:
934 return -ENOBUFS;
935}
936
937static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100938 struct sk_buff *msg,
939 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200940{
941 struct nlattr *nl_combis;
942 int i, j;
943
944 nl_combis = nla_nest_start(msg,
945 NL80211_ATTR_INTERFACE_COMBINATIONS);
946 if (!nl_combis)
947 goto nla_put_failure;
948
949 for (i = 0; i < wiphy->n_iface_combinations; i++) {
950 const struct ieee80211_iface_combination *c;
951 struct nlattr *nl_combi, *nl_limits;
952
953 c = &wiphy->iface_combinations[i];
954
955 nl_combi = nla_nest_start(msg, i + 1);
956 if (!nl_combi)
957 goto nla_put_failure;
958
959 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
960 if (!nl_limits)
961 goto nla_put_failure;
962
963 for (j = 0; j < c->n_limits; j++) {
964 struct nlattr *nl_limit;
965
966 nl_limit = nla_nest_start(msg, j + 1);
967 if (!nl_limit)
968 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400969 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
970 c->limits[j].max))
971 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200972 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
973 c->limits[j].types))
974 goto nla_put_failure;
975 nla_nest_end(msg, nl_limit);
976 }
977
978 nla_nest_end(msg, nl_limits);
979
David S. Miller9360ffd2012-03-29 04:41:26 -0400980 if (c->beacon_int_infra_match &&
981 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
982 goto nla_put_failure;
983 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
984 c->num_different_channels) ||
985 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
986 c->max_interfaces))
987 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100988 if (large &&
Felix Fietkau8c48b502014-05-05 11:48:40 +0200989 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
990 c->radar_detect_widths) ||
991 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
992 c->radar_detect_regions)))
Johannes Bergcdc89b92013-02-18 23:54:36 +0100993 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200994
995 nla_nest_end(msg, nl_combi);
996 }
997
998 nla_nest_end(msg, nl_combis);
999
1000 return 0;
1001nla_put_failure:
1002 return -ENOBUFS;
1003}
1004
Johannes Berg3713b4e2013-02-14 16:19:38 +01001005#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001006static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1007 struct sk_buff *msg)
1008{
Johannes Berg964dc9e2013-06-03 17:25:34 +02001009 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +01001010 struct nlattr *nl_tcp;
1011
1012 if (!tcp)
1013 return 0;
1014
1015 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1016 if (!nl_tcp)
1017 return -ENOBUFS;
1018
1019 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1020 tcp->data_payload_max))
1021 return -ENOBUFS;
1022
1023 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1024 tcp->data_payload_max))
1025 return -ENOBUFS;
1026
1027 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1028 return -ENOBUFS;
1029
1030 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1031 sizeof(*tcp->tok), tcp->tok))
1032 return -ENOBUFS;
1033
1034 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1035 tcp->data_interval_max))
1036 return -ENOBUFS;
1037
1038 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1039 tcp->wake_payload_max))
1040 return -ENOBUFS;
1041
1042 nla_nest_end(msg, nl_tcp);
1043 return 0;
1044}
1045
Johannes Berg3713b4e2013-02-14 16:19:38 +01001046static int nl80211_send_wowlan(struct sk_buff *msg,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001047 struct cfg80211_registered_device *rdev,
Johannes Bergb56cf722013-02-20 01:02:38 +01001048 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001049{
1050 struct nlattr *nl_wowlan;
1051
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001052 if (!rdev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001053 return 0;
1054
1055 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1056 if (!nl_wowlan)
1057 return -ENOBUFS;
1058
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001059 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001060 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001061 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001062 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001063 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001064 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001065 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001066 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001067 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001068 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001069 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001070 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001071 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001072 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001073 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001074 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1075 return -ENOBUFS;
1076
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001077 if (rdev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001078 struct nl80211_pattern_support pat = {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001079 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1080 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1081 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1082 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001083 };
1084
1085 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1086 sizeof(pat), &pat))
1087 return -ENOBUFS;
1088 }
1089
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001090 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
Johannes Bergb56cf722013-02-20 01:02:38 +01001091 return -ENOBUFS;
1092
Luciano Coelho8cd4d452014-09-17 11:55:28 +03001093 /* TODO: send wowlan net detect */
1094
Johannes Berg3713b4e2013-02-14 16:19:38 +01001095 nla_nest_end(msg, nl_wowlan);
1096
1097 return 0;
1098}
1099#endif
1100
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001101static int nl80211_send_coalesce(struct sk_buff *msg,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001102 struct cfg80211_registered_device *rdev)
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001103{
1104 struct nl80211_coalesce_rule_support rule;
1105
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001106 if (!rdev->wiphy.coalesce)
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001107 return 0;
1108
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001109 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1110 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1111 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1112 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1113 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1114 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001115
1116 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1117 return -ENOBUFS;
1118
1119 return 0;
1120}
1121
Johannes Berg3713b4e2013-02-14 16:19:38 +01001122static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1123 struct ieee80211_supported_band *sband)
1124{
1125 struct nlattr *nl_rates, *nl_rate;
1126 struct ieee80211_rate *rate;
1127 int i;
1128
1129 /* add HT info */
1130 if (sband->ht_cap.ht_supported &&
1131 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1132 sizeof(sband->ht_cap.mcs),
1133 &sband->ht_cap.mcs) ||
1134 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1135 sband->ht_cap.cap) ||
1136 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1137 sband->ht_cap.ampdu_factor) ||
1138 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1139 sband->ht_cap.ampdu_density)))
1140 return -ENOBUFS;
1141
1142 /* add VHT info */
1143 if (sband->vht_cap.vht_supported &&
1144 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1145 sizeof(sband->vht_cap.vht_mcs),
1146 &sband->vht_cap.vht_mcs) ||
1147 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1148 sband->vht_cap.cap)))
1149 return -ENOBUFS;
1150
1151 /* add bitrates */
1152 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1153 if (!nl_rates)
1154 return -ENOBUFS;
1155
1156 for (i = 0; i < sband->n_bitrates; i++) {
1157 nl_rate = nla_nest_start(msg, i);
1158 if (!nl_rate)
1159 return -ENOBUFS;
1160
1161 rate = &sband->bitrates[i];
1162 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1163 rate->bitrate))
1164 return -ENOBUFS;
1165 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1166 nla_put_flag(msg,
1167 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1168 return -ENOBUFS;
1169
1170 nla_nest_end(msg, nl_rate);
1171 }
1172
1173 nla_nest_end(msg, nl_rates);
1174
1175 return 0;
1176}
1177
1178static int
1179nl80211_send_mgmt_stypes(struct sk_buff *msg,
1180 const struct ieee80211_txrx_stypes *mgmt_stypes)
1181{
1182 u16 stypes;
1183 struct nlattr *nl_ftypes, *nl_ifs;
1184 enum nl80211_iftype ift;
1185 int i;
1186
1187 if (!mgmt_stypes)
1188 return 0;
1189
1190 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1191 if (!nl_ifs)
1192 return -ENOBUFS;
1193
1194 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1195 nl_ftypes = nla_nest_start(msg, ift);
1196 if (!nl_ftypes)
1197 return -ENOBUFS;
1198 i = 0;
1199 stypes = mgmt_stypes[ift].tx;
1200 while (stypes) {
1201 if ((stypes & 1) &&
1202 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1203 (i << 4) | IEEE80211_FTYPE_MGMT))
1204 return -ENOBUFS;
1205 stypes >>= 1;
1206 i++;
1207 }
1208 nla_nest_end(msg, nl_ftypes);
1209 }
1210
1211 nla_nest_end(msg, nl_ifs);
1212
1213 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1214 if (!nl_ifs)
1215 return -ENOBUFS;
1216
1217 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1218 nl_ftypes = nla_nest_start(msg, ift);
1219 if (!nl_ftypes)
1220 return -ENOBUFS;
1221 i = 0;
1222 stypes = mgmt_stypes[ift].rx;
1223 while (stypes) {
1224 if ((stypes & 1) &&
1225 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1226 (i << 4) | IEEE80211_FTYPE_MGMT))
1227 return -ENOBUFS;
1228 stypes >>= 1;
1229 i++;
1230 }
1231 nla_nest_end(msg, nl_ftypes);
1232 }
1233 nla_nest_end(msg, nl_ifs);
1234
1235 return 0;
1236}
1237
Johannes Berg86e8cf92013-06-19 10:57:22 +02001238struct nl80211_dump_wiphy_state {
1239 s64 filter_wiphy;
1240 long start;
1241 long split_start, band_start, chan_start;
1242 bool split;
1243};
1244
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001245static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
Johannes Berg3bb20552014-05-26 13:52:25 +02001246 enum nl80211_commands cmd,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001247 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001248 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001249{
1250 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001251 struct nlattr *nl_bands, *nl_band;
1252 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001253 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001254 enum ieee80211_band band;
1255 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001256 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001257 const struct ieee80211_txrx_stypes *mgmt_stypes =
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001258 rdev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001259 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001260
Johannes Berg3bb20552014-05-26 13:52:25 +02001261 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -04001262 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001263 return -ENOBUFS;
1264
Johannes Berg86e8cf92013-06-19 10:57:22 +02001265 if (WARN_ON(!state))
1266 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001267
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001268 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001269 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001270 wiphy_name(&rdev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001271 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001272 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001273 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001274
Johannes Berg3bb20552014-05-26 13:52:25 +02001275 if (cmd != NL80211_CMD_NEW_WIPHY)
1276 goto finish;
1277
Johannes Berg86e8cf92013-06-19 10:57:22 +02001278 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001279 case 0:
1280 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001281 rdev->wiphy.retry_short) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001282 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001283 rdev->wiphy.retry_long) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001284 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001285 rdev->wiphy.frag_threshold) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001286 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001287 rdev->wiphy.rts_threshold) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001288 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001289 rdev->wiphy.coverage_class) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001290 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001291 rdev->wiphy.max_scan_ssids) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001293 rdev->wiphy.max_sched_scan_ssids) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001294 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001295 rdev->wiphy.max_scan_ie_len) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001296 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001297 rdev->wiphy.max_sched_scan_ie_len) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001298 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001299 rdev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001300 goto nla_put_failure;
1301
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001302 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001303 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1304 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001305 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001306 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1307 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001308 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001309 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1310 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001311 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001312 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1313 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001314 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001315 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1316 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001317 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001318 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001319 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001320 state->split_start++;
1321 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001322 break;
1323 case 1:
1324 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001325 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1326 rdev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001327 goto nla_put_failure;
1328
Johannes Berg3713b4e2013-02-14 16:19:38 +01001329 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001330 rdev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001331 goto nla_put_failure;
1332
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001333 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001334 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1335 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001336
Johannes Berg3713b4e2013-02-14 16:19:38 +01001337 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001338 rdev->wiphy.available_antennas_tx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001339 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001340 rdev->wiphy.available_antennas_rx))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001341 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001342
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001343 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001344 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001345 rdev->wiphy.probe_resp_offload))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001346 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001347
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001348 if ((rdev->wiphy.available_antennas_tx ||
1349 rdev->wiphy.available_antennas_rx) &&
1350 rdev->ops->get_antenna) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 u32 tx_ant = 0, rx_ant = 0;
1352 int res;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001353 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001354 if (!res) {
1355 if (nla_put_u32(msg,
1356 NL80211_ATTR_WIPHY_ANTENNA_TX,
1357 tx_ant) ||
1358 nla_put_u32(msg,
1359 NL80211_ATTR_WIPHY_ANTENNA_RX,
1360 rx_ant))
1361 goto nla_put_failure;
1362 }
Johannes Bergee688b002008-01-24 19:38:39 +01001363 }
1364
Johannes Berg86e8cf92013-06-19 10:57:22 +02001365 state->split_start++;
1366 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001367 break;
1368 case 2:
1369 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001370 rdev->wiphy.interface_modes))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001372 state->split_start++;
1373 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001374 break;
1375 case 3:
1376 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1377 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001378 goto nla_put_failure;
1379
Johannes Berg86e8cf92013-06-19 10:57:22 +02001380 for (band = state->band_start;
1381 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001382 struct ieee80211_supported_band *sband;
1383
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001384 sband = rdev->wiphy.bands[band];
Johannes Berg3713b4e2013-02-14 16:19:38 +01001385
1386 if (!sband)
1387 continue;
1388
1389 nl_band = nla_nest_start(msg, band);
1390 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001391 goto nla_put_failure;
1392
Johannes Berg86e8cf92013-06-19 10:57:22 +02001393 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001394 case 0:
1395 if (nl80211_send_band_rateinfo(msg, sband))
1396 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001397 state->chan_start++;
1398 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001399 break;
1400 default:
1401 /* add frequencies */
1402 nl_freqs = nla_nest_start(
1403 msg, NL80211_BAND_ATTR_FREQS);
1404 if (!nl_freqs)
1405 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001406
Johannes Berg86e8cf92013-06-19 10:57:22 +02001407 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001408 i < sband->n_channels;
1409 i++) {
1410 nl_freq = nla_nest_start(msg, i);
1411 if (!nl_freq)
1412 goto nla_put_failure;
1413
1414 chan = &sband->channels[i];
1415
Johannes Berg86e8cf92013-06-19 10:57:22 +02001416 if (nl80211_msg_put_channel(
1417 msg, chan,
1418 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001419 goto nla_put_failure;
1420
1421 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001422 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001423 break;
1424 }
1425 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001426 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001427 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001428 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001429 nla_nest_end(msg, nl_freqs);
1430 }
1431
1432 nla_nest_end(msg, nl_band);
1433
Johannes Berg86e8cf92013-06-19 10:57:22 +02001434 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001435 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001436 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001437 band--;
1438 break;
1439 }
Johannes Bergee688b002008-01-24 19:38:39 +01001440 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001441 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001442
Johannes Berg3713b4e2013-02-14 16:19:38 +01001443 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001444 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001445 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001446 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001447
Johannes Berg3713b4e2013-02-14 16:19:38 +01001448 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001449 if (state->band_start == 0 && state->chan_start == 0)
1450 state->split_start++;
1451 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001452 break;
1453 case 4:
1454 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1455 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001456 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001457
1458 i = 0;
1459#define CMD(op, n) \
1460 do { \
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001461 if (rdev->ops->op) { \
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 i++; \
1463 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1464 goto nla_put_failure; \
1465 } \
1466 } while (0)
1467
1468 CMD(add_virtual_intf, NEW_INTERFACE);
1469 CMD(change_virtual_intf, SET_INTERFACE);
1470 CMD(add_key, NEW_KEY);
1471 CMD(start_ap, START_AP);
1472 CMD(add_station, NEW_STATION);
1473 CMD(add_mpath, NEW_MPATH);
1474 CMD(update_mesh_config, SET_MESH_CONFIG);
1475 CMD(change_bss, SET_BSS);
1476 CMD(auth, AUTHENTICATE);
1477 CMD(assoc, ASSOCIATE);
1478 CMD(deauth, DEAUTHENTICATE);
1479 CMD(disassoc, DISASSOCIATE);
1480 CMD(join_ibss, JOIN_IBSS);
1481 CMD(join_mesh, JOIN_MESH);
1482 CMD(set_pmksa, SET_PMKSA);
1483 CMD(del_pmksa, DEL_PMKSA);
1484 CMD(flush_pmksa, FLUSH_PMKSA);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001485 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001486 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1487 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1488 CMD(mgmt_tx, FRAME);
1489 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001490 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001491 i++;
1492 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1493 goto nla_put_failure;
1494 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001495 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1496 rdev->ops->join_mesh) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001497 i++;
1498 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1499 goto nla_put_failure;
1500 }
1501 CMD(set_wds_peer, SET_WDS_PEER);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001502 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001503 CMD(tdls_mgmt, TDLS_MGMT);
1504 CMD(tdls_oper, TDLS_OPER);
1505 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001506 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 CMD(sched_scan_start, START_SCHED_SCAN);
1508 CMD(probe_client, PROBE_CLIENT);
1509 CMD(set_noack_map, SET_NOACK_MAP);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001510 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001511 i++;
1512 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1513 goto nla_put_failure;
1514 }
1515 CMD(start_p2p_device, START_P2P_DEVICE);
1516 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg02df00e2014-06-10 14:06:25 +02001517#ifdef CONFIG_NL80211_TESTMODE
1518 CMD(testmode_cmd, TESTMODE);
1519#endif
Johannes Berg86e8cf92013-06-19 10:57:22 +02001520 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001521 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1522 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001523 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001524 CMD(channel_switch, CHANNEL_SWITCH);
Johannes Berg02df00e2014-06-10 14:06:25 +02001525 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg723e73a2014-10-22 09:25:06 +02001526 if (rdev->wiphy.features &
1527 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
Johannes Berg960d01a2014-09-09 22:55:35 +03001528 CMD(add_tx_ts, ADD_TX_TS);
Arend van Spriel5de17982013-04-18 15:49:00 +02001529 }
Johannes Berg02df00e2014-06-10 14:06:25 +02001530 /* add into the if now */
Johannes Berg8fdc6212009-03-14 09:34:01 +01001531#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001532
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001533 if (rdev->ops->connect || rdev->ops->auth) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001534 i++;
1535 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001536 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001537 }
1538
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001539 if (rdev->ops->disconnect || rdev->ops->deauth) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001540 i++;
1541 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1542 goto nla_put_failure;
1543 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001544
Johannes Berg3713b4e2013-02-14 16:19:38 +01001545 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001546 state->split_start++;
1547 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001548 break;
1549 case 5:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001550 if (rdev->ops->remain_on_channel &&
1551 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001552 nla_put_u32(msg,
1553 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001554 rdev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001555 goto nla_put_failure;
1556
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001557 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001558 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1559 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001560
Johannes Berg3713b4e2013-02-14 16:19:38 +01001561 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1562 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001563 state->split_start++;
1564 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001565 break;
1566 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001567#ifdef CONFIG_PM
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001568 if (nl80211_send_wowlan(msg, rdev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001569 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001570 state->split_start++;
1571 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001572 break;
1573#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001574 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001575#endif
1576 case 7:
1577 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001578 rdev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001579 goto nla_put_failure;
1580
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001581 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001582 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001583 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001584
Johannes Berg86e8cf92013-06-19 10:57:22 +02001585 state->split_start++;
1586 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001587 break;
1588 case 8:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001589 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001590 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001591 rdev->wiphy.ap_sme_capa))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001592 goto nla_put_failure;
1593
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001594 features = rdev->wiphy.features;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001595 /*
1596 * We can only add the per-channel limit information if the
1597 * dump is split, otherwise it makes it too big. Therefore
1598 * only advertise it in that case.
1599 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001600 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001601 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1602 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001603 goto nla_put_failure;
1604
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001605 if (rdev->wiphy.ht_capa_mod_mask &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001606 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001607 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1608 rdev->wiphy.ht_capa_mod_mask))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001609 goto nla_put_failure;
1610
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001611 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1612 rdev->wiphy.max_acl_mac_addrs &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001613 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001614 rdev->wiphy.max_acl_mac_addrs))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001615 goto nla_put_failure;
1616
1617 /*
1618 * Any information below this point is only available to
1619 * applications that can deal with it being split. This
1620 * helps ensure that newly added capabilities don't break
1621 * older tools by overrunning their buffers.
1622 *
1623 * We still increment split_start so that in the split
1624 * case we'll continue with more data in the next round,
1625 * but break unconditionally so unsplit data stops here.
1626 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001627 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001628 break;
1629 case 9:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001630 if (rdev->wiphy.extended_capabilities &&
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001631 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001632 rdev->wiphy.extended_capabilities_len,
1633 rdev->wiphy.extended_capabilities) ||
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001634 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001635 rdev->wiphy.extended_capabilities_len,
1636 rdev->wiphy.extended_capabilities_mask)))
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001637 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001638
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001639 if (rdev->wiphy.vht_capa_mod_mask &&
Johannes Bergee2aca32013-02-21 17:36:01 +01001640 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001641 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1642 rdev->wiphy.vht_capa_mod_mask))
Johannes Bergee2aca32013-02-21 17:36:01 +01001643 goto nla_put_failure;
1644
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001645 state->split_start++;
1646 break;
1647 case 10:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001648 if (nl80211_send_coalesce(msg, rdev))
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001649 goto nla_put_failure;
1650
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001651 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001652 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1653 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1654 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001655
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001656 if (rdev->wiphy.max_ap_assoc_sta &&
Jouni Malinenb43504c2014-01-15 00:01:08 +02001657 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001658 rdev->wiphy.max_ap_assoc_sta))
Jouni Malinenb43504c2014-01-15 00:01:08 +02001659 goto nla_put_failure;
1660
Johannes Bergad7e7182013-11-13 13:37:47 +01001661 state->split_start++;
1662 break;
1663 case 11:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001664 if (rdev->wiphy.n_vendor_commands) {
Johannes Berg567ffc32013-12-18 14:43:31 +01001665 const struct nl80211_vendor_cmd_info *info;
1666 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001667
Johannes Berg567ffc32013-12-18 14:43:31 +01001668 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1669 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001670 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001671
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001672 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1673 info = &rdev->wiphy.vendor_commands[i].info;
Johannes Berg567ffc32013-12-18 14:43:31 +01001674 if (nla_put(msg, i + 1, sizeof(*info), info))
1675 goto nla_put_failure;
1676 }
1677 nla_nest_end(msg, nested);
1678 }
1679
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001680 if (rdev->wiphy.n_vendor_events) {
Johannes Berg567ffc32013-12-18 14:43:31 +01001681 const struct nl80211_vendor_cmd_info *info;
1682 struct nlattr *nested;
1683
1684 nested = nla_nest_start(msg,
1685 NL80211_ATTR_VENDOR_EVENTS);
1686 if (!nested)
1687 goto nla_put_failure;
1688
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001689 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1690 info = &rdev->wiphy.vendor_events[i];
Johannes Berg567ffc32013-12-18 14:43:31 +01001691 if (nla_put(msg, i + 1, sizeof(*info), info))
1692 goto nla_put_failure;
1693 }
1694 nla_nest_end(msg, nested);
1695 }
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03001696 state->split_start++;
1697 break;
1698 case 12:
1699 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1700 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1701 rdev->wiphy.max_num_csa_counters))
1702 goto nla_put_failure;
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001703
Johannes Berg3713b4e2013-02-14 16:19:38 +01001704 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001705 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001706 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001707 }
Johannes Berg3bb20552014-05-26 13:52:25 +02001708 finish:
Johannes Berg55682962007-09-20 13:09:35 -04001709 return genlmsg_end(msg, hdr);
1710
1711 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001712 genlmsg_cancel(msg, hdr);
1713 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001714}
1715
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1717 struct netlink_callback *cb,
1718 struct nl80211_dump_wiphy_state *state)
1719{
1720 struct nlattr **tb = nl80211_fam.attrbuf;
1721 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1722 tb, nl80211_fam.maxattr, nl80211_policy);
1723 /* ignore parse errors for backward compatibility */
1724 if (ret)
1725 return 0;
1726
1727 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1728 if (tb[NL80211_ATTR_WIPHY])
1729 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1730 if (tb[NL80211_ATTR_WDEV])
1731 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1732 if (tb[NL80211_ATTR_IFINDEX]) {
1733 struct net_device *netdev;
1734 struct cfg80211_registered_device *rdev;
1735 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1736
Ying Xue7f2b8562014-01-15 10:23:45 +08001737 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001738 if (!netdev)
1739 return -ENODEV;
1740 if (netdev->ieee80211_ptr) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001741 rdev = wiphy_to_rdev(
Johannes Berg86e8cf92013-06-19 10:57:22 +02001742 netdev->ieee80211_ptr->wiphy);
1743 state->filter_wiphy = rdev->wiphy_idx;
1744 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001745 }
1746
1747 return 0;
1748}
1749
Johannes Berg55682962007-09-20 13:09:35 -04001750static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1751{
Johannes Berg645e77d2013-03-01 14:03:49 +01001752 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001753 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001754 struct cfg80211_registered_device *rdev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001755
Johannes Berg5fe231e2013-05-08 21:45:15 +02001756 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001757 if (!state) {
1758 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001759 if (!state) {
1760 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001761 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001762 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001763 state->filter_wiphy = -1;
1764 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1765 if (ret) {
1766 kfree(state);
1767 rtnl_unlock();
1768 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001769 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001770 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001771 }
1772
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001773 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1774 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001775 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001776 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001777 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001778 if (state->filter_wiphy != -1 &&
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001779 state->filter_wiphy != rdev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001780 continue;
1781 /* attempt to fit multiple wiphy data chunks into the skb */
1782 do {
Johannes Berg3bb20552014-05-26 13:52:25 +02001783 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
1784 skb,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001785 NETLINK_CB(cb->skb).portid,
1786 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001787 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001788 if (ret < 0) {
1789 /*
1790 * If sending the wiphy data didn't fit (ENOBUFS
1791 * or EMSGSIZE returned), this SKB is still
1792 * empty (so it's not too big because another
1793 * wiphy dataset is already in the skb) and
1794 * we've not tried to adjust the dump allocation
1795 * yet ... then adjust the alloc size to be
1796 * bigger, and return 1 but with the empty skb.
1797 * This results in an empty message being RX'ed
1798 * in userspace, but that is ignored.
1799 *
1800 * We can then retry with the larger buffer.
1801 */
1802 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001803 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001804 cb->min_dump_alloc < 4096) {
1805 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001806 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001807 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001808 return 1;
1809 }
1810 idx--;
1811 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001812 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001813 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001814 break;
Johannes Berg55682962007-09-20 13:09:35 -04001815 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001816 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001817
Johannes Berg86e8cf92013-06-19 10:57:22 +02001818 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001819
1820 return skb->len;
1821}
1822
Johannes Berg86e8cf92013-06-19 10:57:22 +02001823static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1824{
1825 kfree((void *)cb->args[0]);
1826 return 0;
1827}
1828
Johannes Berg55682962007-09-20 13:09:35 -04001829static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1830{
1831 struct sk_buff *msg;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001833 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001834
Johannes Berg645e77d2013-03-01 14:03:49 +01001835 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001836 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001837 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001838
Johannes Berg3bb20552014-05-26 13:52:25 +02001839 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
1840 info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001841 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001842 nlmsg_free(msg);
1843 return -ENOBUFS;
1844 }
Johannes Berg55682962007-09-20 13:09:35 -04001845
Johannes Berg134e6372009-07-10 09:51:34 +00001846 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001847}
1848
Jouni Malinen31888482008-10-30 16:59:24 +02001849static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1850 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1851 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1852 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1853 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1854 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1855};
1856
1857static int parse_txq_params(struct nlattr *tb[],
1858 struct ieee80211_txq_params *txq_params)
1859{
Johannes Berga3304b02012-03-28 11:04:24 +02001860 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001861 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1862 !tb[NL80211_TXQ_ATTR_AIFS])
1863 return -EINVAL;
1864
Johannes Berga3304b02012-03-28 11:04:24 +02001865 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001866 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1867 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1868 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1869 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1870
Johannes Berga3304b02012-03-28 11:04:24 +02001871 if (txq_params->ac >= NL80211_NUM_ACS)
1872 return -EINVAL;
1873
Jouni Malinen31888482008-10-30 16:59:24 +02001874 return 0;
1875}
1876
Johannes Bergf444de02010-05-05 15:25:02 +02001877static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1878{
1879 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001880 * You can only set the channel explicitly for WDS interfaces,
1881 * all others have their channel managed via their respective
1882 * "establish a connection" command (connect, join, ...)
1883 *
1884 * For AP/GO and mesh mode, the channel can be set with the
1885 * channel userspace API, but is only stored and passed to the
1886 * low-level driver when the AP starts or the mesh is joined.
1887 * This is for backward compatibility, userspace can also give
1888 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001889 *
1890 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001891 * whatever else is going on, so they have their own special
1892 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001893 */
1894 return !wdev ||
1895 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001896 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001897 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1898 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001899}
1900
Johannes Berg683b6d32012-11-08 21:25:48 +01001901static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1902 struct genl_info *info,
1903 struct cfg80211_chan_def *chandef)
1904{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301905 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001906
1907 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1908 return -EINVAL;
1909
1910 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1911
1912 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001913 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1914 chandef->center_freq1 = control_freq;
1915 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001916
1917 /* Primary channel not allowed */
1918 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1919 return -EINVAL;
1920
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001921 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1922 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001923
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001924 chantype = nla_get_u32(
1925 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1926
1927 switch (chantype) {
1928 case NL80211_CHAN_NO_HT:
1929 case NL80211_CHAN_HT20:
1930 case NL80211_CHAN_HT40PLUS:
1931 case NL80211_CHAN_HT40MINUS:
1932 cfg80211_chandef_create(chandef, chandef->chan,
1933 chantype);
1934 break;
1935 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001936 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001937 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001938 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1939 chandef->width =
1940 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1941 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1942 chandef->center_freq1 =
1943 nla_get_u32(
1944 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1945 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1946 chandef->center_freq2 =
1947 nla_get_u32(
1948 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1949 }
1950
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001951 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001952 return -EINVAL;
1953
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001954 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1955 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001956 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001957
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001958 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1959 chandef->width == NL80211_CHAN_WIDTH_10) &&
1960 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1961 return -EINVAL;
1962
Johannes Berg683b6d32012-11-08 21:25:48 +01001963 return 0;
1964}
1965
Johannes Bergf444de02010-05-05 15:25:02 +02001966static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
Jouni Malinene16821b2014-04-28 11:22:08 +03001967 struct net_device *dev,
Johannes Bergf444de02010-05-05 15:25:02 +02001968 struct genl_info *info)
1969{
Johannes Berg683b6d32012-11-08 21:25:48 +01001970 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001971 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001972 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
Jouni Malinene16821b2014-04-28 11:22:08 +03001973 struct wireless_dev *wdev = NULL;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001974
Jouni Malinene16821b2014-04-28 11:22:08 +03001975 if (dev)
1976 wdev = dev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001977 if (!nl80211_can_set_dev_channel(wdev))
1978 return -EOPNOTSUPP;
Jouni Malinene16821b2014-04-28 11:22:08 +03001979 if (wdev)
1980 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001981
Johannes Berg683b6d32012-11-08 21:25:48 +01001982 result = nl80211_parse_chandef(rdev, info, &chandef);
1983 if (result)
1984 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001985
Johannes Berge8c9bd52012-06-06 08:18:22 +02001986 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001987 case NL80211_IFTYPE_AP:
1988 case NL80211_IFTYPE_P2P_GO:
Ilan Peer174e0cd2014-02-23 09:13:01 +02001989 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, iftype)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001990 result = -EINVAL;
1991 break;
1992 }
Jouni Malinene16821b2014-04-28 11:22:08 +03001993 if (wdev->beacon_interval) {
1994 if (!dev || !rdev->ops->set_ap_chanwidth ||
1995 !(rdev->wiphy.features &
1996 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
1997 result = -EBUSY;
1998 break;
1999 }
2000
2001 /* Only allow dynamic channel width changes */
2002 if (chandef.chan != wdev->preset_chandef.chan) {
2003 result = -EBUSY;
2004 break;
2005 }
2006 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2007 if (result)
2008 break;
2009 }
Johannes Berg683b6d32012-11-08 21:25:48 +01002010 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02002011 result = 0;
2012 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02002013 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01002014 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02002015 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02002016 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01002017 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02002018 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02002019 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02002020 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02002021 }
Johannes Bergf444de02010-05-05 15:25:02 +02002022
2023 return result;
2024}
2025
2026static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2027{
Johannes Berg4c476992010-10-04 21:36:35 +02002028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2029 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02002030
Jouni Malinene16821b2014-04-28 11:22:08 +03002031 return __nl80211_set_channel(rdev, netdev, info);
Johannes Bergf444de02010-05-05 15:25:02 +02002032}
2033
Bill Jordane8347eb2010-10-01 13:54:28 -04002034static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2035{
Johannes Berg43b19952010-10-07 13:10:30 +02002036 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2037 struct net_device *dev = info->user_ptr[1];
2038 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02002039 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04002040
2041 if (!info->attrs[NL80211_ATTR_MAC])
2042 return -EINVAL;
2043
Johannes Berg43b19952010-10-07 13:10:30 +02002044 if (netif_running(dev))
2045 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04002046
Johannes Berg43b19952010-10-07 13:10:30 +02002047 if (!rdev->ops->set_wds_peer)
2048 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04002049
Johannes Berg43b19952010-10-07 13:10:30 +02002050 if (wdev->iftype != NL80211_IFTYPE_WDS)
2051 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04002052
2053 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03002054 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04002055}
2056
2057
Johannes Berg55682962007-09-20 13:09:35 -04002058static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2059{
2060 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02002061 struct net_device *netdev = NULL;
2062 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04002063 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02002064 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002065 u32 changed;
2066 u8 retry_short = 0, retry_long = 0;
2067 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01002068 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04002069
Johannes Berg5fe231e2013-05-08 21:45:15 +02002070 ASSERT_RTNL();
2071
Johannes Bergf444de02010-05-05 15:25:02 +02002072 /*
2073 * Try to find the wiphy and netdev. Normally this
2074 * function shouldn't need the netdev, but this is
2075 * done for backward compatibility -- previously
2076 * setting the channel was done per wiphy, but now
2077 * it is per netdev. Previous userland like hostapd
2078 * also passed a netdev to set_wiphy, so that it is
2079 * possible to let that go to the right netdev!
2080 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002081
Johannes Bergf444de02010-05-05 15:25:02 +02002082 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2083 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2084
Ying Xue7f2b8562014-01-15 10:23:45 +08002085 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002086 if (netdev && netdev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002087 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002088 else
Johannes Bergf444de02010-05-05 15:25:02 +02002089 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002090 }
2091
Johannes Bergf444de02010-05-05 15:25:02 +02002092 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002093 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2094 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002095 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002096 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002097 wdev = NULL;
2098 netdev = NULL;
2099 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002100 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002101 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002102
2103 /*
2104 * end workaround code, by now the rdev is available
2105 * and locked, and wdev may or may not be NULL.
2106 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002107
2108 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002109 result = cfg80211_dev_rename(
2110 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002111
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002112 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002113 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002114
Jouni Malinen31888482008-10-30 16:59:24 +02002115 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2116 struct ieee80211_txq_params txq_params;
2117 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2118
Ying Xue7f2b8562014-01-15 10:23:45 +08002119 if (!rdev->ops->set_txq_params)
2120 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002121
Ying Xue7f2b8562014-01-15 10:23:45 +08002122 if (!netdev)
2123 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002124
Johannes Berg133a3ff2011-11-03 14:50:13 +01002125 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002126 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2127 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002128
Ying Xue7f2b8562014-01-15 10:23:45 +08002129 if (!netif_running(netdev))
2130 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002131
Jouni Malinen31888482008-10-30 16:59:24 +02002132 nla_for_each_nested(nl_txq_params,
2133 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2134 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002135 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2136 nla_data(nl_txq_params),
2137 nla_len(nl_txq_params),
2138 txq_params_policy);
2139 if (result)
2140 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002141 result = parse_txq_params(tb, &txq_params);
2142 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002143 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002144
Hila Gonene35e4d22012-06-27 17:19:42 +03002145 result = rdev_set_txq_params(rdev, netdev,
2146 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002147 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002148 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002149 }
2150 }
2151
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002152 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinene16821b2014-04-28 11:22:08 +03002153 result = __nl80211_set_channel(
2154 rdev,
2155 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2156 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002157 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002158 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002159 }
2160
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002161 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002162 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002163 enum nl80211_tx_power_setting type;
2164 int idx, mbm = 0;
2165
Johannes Bergc8442112012-10-24 10:17:18 +02002166 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2167 txp_wdev = NULL;
2168
Ying Xue7f2b8562014-01-15 10:23:45 +08002169 if (!rdev->ops->set_tx_power)
2170 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002171
2172 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2173 type = nla_get_u32(info->attrs[idx]);
2174
2175 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002176 (type != NL80211_TX_POWER_AUTOMATIC))
2177 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002178
2179 if (type != NL80211_TX_POWER_AUTOMATIC) {
2180 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2181 mbm = nla_get_u32(info->attrs[idx]);
2182 }
2183
Johannes Bergc8442112012-10-24 10:17:18 +02002184 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002185 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002186 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002187 }
2188
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002189 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2190 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2191 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002192 if ((!rdev->wiphy.available_antennas_tx &&
2193 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002194 !rdev->ops->set_antenna)
2195 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002196
2197 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2198 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2199
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002200 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002201 * available antenna masks, except for the "all" mask */
2202 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002203 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2204 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002205
Bruno Randolf7f531e02010-12-16 11:30:22 +09002206 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2207 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002208
Hila Gonene35e4d22012-06-27 17:19:42 +03002209 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002210 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002211 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002212 }
2213
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002214 changed = 0;
2215
2216 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2217 retry_short = nla_get_u8(
2218 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002219 if (retry_short == 0)
2220 return -EINVAL;
2221
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002222 changed |= WIPHY_PARAM_RETRY_SHORT;
2223 }
2224
2225 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2226 retry_long = nla_get_u8(
2227 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002228 if (retry_long == 0)
2229 return -EINVAL;
2230
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002231 changed |= WIPHY_PARAM_RETRY_LONG;
2232 }
2233
2234 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2235 frag_threshold = nla_get_u32(
2236 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002237 if (frag_threshold < 256)
2238 return -EINVAL;
2239
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002240 if (frag_threshold != (u32) -1) {
2241 /*
2242 * Fragments (apart from the last one) are required to
2243 * have even length. Make the fragmentation code
2244 * simpler by stripping LSB should someone try to use
2245 * odd threshold value.
2246 */
2247 frag_threshold &= ~0x1;
2248 }
2249 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2250 }
2251
2252 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2253 rts_threshold = nla_get_u32(
2254 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2255 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2256 }
2257
Lukáš Turek81077e82009-12-21 22:50:47 +01002258 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +02002259 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2260 return -EINVAL;
2261
Lukáš Turek81077e82009-12-21 22:50:47 +01002262 coverage_class = nla_get_u8(
2263 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2264 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2265 }
2266
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +02002267 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2268 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2269 return -EOPNOTSUPP;
2270
2271 changed |= WIPHY_PARAM_DYN_ACK;
2272 }
2273
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002274 if (changed) {
2275 u8 old_retry_short, old_retry_long;
2276 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002277 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002278
Ying Xue7f2b8562014-01-15 10:23:45 +08002279 if (!rdev->ops->set_wiphy_params)
2280 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002281
2282 old_retry_short = rdev->wiphy.retry_short;
2283 old_retry_long = rdev->wiphy.retry_long;
2284 old_frag_threshold = rdev->wiphy.frag_threshold;
2285 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002286 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002287
2288 if (changed & WIPHY_PARAM_RETRY_SHORT)
2289 rdev->wiphy.retry_short = retry_short;
2290 if (changed & WIPHY_PARAM_RETRY_LONG)
2291 rdev->wiphy.retry_long = retry_long;
2292 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2293 rdev->wiphy.frag_threshold = frag_threshold;
2294 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2295 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002296 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2297 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002298
Hila Gonene35e4d22012-06-27 17:19:42 +03002299 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002300 if (result) {
2301 rdev->wiphy.retry_short = old_retry_short;
2302 rdev->wiphy.retry_long = old_retry_long;
2303 rdev->wiphy.frag_threshold = old_frag_threshold;
2304 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002305 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002306 }
2307 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002308 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002309}
2310
Johannes Berg71bbc992012-06-15 15:30:18 +02002311static inline u64 wdev_id(struct wireless_dev *wdev)
2312{
2313 return (u64)wdev->identifier |
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002314 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
Johannes Berg71bbc992012-06-15 15:30:18 +02002315}
Johannes Berg55682962007-09-20 13:09:35 -04002316
Johannes Berg683b6d32012-11-08 21:25:48 +01002317static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002318 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002319{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002320 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002321
Johannes Berg683b6d32012-11-08 21:25:48 +01002322 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2323 chandef->chan->center_freq))
2324 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002325 switch (chandef->width) {
2326 case NL80211_CHAN_WIDTH_20_NOHT:
2327 case NL80211_CHAN_WIDTH_20:
2328 case NL80211_CHAN_WIDTH_40:
2329 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2330 cfg80211_get_chandef_type(chandef)))
2331 return -ENOBUFS;
2332 break;
2333 default:
2334 break;
2335 }
2336 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2337 return -ENOBUFS;
2338 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2339 return -ENOBUFS;
2340 if (chandef->center_freq2 &&
2341 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002342 return -ENOBUFS;
2343 return 0;
2344}
2345
Eric W. Biederman15e47302012-09-07 20:12:54 +00002346static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002347 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002348 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002349{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002350 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002351 void *hdr;
2352
Eric W. Biederman15e47302012-09-07 20:12:54 +00002353 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002354 if (!hdr)
2355 return -1;
2356
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002357 if (dev &&
2358 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002359 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002360 goto nla_put_failure;
2361
2362 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2363 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002364 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002365 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002366 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2367 rdev->devlist_generation ^
2368 (cfg80211_rdev_list_generation << 2)))
2369 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002370
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002371 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002372 int ret;
2373 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002374
Johannes Berg683b6d32012-11-08 21:25:48 +01002375 ret = rdev_get_channel(rdev, wdev, &chandef);
2376 if (ret == 0) {
2377 if (nl80211_send_chandef(msg, &chandef))
2378 goto nla_put_failure;
2379 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002380 }
2381
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002382 if (wdev->ssid_len) {
2383 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2384 goto nla_put_failure;
2385 }
2386
Johannes Berg55682962007-09-20 13:09:35 -04002387 return genlmsg_end(msg, hdr);
2388
2389 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002390 genlmsg_cancel(msg, hdr);
2391 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002392}
2393
2394static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2395{
2396 int wp_idx = 0;
2397 int if_idx = 0;
2398 int wp_start = cb->args[0];
2399 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002400 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002401 struct wireless_dev *wdev;
2402
Johannes Berg5fe231e2013-05-08 21:45:15 +02002403 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002404 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2405 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002406 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002407 if (wp_idx < wp_start) {
2408 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002409 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002410 }
Johannes Berg55682962007-09-20 13:09:35 -04002411 if_idx = 0;
2412
Johannes Berg89a54e42012-06-15 14:33:17 +02002413 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002414 if (if_idx < if_start) {
2415 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002416 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002417 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002418 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002419 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002420 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002421 goto out;
2422 }
2423 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002424 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002425
2426 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002427 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002428 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002429 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002430
2431 cb->args[0] = wp_idx;
2432 cb->args[1] = if_idx;
2433
2434 return skb->len;
2435}
2436
2437static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2438{
2439 struct sk_buff *msg;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002441 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002442
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002444 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002445 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002446
Eric W. Biederman15e47302012-09-07 20:12:54 +00002447 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002448 rdev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002449 nlmsg_free(msg);
2450 return -ENOBUFS;
2451 }
Johannes Berg55682962007-09-20 13:09:35 -04002452
Johannes Berg134e6372009-07-10 09:51:34 +00002453 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002454}
2455
Michael Wu66f7ac52008-01-31 19:48:22 +01002456static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2457 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2458 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2459 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2460 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2461 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002462 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002463};
2464
2465static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2466{
2467 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2468 int flag;
2469
2470 *mntrflags = 0;
2471
2472 if (!nla)
2473 return -EINVAL;
2474
2475 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2476 nla, mntr_flags_policy))
2477 return -EINVAL;
2478
2479 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2480 if (flags[flag])
2481 *mntrflags |= (1<<flag);
2482
2483 return 0;
2484}
2485
Johannes Berg9bc383d2009-11-19 11:55:19 +01002486static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002487 struct net_device *netdev, u8 use_4addr,
2488 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002489{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002490 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002491 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002492 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002493 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002494 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002495
2496 switch (iftype) {
2497 case NL80211_IFTYPE_AP_VLAN:
2498 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2499 return 0;
2500 break;
2501 case NL80211_IFTYPE_STATION:
2502 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2503 return 0;
2504 break;
2505 default:
2506 break;
2507 }
2508
2509 return -EOPNOTSUPP;
2510}
2511
Johannes Berg55682962007-09-20 13:09:35 -04002512static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2513{
Johannes Berg4c476992010-10-04 21:36:35 +02002514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002515 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002516 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002517 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002518 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002519 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002520 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002521
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002522 memset(&params, 0, sizeof(params));
2523
Johannes Berg04a773a2009-04-19 21:24:32 +02002524 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002525
Johannes Berg723b0382008-09-16 20:22:09 +02002526 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002527 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002528 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002529 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002530 if (ntype > NL80211_IFTYPE_MAX)
2531 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002532 }
2533
Johannes Berg92ffe052008-09-16 20:39:36 +02002534 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002535 struct wireless_dev *wdev = dev->ieee80211_ptr;
2536
Johannes Berg4c476992010-10-04 21:36:35 +02002537 if (ntype != NL80211_IFTYPE_MESH_POINT)
2538 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002539 if (netif_running(dev))
2540 return -EBUSY;
2541
2542 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);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002550 }
2551
Felix Fietkau8b787642009-11-10 18:53:10 +01002552 if (info->attrs[NL80211_ATTR_4ADDR]) {
2553 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2554 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002555 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002556 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002557 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002558 } else {
2559 params.use_4addr = -1;
2560 }
2561
Johannes Berg92ffe052008-09-16 20:39:36 +02002562 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002563 if (ntype != NL80211_IFTYPE_MONITOR)
2564 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002565 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2566 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002567 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002568 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002569
2570 flags = &_flags;
2571 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002572 }
Johannes Berg3b858752009-03-12 09:55:09 +01002573
Luciano Coelho18003292013-08-29 13:26:57 +03002574 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002575 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2576 return -EOPNOTSUPP;
2577
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002578 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002579 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002580 else
2581 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002582
Johannes Berg9bc383d2009-11-19 11:55:19 +01002583 if (!err && params.use_4addr != -1)
2584 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2585
Johannes Berg55682962007-09-20 13:09:35 -04002586 return err;
2587}
2588
2589static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2590{
Johannes Berg4c476992010-10-04 21:36:35 +02002591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002592 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002593 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002594 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002595 int err;
2596 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002597 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002598
Johannes Berg78f22b62014-03-24 17:57:27 +01002599 /* to avoid failing a new interface creation due to pending removal */
2600 cfg80211_destroy_ifaces(rdev);
2601
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002602 memset(&params, 0, sizeof(params));
2603
Johannes Berg55682962007-09-20 13:09:35 -04002604 if (!info->attrs[NL80211_ATTR_IFNAME])
2605 return -EINVAL;
2606
2607 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2608 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2609 if (type > NL80211_IFTYPE_MAX)
2610 return -EINVAL;
2611 }
2612
Johannes Berg79c97e92009-07-07 03:56:12 +02002613 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002614 !(rdev->wiphy.interface_modes & (1 << type)))
2615 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002616
Ben Greeare8f479b2014-10-22 12:23:05 -07002617 if ((type == NL80211_IFTYPE_P2P_DEVICE ||
2618 rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2619 info->attrs[NL80211_ATTR_MAC]) {
Arend van Spriel1c18f142013-01-08 10:17:27 +01002620 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2621 ETH_ALEN);
2622 if (!is_valid_ether_addr(params.macaddr))
2623 return -EADDRNOTAVAIL;
2624 }
2625
Johannes Berg9bc383d2009-11-19 11:55:19 +01002626 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002627 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002628 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002629 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002630 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002631 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002632
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2634 if (!msg)
2635 return -ENOMEM;
2636
Michael Wu66f7ac52008-01-31 19:48:22 +01002637 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2638 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2639 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002640
Luciano Coelho18003292013-08-29 13:26:57 +03002641 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002642 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2643 return -EOPNOTSUPP;
2644
Hila Gonene35e4d22012-06-27 17:19:42 +03002645 wdev = rdev_add_virtual_intf(rdev,
2646 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2647 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002648 if (IS_ERR(wdev)) {
2649 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002650 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002651 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002652
Johannes Berg78f22b62014-03-24 17:57:27 +01002653 if (info->attrs[NL80211_ATTR_IFACE_SOCKET_OWNER])
2654 wdev->owner_nlportid = info->snd_portid;
2655
Johannes Berg98104fde2012-06-16 00:19:54 +02002656 switch (type) {
2657 case NL80211_IFTYPE_MESH_POINT:
2658 if (!info->attrs[NL80211_ATTR_MESH_ID])
2659 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002660 wdev_lock(wdev);
2661 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2662 IEEE80211_MAX_MESH_ID_LEN);
2663 wdev->mesh_id_up_len =
2664 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2665 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2666 wdev->mesh_id_up_len);
2667 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002668 break;
2669 case NL80211_IFTYPE_P2P_DEVICE:
2670 /*
2671 * P2P Device doesn't have a netdev, so doesn't go
2672 * through the netdev notifier and must be added here
2673 */
2674 mutex_init(&wdev->mtx);
2675 INIT_LIST_HEAD(&wdev->event_list);
2676 spin_lock_init(&wdev->event_lock);
2677 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2678 spin_lock_init(&wdev->mgmt_registrations_lock);
2679
Johannes Berg98104fde2012-06-16 00:19:54 +02002680 wdev->identifier = ++rdev->wdev_id;
2681 list_add_rcu(&wdev->list, &rdev->wdev_list);
2682 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002683 break;
2684 default:
2685 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002686 }
2687
Eric W. Biederman15e47302012-09-07 20:12:54 +00002688 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002689 rdev, wdev) < 0) {
2690 nlmsg_free(msg);
2691 return -ENOBUFS;
2692 }
2693
2694 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002695}
2696
2697static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2698{
Johannes Berg4c476992010-10-04 21:36:35 +02002699 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002700 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002701
Johannes Berg4c476992010-10-04 21:36:35 +02002702 if (!rdev->ops->del_virtual_intf)
2703 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002704
Johannes Berg84efbb82012-06-16 00:00:26 +02002705 /*
2706 * If we remove a wireless device without a netdev then clear
2707 * user_ptr[1] so that nl80211_post_doit won't dereference it
2708 * to check if it needs to do dev_put(). Otherwise it crashes
2709 * since the wdev has been freed, unlike with a netdev where
2710 * we need the dev_put() for the netdev to really be freed.
2711 */
2712 if (!wdev->netdev)
2713 info->user_ptr[1] = NULL;
2714
Hila Gonene35e4d22012-06-27 17:19:42 +03002715 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002716}
2717
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002718static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2719{
2720 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2721 struct net_device *dev = info->user_ptr[1];
2722 u16 noack_map;
2723
2724 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2725 return -EINVAL;
2726
2727 if (!rdev->ops->set_noack_map)
2728 return -EOPNOTSUPP;
2729
2730 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2731
Hila Gonene35e4d22012-06-27 17:19:42 +03002732 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002733}
2734
Johannes Berg41ade002007-12-19 02:03:29 +01002735struct get_key_cookie {
2736 struct sk_buff *msg;
2737 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002738 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002739};
2740
2741static void get_key_callback(void *c, struct key_params *params)
2742{
Johannes Bergb9454e82009-07-08 13:29:08 +02002743 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002744 struct get_key_cookie *cookie = c;
2745
David S. Miller9360ffd2012-03-29 04:41:26 -04002746 if ((params->key &&
2747 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2748 params->key_len, params->key)) ||
2749 (params->seq &&
2750 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2751 params->seq_len, params->seq)) ||
2752 (params->cipher &&
2753 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2754 params->cipher)))
2755 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002756
Johannes Bergb9454e82009-07-08 13:29:08 +02002757 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2758 if (!key)
2759 goto nla_put_failure;
2760
David S. Miller9360ffd2012-03-29 04:41:26 -04002761 if ((params->key &&
2762 nla_put(cookie->msg, NL80211_KEY_DATA,
2763 params->key_len, params->key)) ||
2764 (params->seq &&
2765 nla_put(cookie->msg, NL80211_KEY_SEQ,
2766 params->seq_len, params->seq)) ||
2767 (params->cipher &&
2768 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2769 params->cipher)))
2770 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002771
David S. Miller9360ffd2012-03-29 04:41:26 -04002772 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2773 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002774
2775 nla_nest_end(cookie->msg, key);
2776
Johannes Berg41ade002007-12-19 02:03:29 +01002777 return;
2778 nla_put_failure:
2779 cookie->error = 1;
2780}
2781
2782static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2783{
Johannes Berg4c476992010-10-04 21:36:35 +02002784 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002785 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002786 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002787 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002788 const u8 *mac_addr = NULL;
2789 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002790 struct get_key_cookie cookie = {
2791 .error = 0,
2792 };
2793 void *hdr;
2794 struct sk_buff *msg;
2795
2796 if (info->attrs[NL80211_ATTR_KEY_IDX])
2797 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2798
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002799 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002800 return -EINVAL;
2801
2802 if (info->attrs[NL80211_ATTR_MAC])
2803 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2804
Johannes Berge31b8212010-10-05 19:39:30 +02002805 pairwise = !!mac_addr;
2806 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2807 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2808 if (kt >= NUM_NL80211_KEYTYPES)
2809 return -EINVAL;
2810 if (kt != NL80211_KEYTYPE_GROUP &&
2811 kt != NL80211_KEYTYPE_PAIRWISE)
2812 return -EINVAL;
2813 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2814 }
2815
Johannes Berg4c476992010-10-04 21:36:35 +02002816 if (!rdev->ops->get_key)
2817 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002818
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002819 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002820 if (!msg)
2821 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002822
Eric W. Biederman15e47302012-09-07 20:12:54 +00002823 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002824 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002825 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002826 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002827
2828 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002829 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002830
David S. Miller9360ffd2012-03-29 04:41:26 -04002831 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2832 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2833 goto nla_put_failure;
2834 if (mac_addr &&
2835 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2836 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002837
Johannes Berge31b8212010-10-05 19:39:30 +02002838 if (pairwise && mac_addr &&
2839 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2840 return -ENOENT;
2841
Hila Gonene35e4d22012-06-27 17:19:42 +03002842 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2843 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002844
2845 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002846 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002847
2848 if (cookie.error)
2849 goto nla_put_failure;
2850
2851 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002852 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002853
2854 nla_put_failure:
2855 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002856 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002857 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002858 return err;
2859}
2860
2861static int nl80211_set_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 Bergb9454e82009-07-08 13:29:08 +02002864 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002865 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002866 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002867
Johannes Bergb9454e82009-07-08 13:29:08 +02002868 err = nl80211_parse_key(info, &key);
2869 if (err)
2870 return err;
2871
2872 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002873 return -EINVAL;
2874
Johannes Bergb9454e82009-07-08 13:29:08 +02002875 /* only support setting default key */
2876 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002877 return -EINVAL;
2878
Johannes Bergfffd0932009-07-08 14:22:54 +02002879 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002880
2881 if (key.def) {
2882 if (!rdev->ops->set_default_key) {
2883 err = -EOPNOTSUPP;
2884 goto out;
2885 }
2886
2887 err = nl80211_key_allowed(dev->ieee80211_ptr);
2888 if (err)
2889 goto out;
2890
Hila Gonene35e4d22012-06-27 17:19:42 +03002891 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002892 key.def_uni, key.def_multi);
2893
2894 if (err)
2895 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002896
Johannes Berg3d23e342009-09-29 23:27:28 +02002897#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002898 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002899#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002900 } else {
2901 if (key.def_uni || !key.def_multi) {
2902 err = -EINVAL;
2903 goto out;
2904 }
2905
2906 if (!rdev->ops->set_default_mgmt_key) {
2907 err = -EOPNOTSUPP;
2908 goto out;
2909 }
2910
2911 err = nl80211_key_allowed(dev->ieee80211_ptr);
2912 if (err)
2913 goto out;
2914
Hila Gonene35e4d22012-06-27 17:19:42 +03002915 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002916 if (err)
2917 goto out;
2918
2919#ifdef CONFIG_CFG80211_WEXT
2920 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2921#endif
2922 }
2923
2924 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002925 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002926
Johannes Berg41ade002007-12-19 02:03:29 +01002927 return err;
2928}
2929
2930static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2931{
Johannes Berg4c476992010-10-04 21:36:35 +02002932 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002933 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002934 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002935 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002936 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002937
Johannes Bergb9454e82009-07-08 13:29:08 +02002938 err = nl80211_parse_key(info, &key);
2939 if (err)
2940 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002941
Johannes Bergb9454e82009-07-08 13:29:08 +02002942 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002943 return -EINVAL;
2944
Johannes Berg41ade002007-12-19 02:03:29 +01002945 if (info->attrs[NL80211_ATTR_MAC])
2946 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2947
Johannes Berge31b8212010-10-05 19:39:30 +02002948 if (key.type == -1) {
2949 if (mac_addr)
2950 key.type = NL80211_KEYTYPE_PAIRWISE;
2951 else
2952 key.type = NL80211_KEYTYPE_GROUP;
2953 }
2954
2955 /* for now */
2956 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2957 key.type != NL80211_KEYTYPE_GROUP)
2958 return -EINVAL;
2959
Johannes Berg4c476992010-10-04 21:36:35 +02002960 if (!rdev->ops->add_key)
2961 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002962
Johannes Berge31b8212010-10-05 19:39:30 +02002963 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2964 key.type == NL80211_KEYTYPE_PAIRWISE,
2965 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002966 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002967
2968 wdev_lock(dev->ieee80211_ptr);
2969 err = nl80211_key_allowed(dev->ieee80211_ptr);
2970 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002971 err = rdev_add_key(rdev, dev, key.idx,
2972 key.type == NL80211_KEYTYPE_PAIRWISE,
2973 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002974 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002975
Johannes Berg41ade002007-12-19 02:03:29 +01002976 return err;
2977}
2978
2979static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2980{
Johannes Berg4c476992010-10-04 21:36:35 +02002981 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002982 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002983 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002984 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002985 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002986
Johannes Bergb9454e82009-07-08 13:29:08 +02002987 err = nl80211_parse_key(info, &key);
2988 if (err)
2989 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002990
2991 if (info->attrs[NL80211_ATTR_MAC])
2992 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2993
Johannes Berge31b8212010-10-05 19:39:30 +02002994 if (key.type == -1) {
2995 if (mac_addr)
2996 key.type = NL80211_KEYTYPE_PAIRWISE;
2997 else
2998 key.type = NL80211_KEYTYPE_GROUP;
2999 }
3000
3001 /* for now */
3002 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3003 key.type != NL80211_KEYTYPE_GROUP)
3004 return -EINVAL;
3005
Johannes Berg4c476992010-10-04 21:36:35 +02003006 if (!rdev->ops->del_key)
3007 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01003008
Johannes Bergfffd0932009-07-08 14:22:54 +02003009 wdev_lock(dev->ieee80211_ptr);
3010 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02003011
3012 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
3013 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3014 err = -ENOENT;
3015
Johannes Bergfffd0932009-07-08 14:22:54 +02003016 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03003017 err = rdev_del_key(rdev, dev, key.idx,
3018 key.type == NL80211_KEYTYPE_PAIRWISE,
3019 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01003020
Johannes Berg3d23e342009-09-29 23:27:28 +02003021#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02003022 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02003023 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02003024 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02003025 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02003026 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3027 }
3028#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02003029 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02003030
Johannes Berg41ade002007-12-19 02:03:29 +01003031 return err;
3032}
3033
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303034/* This function returns an error or the number of nested attributes */
3035static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3036{
3037 struct nlattr *attr;
3038 int n_entries = 0, tmp;
3039
3040 nla_for_each_nested(attr, nl_attr, tmp) {
3041 if (nla_len(attr) != ETH_ALEN)
3042 return -EINVAL;
3043
3044 n_entries++;
3045 }
3046
3047 return n_entries;
3048}
3049
3050/*
3051 * This function parses ACL information and allocates memory for ACL data.
3052 * On successful return, the calling function is responsible to free the
3053 * ACL buffer returned by this function.
3054 */
3055static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3056 struct genl_info *info)
3057{
3058 enum nl80211_acl_policy acl_policy;
3059 struct nlattr *attr;
3060 struct cfg80211_acl_data *acl;
3061 int i = 0, n_entries, tmp;
3062
3063 if (!wiphy->max_acl_mac_addrs)
3064 return ERR_PTR(-EOPNOTSUPP);
3065
3066 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3067 return ERR_PTR(-EINVAL);
3068
3069 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3070 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3071 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3072 return ERR_PTR(-EINVAL);
3073
3074 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3075 return ERR_PTR(-EINVAL);
3076
3077 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3078 if (n_entries < 0)
3079 return ERR_PTR(n_entries);
3080
3081 if (n_entries > wiphy->max_acl_mac_addrs)
3082 return ERR_PTR(-ENOTSUPP);
3083
3084 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3085 GFP_KERNEL);
3086 if (!acl)
3087 return ERR_PTR(-ENOMEM);
3088
3089 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3090 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3091 i++;
3092 }
3093
3094 acl->n_acl_entries = n_entries;
3095 acl->acl_policy = acl_policy;
3096
3097 return acl;
3098}
3099
3100static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3101{
3102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3103 struct net_device *dev = info->user_ptr[1];
3104 struct cfg80211_acl_data *acl;
3105 int err;
3106
3107 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3108 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3109 return -EOPNOTSUPP;
3110
3111 if (!dev->ieee80211_ptr->beacon_interval)
3112 return -EINVAL;
3113
3114 acl = parse_acl_data(&rdev->wiphy, info);
3115 if (IS_ERR(acl))
3116 return PTR_ERR(acl);
3117
3118 err = rdev_set_mac_acl(rdev, dev, acl);
3119
3120 kfree(acl);
3121
3122 return err;
3123}
3124
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003125static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003126 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003127{
Johannes Berg88600202012-02-13 15:17:18 +01003128 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003129
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003130 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3131 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3132 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3133 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003134 return -EINVAL;
3135
Johannes Berg88600202012-02-13 15:17:18 +01003136 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003137
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003138 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3139 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3140 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003141 if (!bcn->head_len)
3142 return -EINVAL;
3143 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003144 }
3145
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003146 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3147 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3148 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003149 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003150 }
3151
Johannes Berg4c476992010-10-04 21:36:35 +02003152 if (!haveinfo)
3153 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003154
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003155 if (attrs[NL80211_ATTR_IE]) {
3156 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3157 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003158 }
3159
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003160 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003161 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003162 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003163 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003164 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003165 }
3166
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003167 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003168 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003169 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003170 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003171 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003172 }
3173
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003174 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3175 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3176 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003177 }
3178
Johannes Berg88600202012-02-13 15:17:18 +01003179 return 0;
3180}
3181
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003182static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3183 struct cfg80211_ap_settings *params)
3184{
3185 struct wireless_dev *wdev;
3186 bool ret = false;
3187
Johannes Berg89a54e42012-06-15 14:33:17 +02003188 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003189 if (wdev->iftype != NL80211_IFTYPE_AP &&
3190 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3191 continue;
3192
Johannes Berg683b6d32012-11-08 21:25:48 +01003193 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003194 continue;
3195
Johannes Berg683b6d32012-11-08 21:25:48 +01003196 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003197 ret = true;
3198 break;
3199 }
3200
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003201 return ret;
3202}
3203
Jouni Malinene39e5b52012-09-30 19:29:39 +03003204static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3205 enum nl80211_auth_type auth_type,
3206 enum nl80211_commands cmd)
3207{
3208 if (auth_type > NL80211_AUTHTYPE_MAX)
3209 return false;
3210
3211 switch (cmd) {
3212 case NL80211_CMD_AUTHENTICATE:
3213 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3214 auth_type == NL80211_AUTHTYPE_SAE)
3215 return false;
3216 return true;
3217 case NL80211_CMD_CONNECT:
3218 case NL80211_CMD_START_AP:
3219 /* SAE not supported yet */
3220 if (auth_type == NL80211_AUTHTYPE_SAE)
3221 return false;
3222 return true;
3223 default:
3224 return false;
3225 }
3226}
3227
Johannes Berg88600202012-02-13 15:17:18 +01003228static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3229{
3230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3231 struct net_device *dev = info->user_ptr[1];
3232 struct wireless_dev *wdev = dev->ieee80211_ptr;
3233 struct cfg80211_ap_settings params;
3234 int err;
3235
3236 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3237 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3238 return -EOPNOTSUPP;
3239
3240 if (!rdev->ops->start_ap)
3241 return -EOPNOTSUPP;
3242
3243 if (wdev->beacon_interval)
3244 return -EALREADY;
3245
3246 memset(&params, 0, sizeof(params));
3247
3248 /* these are required for START_AP */
3249 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3250 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3251 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3252 return -EINVAL;
3253
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003254 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003255 if (err)
3256 return err;
3257
3258 params.beacon_interval =
3259 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3260 params.dtim_period =
3261 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3262
3263 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3264 if (err)
3265 return err;
3266
3267 /*
3268 * In theory, some of these attributes should be required here
3269 * but since they were not used when the command was originally
3270 * added, keep them optional for old user space programs to let
3271 * them continue to work with drivers that do not need the
3272 * additional information -- drivers must check!
3273 */
3274 if (info->attrs[NL80211_ATTR_SSID]) {
3275 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3276 params.ssid_len =
3277 nla_len(info->attrs[NL80211_ATTR_SSID]);
3278 if (params.ssid_len == 0 ||
3279 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3280 return -EINVAL;
3281 }
3282
3283 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3284 params.hidden_ssid = nla_get_u32(
3285 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3286 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3287 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3288 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3289 return -EINVAL;
3290 }
3291
3292 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3293
3294 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3295 params.auth_type = nla_get_u32(
3296 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003297 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3298 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003299 return -EINVAL;
3300 } else
3301 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3302
3303 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3304 NL80211_MAX_NR_CIPHER_SUITES);
3305 if (err)
3306 return err;
3307
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303308 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3309 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3310 return -EOPNOTSUPP;
3311 params.inactivity_timeout = nla_get_u16(
3312 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3313 }
3314
Johannes Berg53cabad2012-11-14 15:17:28 +01003315 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3316 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3317 return -EINVAL;
3318 params.p2p_ctwindow =
3319 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3320 if (params.p2p_ctwindow > 127)
3321 return -EINVAL;
3322 if (params.p2p_ctwindow != 0 &&
3323 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3324 return -EINVAL;
3325 }
3326
3327 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3328 u8 tmp;
3329
3330 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3331 return -EINVAL;
3332 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3333 if (tmp > 1)
3334 return -EINVAL;
3335 params.p2p_opp_ps = tmp;
3336 if (params.p2p_opp_ps != 0 &&
3337 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3338 return -EINVAL;
3339 }
3340
Johannes Bergaa430da2012-05-16 23:50:18 +02003341 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003342 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3343 if (err)
3344 return err;
3345 } else if (wdev->preset_chandef.chan) {
3346 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003347 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003348 return -EINVAL;
3349
Ilan Peer174e0cd2014-02-23 09:13:01 +02003350 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
3351 wdev->iftype))
Johannes Bergaa430da2012-05-16 23:50:18 +02003352 return -EINVAL;
3353
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303354 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3355 params.acl = parse_acl_data(&rdev->wiphy, info);
3356 if (IS_ERR(params.acl))
3357 return PTR_ERR(params.acl);
3358 }
3359
Eliad Peller18998c32014-09-10 14:07:34 +03003360 if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
3361 params.smps_mode =
3362 nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
3363 switch (params.smps_mode) {
3364 case NL80211_SMPS_OFF:
3365 break;
3366 case NL80211_SMPS_STATIC:
3367 if (!(rdev->wiphy.features &
3368 NL80211_FEATURE_STATIC_SMPS))
3369 return -EINVAL;
3370 break;
3371 case NL80211_SMPS_DYNAMIC:
3372 if (!(rdev->wiphy.features &
3373 NL80211_FEATURE_DYNAMIC_SMPS))
3374 return -EINVAL;
3375 break;
3376 default:
3377 return -EINVAL;
3378 }
3379 } else {
3380 params.smps_mode = NL80211_SMPS_OFF;
3381 }
3382
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003383 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003384 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003385 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003386 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003387 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003388 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003389 wdev->ssid_len = params.ssid_len;
3390 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003391 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003392 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303393
3394 kfree(params.acl);
3395
Johannes Berg56d18932011-05-09 18:41:15 +02003396 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003397}
3398
Johannes Berg88600202012-02-13 15:17:18 +01003399static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3400{
3401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3402 struct net_device *dev = info->user_ptr[1];
3403 struct wireless_dev *wdev = dev->ieee80211_ptr;
3404 struct cfg80211_beacon_data params;
3405 int err;
3406
3407 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3408 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3409 return -EOPNOTSUPP;
3410
3411 if (!rdev->ops->change_beacon)
3412 return -EOPNOTSUPP;
3413
3414 if (!wdev->beacon_interval)
3415 return -EINVAL;
3416
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003417 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003418 if (err)
3419 return err;
3420
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003421 wdev_lock(wdev);
3422 err = rdev_change_beacon(rdev, dev, &params);
3423 wdev_unlock(wdev);
3424
3425 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003426}
3427
3428static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003429{
Johannes Berg4c476992010-10-04 21:36:35 +02003430 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3431 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003432
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003433 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003434}
3435
Johannes Berg5727ef12007-12-19 02:03:34 +01003436static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3437 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3438 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3439 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003440 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003441 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003442 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003443};
3444
Johannes Bergeccb8e82009-05-11 21:57:56 +03003445static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003446 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003447 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003448{
3449 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003450 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003451 int flag;
3452
Johannes Bergeccb8e82009-05-11 21:57:56 +03003453 /*
3454 * Try parsing the new attribute first so userspace
3455 * can specify both for older kernels.
3456 */
3457 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3458 if (nla) {
3459 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003460
Johannes Bergeccb8e82009-05-11 21:57:56 +03003461 sta_flags = nla_data(nla);
3462 params->sta_flags_mask = sta_flags->mask;
3463 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003464 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003465 if ((params->sta_flags_mask |
3466 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3467 return -EINVAL;
3468 return 0;
3469 }
3470
3471 /* if present, parse the old attribute */
3472
3473 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003474 if (!nla)
3475 return 0;
3476
3477 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3478 nla, sta_flags_policy))
3479 return -EINVAL;
3480
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003481 /*
3482 * Only allow certain flags for interface types so that
3483 * other attributes are silently ignored. Remember that
3484 * this is backward compatibility code with old userspace
3485 * and shouldn't be hit in other cases anyway.
3486 */
3487 switch (iftype) {
3488 case NL80211_IFTYPE_AP:
3489 case NL80211_IFTYPE_AP_VLAN:
3490 case NL80211_IFTYPE_P2P_GO:
3491 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3492 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3493 BIT(NL80211_STA_FLAG_WME) |
3494 BIT(NL80211_STA_FLAG_MFP);
3495 break;
3496 case NL80211_IFTYPE_P2P_CLIENT:
3497 case NL80211_IFTYPE_STATION:
3498 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3499 BIT(NL80211_STA_FLAG_TDLS_PEER);
3500 break;
3501 case NL80211_IFTYPE_MESH_POINT:
3502 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3503 BIT(NL80211_STA_FLAG_MFP) |
3504 BIT(NL80211_STA_FLAG_AUTHORIZED);
3505 default:
3506 return -EINVAL;
3507 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003508
Johannes Berg3383b5a2012-05-10 20:14:43 +02003509 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3510 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003511 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003512
Johannes Berg3383b5a2012-05-10 20:14:43 +02003513 /* no longer support new API additions in old API */
3514 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3515 return -EINVAL;
3516 }
3517 }
3518
Johannes Berg5727ef12007-12-19 02:03:34 +01003519 return 0;
3520}
3521
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003522static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3523 int attr)
3524{
3525 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003526 u32 bitrate;
3527 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003528
3529 rate = nla_nest_start(msg, attr);
3530 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003531 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003532
3533 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3534 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003535 /* report 16-bit bitrate only if we can */
3536 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003537 if (bitrate > 0 &&
3538 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3539 return false;
3540 if (bitrate_compat > 0 &&
3541 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3542 return false;
3543
3544 if (info->flags & RATE_INFO_FLAGS_MCS) {
3545 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3546 return false;
3547 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3548 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3549 return false;
3550 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3551 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3552 return false;
3553 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3554 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3555 return false;
3556 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3557 return false;
3558 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3559 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3560 return false;
3561 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3562 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3563 return false;
3564 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3565 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3566 return false;
3567 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3568 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3569 return false;
3570 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3571 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3572 return false;
3573 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003574
3575 nla_nest_end(msg, rate);
3576 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003577}
3578
Felix Fietkau119363c2013-04-22 16:29:30 +02003579static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3580 int id)
3581{
3582 void *attr;
3583 int i = 0;
3584
3585 if (!mask)
3586 return true;
3587
3588 attr = nla_nest_start(msg, id);
3589 if (!attr)
3590 return false;
3591
3592 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3593 if (!(mask & BIT(i)))
3594 continue;
3595
3596 if (nla_put_u8(msg, i, signal[i]))
3597 return false;
3598 }
3599
3600 nla_nest_end(msg, attr);
3601
3602 return true;
3603}
3604
Eric W. Biederman15e47302012-09-07 20:12:54 +00003605static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003606 int flags,
3607 struct cfg80211_registered_device *rdev,
3608 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003609 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003610{
3611 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003612 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003613
Eric W. Biederman15e47302012-09-07 20:12:54 +00003614 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003615 if (!hdr)
3616 return -1;
3617
David S. Miller9360ffd2012-03-29 04:41:26 -04003618 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3619 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3620 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3621 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003622
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003623 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3624 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003625 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003626 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3627 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3628 sinfo->connected_time))
3629 goto nla_put_failure;
3630 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3631 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3632 sinfo->inactive_time))
3633 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003634 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3635 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003636 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003637 (u32)sinfo->rx_bytes))
3638 goto nla_put_failure;
3639 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003640 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003641 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3642 (u32)sinfo->tx_bytes))
3643 goto nla_put_failure;
3644 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3645 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003646 sinfo->rx_bytes))
3647 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003648 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3649 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003650 sinfo->tx_bytes))
3651 goto nla_put_failure;
3652 if ((sinfo->filled & STATION_INFO_LLID) &&
3653 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3654 goto nla_put_failure;
3655 if ((sinfo->filled & STATION_INFO_PLID) &&
3656 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3657 goto nla_put_failure;
3658 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3659 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3660 sinfo->plink_state))
3661 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003662 switch (rdev->wiphy.signal_type) {
3663 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003664 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3665 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3666 sinfo->signal))
3667 goto nla_put_failure;
3668 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3669 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3670 sinfo->signal_avg))
3671 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003672 break;
3673 default:
3674 break;
3675 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003676 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3677 if (!nl80211_put_signal(msg, sinfo->chains,
3678 sinfo->chain_signal,
3679 NL80211_STA_INFO_CHAIN_SIGNAL))
3680 goto nla_put_failure;
3681 }
3682 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3683 if (!nl80211_put_signal(msg, sinfo->chains,
3684 sinfo->chain_signal_avg,
3685 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3686 goto nla_put_failure;
3687 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003688 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003689 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3690 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003691 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003692 }
3693 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3694 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3695 NL80211_STA_INFO_RX_BITRATE))
3696 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003697 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003698 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3699 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3700 sinfo->rx_packets))
3701 goto nla_put_failure;
3702 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3703 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3704 sinfo->tx_packets))
3705 goto nla_put_failure;
3706 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3707 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3708 sinfo->tx_retries))
3709 goto nla_put_failure;
3710 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3711 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3712 sinfo->tx_failed))
3713 goto nla_put_failure;
Antonio Quartulli867d8492014-05-19 21:53:19 +02003714 if ((sinfo->filled & STATION_INFO_EXPECTED_THROUGHPUT) &&
3715 nla_put_u32(msg, NL80211_STA_INFO_EXPECTED_THROUGHPUT,
3716 sinfo->expected_throughput))
3717 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003718 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3719 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3720 sinfo->beacon_loss_count))
3721 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003722 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3723 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3724 sinfo->local_pm))
3725 goto nla_put_failure;
3726 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3727 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3728 sinfo->peer_pm))
3729 goto nla_put_failure;
3730 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3731 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3732 sinfo->nonpeer_pm))
3733 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003734 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3735 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3736 if (!bss_param)
3737 goto nla_put_failure;
3738
David S. Miller9360ffd2012-03-29 04:41:26 -04003739 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3740 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3741 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3742 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3743 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3744 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3745 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3746 sinfo->bss_param.dtim_period) ||
3747 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3748 sinfo->bss_param.beacon_interval))
3749 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003750
3751 nla_nest_end(msg, bss_param);
3752 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003753 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3754 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3755 sizeof(struct nl80211_sta_flag_update),
3756 &sinfo->sta_flags))
3757 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003758 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3759 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3760 sinfo->t_offset))
3761 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003762 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003763
David S. Miller9360ffd2012-03-29 04:41:26 -04003764 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3765 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3766 sinfo->assoc_req_ies))
3767 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003768
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003769 return genlmsg_end(msg, hdr);
3770
3771 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003772 genlmsg_cancel(msg, hdr);
3773 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003774}
3775
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003776static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003777 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003778{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003779 struct station_info sinfo;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003780 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02003781 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003782 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003783 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003784 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003785
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003786 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003787 if (err)
3788 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003789
Johannes Berg97990a02013-04-19 01:02:55 +02003790 if (!wdev->netdev) {
3791 err = -EINVAL;
3792 goto out_err;
3793 }
3794
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003795 if (!rdev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003796 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003797 goto out_err;
3798 }
3799
Johannes Bergbba95fe2008-07-29 13:22:51 +02003800 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003801 memset(&sinfo, 0, sizeof(sinfo));
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003802 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003803 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003804 if (err == -ENOENT)
3805 break;
3806 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003807 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003808
3809 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003810 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003811 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003812 rdev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003813 &sinfo) < 0)
3814 goto out;
3815
3816 sta_idx++;
3817 }
3818
3819
3820 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003821 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003822 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003823 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003824 nl80211_finish_wdev_dump(rdev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003825
3826 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003827}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003828
Johannes Berg5727ef12007-12-19 02:03:34 +01003829static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3830{
Johannes Berg4c476992010-10-04 21:36:35 +02003831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3832 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003833 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003834 struct sk_buff *msg;
3835 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003836 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003837
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003838 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003839
3840 if (!info->attrs[NL80211_ATTR_MAC])
3841 return -EINVAL;
3842
3843 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3844
Johannes Berg4c476992010-10-04 21:36:35 +02003845 if (!rdev->ops->get_station)
3846 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003847
Hila Gonene35e4d22012-06-27 17:19:42 +03003848 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003849 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003850 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003851
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003852 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003853 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003854 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003855
Eric W. Biederman15e47302012-09-07 20:12:54 +00003856 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003857 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003858 nlmsg_free(msg);
3859 return -ENOBUFS;
3860 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003861
Johannes Berg4c476992010-10-04 21:36:35 +02003862 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003863}
3864
Johannes Berg77ee7c82013-02-15 00:48:33 +01003865int cfg80211_check_station_change(struct wiphy *wiphy,
3866 struct station_parameters *params,
3867 enum cfg80211_station_type statype)
3868{
3869 if (params->listen_interval != -1)
3870 return -EINVAL;
Arik Nemtsovc72e1142014-07-17 17:14:29 +03003871 if (params->aid &&
3872 !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berg77ee7c82013-02-15 00:48:33 +01003873 return -EINVAL;
3874
3875 /* When you run into this, adjust the code below for the new flag */
3876 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3877
3878 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003879 case CFG80211_STA_MESH_PEER_KERNEL:
3880 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003881 /*
3882 * No ignoring the TDLS flag here -- the userspace mesh
3883 * code doesn't have the bug of including TDLS in the
3884 * mask everywhere.
3885 */
3886 if (params->sta_flags_mask &
3887 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3888 BIT(NL80211_STA_FLAG_MFP) |
3889 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3890 return -EINVAL;
3891 break;
3892 case CFG80211_STA_TDLS_PEER_SETUP:
3893 case CFG80211_STA_TDLS_PEER_ACTIVE:
3894 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3895 return -EINVAL;
3896 /* ignore since it can't change */
3897 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3898 break;
3899 default:
3900 /* disallow mesh-specific things */
3901 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3902 return -EINVAL;
3903 if (params->local_pm)
3904 return -EINVAL;
3905 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3906 return -EINVAL;
3907 }
3908
3909 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3910 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3911 /* TDLS can't be set, ... */
3912 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3913 return -EINVAL;
3914 /*
3915 * ... but don't bother the driver with it. This works around
3916 * a hostapd/wpa_supplicant issue -- it always includes the
3917 * TLDS_PEER flag in the mask even for AP mode.
3918 */
3919 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3920 }
3921
3922 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3923 /* reject other things that can't change */
3924 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3925 return -EINVAL;
3926 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3927 return -EINVAL;
3928 if (params->supported_rates)
3929 return -EINVAL;
3930 if (params->ext_capab || params->ht_capa || params->vht_capa)
3931 return -EINVAL;
3932 }
3933
3934 if (statype != CFG80211_STA_AP_CLIENT) {
3935 if (params->vlan)
3936 return -EINVAL;
3937 }
3938
3939 switch (statype) {
3940 case CFG80211_STA_AP_MLME_CLIENT:
3941 /* Use this only for authorizing/unauthorizing a station */
3942 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3943 return -EOPNOTSUPP;
3944 break;
3945 case CFG80211_STA_AP_CLIENT:
3946 /* accept only the listed bits */
3947 if (params->sta_flags_mask &
3948 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3949 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3950 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3951 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3952 BIT(NL80211_STA_FLAG_WME) |
3953 BIT(NL80211_STA_FLAG_MFP)))
3954 return -EINVAL;
3955
3956 /* but authenticated/associated only if driver handles it */
3957 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3958 params->sta_flags_mask &
3959 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3960 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3961 return -EINVAL;
3962 break;
3963 case CFG80211_STA_IBSS:
3964 case CFG80211_STA_AP_STA:
3965 /* reject any changes other than AUTHORIZED */
3966 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3967 return -EINVAL;
3968 break;
3969 case CFG80211_STA_TDLS_PEER_SETUP:
3970 /* reject any changes other than AUTHORIZED or WME */
3971 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3972 BIT(NL80211_STA_FLAG_WME)))
3973 return -EINVAL;
3974 /* force (at least) rates when authorizing */
3975 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3976 !params->supported_rates)
3977 return -EINVAL;
3978 break;
3979 case CFG80211_STA_TDLS_PEER_ACTIVE:
3980 /* reject any changes */
3981 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003982 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003983 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3984 return -EINVAL;
3985 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003986 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003987 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3988 return -EINVAL;
3989 break;
3990 }
3991
3992 return 0;
3993}
3994EXPORT_SYMBOL(cfg80211_check_station_change);
3995
Johannes Berg5727ef12007-12-19 02:03:34 +01003996/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003997 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003998 */
Johannes Berg80b99892011-11-18 16:23:01 +01003999static struct net_device *get_vlan(struct genl_info *info,
4000 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01004001{
Johannes Berg463d0182009-07-14 00:33:35 +02004002 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01004003 struct net_device *v;
4004 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01004005
Johannes Berg80b99892011-11-18 16:23:01 +01004006 if (!vlanattr)
4007 return NULL;
4008
4009 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4010 if (!v)
4011 return ERR_PTR(-ENODEV);
4012
4013 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4014 ret = -EINVAL;
4015 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01004016 }
Johannes Berg80b99892011-11-18 16:23:01 +01004017
Johannes Berg77ee7c82013-02-15 00:48:33 +01004018 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4019 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4020 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4021 ret = -EINVAL;
4022 goto error;
4023 }
4024
Johannes Berg80b99892011-11-18 16:23:01 +01004025 if (!netif_running(v)) {
4026 ret = -ENETDOWN;
4027 goto error;
4028 }
4029
4030 return v;
4031 error:
4032 dev_put(v);
4033 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01004034}
4035
Johannes Berg94e860f2014-01-20 23:58:15 +01004036static const struct nla_policy
4037nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02004038 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4039 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4040};
4041
Johannes Bergff276692013-02-15 00:09:01 +01004042static int nl80211_parse_sta_wme(struct genl_info *info,
4043 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02004044{
Jouni Malinendf881292013-02-14 21:10:54 +02004045 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4046 struct nlattr *nla;
4047 int err;
4048
Jouni Malinendf881292013-02-14 21:10:54 +02004049 /* parse WME attributes if present */
4050 if (!info->attrs[NL80211_ATTR_STA_WME])
4051 return 0;
4052
4053 nla = info->attrs[NL80211_ATTR_STA_WME];
4054 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4055 nl80211_sta_wme_policy);
4056 if (err)
4057 return err;
4058
4059 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4060 params->uapsd_queues = nla_get_u8(
4061 tb[NL80211_STA_WME_UAPSD_QUEUES]);
4062 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4063 return -EINVAL;
4064
4065 if (tb[NL80211_STA_WME_MAX_SP])
4066 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4067
4068 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4069 return -EINVAL;
4070
4071 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4072
4073 return 0;
4074}
4075
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304076static int nl80211_parse_sta_channel_info(struct genl_info *info,
4077 struct station_parameters *params)
4078{
4079 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4080 params->supported_channels =
4081 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4082 params->supported_channels_len =
4083 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4084 /*
4085 * Need to include at least one (first channel, number of
4086 * channels) tuple for each subband, and must have proper
4087 * tuples for the rest of the data as well.
4088 */
4089 if (params->supported_channels_len < 2)
4090 return -EINVAL;
4091 if (params->supported_channels_len % 2)
4092 return -EINVAL;
4093 }
4094
4095 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4096 params->supported_oper_classes =
4097 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4098 params->supported_oper_classes_len =
4099 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4100 /*
4101 * The value of the Length field of the Supported Operating
4102 * Classes element is between 2 and 253.
4103 */
4104 if (params->supported_oper_classes_len < 2 ||
4105 params->supported_oper_classes_len > 253)
4106 return -EINVAL;
4107 }
4108 return 0;
4109}
4110
Johannes Bergff276692013-02-15 00:09:01 +01004111static int nl80211_set_station_tdls(struct genl_info *info,
4112 struct station_parameters *params)
4113{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304114 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004115 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004116 if (info->attrs[NL80211_ATTR_PEER_AID])
4117 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004118 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4119 params->ht_capa =
4120 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4121 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4122 params->vht_capa =
4123 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4124
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304125 err = nl80211_parse_sta_channel_info(info, params);
4126 if (err)
4127 return err;
4128
Johannes Bergff276692013-02-15 00:09:01 +01004129 return nl80211_parse_sta_wme(info, params);
4130}
4131
Johannes Berg5727ef12007-12-19 02:03:34 +01004132static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4133{
Johannes Berg4c476992010-10-04 21:36:35 +02004134 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004135 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004136 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004137 u8 *mac_addr;
4138 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004139
4140 memset(&params, 0, sizeof(params));
4141
4142 params.listen_interval = -1;
4143
Johannes Berg77ee7c82013-02-15 00:48:33 +01004144 if (!rdev->ops->change_station)
4145 return -EOPNOTSUPP;
4146
Johannes Berg5727ef12007-12-19 02:03:34 +01004147 if (info->attrs[NL80211_ATTR_STA_AID])
4148 return -EINVAL;
4149
4150 if (!info->attrs[NL80211_ATTR_MAC])
4151 return -EINVAL;
4152
4153 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4154
4155 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4156 params.supported_rates =
4157 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4158 params.supported_rates_len =
4159 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4160 }
4161
Jouni Malinen9d62a982013-02-14 21:10:13 +02004162 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4163 params.capability =
4164 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4165 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4166 }
4167
4168 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4169 params.ext_capab =
4170 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4171 params.ext_capab_len =
4172 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4173 }
4174
Jouni Malinendf881292013-02-14 21:10:54 +02004175 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004176 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004177
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004178 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004179 return -EINVAL;
4180
Johannes Bergf8bacc22013-02-14 23:27:01 +01004181 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004182 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004183 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4184 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4185 return -EINVAL;
4186 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004187
Johannes Bergf8bacc22013-02-14 23:27:01 +01004188 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004189 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004190 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4191 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4192 return -EINVAL;
4193 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4194 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004195
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004196 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4197 enum nl80211_mesh_power_mode pm = nla_get_u32(
4198 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4199
4200 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4201 pm > NL80211_MESH_POWER_MAX)
4202 return -EINVAL;
4203
4204 params.local_pm = pm;
4205 }
4206
Johannes Berg77ee7c82013-02-15 00:48:33 +01004207 /* Include parameters for TDLS peer (will check later) */
4208 err = nl80211_set_station_tdls(info, &params);
4209 if (err)
4210 return err;
4211
4212 params.vlan = get_vlan(info, rdev);
4213 if (IS_ERR(params.vlan))
4214 return PTR_ERR(params.vlan);
4215
Johannes Berga97f4422009-06-18 17:23:43 +02004216 switch (dev->ieee80211_ptr->iftype) {
4217 case NL80211_IFTYPE_AP:
4218 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004219 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004220 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004221 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004222 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004223 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004224 break;
4225 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004226 err = -EOPNOTSUPP;
4227 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004228 }
4229
Johannes Berg77ee7c82013-02-15 00:48:33 +01004230 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004231 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004232
Johannes Berg77ee7c82013-02-15 00:48:33 +01004233 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004234 if (params.vlan)
4235 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004236
Johannes Berg5727ef12007-12-19 02:03:34 +01004237 return err;
4238}
4239
4240static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4241{
Johannes Berg4c476992010-10-04 21:36:35 +02004242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004243 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004244 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004245 struct station_parameters params;
4246 u8 *mac_addr = NULL;
4247
4248 memset(&params, 0, sizeof(params));
4249
Johannes Berg984c3112013-02-14 23:43:25 +01004250 if (!rdev->ops->add_station)
4251 return -EOPNOTSUPP;
4252
Johannes Berg5727ef12007-12-19 02:03:34 +01004253 if (!info->attrs[NL80211_ATTR_MAC])
4254 return -EINVAL;
4255
Johannes Berg5727ef12007-12-19 02:03:34 +01004256 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4257 return -EINVAL;
4258
4259 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4260 return -EINVAL;
4261
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004262 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4263 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004264 return -EINVAL;
4265
Johannes Berg5727ef12007-12-19 02:03:34 +01004266 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4267 params.supported_rates =
4268 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4269 params.supported_rates_len =
4270 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4271 params.listen_interval =
4272 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004273
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004274 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004275 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004276 else
4277 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004278 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4279 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004280
Jouni Malinen9d62a982013-02-14 21:10:13 +02004281 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4282 params.capability =
4283 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4284 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4285 }
4286
4287 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4288 params.ext_capab =
4289 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4290 params.ext_capab_len =
4291 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4292 }
4293
Jouni Malinen36aedc902008-08-25 11:58:58 +03004294 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4295 params.ht_capa =
4296 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004297
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004298 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4299 params.vht_capa =
4300 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4301
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004302 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4303 params.opmode_notif_used = true;
4304 params.opmode_notif =
4305 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4306 }
4307
Johannes Bergf8bacc22013-02-14 23:27:01 +01004308 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004309 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004310 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4311 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4312 return -EINVAL;
4313 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004314
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304315 err = nl80211_parse_sta_channel_info(info, &params);
4316 if (err)
4317 return err;
4318
Johannes Bergff276692013-02-15 00:09:01 +01004319 err = nl80211_parse_sta_wme(info, &params);
4320 if (err)
4321 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004322
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004323 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004324 return -EINVAL;
4325
Johannes Berg77ee7c82013-02-15 00:48:33 +01004326 /* When you run into this, adjust the code below for the new flag */
4327 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4328
Johannes Bergbdd90d52011-12-14 12:20:27 +01004329 switch (dev->ieee80211_ptr->iftype) {
4330 case NL80211_IFTYPE_AP:
4331 case NL80211_IFTYPE_AP_VLAN:
4332 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004333 /* ignore WME attributes if iface/sta is not capable */
4334 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4335 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4336 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004337
Johannes Bergbdd90d52011-12-14 12:20:27 +01004338 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004339 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4340 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004341 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004342 /* but don't bother the driver with it */
4343 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004344
Johannes Bergd582cff2012-10-26 17:53:44 +02004345 /* allow authenticated/associated only if driver handles it */
4346 if (!(rdev->wiphy.features &
4347 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4348 params.sta_flags_mask &
4349 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4350 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4351 return -EINVAL;
4352
Johannes Bergbdd90d52011-12-14 12:20:27 +01004353 /* must be last in here for error handling */
4354 params.vlan = get_vlan(info, rdev);
4355 if (IS_ERR(params.vlan))
4356 return PTR_ERR(params.vlan);
4357 break;
4358 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004359 /* ignore uAPSD data */
4360 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4361
Johannes Bergd582cff2012-10-26 17:53:44 +02004362 /* associated is disallowed */
4363 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4364 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004365 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004366 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4367 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004368 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004369 break;
4370 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004371 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004372 /* ignore uAPSD data */
4373 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4374
Johannes Berg77ee7c82013-02-15 00:48:33 +01004375 /* these are disallowed */
4376 if (params.sta_flags_mask &
4377 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4378 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004379 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004380 /* Only TDLS peers can be added */
4381 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4382 return -EINVAL;
4383 /* Can only add if TDLS ... */
4384 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4385 return -EOPNOTSUPP;
4386 /* ... with external setup is supported */
4387 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4388 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004389 /*
4390 * Older wpa_supplicant versions always mark the TDLS peer
4391 * as authorized, but it shouldn't yet be.
4392 */
4393 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004394 break;
4395 default:
4396 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004397 }
4398
Johannes Bergbdd90d52011-12-14 12:20:27 +01004399 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004400
Hila Gonene35e4d22012-06-27 17:19:42 +03004401 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004402
Johannes Berg5727ef12007-12-19 02:03:34 +01004403 if (params.vlan)
4404 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004405 return err;
4406}
4407
4408static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4409{
Johannes Berg4c476992010-10-04 21:36:35 +02004410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4411 struct net_device *dev = info->user_ptr[1];
Jouni Malinen89c771e2014-10-10 20:52:40 +03004412 struct station_del_parameters params;
4413
4414 memset(&params, 0, sizeof(params));
Johannes Berg5727ef12007-12-19 02:03:34 +01004415
4416 if (info->attrs[NL80211_ATTR_MAC])
Jouni Malinen89c771e2014-10-10 20:52:40 +03004417 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004418
Johannes Berge80cf852009-05-11 14:43:13 +02004419 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004420 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004421 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004422 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4423 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004424
Johannes Berg4c476992010-10-04 21:36:35 +02004425 if (!rdev->ops->del_station)
4426 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004427
Jouni Malinen98856862014-10-20 13:20:45 +03004428 if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
4429 params.subtype =
4430 nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
4431 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
4432 params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
4433 return -EINVAL;
4434 } else {
4435 /* Default to Deauthentication frame */
4436 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
4437 }
4438
4439 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
4440 params.reason_code =
4441 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4442 if (params.reason_code == 0)
4443 return -EINVAL; /* 0 is reserved */
4444 } else {
4445 /* Default to reason code 2 */
4446 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
4447 }
4448
Jouni Malinen89c771e2014-10-10 20:52:40 +03004449 return rdev_del_station(rdev, dev, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004450}
4451
Eric W. Biederman15e47302012-09-07 20:12:54 +00004452static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004453 int flags, struct net_device *dev,
4454 u8 *dst, u8 *next_hop,
4455 struct mpath_info *pinfo)
4456{
4457 void *hdr;
4458 struct nlattr *pinfoattr;
4459
Henning Rogge1ef4c852014-11-04 16:14:58 +01004460 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004461 if (!hdr)
4462 return -1;
4463
David S. Miller9360ffd2012-03-29 04:41:26 -04004464 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4465 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4466 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4467 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4468 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004469
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4471 if (!pinfoattr)
4472 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004473 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4474 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4475 pinfo->frame_qlen))
4476 goto nla_put_failure;
4477 if (((pinfo->filled & MPATH_INFO_SN) &&
4478 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4479 ((pinfo->filled & MPATH_INFO_METRIC) &&
4480 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4481 pinfo->metric)) ||
4482 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4483 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4484 pinfo->exptime)) ||
4485 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4486 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4487 pinfo->flags)) ||
4488 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4489 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4490 pinfo->discovery_timeout)) ||
4491 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4492 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4493 pinfo->discovery_retries)))
4494 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004495
4496 nla_nest_end(msg, pinfoattr);
4497
4498 return genlmsg_end(msg, hdr);
4499
4500 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004501 genlmsg_cancel(msg, hdr);
4502 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004503}
4504
4505static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004506 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004507{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004508 struct mpath_info pinfo;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004509 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02004510 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004511 u8 dst[ETH_ALEN];
4512 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004513 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004514 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004515
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004516 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004517 if (err)
4518 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004519
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004520 if (!rdev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004521 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004522 goto out_err;
4523 }
4524
Johannes Berg97990a02013-04-19 01:02:55 +02004525 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004526 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004527 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004528 }
4529
Johannes Bergbba95fe2008-07-29 13:22:51 +02004530 while (1) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004531 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
Johannes Berg97990a02013-04-19 01:02:55 +02004532 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004533 if (err == -ENOENT)
4534 break;
4535 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004536 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004537
Eric W. Biederman15e47302012-09-07 20:12:54 +00004538 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004539 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004540 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004541 &pinfo) < 0)
4542 goto out;
4543
4544 path_idx++;
4545 }
4546
4547
4548 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004549 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004550 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004551 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004552 nl80211_finish_wdev_dump(rdev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004553 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004554}
4555
4556static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4557{
Johannes Berg4c476992010-10-04 21:36:35 +02004558 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004559 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004560 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004561 struct mpath_info pinfo;
4562 struct sk_buff *msg;
4563 u8 *dst = NULL;
4564 u8 next_hop[ETH_ALEN];
4565
4566 memset(&pinfo, 0, sizeof(pinfo));
4567
4568 if (!info->attrs[NL80211_ATTR_MAC])
4569 return -EINVAL;
4570
4571 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4572
Johannes Berg4c476992010-10-04 21:36:35 +02004573 if (!rdev->ops->get_mpath)
4574 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004575
Johannes Berg4c476992010-10-04 21:36:35 +02004576 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4577 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004578
Hila Gonene35e4d22012-06-27 17:19:42 +03004579 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004580 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004581 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004582
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004583 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004584 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004585 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004586
Eric W. Biederman15e47302012-09-07 20:12:54 +00004587 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004588 dev, dst, next_hop, &pinfo) < 0) {
4589 nlmsg_free(msg);
4590 return -ENOBUFS;
4591 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004592
Johannes Berg4c476992010-10-04 21:36:35 +02004593 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004594}
4595
4596static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4597{
Johannes Berg4c476992010-10-04 21:36:35 +02004598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4599 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004600 u8 *dst = NULL;
4601 u8 *next_hop = NULL;
4602
4603 if (!info->attrs[NL80211_ATTR_MAC])
4604 return -EINVAL;
4605
4606 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4607 return -EINVAL;
4608
4609 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4610 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4611
Johannes Berg4c476992010-10-04 21:36:35 +02004612 if (!rdev->ops->change_mpath)
4613 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004614
Johannes Berg4c476992010-10-04 21:36:35 +02004615 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4616 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004617
Hila Gonene35e4d22012-06-27 17:19:42 +03004618 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004619}
Johannes Berg4c476992010-10-04 21:36:35 +02004620
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004621static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4622{
Johannes Berg4c476992010-10-04 21:36:35 +02004623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4624 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004625 u8 *dst = NULL;
4626 u8 *next_hop = NULL;
4627
4628 if (!info->attrs[NL80211_ATTR_MAC])
4629 return -EINVAL;
4630
4631 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4632 return -EINVAL;
4633
4634 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4635 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4636
Johannes Berg4c476992010-10-04 21:36:35 +02004637 if (!rdev->ops->add_mpath)
4638 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004639
Johannes Berg4c476992010-10-04 21:36:35 +02004640 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4641 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004642
Hila Gonene35e4d22012-06-27 17:19:42 +03004643 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004644}
4645
4646static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4647{
Johannes Berg4c476992010-10-04 21:36:35 +02004648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4649 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004650 u8 *dst = NULL;
4651
4652 if (info->attrs[NL80211_ATTR_MAC])
4653 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4654
Johannes Berg4c476992010-10-04 21:36:35 +02004655 if (!rdev->ops->del_mpath)
4656 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004657
Hila Gonene35e4d22012-06-27 17:19:42 +03004658 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004659}
4660
Henning Rogge66be7d22014-09-12 08:58:49 +02004661static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
4662{
4663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4664 int err;
4665 struct net_device *dev = info->user_ptr[1];
4666 struct mpath_info pinfo;
4667 struct sk_buff *msg;
4668 u8 *dst = NULL;
4669 u8 mpp[ETH_ALEN];
4670
4671 memset(&pinfo, 0, sizeof(pinfo));
4672
4673 if (!info->attrs[NL80211_ATTR_MAC])
4674 return -EINVAL;
4675
4676 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4677
4678 if (!rdev->ops->get_mpp)
4679 return -EOPNOTSUPP;
4680
4681 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4682 return -EOPNOTSUPP;
4683
4684 err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
4685 if (err)
4686 return err;
4687
4688 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4689 if (!msg)
4690 return -ENOMEM;
4691
4692 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4693 dev, dst, mpp, &pinfo) < 0) {
4694 nlmsg_free(msg);
4695 return -ENOBUFS;
4696 }
4697
4698 return genlmsg_reply(msg, info);
4699}
4700
4701static int nl80211_dump_mpp(struct sk_buff *skb,
4702 struct netlink_callback *cb)
4703{
4704 struct mpath_info pinfo;
4705 struct cfg80211_registered_device *rdev;
4706 struct wireless_dev *wdev;
4707 u8 dst[ETH_ALEN];
4708 u8 mpp[ETH_ALEN];
4709 int path_idx = cb->args[2];
4710 int err;
4711
4712 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4713 if (err)
4714 return err;
4715
4716 if (!rdev->ops->dump_mpp) {
4717 err = -EOPNOTSUPP;
4718 goto out_err;
4719 }
4720
4721 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4722 err = -EOPNOTSUPP;
4723 goto out_err;
4724 }
4725
4726 while (1) {
4727 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
4728 mpp, &pinfo);
4729 if (err == -ENOENT)
4730 break;
4731 if (err)
4732 goto out_err;
4733
4734 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4735 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4736 wdev->netdev, dst, mpp,
4737 &pinfo) < 0)
4738 goto out;
4739
4740 path_idx++;
4741 }
4742
4743 out:
4744 cb->args[2] = path_idx;
4745 err = skb->len;
4746 out_err:
4747 nl80211_finish_wdev_dump(rdev);
4748 return err;
4749}
4750
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004751static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4752{
Johannes Berg4c476992010-10-04 21:36:35 +02004753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4754 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004755 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004756 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004757 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004758
4759 memset(&params, 0, sizeof(params));
4760 /* default to not changing parameters */
4761 params.use_cts_prot = -1;
4762 params.use_short_preamble = -1;
4763 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004764 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004765 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004766 params.p2p_ctwindow = -1;
4767 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004768
4769 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4770 params.use_cts_prot =
4771 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4772 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4773 params.use_short_preamble =
4774 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4775 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4776 params.use_short_slot_time =
4777 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004778 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4779 params.basic_rates =
4780 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4781 params.basic_rates_len =
4782 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4783 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004784 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4785 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004786 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4787 params.ht_opmode =
4788 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004789
Johannes Berg53cabad2012-11-14 15:17:28 +01004790 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4791 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4792 return -EINVAL;
4793 params.p2p_ctwindow =
4794 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4795 if (params.p2p_ctwindow < 0)
4796 return -EINVAL;
4797 if (params.p2p_ctwindow != 0 &&
4798 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4799 return -EINVAL;
4800 }
4801
4802 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4803 u8 tmp;
4804
4805 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4806 return -EINVAL;
4807 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4808 if (tmp > 1)
4809 return -EINVAL;
4810 params.p2p_opp_ps = tmp;
4811 if (params.p2p_opp_ps &&
4812 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4813 return -EINVAL;
4814 }
4815
Johannes Berg4c476992010-10-04 21:36:35 +02004816 if (!rdev->ops->change_bss)
4817 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004818
Johannes Berg074ac8d2010-09-16 14:58:22 +02004819 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004820 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4821 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004822
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004823 wdev_lock(wdev);
4824 err = rdev_change_bss(rdev, dev, &params);
4825 wdev_unlock(wdev);
4826
4827 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004828}
4829
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004830static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004831 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4832 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4833 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4834 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4835 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4836 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004837 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004838};
4839
4840static int parse_reg_rule(struct nlattr *tb[],
4841 struct ieee80211_reg_rule *reg_rule)
4842{
4843 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4844 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4845
4846 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4847 return -EINVAL;
4848 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4849 return -EINVAL;
4850 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4851 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004852 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4853 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004854 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4855 return -EINVAL;
4856
4857 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4858
4859 freq_range->start_freq_khz =
4860 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4861 freq_range->end_freq_khz =
4862 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004863 freq_range->max_bandwidth_khz =
4864 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004865
4866 power_rule->max_eirp =
4867 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4868
4869 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4870 power_rule->max_antenna_gain =
4871 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4872
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004873 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4874 reg_rule->dfs_cac_ms =
4875 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4876
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004877 return 0;
4878}
4879
4880static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4881{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004882 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004883 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004884
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004885 /*
4886 * You should only get this when cfg80211 hasn't yet initialized
4887 * completely when built-in to the kernel right between the time
4888 * window between nl80211_init() and regulatory_init(), if that is
4889 * even possible.
4890 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004891 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004892 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004893
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004894 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4895 user_reg_hint_type =
4896 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4897 else
4898 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4899
4900 switch (user_reg_hint_type) {
4901 case NL80211_USER_REG_HINT_USER:
4902 case NL80211_USER_REG_HINT_CELL_BASE:
Ilan Peer52616f22014-02-25 16:26:00 +02004903 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4904 return -EINVAL;
4905
4906 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4907 return regulatory_hint_user(data, user_reg_hint_type);
4908 case NL80211_USER_REG_HINT_INDOOR:
4909 return regulatory_hint_indoor_user();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004910 default:
4911 return -EINVAL;
4912 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004913}
4914
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004915static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004916 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004917{
Johannes Berg4c476992010-10-04 21:36:35 +02004918 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004919 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004920 struct wireless_dev *wdev = dev->ieee80211_ptr;
4921 struct mesh_config cur_params;
4922 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004923 void *hdr;
4924 struct nlattr *pinfoattr;
4925 struct sk_buff *msg;
4926
Johannes Berg29cbe682010-12-03 09:20:44 +01004927 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4928 return -EOPNOTSUPP;
4929
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004930 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004931 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004932
Johannes Berg29cbe682010-12-03 09:20:44 +01004933 wdev_lock(wdev);
4934 /* If not connected, get default parameters */
4935 if (!wdev->mesh_id_len)
4936 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4937 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004938 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004939 wdev_unlock(wdev);
4940
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004941 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004942 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004943
4944 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004945 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004946 if (!msg)
4947 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004948 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004949 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004950 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004951 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004952 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004953 if (!pinfoattr)
4954 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004955 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4956 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4957 cur_params.dot11MeshRetryTimeout) ||
4958 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4959 cur_params.dot11MeshConfirmTimeout) ||
4960 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4961 cur_params.dot11MeshHoldingTimeout) ||
4962 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4963 cur_params.dot11MeshMaxPeerLinks) ||
4964 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4965 cur_params.dot11MeshMaxRetries) ||
4966 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4967 cur_params.dot11MeshTTL) ||
4968 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4969 cur_params.element_ttl) ||
4970 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4971 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004972 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4973 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004974 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4975 cur_params.dot11MeshHWMPmaxPREQretries) ||
4976 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4977 cur_params.path_refresh_time) ||
4978 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4979 cur_params.min_discovery_timeout) ||
4980 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4981 cur_params.dot11MeshHWMPactivePathTimeout) ||
4982 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4983 cur_params.dot11MeshHWMPpreqMinInterval) ||
4984 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4985 cur_params.dot11MeshHWMPperrMinInterval) ||
4986 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4987 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4988 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4989 cur_params.dot11MeshHWMPRootMode) ||
4990 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4991 cur_params.dot11MeshHWMPRannInterval) ||
4992 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4993 cur_params.dot11MeshGateAnnouncementProtocol) ||
4994 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4995 cur_params.dot11MeshForwarding) ||
4996 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004997 cur_params.rssi_threshold) ||
4998 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004999 cur_params.ht_opmode) ||
5000 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5001 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5002 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005003 cur_params.dot11MeshHWMProotInterval) ||
5004 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005005 cur_params.dot11MeshHWMPconfirmationInterval) ||
5006 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5007 cur_params.power_mode) ||
5008 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005009 cur_params.dot11MeshAwakeWindowDuration) ||
5010 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5011 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04005012 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005013 nla_nest_end(msg, pinfoattr);
5014 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005015 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005016
Johannes Berg3b858752009-03-12 09:55:09 +01005017 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005018 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005019 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04005020 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02005021 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005022}
5023
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005024static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005025 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5026 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5027 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5028 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5029 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5030 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01005031 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005032 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07005033 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005034 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5035 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5036 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5037 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5038 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08005039 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005040 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07005041 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07005042 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07005043 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08005044 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005045 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5046 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005047 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5048 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005049 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005050 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5051 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005052 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005053};
5054
Javier Cardonac80d5452010-12-16 17:37:49 -08005055static const struct nla_policy
5056 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07005057 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08005058 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5059 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07005060 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07005061 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005062 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07005063 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005064 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07005065 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08005066};
5067
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005068static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005069 struct mesh_config *cfg,
5070 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005071{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005072 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005073 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005074
Marco Porschea54fba2013-01-07 16:04:48 +01005075#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5076do { \
5077 if (tb[attr]) { \
5078 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
5079 return -EINVAL; \
5080 cfg->param = fn(tb[attr]); \
5081 mask |= (1 << (attr - 1)); \
5082 } \
5083} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005084
5085
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005086 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005087 return -EINVAL;
5088 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005089 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005090 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005091 return -EINVAL;
5092
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005093 /* This makes sure that there aren't more than 32 mesh config
5094 * parameters (otherwise our bitfield scheme would not work.) */
5095 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5096
5097 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01005098 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005099 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5100 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005101 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005102 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5103 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005104 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005105 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
5106 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005107 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005108 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
5109 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005110 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005111 mask, NL80211_MESHCONF_MAX_RETRIES,
5112 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005113 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005114 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005115 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005116 mask, NL80211_MESHCONF_ELEMENT_TTL,
5117 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005118 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005119 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5120 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005121 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
5122 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005123 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5124 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005125 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005126 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5127 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005128 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005129 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
5130 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005131 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005132 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5133 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005134 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
5135 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005136 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5137 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005138 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01005139 1, 65535, mask,
5140 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005141 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08005142 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01005143 1, 65535, mask,
5144 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005145 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005146 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005147 dot11MeshHWMPnetDiameterTraversalTime,
5148 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005149 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5150 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005151 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
5152 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
5153 nla_get_u8);
5154 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
5155 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005156 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00005157 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005158 dot11MeshGateAnnouncementProtocol, 0, 1,
5159 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005160 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005161 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005162 mask, NL80211_MESHCONF_FORWARDING,
5163 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08005164 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005165 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08005166 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01005167 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005168 mask, NL80211_MESHCONF_HT_OPMODE,
5169 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005170 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01005171 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005172 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5173 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005174 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005175 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5176 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005177 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005178 dot11MeshHWMPconfirmationInterval,
5179 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005180 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5181 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005182 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
5183 NL80211_MESH_POWER_ACTIVE,
5184 NL80211_MESH_POWER_MAX,
5185 mask, NL80211_MESHCONF_POWER_MODE,
5186 nla_get_u32);
5187 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
5188 0, 65535, mask,
5189 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005190 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
5191 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
5192 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005193 if (mask_out)
5194 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08005195
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005196 return 0;
5197
5198#undef FILL_IN_MESH_PARAM_IF_SET
5199}
5200
Javier Cardonac80d5452010-12-16 17:37:49 -08005201static int nl80211_parse_mesh_setup(struct genl_info *info,
5202 struct mesh_setup *setup)
5203{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08005205 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5206
5207 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5208 return -EINVAL;
5209 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5210 info->attrs[NL80211_ATTR_MESH_SETUP],
5211 nl80211_mesh_setup_params_policy))
5212 return -EINVAL;
5213
Javier Cardonad299a1f2012-03-31 11:31:33 -07005214 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5215 setup->sync_method =
5216 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5217 IEEE80211_SYNC_METHOD_VENDOR :
5218 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5219
Javier Cardonac80d5452010-12-16 17:37:49 -08005220 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5221 setup->path_sel_proto =
5222 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5223 IEEE80211_PATH_PROTOCOL_VENDOR :
5224 IEEE80211_PATH_PROTOCOL_HWMP;
5225
5226 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5227 setup->path_metric =
5228 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5229 IEEE80211_PATH_METRIC_VENDOR :
5230 IEEE80211_PATH_METRIC_AIRTIME;
5231
Javier Cardona581a8b02011-04-07 15:08:27 -07005232
5233 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005234 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005235 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005236 if (!is_valid_ie_attr(ieattr))
5237 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005238 setup->ie = nla_data(ieattr);
5239 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005240 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005241 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5242 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5243 return -EINVAL;
5244 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005245 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5246 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005247 if (setup->is_secure)
5248 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005249
Colleen Twitty6e16d902013-05-08 11:45:59 -07005250 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5251 if (!setup->user_mpm)
5252 return -EINVAL;
5253 setup->auth_id =
5254 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5255 }
5256
Javier Cardonac80d5452010-12-16 17:37:49 -08005257 return 0;
5258}
5259
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005260static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005261 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005262{
5263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5264 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005265 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005266 struct mesh_config cfg;
5267 u32 mask;
5268 int err;
5269
Johannes Berg29cbe682010-12-03 09:20:44 +01005270 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5271 return -EOPNOTSUPP;
5272
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005273 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005274 return -EOPNOTSUPP;
5275
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005276 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005277 if (err)
5278 return err;
5279
Johannes Berg29cbe682010-12-03 09:20:44 +01005280 wdev_lock(wdev);
5281 if (!wdev->mesh_id_len)
5282 err = -ENOLINK;
5283
5284 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005285 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005286
5287 wdev_unlock(wdev);
5288
5289 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005290}
5291
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005292static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5293{
Johannes Berg458f4f92012-12-06 15:47:38 +01005294 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005295 struct sk_buff *msg;
5296 void *hdr = NULL;
5297 struct nlattr *nl_reg_rules;
5298 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005299
5300 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005301 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005302
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005303 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005304 if (!msg)
5305 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005306
Eric W. Biederman15e47302012-09-07 20:12:54 +00005307 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005308 NL80211_CMD_GET_REG);
5309 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005310 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005311
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005312 if (reg_last_request_cell_base() &&
5313 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5314 NL80211_USER_REG_HINT_CELL_BASE))
5315 goto nla_put_failure;
5316
Johannes Berg458f4f92012-12-06 15:47:38 +01005317 rcu_read_lock();
5318 regdom = rcu_dereference(cfg80211_regdomain);
5319
5320 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5321 (regdom->dfs_region &&
5322 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5323 goto nla_put_failure_rcu;
5324
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005325 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5326 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005327 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005328
Johannes Berg458f4f92012-12-06 15:47:38 +01005329 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005330 struct nlattr *nl_reg_rule;
5331 const struct ieee80211_reg_rule *reg_rule;
5332 const struct ieee80211_freq_range *freq_range;
5333 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005334 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005335
Johannes Berg458f4f92012-12-06 15:47:38 +01005336 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005337 freq_range = &reg_rule->freq_range;
5338 power_rule = &reg_rule->power_rule;
5339
5340 nl_reg_rule = nla_nest_start(msg, i);
5341 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005342 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005343
Janusz Dziedzic97524822014-01-30 09:52:20 +01005344 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5345 if (!max_bandwidth_khz)
5346 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5347 reg_rule);
5348
David S. Miller9360ffd2012-03-29 04:41:26 -04005349 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5350 reg_rule->flags) ||
5351 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5352 freq_range->start_freq_khz) ||
5353 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5354 freq_range->end_freq_khz) ||
5355 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005356 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005357 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5358 power_rule->max_antenna_gain) ||
5359 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005360 power_rule->max_eirp) ||
5361 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5362 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005363 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005364
5365 nla_nest_end(msg, nl_reg_rule);
5366 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005367 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005368
5369 nla_nest_end(msg, nl_reg_rules);
5370
5371 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005372 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005373
Johannes Berg458f4f92012-12-06 15:47:38 +01005374nla_put_failure_rcu:
5375 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005376nla_put_failure:
5377 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005378put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005379 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005380 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005381}
5382
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005383static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5384{
5385 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5386 struct nlattr *nl_reg_rule;
5387 char *alpha2 = NULL;
5388 int rem_reg_rules = 0, r = 0;
5389 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005390 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005391 struct ieee80211_regdomain *rd = NULL;
5392
5393 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5394 return -EINVAL;
5395
5396 if (!info->attrs[NL80211_ATTR_REG_RULES])
5397 return -EINVAL;
5398
5399 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5400
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005401 if (info->attrs[NL80211_ATTR_DFS_REGION])
5402 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5403
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005404 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005405 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005406 num_rules++;
5407 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005408 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005409 }
5410
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005411 if (!reg_is_valid_request(alpha2))
5412 return -EINVAL;
5413
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005414 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005415 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005416
5417 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005418 if (!rd)
5419 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005420
5421 rd->n_reg_rules = num_rules;
5422 rd->alpha2[0] = alpha2[0];
5423 rd->alpha2[1] = alpha2[1];
5424
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005425 /*
5426 * Disable DFS master mode if the DFS region was
5427 * not supported or known on this kernel.
5428 */
5429 if (reg_supported_dfs_region(dfs_region))
5430 rd->dfs_region = dfs_region;
5431
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005432 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005433 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005434 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5435 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5436 reg_rule_policy);
5437 if (r)
5438 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005439 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5440 if (r)
5441 goto bad_reg;
5442
5443 rule_idx++;
5444
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005445 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5446 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005447 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005448 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005449 }
5450
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005451 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005452 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005453 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005454
Johannes Bergd2372b32008-10-24 20:32:20 +02005455 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005456 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005457 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005458}
5459
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005460static int validate_scan_freqs(struct nlattr *freqs)
5461{
5462 struct nlattr *attr1, *attr2;
5463 int n_channels = 0, tmp1, tmp2;
5464
5465 nla_for_each_nested(attr1, freqs, tmp1) {
5466 n_channels++;
5467 /*
5468 * Some hardware has a limited channel list for
5469 * scanning, and it is pretty much nonsensical
5470 * to scan for a channel twice, so disallow that
5471 * and don't require drivers to check that the
5472 * channel list they get isn't longer than what
5473 * they can scan, as long as they can scan all
5474 * the channels they registered at once.
5475 */
5476 nla_for_each_nested(attr2, freqs, tmp2)
5477 if (attr1 != attr2 &&
5478 nla_get_u32(attr1) == nla_get_u32(attr2))
5479 return 0;
5480 }
5481
5482 return n_channels;
5483}
5484
Johannes Bergad2b26a2014-06-12 21:39:05 +02005485static int nl80211_parse_random_mac(struct nlattr **attrs,
5486 u8 *mac_addr, u8 *mac_addr_mask)
5487{
5488 int i;
5489
5490 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
5491 memset(mac_addr, 0, ETH_ALEN);
5492 memset(mac_addr_mask, 0, ETH_ALEN);
5493 mac_addr[0] = 0x2;
5494 mac_addr_mask[0] = 0x3;
5495
5496 return 0;
5497 }
5498
5499 /* need both or none */
5500 if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
5501 return -EINVAL;
5502
5503 memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
5504 memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
5505
5506 /* don't allow or configure an mcast address */
5507 if (!is_multicast_ether_addr(mac_addr_mask) ||
5508 is_multicast_ether_addr(mac_addr))
5509 return -EINVAL;
5510
5511 /*
5512 * allow users to pass a MAC address that has bits set outside
5513 * of the mask, but don't bother drivers with having to deal
5514 * with such bits
5515 */
5516 for (i = 0; i < ETH_ALEN; i++)
5517 mac_addr[i] &= mac_addr_mask[i];
5518
5519 return 0;
5520}
5521
Johannes Berg2a519312009-02-10 21:25:55 +01005522static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5523{
Johannes Berg4c476992010-10-04 21:36:35 +02005524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005525 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005526 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005527 struct nlattr *attr;
5528 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005529 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005530 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005531
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005532 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5533 return -EINVAL;
5534
Johannes Berg79c97e92009-07-07 03:56:12 +02005535 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005536
Johannes Berg4c476992010-10-04 21:36:35 +02005537 if (!rdev->ops->scan)
5538 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005539
Johannes Bergf9d15d12014-01-22 11:14:19 +02005540 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005541 err = -EBUSY;
5542 goto unlock;
5543 }
Johannes Berg2a519312009-02-10 21:25:55 +01005544
5545 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005546 n_channels = validate_scan_freqs(
5547 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005548 if (!n_channels) {
5549 err = -EINVAL;
5550 goto unlock;
5551 }
Johannes Berg2a519312009-02-10 21:25:55 +01005552 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005553 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005554 }
5555
5556 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5557 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5558 n_ssids++;
5559
Johannes Bergf9f47522013-03-19 15:04:07 +01005560 if (n_ssids > wiphy->max_scan_ssids) {
5561 err = -EINVAL;
5562 goto unlock;
5563 }
Johannes Berg2a519312009-02-10 21:25:55 +01005564
Jouni Malinen70692ad2009-02-16 19:39:13 +02005565 if (info->attrs[NL80211_ATTR_IE])
5566 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5567 else
5568 ie_len = 0;
5569
Johannes Bergf9f47522013-03-19 15:04:07 +01005570 if (ie_len > wiphy->max_scan_ie_len) {
5571 err = -EINVAL;
5572 goto unlock;
5573 }
Johannes Berg18a83652009-03-31 12:12:05 +02005574
Johannes Berg2a519312009-02-10 21:25:55 +01005575 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005576 + sizeof(*request->ssids) * n_ssids
5577 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005578 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005579 if (!request) {
5580 err = -ENOMEM;
5581 goto unlock;
5582 }
Johannes Berg2a519312009-02-10 21:25:55 +01005583
Johannes Berg2a519312009-02-10 21:25:55 +01005584 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005585 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005586 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005587 if (ie_len) {
5588 if (request->ssids)
5589 request->ie = (void *)(request->ssids + n_ssids);
5590 else
5591 request->ie = (void *)(request->channels + n_channels);
5592 }
Johannes Berg2a519312009-02-10 21:25:55 +01005593
Johannes Berg584991d2009-11-02 13:32:03 +01005594 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005595 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5596 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005597 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005598 struct ieee80211_channel *chan;
5599
5600 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5601
5602 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005603 err = -EINVAL;
5604 goto out_free;
5605 }
Johannes Berg584991d2009-11-02 13:32:03 +01005606
5607 /* ignore disabled channels */
5608 if (chan->flags & IEEE80211_CHAN_DISABLED)
5609 continue;
5610
5611 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005612 i++;
5613 }
5614 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005615 enum ieee80211_band band;
5616
Johannes Berg2a519312009-02-10 21:25:55 +01005617 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005618 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5619 int j;
5620 if (!wiphy->bands[band])
5621 continue;
5622 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005623 struct ieee80211_channel *chan;
5624
5625 chan = &wiphy->bands[band]->channels[j];
5626
5627 if (chan->flags & IEEE80211_CHAN_DISABLED)
5628 continue;
5629
5630 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005631 i++;
5632 }
5633 }
5634 }
5635
Johannes Berg584991d2009-11-02 13:32:03 +01005636 if (!i) {
5637 err = -EINVAL;
5638 goto out_free;
5639 }
5640
5641 request->n_channels = i;
5642
Johannes Berg2a519312009-02-10 21:25:55 +01005643 i = 0;
5644 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5645 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005646 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005647 err = -EINVAL;
5648 goto out_free;
5649 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005650 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005651 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005652 i++;
5653 }
5654 }
5655
Jouni Malinen70692ad2009-02-16 19:39:13 +02005656 if (info->attrs[NL80211_ATTR_IE]) {
5657 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005658 memcpy((void *)request->ie,
5659 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005660 request->ie_len);
5661 }
5662
Johannes Berg34850ab2011-07-18 18:08:35 +02005663 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005664 if (wiphy->bands[i])
5665 request->rates[i] =
5666 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005667
5668 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5669 nla_for_each_nested(attr,
5670 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5671 tmp) {
5672 enum ieee80211_band band = nla_type(attr);
5673
Dan Carpenter84404622011-07-29 11:52:18 +03005674 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005675 err = -EINVAL;
5676 goto out_free;
5677 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005678
5679 if (!wiphy->bands[band])
5680 continue;
5681
Johannes Berg34850ab2011-07-18 18:08:35 +02005682 err = ieee80211_get_ratemask(wiphy->bands[band],
5683 nla_data(attr),
5684 nla_len(attr),
5685 &request->rates[band]);
5686 if (err)
5687 goto out_free;
5688 }
5689 }
5690
Sam Leffler46856bb2012-10-11 21:03:32 -07005691 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005692 request->flags = nla_get_u32(
5693 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005694 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5695 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005696 err = -EOPNOTSUPP;
5697 goto out_free;
5698 }
Johannes Bergad2b26a2014-06-12 21:39:05 +02005699
5700 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
5701 if (!(wiphy->features &
5702 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)) {
5703 err = -EOPNOTSUPP;
5704 goto out_free;
5705 }
5706
5707 if (wdev->current_bss) {
5708 err = -EOPNOTSUPP;
5709 goto out_free;
5710 }
5711
5712 err = nl80211_parse_random_mac(info->attrs,
5713 request->mac_addr,
5714 request->mac_addr_mask);
5715 if (err)
5716 goto out_free;
5717 }
Sam Leffler46856bb2012-10-11 21:03:32 -07005718 }
Sam Lefflered4737712012-10-11 21:03:31 -07005719
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305720 request->no_cck =
5721 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5722
Johannes Bergfd014282012-06-18 19:17:03 +02005723 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005724 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005725 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005726
Johannes Berg79c97e92009-07-07 03:56:12 +02005727 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005728 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005729
Johannes Berg463d0182009-07-14 00:33:35 +02005730 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005731 nl80211_send_scan_start(rdev, wdev);
5732 if (wdev->netdev)
5733 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005734 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005735 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005736 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005737 kfree(request);
5738 }
Johannes Berg3b858752009-03-12 09:55:09 +01005739
Johannes Bergf9f47522013-03-19 15:04:07 +01005740 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005741 return err;
5742}
5743
Luciano Coelho256da022014-11-10 16:13:46 +02005744static struct cfg80211_sched_scan_request *
Johannes Bergad2b26a2014-06-12 21:39:05 +02005745nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
Luciano Coelho256da022014-11-10 16:13:46 +02005746 struct nlattr **attrs)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005747{
5748 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005749 struct nlattr *attr;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005750 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005751 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005752 enum ieee80211_band band;
5753 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005754 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005755 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005756
Luciano Coelho256da022014-11-10 16:13:46 +02005757 if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
5758 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005759
Luciano Coelho256da022014-11-10 16:13:46 +02005760 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5761 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005762
Luciano Coelho256da022014-11-10 16:13:46 +02005763 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005764 if (interval == 0)
Luciano Coelho256da022014-11-10 16:13:46 +02005765 return ERR_PTR(-EINVAL);
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005766
Luciano Coelho256da022014-11-10 16:13:46 +02005767 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005768 n_channels = validate_scan_freqs(
Luciano Coelho256da022014-11-10 16:13:46 +02005769 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005770 if (!n_channels)
Luciano Coelho256da022014-11-10 16:13:46 +02005771 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005772 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005773 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005774 }
5775
Luciano Coelho256da022014-11-10 16:13:46 +02005776 if (attrs[NL80211_ATTR_SCAN_SSIDS])
5777 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
Luciano Coelho807f8a82011-05-11 17:09:35 +03005778 tmp)
5779 n_ssids++;
5780
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005781 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho256da022014-11-10 16:13:46 +02005782 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005783
Johannes Bergea73cbc2014-01-24 10:53:53 +01005784 /*
5785 * First, count the number of 'real' matchsets. Due to an issue with
5786 * the old implementation, matchsets containing only the RSSI attribute
5787 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5788 * RSSI for all matchsets, rather than their own matchset for reporting
5789 * all APs with a strong RSSI. This is needed to be compatible with
5790 * older userspace that treated a matchset with only the RSSI as the
5791 * global RSSI for all other matchsets - if there are other matchsets.
5792 */
Luciano Coelho256da022014-11-10 16:13:46 +02005793 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005794 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02005795 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005796 tmp) {
5797 struct nlattr *rssi;
5798
5799 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5800 nla_data(attr), nla_len(attr),
5801 nl80211_match_policy);
5802 if (err)
Luciano Coelho256da022014-11-10 16:13:46 +02005803 return ERR_PTR(err);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005804 /* add other standalone attributes here */
5805 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5806 n_match_sets++;
5807 continue;
5808 }
5809 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5810 if (rssi)
5811 default_match_rssi = nla_get_s32(rssi);
5812 }
5813 }
5814
5815 /* However, if there's no other matchset, add the RSSI one */
5816 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5817 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005818
5819 if (n_match_sets > wiphy->max_match_sets)
Luciano Coelho256da022014-11-10 16:13:46 +02005820 return ERR_PTR(-EINVAL);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005821
Luciano Coelho256da022014-11-10 16:13:46 +02005822 if (attrs[NL80211_ATTR_IE])
5823 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005824 else
5825 ie_len = 0;
5826
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005827 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho256da022014-11-10 16:13:46 +02005828 return ERR_PTR(-EINVAL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005829
Luciano Coelho807f8a82011-05-11 17:09:35 +03005830 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005831 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005832 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005833 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005834 + ie_len, GFP_KERNEL);
Luciano Coelho256da022014-11-10 16:13:46 +02005835 if (!request)
5836 return ERR_PTR(-ENOMEM);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005837
5838 if (n_ssids)
5839 request->ssids = (void *)&request->channels[n_channels];
5840 request->n_ssids = n_ssids;
5841 if (ie_len) {
5842 if (request->ssids)
5843 request->ie = (void *)(request->ssids + n_ssids);
5844 else
5845 request->ie = (void *)(request->channels + n_channels);
5846 }
5847
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005848 if (n_match_sets) {
5849 if (request->ie)
5850 request->match_sets = (void *)(request->ie + ie_len);
5851 else if (request->ssids)
5852 request->match_sets =
5853 (void *)(request->ssids + n_ssids);
5854 else
5855 request->match_sets =
5856 (void *)(request->channels + n_channels);
5857 }
5858 request->n_match_sets = n_match_sets;
5859
Luciano Coelho807f8a82011-05-11 17:09:35 +03005860 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02005861 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005862 /* user specified, bail out if channel not found */
5863 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02005864 attrs[NL80211_ATTR_SCAN_FREQUENCIES],
Luciano Coelho807f8a82011-05-11 17:09:35 +03005865 tmp) {
5866 struct ieee80211_channel *chan;
5867
5868 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5869
5870 if (!chan) {
5871 err = -EINVAL;
5872 goto out_free;
5873 }
5874
5875 /* ignore disabled channels */
5876 if (chan->flags & IEEE80211_CHAN_DISABLED)
5877 continue;
5878
5879 request->channels[i] = chan;
5880 i++;
5881 }
5882 } else {
5883 /* all channels */
5884 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5885 int j;
5886 if (!wiphy->bands[band])
5887 continue;
5888 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5889 struct ieee80211_channel *chan;
5890
5891 chan = &wiphy->bands[band]->channels[j];
5892
5893 if (chan->flags & IEEE80211_CHAN_DISABLED)
5894 continue;
5895
5896 request->channels[i] = chan;
5897 i++;
5898 }
5899 }
5900 }
5901
5902 if (!i) {
5903 err = -EINVAL;
5904 goto out_free;
5905 }
5906
5907 request->n_channels = i;
5908
5909 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02005910 if (attrs[NL80211_ATTR_SCAN_SSIDS]) {
5911 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
Luciano Coelho807f8a82011-05-11 17:09:35 +03005912 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005913 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005914 err = -EINVAL;
5915 goto out_free;
5916 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005917 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005918 memcpy(request->ssids[i].ssid, nla_data(attr),
5919 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005920 i++;
5921 }
5922 }
5923
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005924 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02005925 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005926 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02005927 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005928 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005929 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005930
Johannes Bergae811e22014-01-24 10:17:47 +01005931 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5932 nla_data(attr), nla_len(attr),
5933 nl80211_match_policy);
5934 if (err)
5935 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005936 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005937 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005938 if (WARN_ON(i >= n_match_sets)) {
5939 /* this indicates a programming error,
5940 * the loop above should have verified
5941 * things properly
5942 */
5943 err = -EINVAL;
5944 goto out_free;
5945 }
5946
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005947 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5948 err = -EINVAL;
5949 goto out_free;
5950 }
5951 memcpy(request->match_sets[i].ssid.ssid,
5952 nla_data(ssid), nla_len(ssid));
5953 request->match_sets[i].ssid.ssid_len =
5954 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005955 /* special attribute - old implemenation w/a */
5956 request->match_sets[i].rssi_thold =
5957 default_match_rssi;
5958 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5959 if (rssi)
5960 request->match_sets[i].rssi_thold =
5961 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005962 }
5963 i++;
5964 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005965
5966 /* there was no other matchset, so the RSSI one is alone */
5967 if (i == 0)
5968 request->match_sets[0].rssi_thold = default_match_rssi;
5969
5970 request->min_rssi_thold = INT_MAX;
5971 for (i = 0; i < n_match_sets; i++)
5972 request->min_rssi_thold =
5973 min(request->match_sets[i].rssi_thold,
5974 request->min_rssi_thold);
5975 } else {
5976 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005977 }
5978
Johannes Berg9900e482014-02-04 21:01:25 +01005979 if (ie_len) {
5980 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005981 memcpy((void *)request->ie,
Luciano Coelho256da022014-11-10 16:13:46 +02005982 nla_data(attrs[NL80211_ATTR_IE]),
Luciano Coelho807f8a82011-05-11 17:09:35 +03005983 request->ie_len);
5984 }
5985
Luciano Coelho256da022014-11-10 16:13:46 +02005986 if (attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005987 request->flags = nla_get_u32(
Luciano Coelho256da022014-11-10 16:13:46 +02005988 attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005989 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5990 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005991 err = -EOPNOTSUPP;
5992 goto out_free;
5993 }
Johannes Bergad2b26a2014-06-12 21:39:05 +02005994
5995 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
5996 u32 flg = NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
5997
5998 if (!wdev) /* must be net-detect */
5999 flg = NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
6000
6001 if (!(wiphy->features & flg)) {
6002 err = -EOPNOTSUPP;
6003 goto out_free;
6004 }
6005
6006 if (wdev && wdev->current_bss) {
6007 err = -EOPNOTSUPP;
6008 goto out_free;
6009 }
6010
6011 err = nl80211_parse_random_mac(attrs, request->mac_addr,
6012 request->mac_addr_mask);
6013 if (err)
6014 goto out_free;
6015 }
Sam Leffler46856bb2012-10-11 21:03:32 -07006016 }
Sam Lefflered4737712012-10-11 21:03:31 -07006017
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03006018 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07006019 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006020
Luciano Coelho256da022014-11-10 16:13:46 +02006021 return request;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006022
6023out_free:
6024 kfree(request);
Luciano Coelho256da022014-11-10 16:13:46 +02006025 return ERR_PTR(err);
6026}
6027
6028static int nl80211_start_sched_scan(struct sk_buff *skb,
6029 struct genl_info *info)
6030{
6031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6032 struct net_device *dev = info->user_ptr[1];
Johannes Bergad2b26a2014-06-12 21:39:05 +02006033 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luciano Coelho256da022014-11-10 16:13:46 +02006034 int err;
6035
6036 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6037 !rdev->ops->sched_scan_start)
6038 return -EOPNOTSUPP;
6039
6040 if (rdev->sched_scan_req)
6041 return -EINPROGRESS;
6042
Johannes Bergad2b26a2014-06-12 21:39:05 +02006043 rdev->sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
Luciano Coelho256da022014-11-10 16:13:46 +02006044 info->attrs);
6045 err = PTR_ERR_OR_ZERO(rdev->sched_scan_req);
6046 if (err)
6047 goto out_err;
6048
6049 err = rdev_sched_scan_start(rdev, dev, rdev->sched_scan_req);
6050 if (err)
6051 goto out_free;
6052
6053 rdev->sched_scan_req->dev = dev;
6054 rdev->sched_scan_req->wiphy = &rdev->wiphy;
6055
6056 nl80211_send_sched_scan(rdev, dev,
6057 NL80211_CMD_START_SCHED_SCAN);
6058 return 0;
6059
6060out_free:
6061 kfree(rdev->sched_scan_req);
6062out_err:
6063 rdev->sched_scan_req = NULL;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006064 return err;
6065}
6066
6067static int nl80211_stop_sched_scan(struct sk_buff *skb,
6068 struct genl_info *info)
6069{
6070 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6071
6072 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6073 !rdev->ops->sched_scan_stop)
6074 return -EOPNOTSUPP;
6075
Johannes Berg5fe231e2013-05-08 21:45:15 +02006076 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03006077}
6078
Simon Wunderlich04f39042013-02-08 18:16:19 +01006079static int nl80211_start_radar_detection(struct sk_buff *skb,
6080 struct genl_info *info)
6081{
6082 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6083 struct net_device *dev = info->user_ptr[1];
6084 struct wireless_dev *wdev = dev->ieee80211_ptr;
6085 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01006086 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01006087 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01006088 int err;
6089
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01006090 dfs_region = reg_get_dfs_region(wdev->wiphy);
6091 if (dfs_region == NL80211_DFS_UNSET)
6092 return -EINVAL;
6093
Simon Wunderlich04f39042013-02-08 18:16:19 +01006094 err = nl80211_parse_chandef(rdev, info, &chandef);
6095 if (err)
6096 return err;
6097
Simon Wunderlichff311bc2013-09-03 19:43:18 +02006098 if (netif_carrier_ok(dev))
6099 return -EBUSY;
6100
Simon Wunderlich04f39042013-02-08 18:16:19 +01006101 if (wdev->cac_started)
6102 return -EBUSY;
6103
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02006104 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
Luciano Coelho00ec75f2014-05-15 13:05:39 +03006105 wdev->iftype);
Simon Wunderlich04f39042013-02-08 18:16:19 +01006106 if (err < 0)
6107 return err;
6108
6109 if (err == 0)
6110 return -EINVAL;
6111
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01006112 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01006113 return -EINVAL;
6114
6115 if (!rdev->ops->start_radar_detection)
6116 return -EOPNOTSUPP;
6117
Janusz Dziedzic31559f32014-02-21 19:46:13 +01006118 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
6119 if (WARN_ON(!cac_time_ms))
6120 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
6121
6122 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
6123 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01006124 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01006125 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01006126 wdev->cac_started = true;
6127 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01006128 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01006129 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01006130 return err;
6131}
6132
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006133static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
6134{
6135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6136 struct net_device *dev = info->user_ptr[1];
6137 struct wireless_dev *wdev = dev->ieee80211_ptr;
6138 struct cfg80211_csa_settings params;
6139 /* csa_attrs is defined static to avoid waste of stack size - this
6140 * function is called under RTNL lock, so this should not be a problem.
6141 */
6142 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006143 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006144 bool need_new_beacon = false;
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006145 int len, i;
Luciano Coelho252e07c2014-10-08 09:48:34 +03006146 u32 cs_count;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006147
6148 if (!rdev->ops->channel_switch ||
6149 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
6150 return -EOPNOTSUPP;
6151
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006152 switch (dev->ieee80211_ptr->iftype) {
6153 case NL80211_IFTYPE_AP:
6154 case NL80211_IFTYPE_P2P_GO:
6155 need_new_beacon = true;
6156
6157 /* useless if AP is not running */
6158 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01006159 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006160 break;
6161 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01006162 if (!wdev->ssid_len)
6163 return -ENOTCONN;
6164 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07006165 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01006166 if (!wdev->mesh_id_len)
6167 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006168 break;
6169 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006170 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006171 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006172
6173 memset(&params, 0, sizeof(params));
6174
6175 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6176 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
6177 return -EINVAL;
6178
6179 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02006180 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006181 return -EINVAL;
6182
Luciano Coelho252e07c2014-10-08 09:48:34 +03006183 /* Even though the attribute is u32, the specification says
6184 * u8, so let's make sure we don't overflow.
6185 */
6186 cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
6187 if (cs_count > 255)
6188 return -EINVAL;
6189
6190 params.count = cs_count;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006191
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006192 if (!need_new_beacon)
6193 goto skip_beacons;
6194
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006195 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
6196 if (err)
6197 return err;
6198
6199 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
6200 info->attrs[NL80211_ATTR_CSA_IES],
6201 nl80211_policy);
6202 if (err)
6203 return err;
6204
6205 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
6206 if (err)
6207 return err;
6208
6209 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
6210 return -EINVAL;
6211
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006212 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6213 if (!len || (len % sizeof(u16)))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006214 return -EINVAL;
6215
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006216 params.n_counter_offsets_beacon = len / sizeof(u16);
6217 if (rdev->wiphy.max_num_csa_counters &&
6218 (params.n_counter_offsets_beacon >
6219 rdev->wiphy.max_num_csa_counters))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006220 return -EINVAL;
6221
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006222 params.counter_offsets_beacon =
6223 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6224
6225 /* sanity checks - counters should fit and be the same */
6226 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
6227 u16 offset = params.counter_offsets_beacon[i];
6228
6229 if (offset >= params.beacon_csa.tail_len)
6230 return -EINVAL;
6231
6232 if (params.beacon_csa.tail[offset] != params.count)
6233 return -EINVAL;
6234 }
6235
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006236 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006237 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6238 if (!len || (len % sizeof(u16)))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006239 return -EINVAL;
6240
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006241 params.n_counter_offsets_presp = len / sizeof(u16);
6242 if (rdev->wiphy.max_num_csa_counters &&
6243 (params.n_counter_offsets_beacon >
6244 rdev->wiphy.max_num_csa_counters))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006245 return -EINVAL;
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006246
6247 params.counter_offsets_presp =
6248 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6249
6250 /* sanity checks - counters should fit and be the same */
6251 for (i = 0; i < params.n_counter_offsets_presp; i++) {
6252 u16 offset = params.counter_offsets_presp[i];
6253
6254 if (offset >= params.beacon_csa.probe_resp_len)
6255 return -EINVAL;
6256
6257 if (params.beacon_csa.probe_resp[offset] !=
6258 params.count)
6259 return -EINVAL;
6260 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006261 }
6262
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006263skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006264 err = nl80211_parse_chandef(rdev, info, &params.chandef);
6265 if (err)
6266 return err;
6267
Ilan Peer174e0cd2014-02-23 09:13:01 +02006268 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
6269 wdev->iftype))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006270 return -EINVAL;
6271
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02006272 err = cfg80211_chandef_dfs_required(wdev->wiphy,
6273 &params.chandef,
6274 wdev->iftype);
6275 if (err < 0)
6276 return err;
6277
Fabian Frederickdcc6c2f2014-10-25 17:57:35 +02006278 if (err > 0)
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02006279 params.radar_required = true;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006280
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006281 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
6282 params.block_tx = true;
6283
Simon Wunderlichc56589e2013-11-21 18:19:49 +01006284 wdev_lock(wdev);
6285 err = rdev_channel_switch(rdev, dev, &params);
6286 wdev_unlock(wdev);
6287
6288 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006289}
6290
Johannes Berg9720bb32011-06-21 09:45:33 +02006291static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
6292 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01006293 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02006294 struct wireless_dev *wdev,
6295 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01006296{
Johannes Berg48ab9052009-07-10 18:42:31 +02006297 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01006298 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01006299 void *hdr;
6300 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02006301
6302 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006303
Eric W. Biederman15e47302012-09-07 20:12:54 +00006304 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01006305 NL80211_CMD_NEW_SCAN_RESULTS);
6306 if (!hdr)
6307 return -1;
6308
Johannes Berg9720bb32011-06-21 09:45:33 +02006309 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6310
Johannes Berg97990a02013-04-19 01:02:55 +02006311 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6312 goto nla_put_failure;
6313 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04006314 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6315 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02006316 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6317 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006318
6319 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6320 if (!bss)
6321 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006322 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006323 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006324 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006325
6326 rcu_read_lock();
Johannes Berg0e227082014-08-12 20:34:30 +02006327 /* indicate whether we have probe response data or not */
6328 if (rcu_access_pointer(res->proberesp_ies) &&
6329 nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
6330 goto fail_unlock_rcu;
6331
6332 /* this pointer prefers to be pointed to probe response data
6333 * but is always valid
6334 */
Johannes Berg9caf0362012-11-29 01:25:20 +01006335 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006336 if (ies) {
6337 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6338 goto fail_unlock_rcu;
Johannes Berg8cef2c92013-02-05 16:54:31 +01006339 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6340 ies->len, ies->data))
6341 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006342 }
Johannes Berg0e227082014-08-12 20:34:30 +02006343
6344 /* and this pointer is always (unless driver didn't know) beacon data */
Johannes Berg9caf0362012-11-29 01:25:20 +01006345 ies = rcu_dereference(res->beacon_ies);
Johannes Berg0e227082014-08-12 20:34:30 +02006346 if (ies && ies->from_beacon) {
6347 if (nla_put_u64(msg, NL80211_BSS_BEACON_TSF, ies->tsf))
Johannes Berg8cef2c92013-02-05 16:54:31 +01006348 goto fail_unlock_rcu;
6349 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6350 ies->len, ies->data))
6351 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006352 }
6353 rcu_read_unlock();
6354
David S. Miller9360ffd2012-03-29 04:41:26 -04006355 if (res->beacon_interval &&
6356 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6357 goto nla_put_failure;
6358 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6359 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006360 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006361 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6362 jiffies_to_msecs(jiffies - intbss->ts)))
6363 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006364
Johannes Berg77965c92009-02-18 18:45:06 +01006365 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006366 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006367 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6368 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006369 break;
6370 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006371 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6372 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006373 break;
6374 default:
6375 break;
6376 }
6377
Johannes Berg48ab9052009-07-10 18:42:31 +02006378 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006379 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006380 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006381 if (intbss == wdev->current_bss &&
6382 nla_put_u32(msg, NL80211_BSS_STATUS,
6383 NL80211_BSS_STATUS_ASSOCIATED))
6384 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006385 break;
6386 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006387 if (intbss == wdev->current_bss &&
6388 nla_put_u32(msg, NL80211_BSS_STATUS,
6389 NL80211_BSS_STATUS_IBSS_JOINED))
6390 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006391 break;
6392 default:
6393 break;
6394 }
6395
Johannes Berg2a519312009-02-10 21:25:55 +01006396 nla_nest_end(msg, bss);
6397
6398 return genlmsg_end(msg, hdr);
6399
Johannes Berg8cef2c92013-02-05 16:54:31 +01006400 fail_unlock_rcu:
6401 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006402 nla_put_failure:
6403 genlmsg_cancel(msg, hdr);
6404 return -EMSGSIZE;
6405}
6406
Johannes Berg97990a02013-04-19 01:02:55 +02006407static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006408{
Johannes Berg48ab9052009-07-10 18:42:31 +02006409 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006410 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006411 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006412 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006413 int err;
6414
Johannes Berg97990a02013-04-19 01:02:55 +02006415 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006416 if (err)
6417 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006418
Johannes Berg48ab9052009-07-10 18:42:31 +02006419 wdev_lock(wdev);
6420 spin_lock_bh(&rdev->bss_lock);
6421 cfg80211_bss_expire(rdev);
6422
Johannes Berg9720bb32011-06-21 09:45:33 +02006423 cb->seq = rdev->bss_generation;
6424
Johannes Berg48ab9052009-07-10 18:42:31 +02006425 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006426 if (++idx <= start)
6427 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006428 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006429 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006430 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006431 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006432 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006433 }
6434 }
6435
Johannes Berg48ab9052009-07-10 18:42:31 +02006436 spin_unlock_bh(&rdev->bss_lock);
6437 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006438
Johannes Berg97990a02013-04-19 01:02:55 +02006439 cb->args[2] = idx;
6440 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006441
Johannes Berg67748892010-10-04 21:14:06 +02006442 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006443}
6444
Eric W. Biederman15e47302012-09-07 20:12:54 +00006445static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006446 int flags, struct net_device *dev,
6447 struct survey_info *survey)
6448{
6449 void *hdr;
6450 struct nlattr *infoattr;
6451
Eric W. Biederman15e47302012-09-07 20:12:54 +00006452 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006453 NL80211_CMD_NEW_SURVEY_RESULTS);
6454 if (!hdr)
6455 return -ENOMEM;
6456
David S. Miller9360ffd2012-03-29 04:41:26 -04006457 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6458 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006459
6460 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6461 if (!infoattr)
6462 goto nla_put_failure;
6463
David S. Miller9360ffd2012-03-29 04:41:26 -04006464 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6465 survey->channel->center_freq))
6466 goto nla_put_failure;
6467
6468 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6469 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6470 goto nla_put_failure;
6471 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6472 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6473 goto nla_put_failure;
6474 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6475 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6476 survey->channel_time))
6477 goto nla_put_failure;
6478 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6479 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6480 survey->channel_time_busy))
6481 goto nla_put_failure;
6482 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6483 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6484 survey->channel_time_ext_busy))
6485 goto nla_put_failure;
6486 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6487 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6488 survey->channel_time_rx))
6489 goto nla_put_failure;
6490 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6491 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6492 survey->channel_time_tx))
6493 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006494
6495 nla_nest_end(msg, infoattr);
6496
6497 return genlmsg_end(msg, hdr);
6498
6499 nla_put_failure:
6500 genlmsg_cancel(msg, hdr);
6501 return -EMSGSIZE;
6502}
6503
6504static int nl80211_dump_survey(struct sk_buff *skb,
6505 struct netlink_callback *cb)
6506{
6507 struct survey_info survey;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006508 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006509 struct wireless_dev *wdev;
6510 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006511 int res;
6512
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006513 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006514 if (res)
6515 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006516
Johannes Berg97990a02013-04-19 01:02:55 +02006517 if (!wdev->netdev) {
6518 res = -EINVAL;
6519 goto out_err;
6520 }
6521
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006522 if (!rdev->ops->dump_survey) {
Holger Schurig61fa7132009-11-11 12:25:40 +01006523 res = -EOPNOTSUPP;
6524 goto out_err;
6525 }
6526
6527 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006528 struct ieee80211_channel *chan;
6529
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006530 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006531 if (res == -ENOENT)
6532 break;
6533 if (res)
6534 goto out_err;
6535
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006536 /* Survey without a channel doesn't make sense */
6537 if (!survey.channel) {
6538 res = -EINVAL;
6539 goto out;
6540 }
6541
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006542 chan = ieee80211_get_channel(&rdev->wiphy,
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006543 survey.channel->center_freq);
6544 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6545 survey_idx++;
6546 continue;
6547 }
6548
Holger Schurig61fa7132009-11-11 12:25:40 +01006549 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006550 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006551 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006552 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006553 goto out;
6554 survey_idx++;
6555 }
6556
6557 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006558 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006559 res = skb->len;
6560 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006561 nl80211_finish_wdev_dump(rdev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006562 return res;
6563}
6564
Samuel Ortizb23aa672009-07-01 21:26:54 +02006565static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6566{
6567 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6568 NL80211_WPA_VERSION_2));
6569}
6570
Jouni Malinen636a5d32009-03-19 13:39:22 +02006571static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6572{
Johannes Berg4c476992010-10-04 21:36:35 +02006573 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6574 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006575 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006576 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6577 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006578 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006579 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006580 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006581
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006582 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6583 return -EINVAL;
6584
6585 if (!info->attrs[NL80211_ATTR_MAC])
6586 return -EINVAL;
6587
Jouni Malinen17780922009-03-27 20:52:47 +02006588 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6589 return -EINVAL;
6590
Johannes Berg19957bb2009-07-02 17:20:43 +02006591 if (!info->attrs[NL80211_ATTR_SSID])
6592 return -EINVAL;
6593
6594 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6595 return -EINVAL;
6596
Johannes Bergfffd0932009-07-08 14:22:54 +02006597 err = nl80211_parse_key(info, &key);
6598 if (err)
6599 return err;
6600
6601 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006602 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6603 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006604 if (!key.p.key || !key.p.key_len)
6605 return -EINVAL;
6606 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6607 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6608 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6609 key.p.key_len != WLAN_KEY_LEN_WEP104))
6610 return -EINVAL;
6611 if (key.idx > 4)
6612 return -EINVAL;
6613 } else {
6614 key.p.key_len = 0;
6615 key.p.key = NULL;
6616 }
6617
Johannes Bergafea0b72010-08-10 09:46:42 +02006618 if (key.idx >= 0) {
6619 int i;
6620 bool ok = false;
6621 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6622 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6623 ok = true;
6624 break;
6625 }
6626 }
Johannes Berg4c476992010-10-04 21:36:35 +02006627 if (!ok)
6628 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006629 }
6630
Johannes Berg4c476992010-10-04 21:36:35 +02006631 if (!rdev->ops->auth)
6632 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006633
Johannes Berg074ac8d2010-09-16 14:58:22 +02006634 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006635 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6636 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006637
Johannes Berg19957bb2009-07-02 17:20:43 +02006638 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006639 chan = nl80211_get_valid_chan(&rdev->wiphy,
6640 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6641 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006642 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006643
Johannes Berg19957bb2009-07-02 17:20:43 +02006644 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6645 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6646
6647 if (info->attrs[NL80211_ATTR_IE]) {
6648 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6649 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6650 }
6651
6652 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006653 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006654 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006655
Jouni Malinene39e5b52012-09-30 19:29:39 +03006656 if (auth_type == NL80211_AUTHTYPE_SAE &&
6657 !info->attrs[NL80211_ATTR_SAE_DATA])
6658 return -EINVAL;
6659
6660 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6661 if (auth_type != NL80211_AUTHTYPE_SAE)
6662 return -EINVAL;
6663 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6664 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6665 /* need to include at least Auth Transaction and Status Code */
6666 if (sae_data_len < 4)
6667 return -EINVAL;
6668 }
6669
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006670 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6671
Johannes Berg95de8172012-01-20 13:55:25 +01006672 /*
6673 * Since we no longer track auth state, ignore
6674 * requests to only change local state.
6675 */
6676 if (local_state_change)
6677 return 0;
6678
Johannes Berg91bf9b22013-05-15 17:44:01 +02006679 wdev_lock(dev->ieee80211_ptr);
6680 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6681 ssid, ssid_len, ie, ie_len,
6682 key.p.key, key.p.key_len, key.idx,
6683 sae_data, sae_data_len);
6684 wdev_unlock(dev->ieee80211_ptr);
6685 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006686}
6687
Johannes Bergc0692b82010-08-27 14:26:53 +03006688static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6689 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006690 struct cfg80211_crypto_settings *settings,
6691 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006692{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006693 memset(settings, 0, sizeof(*settings));
6694
Samuel Ortizb23aa672009-07-01 21:26:54 +02006695 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6696
Johannes Bergc0692b82010-08-27 14:26:53 +03006697 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6698 u16 proto;
6699 proto = nla_get_u16(
6700 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6701 settings->control_port_ethertype = cpu_to_be16(proto);
6702 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6703 proto != ETH_P_PAE)
6704 return -EINVAL;
6705 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6706 settings->control_port_no_encrypt = true;
6707 } else
6708 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6709
Samuel Ortizb23aa672009-07-01 21:26:54 +02006710 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6711 void *data;
6712 int len, i;
6713
6714 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6715 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6716 settings->n_ciphers_pairwise = len / sizeof(u32);
6717
6718 if (len % sizeof(u32))
6719 return -EINVAL;
6720
Johannes Berg3dc27d22009-07-02 21:36:37 +02006721 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006722 return -EINVAL;
6723
6724 memcpy(settings->ciphers_pairwise, data, len);
6725
6726 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006727 if (!cfg80211_supported_cipher_suite(
6728 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006729 settings->ciphers_pairwise[i]))
6730 return -EINVAL;
6731 }
6732
6733 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6734 settings->cipher_group =
6735 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006736 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6737 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006738 return -EINVAL;
6739 }
6740
6741 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6742 settings->wpa_versions =
6743 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6744 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6745 return -EINVAL;
6746 }
6747
6748 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6749 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006750 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006751
6752 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6753 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6754 settings->n_akm_suites = len / sizeof(u32);
6755
6756 if (len % sizeof(u32))
6757 return -EINVAL;
6758
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006759 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6760 return -EINVAL;
6761
Samuel Ortizb23aa672009-07-01 21:26:54 +02006762 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006763 }
6764
6765 return 0;
6766}
6767
Jouni Malinen636a5d32009-03-19 13:39:22 +02006768static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6769{
Johannes Berg4c476992010-10-04 21:36:35 +02006770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6771 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006772 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006773 struct cfg80211_assoc_request req = {};
6774 const u8 *bssid, *ssid;
6775 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006776
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006777 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6778 return -EINVAL;
6779
6780 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006781 !info->attrs[NL80211_ATTR_SSID] ||
6782 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006783 return -EINVAL;
6784
Johannes Berg4c476992010-10-04 21:36:35 +02006785 if (!rdev->ops->assoc)
6786 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006787
Johannes Berg074ac8d2010-09-16 14:58:22 +02006788 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006789 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6790 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006791
Johannes Berg19957bb2009-07-02 17:20:43 +02006792 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006793
Jouni Malinen664834d2014-01-15 00:01:44 +02006794 chan = nl80211_get_valid_chan(&rdev->wiphy,
6795 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6796 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006797 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006798
Johannes Berg19957bb2009-07-02 17:20:43 +02006799 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6800 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006801
6802 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006803 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6804 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006805 }
6806
Jouni Malinendc6382c2009-05-06 22:09:37 +03006807 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006808 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006809 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006810 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006811 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006812 else if (mfp != NL80211_MFP_NO)
6813 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006814 }
6815
Johannes Berg3e5d7642009-07-07 14:37:26 +02006816 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006817 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006818
Ben Greear7e7c8922011-11-18 11:31:59 -08006819 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006820 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006821
6822 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006823 memcpy(&req.ht_capa_mask,
6824 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6825 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006826
6827 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006828 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006829 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006830 memcpy(&req.ht_capa,
6831 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6832 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006833 }
6834
Johannes Bergee2aca32013-02-21 17:36:01 +01006835 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006836 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006837
6838 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006839 memcpy(&req.vht_capa_mask,
6840 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6841 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006842
6843 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006844 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006845 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006846 memcpy(&req.vht_capa,
6847 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6848 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006849 }
6850
Assaf Kraussbab5ab72014-09-03 15:25:01 +03006851 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
6852 if (!(rdev->wiphy.features &
6853 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
6854 !(rdev->wiphy.features & NL80211_FEATURE_QUIET))
6855 return -EINVAL;
6856 req.flags |= ASSOC_REQ_USE_RRM;
6857 }
6858
Johannes Bergf62fab72013-02-21 20:09:09 +01006859 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006860 if (!err) {
6861 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006862 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6863 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006864 wdev_unlock(dev->ieee80211_ptr);
6865 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006866
Jouni Malinen636a5d32009-03-19 13:39:22 +02006867 return err;
6868}
6869
6870static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6871{
Johannes Berg4c476992010-10-04 21:36:35 +02006872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6873 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006874 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006875 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006876 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006877 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006878
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006879 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6880 return -EINVAL;
6881
6882 if (!info->attrs[NL80211_ATTR_MAC])
6883 return -EINVAL;
6884
6885 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6886 return -EINVAL;
6887
Johannes Berg4c476992010-10-04 21:36:35 +02006888 if (!rdev->ops->deauth)
6889 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006890
Johannes Berg074ac8d2010-09-16 14:58:22 +02006891 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006892 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6893 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006894
Johannes Berg19957bb2009-07-02 17:20:43 +02006895 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006896
Johannes Berg19957bb2009-07-02 17:20:43 +02006897 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6898 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006899 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006900 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006901 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006902
6903 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006904 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6905 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006906 }
6907
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006908 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6909
Johannes Berg91bf9b22013-05-15 17:44:01 +02006910 wdev_lock(dev->ieee80211_ptr);
6911 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6912 local_state_change);
6913 wdev_unlock(dev->ieee80211_ptr);
6914 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006915}
6916
6917static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6918{
Johannes Berg4c476992010-10-04 21:36:35 +02006919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6920 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006921 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006922 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006923 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006924 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006925
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006926 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6927 return -EINVAL;
6928
6929 if (!info->attrs[NL80211_ATTR_MAC])
6930 return -EINVAL;
6931
6932 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6933 return -EINVAL;
6934
Johannes Berg4c476992010-10-04 21:36:35 +02006935 if (!rdev->ops->disassoc)
6936 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006937
Johannes Berg074ac8d2010-09-16 14:58:22 +02006938 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006939 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6940 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006941
Johannes Berg19957bb2009-07-02 17:20:43 +02006942 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006943
Johannes Berg19957bb2009-07-02 17:20:43 +02006944 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6945 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006946 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006947 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006948 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006949
6950 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006951 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6952 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006953 }
6954
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006955 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6956
Johannes Berg91bf9b22013-05-15 17:44:01 +02006957 wdev_lock(dev->ieee80211_ptr);
6958 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6959 local_state_change);
6960 wdev_unlock(dev->ieee80211_ptr);
6961 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006962}
6963
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006964static bool
6965nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6966 int mcast_rate[IEEE80211_NUM_BANDS],
6967 int rateval)
6968{
6969 struct wiphy *wiphy = &rdev->wiphy;
6970 bool found = false;
6971 int band, i;
6972
6973 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6974 struct ieee80211_supported_band *sband;
6975
6976 sband = wiphy->bands[band];
6977 if (!sband)
6978 continue;
6979
6980 for (i = 0; i < sband->n_bitrates; i++) {
6981 if (sband->bitrates[i].bitrate == rateval) {
6982 mcast_rate[band] = i + 1;
6983 found = true;
6984 break;
6985 }
6986 }
6987 }
6988
6989 return found;
6990}
6991
Johannes Berg04a773a2009-04-19 21:24:32 +02006992static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6993{
Johannes Berg4c476992010-10-04 21:36:35 +02006994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6995 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006996 struct cfg80211_ibss_params ibss;
6997 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006998 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006999 int err;
7000
Johannes Berg8e30bc52009-04-22 17:45:38 +02007001 memset(&ibss, 0, sizeof(ibss));
7002
Johannes Berg04a773a2009-04-19 21:24:32 +02007003 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7004 return -EINVAL;
7005
Johannes Berg683b6d32012-11-08 21:25:48 +01007006 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02007007 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7008 return -EINVAL;
7009
Johannes Berg8e30bc52009-04-22 17:45:38 +02007010 ibss.beacon_interval = 100;
7011
7012 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7013 ibss.beacon_interval =
7014 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7015 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
7016 return -EINVAL;
7017 }
7018
Johannes Berg4c476992010-10-04 21:36:35 +02007019 if (!rdev->ops->join_ibss)
7020 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007021
Johannes Berg4c476992010-10-04 21:36:35 +02007022 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7023 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007024
Johannes Berg79c97e92009-07-07 03:56:12 +02007025 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02007026
Johannes Berg39193492011-09-16 13:45:25 +02007027 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02007028 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02007029
7030 if (!is_valid_ether_addr(ibss.bssid))
7031 return -EINVAL;
7032 }
Johannes Berg04a773a2009-04-19 21:24:32 +02007033 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7034 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7035
7036 if (info->attrs[NL80211_ATTR_IE]) {
7037 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7038 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7039 }
7040
Johannes Berg683b6d32012-11-08 21:25:48 +01007041 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
7042 if (err)
7043 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01007044
Ilan Peer174e0cd2014-02-23 09:13:01 +02007045 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
7046 NL80211_IFTYPE_ADHOC))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01007047 return -EINVAL;
7048
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02007049 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02007050 case NL80211_CHAN_WIDTH_5:
7051 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02007052 case NL80211_CHAN_WIDTH_20_NOHT:
7053 break;
7054 case NL80211_CHAN_WIDTH_20:
7055 case NL80211_CHAN_WIDTH_40:
7056 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
7057 break;
7058 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01007059 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02007060 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01007061
Johannes Berg04a773a2009-04-19 21:24:32 +02007062 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02007063 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02007064
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007065 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7066 u8 *rates =
7067 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7068 int n_rates =
7069 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7070 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01007071 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007072
Johannes Berg34850ab2011-07-18 18:08:35 +02007073 err = ieee80211_get_ratemask(sband, rates, n_rates,
7074 &ibss.basic_rates);
7075 if (err)
7076 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007077 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01007078
Simon Wunderlich803768f2013-06-28 10:39:58 +02007079 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7080 memcpy(&ibss.ht_capa_mask,
7081 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7082 sizeof(ibss.ht_capa_mask));
7083
7084 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7085 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7086 return -EINVAL;
7087 memcpy(&ibss.ht_capa,
7088 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7089 sizeof(ibss.ht_capa));
7090 }
7091
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01007092 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7093 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
7094 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7095 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007096
Johannes Berg4c476992010-10-04 21:36:35 +02007097 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05307098 bool no_ht = false;
7099
Johannes Berg4c476992010-10-04 21:36:35 +02007100 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307101 info->attrs[NL80211_ATTR_KEYS],
7102 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02007103 if (IS_ERR(connkeys))
7104 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05307105
Johannes Berg3d9d1d62012-11-08 23:14:50 +01007106 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
7107 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05307108 kfree(connkeys);
7109 return -EINVAL;
7110 }
Johannes Berg4c476992010-10-04 21:36:35 +02007111 }
Johannes Berg04a773a2009-04-19 21:24:32 +02007112
Antonio Quartulli267335d2012-01-31 20:25:47 +01007113 ibss.control_port =
7114 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
7115
Simon Wunderlich5336fa82013-10-07 18:41:05 +02007116 ibss.userspace_handles_dfs =
7117 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
7118
Johannes Berg4c476992010-10-04 21:36:35 +02007119 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007120 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +03007121 kzfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02007122 return err;
7123}
7124
7125static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
7126{
Johannes Berg4c476992010-10-04 21:36:35 +02007127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7128 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02007129
Johannes Berg4c476992010-10-04 21:36:35 +02007130 if (!rdev->ops->leave_ibss)
7131 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007132
Johannes Berg4c476992010-10-04 21:36:35 +02007133 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7134 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007135
Johannes Berg4c476992010-10-04 21:36:35 +02007136 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02007137}
7138
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007139static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
7140{
7141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7142 struct net_device *dev = info->user_ptr[1];
7143 int mcast_rate[IEEE80211_NUM_BANDS];
7144 u32 nla_rate;
7145 int err;
7146
7147 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
7148 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7149 return -EOPNOTSUPP;
7150
7151 if (!rdev->ops->set_mcast_rate)
7152 return -EOPNOTSUPP;
7153
7154 memset(mcast_rate, 0, sizeof(mcast_rate));
7155
7156 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
7157 return -EINVAL;
7158
7159 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
7160 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
7161 return -EINVAL;
7162
7163 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
7164
7165 return err;
7166}
7167
Johannes Bergad7e7182013-11-13 13:37:47 +01007168static struct sk_buff *
7169__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7170 int approxlen, u32 portid, u32 seq,
7171 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01007172 enum nl80211_attrs attr,
7173 const struct nl80211_vendor_cmd_info *info,
7174 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01007175{
7176 struct sk_buff *skb;
7177 void *hdr;
7178 struct nlattr *data;
7179
7180 skb = nlmsg_new(approxlen + 100, gfp);
7181 if (!skb)
7182 return NULL;
7183
7184 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
7185 if (!hdr) {
7186 kfree_skb(skb);
7187 return NULL;
7188 }
7189
7190 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
7191 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01007192
7193 if (info) {
7194 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
7195 info->vendor_id))
7196 goto nla_put_failure;
7197 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
7198 info->subcmd))
7199 goto nla_put_failure;
7200 }
7201
Johannes Bergad7e7182013-11-13 13:37:47 +01007202 data = nla_nest_start(skb, attr);
7203
7204 ((void **)skb->cb)[0] = rdev;
7205 ((void **)skb->cb)[1] = hdr;
7206 ((void **)skb->cb)[2] = data;
7207
7208 return skb;
7209
7210 nla_put_failure:
7211 kfree_skb(skb);
7212 return NULL;
7213}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007214
Johannes Berge03ad6e2014-01-01 17:22:30 +01007215struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
7216 enum nl80211_commands cmd,
7217 enum nl80211_attrs attr,
7218 int vendor_event_idx,
7219 int approxlen, gfp_t gfp)
7220{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08007221 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berge03ad6e2014-01-01 17:22:30 +01007222 const struct nl80211_vendor_cmd_info *info;
7223
7224 switch (cmd) {
7225 case NL80211_CMD_TESTMODE:
7226 if (WARN_ON(vendor_event_idx != -1))
7227 return NULL;
7228 info = NULL;
7229 break;
7230 case NL80211_CMD_VENDOR:
7231 if (WARN_ON(vendor_event_idx < 0 ||
7232 vendor_event_idx >= wiphy->n_vendor_events))
7233 return NULL;
7234 info = &wiphy->vendor_events[vendor_event_idx];
7235 break;
7236 default:
7237 WARN_ON(1);
7238 return NULL;
7239 }
7240
7241 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
7242 cmd, attr, info, gfp);
7243}
7244EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
7245
7246void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
7247{
7248 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
7249 void *hdr = ((void **)skb->cb)[1];
7250 struct nlattr *data = ((void **)skb->cb)[2];
7251 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
7252
Johannes Bergbd8c78e2014-07-30 14:55:26 +02007253 /* clear CB data for netlink core to own from now on */
7254 memset(skb->cb, 0, sizeof(skb->cb));
7255
Johannes Berge03ad6e2014-01-01 17:22:30 +01007256 nla_nest_end(skb, data);
7257 genlmsg_end(skb, hdr);
7258
7259 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
7260 mcgrp = NL80211_MCGRP_VENDOR;
7261
7262 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
7263 mcgrp, gfp);
7264}
7265EXPORT_SYMBOL(__cfg80211_send_event_skb);
7266
Johannes Bergaff89a92009-07-01 21:26:51 +02007267#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02007268static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
7269{
Johannes Berg4c476992010-10-04 21:36:35 +02007270 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03007271 struct wireless_dev *wdev =
7272 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02007273 int err;
7274
David Spinadelfc73f112013-07-31 18:04:15 +03007275 if (!rdev->ops->testmode_cmd)
7276 return -EOPNOTSUPP;
7277
7278 if (IS_ERR(wdev)) {
7279 err = PTR_ERR(wdev);
7280 if (err != -EINVAL)
7281 return err;
7282 wdev = NULL;
7283 } else if (wdev->wiphy != &rdev->wiphy) {
7284 return -EINVAL;
7285 }
7286
Johannes Bergaff89a92009-07-01 21:26:51 +02007287 if (!info->attrs[NL80211_ATTR_TESTDATA])
7288 return -EINVAL;
7289
Johannes Bergad7e7182013-11-13 13:37:47 +01007290 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03007291 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02007292 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
7293 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01007294 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02007295
Johannes Bergaff89a92009-07-01 21:26:51 +02007296 return err;
7297}
7298
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007299static int nl80211_testmode_dump(struct sk_buff *skb,
7300 struct netlink_callback *cb)
7301{
Johannes Berg00918d32011-12-13 17:22:05 +01007302 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007303 int err;
7304 long phy_idx;
7305 void *data = NULL;
7306 int data_len = 0;
7307
Johannes Berg5fe231e2013-05-08 21:45:15 +02007308 rtnl_lock();
7309
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007310 if (cb->args[0]) {
7311 /*
7312 * 0 is a valid index, but not valid for args[0],
7313 * so we need to offset by 1.
7314 */
7315 phy_idx = cb->args[0] - 1;
7316 } else {
7317 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
7318 nl80211_fam.attrbuf, nl80211_fam.maxattr,
7319 nl80211_policy);
7320 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02007321 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01007322
Johannes Berg2bd7e352012-06-15 14:23:16 +02007323 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
7324 nl80211_fam.attrbuf);
7325 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007326 err = PTR_ERR(rdev);
7327 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01007328 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02007329 phy_idx = rdev->wiphy_idx;
7330 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02007331
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007332 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7333 cb->args[1] =
7334 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7335 }
7336
7337 if (cb->args[1]) {
7338 data = nla_data((void *)cb->args[1]);
7339 data_len = nla_len((void *)cb->args[1]);
7340 }
7341
Johannes Berg00918d32011-12-13 17:22:05 +01007342 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7343 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007344 err = -ENOENT;
7345 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007346 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007347
Johannes Berg00918d32011-12-13 17:22:05 +01007348 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007349 err = -EOPNOTSUPP;
7350 goto out_err;
7351 }
7352
7353 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007354 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007355 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7356 NL80211_CMD_TESTMODE);
7357 struct nlattr *tmdata;
7358
Dan Carpentercb35fba2013-08-14 14:50:01 +03007359 if (!hdr)
7360 break;
7361
David S. Miller9360ffd2012-03-29 04:41:26 -04007362 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007363 genlmsg_cancel(skb, hdr);
7364 break;
7365 }
7366
7367 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7368 if (!tmdata) {
7369 genlmsg_cancel(skb, hdr);
7370 break;
7371 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007372 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007373 nla_nest_end(skb, tmdata);
7374
7375 if (err == -ENOBUFS || err == -ENOENT) {
7376 genlmsg_cancel(skb, hdr);
7377 break;
7378 } else if (err) {
7379 genlmsg_cancel(skb, hdr);
7380 goto out_err;
7381 }
7382
7383 genlmsg_end(skb, hdr);
7384 }
7385
7386 err = skb->len;
7387 /* see above */
7388 cb->args[0] = phy_idx + 1;
7389 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007390 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007391 return err;
7392}
Johannes Bergaff89a92009-07-01 21:26:51 +02007393#endif
7394
Samuel Ortizb23aa672009-07-01 21:26:54 +02007395static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7396{
Johannes Berg4c476992010-10-04 21:36:35 +02007397 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7398 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007399 struct cfg80211_connect_params connect;
7400 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007401 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007402 int err;
7403
7404 memset(&connect, 0, sizeof(connect));
7405
7406 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7407 return -EINVAL;
7408
7409 if (!info->attrs[NL80211_ATTR_SSID] ||
7410 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7411 return -EINVAL;
7412
7413 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7414 connect.auth_type =
7415 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007416 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7417 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007418 return -EINVAL;
7419 } else
7420 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7421
7422 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7423
Johannes Bergc0692b82010-08-27 14:26:53 +03007424 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007425 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007426 if (err)
7427 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007428
Johannes Berg074ac8d2010-09-16 14:58:22 +02007429 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007430 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7431 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007432
Johannes Berg79c97e92009-07-07 03:56:12 +02007433 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007434
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307435 connect.bg_scan_period = -1;
7436 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7437 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7438 connect.bg_scan_period =
7439 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7440 }
7441
Samuel Ortizb23aa672009-07-01 21:26:54 +02007442 if (info->attrs[NL80211_ATTR_MAC])
7443 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007444 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7445 connect.bssid_hint =
7446 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007447 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7448 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7449
7450 if (info->attrs[NL80211_ATTR_IE]) {
7451 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7452 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7453 }
7454
Jouni Malinencee00a92013-01-15 17:15:57 +02007455 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7456 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7457 if (connect.mfp != NL80211_MFP_REQUIRED &&
7458 connect.mfp != NL80211_MFP_NO)
7459 return -EINVAL;
7460 } else {
7461 connect.mfp = NL80211_MFP_NO;
7462 }
7463
Samuel Ortizb23aa672009-07-01 21:26:54 +02007464 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007465 connect.channel = nl80211_get_valid_chan(
7466 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7467 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007468 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007469 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007470 connect.channel_hint = nl80211_get_valid_chan(
7471 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7472 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007473 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007474 }
7475
Johannes Bergfffd0932009-07-08 14:22:54 +02007476 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7477 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307478 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007479 if (IS_ERR(connkeys))
7480 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007481 }
7482
Ben Greear7e7c8922011-11-18 11:31:59 -08007483 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7484 connect.flags |= ASSOC_REQ_DISABLE_HT;
7485
7486 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7487 memcpy(&connect.ht_capa_mask,
7488 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7489 sizeof(connect.ht_capa_mask));
7490
7491 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007492 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
Johannes Bergb47f6102014-09-10 13:39:54 +03007493 kzfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007494 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007495 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007496 memcpy(&connect.ht_capa,
7497 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7498 sizeof(connect.ht_capa));
7499 }
7500
Johannes Bergee2aca32013-02-21 17:36:01 +01007501 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7502 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7503
7504 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7505 memcpy(&connect.vht_capa_mask,
7506 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7507 sizeof(connect.vht_capa_mask));
7508
7509 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7510 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
Johannes Bergb47f6102014-09-10 13:39:54 +03007511 kzfree(connkeys);
Johannes Bergee2aca32013-02-21 17:36:01 +01007512 return -EINVAL;
7513 }
7514 memcpy(&connect.vht_capa,
7515 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7516 sizeof(connect.vht_capa));
7517 }
7518
Assaf Kraussbab5ab72014-09-03 15:25:01 +03007519 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
7520 if (!(rdev->wiphy.features &
7521 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
7522 !(rdev->wiphy.features & NL80211_FEATURE_QUIET))
7523 return -EINVAL;
7524 connect.flags |= ASSOC_REQ_USE_RRM;
7525 }
7526
Johannes Berg83739b02013-05-15 17:44:01 +02007527 wdev_lock(dev->ieee80211_ptr);
7528 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7529 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007530 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +03007531 kzfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007532 return err;
7533}
7534
7535static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7536{
Johannes Berg4c476992010-10-04 21:36:35 +02007537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7538 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007539 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007540 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007541
7542 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7543 reason = WLAN_REASON_DEAUTH_LEAVING;
7544 else
7545 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7546
7547 if (reason == 0)
7548 return -EINVAL;
7549
Johannes Berg074ac8d2010-09-16 14:58:22 +02007550 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007551 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7552 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007553
Johannes Berg83739b02013-05-15 17:44:01 +02007554 wdev_lock(dev->ieee80211_ptr);
7555 ret = cfg80211_disconnect(rdev, dev, reason, true);
7556 wdev_unlock(dev->ieee80211_ptr);
7557 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007558}
7559
Johannes Berg463d0182009-07-14 00:33:35 +02007560static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7561{
Johannes Berg4c476992010-10-04 21:36:35 +02007562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007563 struct net *net;
7564 int err;
7565 u32 pid;
7566
7567 if (!info->attrs[NL80211_ATTR_PID])
7568 return -EINVAL;
7569
7570 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7571
Johannes Berg463d0182009-07-14 00:33:35 +02007572 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007573 if (IS_ERR(net))
7574 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007575
7576 err = 0;
7577
7578 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007579 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7580 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007581
Johannes Berg463d0182009-07-14 00:33:35 +02007582 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007583 return err;
7584}
7585
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007586static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7587{
Johannes Berg4c476992010-10-04 21:36:35 +02007588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007589 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7590 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007591 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007592 struct cfg80211_pmksa pmksa;
7593
7594 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7595
7596 if (!info->attrs[NL80211_ATTR_MAC])
7597 return -EINVAL;
7598
7599 if (!info->attrs[NL80211_ATTR_PMKID])
7600 return -EINVAL;
7601
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007602 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7603 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7604
Johannes Berg074ac8d2010-09-16 14:58:22 +02007605 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007606 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7607 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007608
7609 switch (info->genlhdr->cmd) {
7610 case NL80211_CMD_SET_PMKSA:
7611 rdev_ops = rdev->ops->set_pmksa;
7612 break;
7613 case NL80211_CMD_DEL_PMKSA:
7614 rdev_ops = rdev->ops->del_pmksa;
7615 break;
7616 default:
7617 WARN_ON(1);
7618 break;
7619 }
7620
Johannes Berg4c476992010-10-04 21:36:35 +02007621 if (!rdev_ops)
7622 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007623
Johannes Berg4c476992010-10-04 21:36:35 +02007624 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007625}
7626
7627static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7628{
Johannes Berg4c476992010-10-04 21:36:35 +02007629 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7630 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007631
Johannes Berg074ac8d2010-09-16 14:58:22 +02007632 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007633 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7634 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007635
Johannes Berg4c476992010-10-04 21:36:35 +02007636 if (!rdev->ops->flush_pmksa)
7637 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007638
Hila Gonene35e4d22012-06-27 17:19:42 +03007639 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007640}
7641
Arik Nemtsov109086c2011-09-28 14:12:50 +03007642static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7643{
7644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7645 struct net_device *dev = info->user_ptr[1];
7646 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307647 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007648 u16 status_code;
7649 u8 *peer;
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03007650 bool initiator;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007651
7652 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7653 !rdev->ops->tdls_mgmt)
7654 return -EOPNOTSUPP;
7655
7656 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7657 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7658 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7659 !info->attrs[NL80211_ATTR_IE] ||
7660 !info->attrs[NL80211_ATTR_MAC])
7661 return -EINVAL;
7662
7663 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7664 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7665 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7666 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03007667 initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307668 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7669 peer_capability =
7670 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007671
Hila Gonene35e4d22012-06-27 17:19:42 +03007672 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307673 dialog_token, status_code, peer_capability,
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03007674 initiator,
Hila Gonene35e4d22012-06-27 17:19:42 +03007675 nla_data(info->attrs[NL80211_ATTR_IE]),
7676 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007677}
7678
7679static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7680{
7681 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7682 struct net_device *dev = info->user_ptr[1];
7683 enum nl80211_tdls_operation operation;
7684 u8 *peer;
7685
7686 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7687 !rdev->ops->tdls_oper)
7688 return -EOPNOTSUPP;
7689
7690 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7691 !info->attrs[NL80211_ATTR_MAC])
7692 return -EINVAL;
7693
7694 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7695 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7696
Hila Gonene35e4d22012-06-27 17:19:42 +03007697 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007698}
7699
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007700static int nl80211_remain_on_channel(struct sk_buff *skb,
7701 struct genl_info *info)
7702{
Johannes Berg4c476992010-10-04 21:36:35 +02007703 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007704 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007705 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007706 struct sk_buff *msg;
7707 void *hdr;
7708 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007709 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007710 int err;
7711
7712 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7713 !info->attrs[NL80211_ATTR_DURATION])
7714 return -EINVAL;
7715
7716 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7717
Johannes Berg7c4ef712011-11-18 15:33:48 +01007718 if (!rdev->ops->remain_on_channel ||
7719 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007720 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007721
Johannes Bergebf348f2012-06-01 12:50:54 +02007722 /*
7723 * We should be on that channel for at least a minimum amount of
7724 * time (10ms) but no longer than the driver supports.
7725 */
7726 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7727 duration > rdev->wiphy.max_remain_on_channel_duration)
7728 return -EINVAL;
7729
Johannes Berg683b6d32012-11-08 21:25:48 +01007730 err = nl80211_parse_chandef(rdev, info, &chandef);
7731 if (err)
7732 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007733
7734 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007735 if (!msg)
7736 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007737
Eric W. Biederman15e47302012-09-07 20:12:54 +00007738 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007739 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007740 if (!hdr) {
7741 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007742 goto free_msg;
7743 }
7744
Johannes Berg683b6d32012-11-08 21:25:48 +01007745 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7746 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007747
7748 if (err)
7749 goto free_msg;
7750
David S. Miller9360ffd2012-03-29 04:41:26 -04007751 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7752 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007753
7754 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007755
7756 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007757
7758 nla_put_failure:
7759 err = -ENOBUFS;
7760 free_msg:
7761 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007762 return err;
7763}
7764
7765static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7766 struct genl_info *info)
7767{
Johannes Berg4c476992010-10-04 21:36:35 +02007768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007769 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007770 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007771
7772 if (!info->attrs[NL80211_ATTR_COOKIE])
7773 return -EINVAL;
7774
Johannes Berg4c476992010-10-04 21:36:35 +02007775 if (!rdev->ops->cancel_remain_on_channel)
7776 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007777
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007778 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7779
Hila Gonene35e4d22012-06-27 17:19:42 +03007780 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007781}
7782
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007783static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7784 u8 *rates, u8 rates_len)
7785{
7786 u8 i;
7787 u32 mask = 0;
7788
7789 for (i = 0; i < rates_len; i++) {
7790 int rate = (rates[i] & 0x7f) * 5;
7791 int ridx;
7792 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7793 struct ieee80211_rate *srate =
7794 &sband->bitrates[ridx];
7795 if (rate == srate->bitrate) {
7796 mask |= 1 << ridx;
7797 break;
7798 }
7799 }
7800 if (ridx == sband->n_bitrates)
7801 return 0; /* rate not found */
7802 }
7803
7804 return mask;
7805}
7806
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007807static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7808 u8 *rates, u8 rates_len,
7809 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7810{
7811 u8 i;
7812
7813 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7814
7815 for (i = 0; i < rates_len; i++) {
7816 int ridx, rbit;
7817
7818 ridx = rates[i] / 8;
7819 rbit = BIT(rates[i] % 8);
7820
7821 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007822 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007823 return false;
7824
7825 /* check availability */
7826 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7827 mcs[ridx] |= rbit;
7828 else
7829 return false;
7830 }
7831
7832 return true;
7833}
7834
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007835static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7836{
7837 u16 mcs_mask = 0;
7838
7839 switch (vht_mcs_map) {
7840 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7841 break;
7842 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7843 mcs_mask = 0x00FF;
7844 break;
7845 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7846 mcs_mask = 0x01FF;
7847 break;
7848 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7849 mcs_mask = 0x03FF;
7850 break;
7851 default:
7852 break;
7853 }
7854
7855 return mcs_mask;
7856}
7857
7858static void vht_build_mcs_mask(u16 vht_mcs_map,
7859 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7860{
7861 u8 nss;
7862
7863 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7864 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7865 vht_mcs_map >>= 2;
7866 }
7867}
7868
7869static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7870 struct nl80211_txrate_vht *txrate,
7871 u16 mcs[NL80211_VHT_NSS_MAX])
7872{
7873 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7874 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7875 u8 i;
7876
7877 if (!sband->vht_cap.vht_supported)
7878 return false;
7879
7880 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7881
7882 /* Build vht_mcs_mask from VHT capabilities */
7883 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7884
7885 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7886 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7887 mcs[i] = txrate->mcs[i];
7888 else
7889 return false;
7890 }
7891
7892 return true;
7893}
7894
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007895static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007896 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7897 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007898 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7899 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007900 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007901 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007902};
7903
7904static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7905 struct genl_info *info)
7906{
7907 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007908 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007909 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007910 int rem, i;
7911 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007912 struct nlattr *tx_rates;
7913 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007914 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007915
Johannes Berg4c476992010-10-04 21:36:35 +02007916 if (!rdev->ops->set_bitrate_mask)
7917 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007918
7919 memset(&mask, 0, sizeof(mask));
7920 /* Default to all rates enabled */
7921 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7922 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007923
7924 if (!sband)
7925 continue;
7926
7927 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007928 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007929 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007930 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007931
7932 if (!sband->vht_cap.vht_supported)
7933 continue;
7934
7935 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7936 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007937 }
7938
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007939 /* if no rates are given set it back to the defaults */
7940 if (!info->attrs[NL80211_ATTR_TX_RATES])
7941 goto out;
7942
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007943 /*
7944 * The nested attribute uses enum nl80211_band as the index. This maps
7945 * directly to the enum ieee80211_band values used in cfg80211.
7946 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007947 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007948 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007949 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007950 int err;
7951
Johannes Berg4c476992010-10-04 21:36:35 +02007952 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7953 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007954 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007955 if (sband == NULL)
7956 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007957 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7958 nla_len(tx_rates), nl80211_txattr_policy);
7959 if (err)
7960 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007961 if (tb[NL80211_TXRATE_LEGACY]) {
7962 mask.control[band].legacy = rateset_to_mask(
7963 sband,
7964 nla_data(tb[NL80211_TXRATE_LEGACY]),
7965 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307966 if ((mask.control[band].legacy == 0) &&
7967 nla_len(tb[NL80211_TXRATE_LEGACY]))
7968 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007969 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007970 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007971 if (!ht_rateset_to_mask(
7972 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007973 nla_data(tb[NL80211_TXRATE_HT]),
7974 nla_len(tb[NL80211_TXRATE_HT]),
7975 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007976 return -EINVAL;
7977 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007978 if (tb[NL80211_TXRATE_VHT]) {
7979 if (!vht_set_mcs_mask(
7980 sband,
7981 nla_data(tb[NL80211_TXRATE_VHT]),
7982 mask.control[band].vht_mcs))
7983 return -EINVAL;
7984 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007985 if (tb[NL80211_TXRATE_GI]) {
7986 mask.control[band].gi =
7987 nla_get_u8(tb[NL80211_TXRATE_GI]);
7988 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7989 return -EINVAL;
7990 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007991
7992 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007993 /* don't allow empty legacy rates if HT or VHT
7994 * are not even supported.
7995 */
7996 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7997 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007998 return -EINVAL;
7999
8000 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01008001 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01008002 goto out;
8003
8004 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
8005 if (mask.control[band].vht_mcs[i])
8006 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008007
8008 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01008009 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008010 }
8011 }
8012
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01008013out:
Hila Gonene35e4d22012-06-27 17:19:42 +03008014 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008015}
8016
Johannes Berg2e161f72010-08-12 15:38:38 +02008017static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02008018{
Johannes Berg4c476992010-10-04 21:36:35 +02008019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02008020 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02008021 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02008022
8023 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
8024 return -EINVAL;
8025
Johannes Berg2e161f72010-08-12 15:38:38 +02008026 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
8027 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02008028
Johannes Berg71bbc992012-06-15 15:30:18 +02008029 switch (wdev->iftype) {
8030 case NL80211_IFTYPE_STATION:
8031 case NL80211_IFTYPE_ADHOC:
8032 case NL80211_IFTYPE_P2P_CLIENT:
8033 case NL80211_IFTYPE_AP:
8034 case NL80211_IFTYPE_AP_VLAN:
8035 case NL80211_IFTYPE_MESH_POINT:
8036 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02008037 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02008038 break;
8039 default:
Johannes Berg4c476992010-10-04 21:36:35 +02008040 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02008041 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008042
8043 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02008044 if (!rdev->ops->mgmt_tx)
8045 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02008046
Eric W. Biederman15e47302012-09-07 20:12:54 +00008047 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02008048 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
8049 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02008050}
8051
Johannes Berg2e161f72010-08-12 15:38:38 +02008052static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02008053{
Johannes Berg4c476992010-10-04 21:36:35 +02008054 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02008055 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01008056 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02008057 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01008058 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02008059 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01008060 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008061 struct cfg80211_mgmt_tx_params params = {
8062 .dont_wait_for_ack =
8063 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
8064 };
Jouni Malinen026331c2010-02-15 12:53:10 +02008065
Johannes Berg683b6d32012-11-08 21:25:48 +01008066 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02008067 return -EINVAL;
8068
Johannes Berg4c476992010-10-04 21:36:35 +02008069 if (!rdev->ops->mgmt_tx)
8070 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02008071
Johannes Berg71bbc992012-06-15 15:30:18 +02008072 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02008073 case NL80211_IFTYPE_P2P_DEVICE:
8074 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8075 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02008076 case NL80211_IFTYPE_STATION:
8077 case NL80211_IFTYPE_ADHOC:
8078 case NL80211_IFTYPE_P2P_CLIENT:
8079 case NL80211_IFTYPE_AP:
8080 case NL80211_IFTYPE_AP_VLAN:
8081 case NL80211_IFTYPE_MESH_POINT:
8082 case NL80211_IFTYPE_P2P_GO:
8083 break;
8084 default:
Johannes Berg4c476992010-10-04 21:36:35 +02008085 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02008086 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008087
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008088 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01008089 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008090 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008091 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02008092
8093 /*
8094 * We should wait on the channel for at least a minimum amount
8095 * of time (10ms) but no longer than the driver supports.
8096 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008097 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
8098 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02008099 return -EINVAL;
8100
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008101 }
8102
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008103 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008104
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008105 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01008106 return -EINVAL;
8107
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008108 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05308109
Antonio Quartulliea141b752013-06-11 14:20:03 +02008110 /* get the channel if any has been specified, otherwise pass NULL to
8111 * the driver. The latter will use the current one
8112 */
8113 chandef.chan = NULL;
8114 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8115 err = nl80211_parse_chandef(rdev, info, &chandef);
8116 if (err)
8117 return err;
8118 }
8119
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008120 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02008121 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02008122
Andrei Otcheretianski34d22ce2014-05-09 14:11:44 +03008123 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
8124 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
8125
8126 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
8127 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8128 int i;
8129
8130 if (len % sizeof(u16))
8131 return -EINVAL;
8132
8133 params.n_csa_offsets = len / sizeof(u16);
8134 params.csa_offsets =
8135 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8136
8137 /* check that all the offsets fit the frame */
8138 for (i = 0; i < params.n_csa_offsets; i++) {
8139 if (params.csa_offsets[i] >= params.len)
8140 return -EINVAL;
8141 }
8142 }
8143
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008144 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01008145 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8146 if (!msg)
8147 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02008148
Eric W. Biederman15e47302012-09-07 20:12:54 +00008149 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01008150 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008151 if (!hdr) {
8152 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01008153 goto free_msg;
8154 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008155 }
Johannes Berge247bd902011-11-04 11:18:21 +01008156
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008157 params.chan = chandef.chan;
8158 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02008159 if (err)
8160 goto free_msg;
8161
Johannes Berge247bd902011-11-04 11:18:21 +01008162 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04008163 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8164 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008165
Johannes Berge247bd902011-11-04 11:18:21 +01008166 genlmsg_end(msg, hdr);
8167 return genlmsg_reply(msg, info);
8168 }
8169
8170 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02008171
8172 nla_put_failure:
8173 err = -ENOBUFS;
8174 free_msg:
8175 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02008176 return err;
8177}
8178
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008179static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
8180{
8181 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02008182 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008183 u64 cookie;
8184
8185 if (!info->attrs[NL80211_ATTR_COOKIE])
8186 return -EINVAL;
8187
8188 if (!rdev->ops->mgmt_tx_cancel_wait)
8189 return -EOPNOTSUPP;
8190
Johannes Berg71bbc992012-06-15 15:30:18 +02008191 switch (wdev->iftype) {
8192 case NL80211_IFTYPE_STATION:
8193 case NL80211_IFTYPE_ADHOC:
8194 case NL80211_IFTYPE_P2P_CLIENT:
8195 case NL80211_IFTYPE_AP:
8196 case NL80211_IFTYPE_AP_VLAN:
8197 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02008198 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02008199 break;
8200 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008201 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02008202 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008203
8204 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8205
Hila Gonene35e4d22012-06-27 17:19:42 +03008206 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008207}
8208
Kalle Valoffb9eb32010-02-17 17:58:10 +02008209static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
8210{
Johannes Berg4c476992010-10-04 21:36:35 +02008211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008212 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008213 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008214 u8 ps_state;
8215 bool state;
8216 int err;
8217
Johannes Berg4c476992010-10-04 21:36:35 +02008218 if (!info->attrs[NL80211_ATTR_PS_STATE])
8219 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008220
8221 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
8222
Johannes Berg4c476992010-10-04 21:36:35 +02008223 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
8224 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008225
8226 wdev = dev->ieee80211_ptr;
8227
Johannes Berg4c476992010-10-04 21:36:35 +02008228 if (!rdev->ops->set_power_mgmt)
8229 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008230
8231 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
8232
8233 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02008234 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008235
Hila Gonene35e4d22012-06-27 17:19:42 +03008236 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02008237 if (!err)
8238 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008239 return err;
8240}
8241
8242static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
8243{
Johannes Berg4c476992010-10-04 21:36:35 +02008244 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008245 enum nl80211_ps_state ps_state;
8246 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008247 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008248 struct sk_buff *msg;
8249 void *hdr;
8250 int err;
8251
Kalle Valoffb9eb32010-02-17 17:58:10 +02008252 wdev = dev->ieee80211_ptr;
8253
Johannes Berg4c476992010-10-04 21:36:35 +02008254 if (!rdev->ops->set_power_mgmt)
8255 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008256
8257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02008258 if (!msg)
8259 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008260
Eric W. Biederman15e47302012-09-07 20:12:54 +00008261 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008262 NL80211_CMD_GET_POWER_SAVE);
8263 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02008264 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008265 goto free_msg;
8266 }
8267
8268 if (wdev->ps)
8269 ps_state = NL80211_PS_ENABLED;
8270 else
8271 ps_state = NL80211_PS_DISABLED;
8272
David S. Miller9360ffd2012-03-29 04:41:26 -04008273 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
8274 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008275
8276 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02008277 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02008278
Johannes Berg4c476992010-10-04 21:36:35 +02008279 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02008280 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02008281 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02008282 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02008283 return err;
8284}
8285
Johannes Berg94e860f2014-01-20 23:58:15 +01008286static const struct nla_policy
8287nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008288 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
8289 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
8290 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07008291 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
8292 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
8293 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008294};
8295
Thomas Pedersen84f10702012-07-12 16:17:33 -07008296static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01008297 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07008298{
8299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07008300 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008301 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07008302
Johannes Bergd9d8b012012-11-26 12:51:52 +01008303 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07008304 return -EINVAL;
8305
Thomas Pedersen84f10702012-07-12 16:17:33 -07008306 if (!rdev->ops->set_cqm_txe_config)
8307 return -EOPNOTSUPP;
8308
8309 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8310 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8311 return -EOPNOTSUPP;
8312
Hila Gonene35e4d22012-06-27 17:19:42 +03008313 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07008314}
8315
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008316static int nl80211_set_cqm_rssi(struct genl_info *info,
8317 s32 threshold, u32 hysteresis)
8318{
Johannes Berg4c476992010-10-04 21:36:35 +02008319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02008320 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008321 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008322
8323 if (threshold > 0)
8324 return -EINVAL;
8325
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008326 /* disabling - hysteresis should also be zero then */
8327 if (threshold == 0)
8328 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008329
Johannes Berg4c476992010-10-04 21:36:35 +02008330 if (!rdev->ops->set_cqm_rssi_config)
8331 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008332
Johannes Berg074ac8d2010-09-16 14:58:22 +02008333 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02008334 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8335 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008336
Hila Gonene35e4d22012-06-27 17:19:42 +03008337 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008338}
8339
8340static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
8341{
8342 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
8343 struct nlattr *cqm;
8344 int err;
8345
8346 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008347 if (!cqm)
8348 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008349
8350 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
8351 nl80211_attr_cqm_policy);
8352 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008353 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008354
8355 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
8356 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008357 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
8358 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008359
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008360 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8361 }
8362
8363 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8364 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8365 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8366 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8367 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8368 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8369
8370 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8371 }
8372
8373 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008374}
8375
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +01008376static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
8377{
8378 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8379 struct net_device *dev = info->user_ptr[1];
8380 struct ocb_setup setup = {};
8381 int err;
8382
8383 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8384 if (err)
8385 return err;
8386
8387 return cfg80211_join_ocb(rdev, dev, &setup);
8388}
8389
8390static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
8391{
8392 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8393 struct net_device *dev = info->user_ptr[1];
8394
8395 return cfg80211_leave_ocb(rdev, dev);
8396}
8397
Johannes Berg29cbe682010-12-03 09:20:44 +01008398static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8399{
8400 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8401 struct net_device *dev = info->user_ptr[1];
8402 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008403 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008404 int err;
8405
8406 /* start with default */
8407 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008408 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008409
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008410 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008411 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008412 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008413 if (err)
8414 return err;
8415 }
8416
8417 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8418 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8419 return -EINVAL;
8420
Javier Cardonac80d5452010-12-16 17:37:49 -08008421 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8422 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8423
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008424 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8425 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8426 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8427 return -EINVAL;
8428
Marco Porsch9bdbf042013-01-07 16:04:51 +01008429 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8430 setup.beacon_interval =
8431 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8432 if (setup.beacon_interval < 10 ||
8433 setup.beacon_interval > 10000)
8434 return -EINVAL;
8435 }
8436
8437 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8438 setup.dtim_period =
8439 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8440 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8441 return -EINVAL;
8442 }
8443
Javier Cardonac80d5452010-12-16 17:37:49 -08008444 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8445 /* parse additional setup parameters if given */
8446 err = nl80211_parse_mesh_setup(info, &setup);
8447 if (err)
8448 return err;
8449 }
8450
Thomas Pedersend37bb182013-03-04 13:06:13 -08008451 if (setup.user_mpm)
8452 cfg.auto_open_plinks = false;
8453
Johannes Bergcc1d2802012-05-16 23:50:20 +02008454 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008455 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8456 if (err)
8457 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008458 } else {
8459 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008460 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008461 }
8462
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008463 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8464 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8465 int n_rates =
8466 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8467 struct ieee80211_supported_band *sband;
8468
8469 if (!setup.chandef.chan)
8470 return -EINVAL;
8471
8472 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8473
8474 err = ieee80211_get_ratemask(sband, rates, n_rates,
8475 &setup.basic_rates);
8476 if (err)
8477 return err;
8478 }
8479
Javier Cardonac80d5452010-12-16 17:37:49 -08008480 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008481}
8482
8483static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8484{
8485 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8486 struct net_device *dev = info->user_ptr[1];
8487
8488 return cfg80211_leave_mesh(rdev, dev);
8489}
8490
Johannes Bergdfb89c52012-06-27 09:23:48 +02008491#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008492static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8493 struct cfg80211_registered_device *rdev)
8494{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008495 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008496 struct nlattr *nl_pats, *nl_pat;
8497 int i, pat_len;
8498
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008499 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008500 return 0;
8501
8502 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8503 if (!nl_pats)
8504 return -ENOBUFS;
8505
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008506 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008507 nl_pat = nla_nest_start(msg, i + 1);
8508 if (!nl_pat)
8509 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008510 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008511 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008512 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008513 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8514 wowlan->patterns[i].pattern) ||
8515 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008516 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008517 return -ENOBUFS;
8518 nla_nest_end(msg, nl_pat);
8519 }
8520 nla_nest_end(msg, nl_pats);
8521
8522 return 0;
8523}
8524
Johannes Berg2a0e0472013-01-23 22:57:40 +01008525static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8526 struct cfg80211_wowlan_tcp *tcp)
8527{
8528 struct nlattr *nl_tcp;
8529
8530 if (!tcp)
8531 return 0;
8532
8533 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8534 if (!nl_tcp)
8535 return -ENOBUFS;
8536
8537 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8538 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8539 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8540 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8541 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8542 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8543 tcp->payload_len, tcp->payload) ||
8544 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8545 tcp->data_interval) ||
8546 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8547 tcp->wake_len, tcp->wake_data) ||
8548 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8549 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8550 return -ENOBUFS;
8551
8552 if (tcp->payload_seq.len &&
8553 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8554 sizeof(tcp->payload_seq), &tcp->payload_seq))
8555 return -ENOBUFS;
8556
8557 if (tcp->payload_tok.len &&
8558 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8559 sizeof(tcp->payload_tok) + tcp->tokens_size,
8560 &tcp->payload_tok))
8561 return -ENOBUFS;
8562
Johannes Berge248ad32013-05-16 10:24:28 +02008563 nla_nest_end(msg, nl_tcp);
8564
Johannes Berg2a0e0472013-01-23 22:57:40 +01008565 return 0;
8566}
8567
Johannes Bergff1b6e62011-05-04 15:37:28 +02008568static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8569{
8570 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8571 struct sk_buff *msg;
8572 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008573 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008574
Johannes Berg964dc9e2013-06-03 17:25:34 +02008575 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008576 return -EOPNOTSUPP;
8577
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008578 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008579 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008580 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8581 rdev->wiphy.wowlan_config->tcp->payload_len +
8582 rdev->wiphy.wowlan_config->tcp->wake_len +
8583 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008584 }
8585
8586 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008587 if (!msg)
8588 return -ENOMEM;
8589
Eric W. Biederman15e47302012-09-07 20:12:54 +00008590 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008591 NL80211_CMD_GET_WOWLAN);
8592 if (!hdr)
8593 goto nla_put_failure;
8594
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008595 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008596 struct nlattr *nl_wowlan;
8597
8598 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8599 if (!nl_wowlan)
8600 goto nla_put_failure;
8601
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008602 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008603 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008604 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008605 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008606 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008607 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008608 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008609 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008610 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008611 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008612 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008613 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008614 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008615 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8616 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008617
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008618 if (nl80211_send_wowlan_patterns(msg, rdev))
8619 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008620
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008621 if (nl80211_send_wowlan_tcp(msg,
8622 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008623 goto nla_put_failure;
8624
Johannes Bergff1b6e62011-05-04 15:37:28 +02008625 nla_nest_end(msg, nl_wowlan);
8626 }
8627
8628 genlmsg_end(msg, hdr);
8629 return genlmsg_reply(msg, info);
8630
8631nla_put_failure:
8632 nlmsg_free(msg);
8633 return -ENOBUFS;
8634}
8635
Johannes Berg2a0e0472013-01-23 22:57:40 +01008636static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8637 struct nlattr *attr,
8638 struct cfg80211_wowlan *trig)
8639{
8640 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8641 struct cfg80211_wowlan_tcp *cfg;
8642 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8643 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8644 u32 size;
8645 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8646 int err, port;
8647
Johannes Berg964dc9e2013-06-03 17:25:34 +02008648 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008649 return -EINVAL;
8650
8651 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8652 nla_data(attr), nla_len(attr),
8653 nl80211_wowlan_tcp_policy);
8654 if (err)
8655 return err;
8656
8657 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8658 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8659 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8660 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8661 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8662 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8663 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8664 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8665 return -EINVAL;
8666
8667 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008668 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008669 return -EINVAL;
8670
8671 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008672 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008673 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008674 return -EINVAL;
8675
8676 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008677 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008678 return -EINVAL;
8679
8680 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8681 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8682 return -EINVAL;
8683
8684 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8685 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8686
8687 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8688 tokens_size = tokln - sizeof(*tok);
8689
8690 if (!tok->len || tokens_size % tok->len)
8691 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008692 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008693 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008694 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008695 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008696 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008697 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008698 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008699 return -EINVAL;
8700 if (tok->offset + tok->len > data_size)
8701 return -EINVAL;
8702 }
8703
8704 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8705 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008706 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008707 return -EINVAL;
8708 if (seq->len == 0 || seq->len > 4)
8709 return -EINVAL;
8710 if (seq->len + seq->offset > data_size)
8711 return -EINVAL;
8712 }
8713
8714 size = sizeof(*cfg);
8715 size += data_size;
8716 size += wake_size + wake_mask_size;
8717 size += tokens_size;
8718
8719 cfg = kzalloc(size, GFP_KERNEL);
8720 if (!cfg)
8721 return -ENOMEM;
8722 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8723 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8724 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8725 ETH_ALEN);
8726 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8727 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8728 else
8729 port = 0;
8730#ifdef CONFIG_INET
8731 /* allocate a socket and port for it and use it */
8732 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8733 IPPROTO_TCP, &cfg->sock, 1);
8734 if (err) {
8735 kfree(cfg);
8736 return err;
8737 }
8738 if (inet_csk_get_port(cfg->sock->sk, port)) {
8739 sock_release(cfg->sock);
8740 kfree(cfg);
8741 return -EADDRINUSE;
8742 }
8743 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8744#else
8745 if (!port) {
8746 kfree(cfg);
8747 return -EINVAL;
8748 }
8749 cfg->src_port = port;
8750#endif
8751
8752 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8753 cfg->payload_len = data_size;
8754 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8755 memcpy((void *)cfg->payload,
8756 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8757 data_size);
8758 if (seq)
8759 cfg->payload_seq = *seq;
8760 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8761 cfg->wake_len = wake_size;
8762 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8763 memcpy((void *)cfg->wake_data,
8764 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8765 wake_size);
8766 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8767 data_size + wake_size;
8768 memcpy((void *)cfg->wake_mask,
8769 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8770 wake_mask_size);
8771 if (tok) {
8772 cfg->tokens_size = tokens_size;
8773 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8774 }
8775
8776 trig->tcp = cfg;
8777
8778 return 0;
8779}
8780
Luciano Coelho8cd4d452014-09-17 11:55:28 +03008781static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
8782 const struct wiphy_wowlan_support *wowlan,
8783 struct nlattr *attr,
8784 struct cfg80211_wowlan *trig)
8785{
8786 struct nlattr **tb;
8787 int err;
8788
8789 tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
8790 if (!tb)
8791 return -ENOMEM;
8792
8793 if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
8794 err = -EOPNOTSUPP;
8795 goto out;
8796 }
8797
8798 err = nla_parse(tb, NL80211_ATTR_MAX,
8799 nla_data(attr), nla_len(attr),
8800 nl80211_policy);
8801 if (err)
8802 goto out;
8803
Johannes Bergad2b26a2014-06-12 21:39:05 +02008804 trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb);
Luciano Coelho8cd4d452014-09-17 11:55:28 +03008805 err = PTR_ERR_OR_ZERO(trig->nd_config);
8806 if (err)
8807 trig->nd_config = NULL;
8808
8809out:
8810 kfree(tb);
8811 return err;
8812}
8813
Johannes Bergff1b6e62011-05-04 15:37:28 +02008814static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8815{
8816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8817 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008818 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008819 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008820 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008821 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008822 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008823
Johannes Berg964dc9e2013-06-03 17:25:34 +02008824 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008825 return -EOPNOTSUPP;
8826
Johannes Bergae33bd82012-07-12 16:25:02 +02008827 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8828 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008829 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008830 goto set_wakeup;
8831 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008832
8833 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8834 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8835 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8836 nl80211_wowlan_policy);
8837 if (err)
8838 return err;
8839
8840 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8841 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8842 return -EINVAL;
8843 new_triggers.any = true;
8844 }
8845
8846 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8847 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8848 return -EINVAL;
8849 new_triggers.disconnect = true;
8850 }
8851
8852 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8853 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8854 return -EINVAL;
8855 new_triggers.magic_pkt = true;
8856 }
8857
Johannes Berg77dbbb12011-07-13 10:48:55 +02008858 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8859 return -EINVAL;
8860
8861 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8862 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8863 return -EINVAL;
8864 new_triggers.gtk_rekey_failure = true;
8865 }
8866
8867 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8868 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8869 return -EINVAL;
8870 new_triggers.eap_identity_req = true;
8871 }
8872
8873 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8874 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8875 return -EINVAL;
8876 new_triggers.four_way_handshake = true;
8877 }
8878
8879 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8880 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8881 return -EINVAL;
8882 new_triggers.rfkill_release = true;
8883 }
8884
Johannes Bergff1b6e62011-05-04 15:37:28 +02008885 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8886 struct nlattr *pat;
8887 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008888 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008889 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008890
8891 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8892 rem)
8893 n_patterns++;
8894 if (n_patterns > wowlan->n_patterns)
8895 return -EINVAL;
8896
8897 new_triggers.patterns = kcalloc(n_patterns,
8898 sizeof(new_triggers.patterns[0]),
8899 GFP_KERNEL);
8900 if (!new_triggers.patterns)
8901 return -ENOMEM;
8902
8903 new_triggers.n_patterns = n_patterns;
8904 i = 0;
8905
8906 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8907 rem) {
Johannes Berg922bd802014-05-19 17:59:50 +02008908 u8 *mask_pat;
8909
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008910 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8911 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008912 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008913 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8914 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008915 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008916 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008917 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008918 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008919 goto error;
8920 if (pat_len > wowlan->pattern_max_len ||
8921 pat_len < wowlan->pattern_min_len)
8922 goto error;
8923
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008924 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008925 pkt_offset = 0;
8926 else
8927 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008928 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008929 if (pkt_offset > wowlan->max_pkt_offset)
8930 goto error;
8931 new_triggers.patterns[i].pkt_offset = pkt_offset;
8932
Johannes Berg922bd802014-05-19 17:59:50 +02008933 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
8934 if (!mask_pat) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008935 err = -ENOMEM;
8936 goto error;
8937 }
Johannes Berg922bd802014-05-19 17:59:50 +02008938 new_triggers.patterns[i].mask = mask_pat;
8939 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008940 mask_len);
Johannes Berg922bd802014-05-19 17:59:50 +02008941 mask_pat += mask_len;
8942 new_triggers.patterns[i].pattern = mask_pat;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008943 new_triggers.patterns[i].pattern_len = pat_len;
Johannes Berg922bd802014-05-19 17:59:50 +02008944 memcpy(mask_pat,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008945 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008946 pat_len);
8947 i++;
8948 }
8949 }
8950
Johannes Berg2a0e0472013-01-23 22:57:40 +01008951 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8952 err = nl80211_parse_wowlan_tcp(
8953 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8954 &new_triggers);
8955 if (err)
8956 goto error;
8957 }
8958
Luciano Coelho8cd4d452014-09-17 11:55:28 +03008959 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
8960 err = nl80211_parse_wowlan_nd(
8961 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
8962 &new_triggers);
8963 if (err)
8964 goto error;
8965 }
8966
Johannes Bergae33bd82012-07-12 16:25:02 +02008967 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8968 if (!ntrig) {
8969 err = -ENOMEM;
8970 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008971 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008972 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008973 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008974
Johannes Bergae33bd82012-07-12 16:25:02 +02008975 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008976 if (rdev->ops->set_wakeup &&
8977 prev_enabled != !!rdev->wiphy.wowlan_config)
8978 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008979
Johannes Bergff1b6e62011-05-04 15:37:28 +02008980 return 0;
8981 error:
8982 for (i = 0; i < new_triggers.n_patterns; i++)
8983 kfree(new_triggers.patterns[i].mask);
8984 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008985 if (new_triggers.tcp && new_triggers.tcp->sock)
8986 sock_release(new_triggers.tcp->sock);
8987 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008988 return err;
8989}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008990#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008991
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008992static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8993 struct cfg80211_registered_device *rdev)
8994{
8995 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8996 int i, j, pat_len;
8997 struct cfg80211_coalesce_rules *rule;
8998
8999 if (!rdev->coalesce->n_rules)
9000 return 0;
9001
9002 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
9003 if (!nl_rules)
9004 return -ENOBUFS;
9005
9006 for (i = 0; i < rdev->coalesce->n_rules; i++) {
9007 nl_rule = nla_nest_start(msg, i + 1);
9008 if (!nl_rule)
9009 return -ENOBUFS;
9010
9011 rule = &rdev->coalesce->rules[i];
9012 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
9013 rule->delay))
9014 return -ENOBUFS;
9015
9016 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
9017 rule->condition))
9018 return -ENOBUFS;
9019
9020 nl_pats = nla_nest_start(msg,
9021 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
9022 if (!nl_pats)
9023 return -ENOBUFS;
9024
9025 for (j = 0; j < rule->n_patterns; j++) {
9026 nl_pat = nla_nest_start(msg, j + 1);
9027 if (!nl_pat)
9028 return -ENOBUFS;
9029 pat_len = rule->patterns[j].pattern_len;
9030 if (nla_put(msg, NL80211_PKTPAT_MASK,
9031 DIV_ROUND_UP(pat_len, 8),
9032 rule->patterns[j].mask) ||
9033 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
9034 rule->patterns[j].pattern) ||
9035 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
9036 rule->patterns[j].pkt_offset))
9037 return -ENOBUFS;
9038 nla_nest_end(msg, nl_pat);
9039 }
9040 nla_nest_end(msg, nl_pats);
9041 nla_nest_end(msg, nl_rule);
9042 }
9043 nla_nest_end(msg, nl_rules);
9044
9045 return 0;
9046}
9047
9048static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
9049{
9050 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9051 struct sk_buff *msg;
9052 void *hdr;
9053
9054 if (!rdev->wiphy.coalesce)
9055 return -EOPNOTSUPP;
9056
9057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9058 if (!msg)
9059 return -ENOMEM;
9060
9061 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9062 NL80211_CMD_GET_COALESCE);
9063 if (!hdr)
9064 goto nla_put_failure;
9065
9066 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
9067 goto nla_put_failure;
9068
9069 genlmsg_end(msg, hdr);
9070 return genlmsg_reply(msg, info);
9071
9072nla_put_failure:
9073 nlmsg_free(msg);
9074 return -ENOBUFS;
9075}
9076
9077void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
9078{
9079 struct cfg80211_coalesce *coalesce = rdev->coalesce;
9080 int i, j;
9081 struct cfg80211_coalesce_rules *rule;
9082
9083 if (!coalesce)
9084 return;
9085
9086 for (i = 0; i < coalesce->n_rules; i++) {
9087 rule = &coalesce->rules[i];
9088 for (j = 0; j < rule->n_patterns; j++)
9089 kfree(rule->patterns[j].mask);
9090 kfree(rule->patterns);
9091 }
9092 kfree(coalesce->rules);
9093 kfree(coalesce);
9094 rdev->coalesce = NULL;
9095}
9096
9097static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
9098 struct nlattr *rule,
9099 struct cfg80211_coalesce_rules *new_rule)
9100{
9101 int err, i;
9102 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9103 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
9104 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
9105 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9106
9107 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
9108 nla_len(rule), nl80211_coalesce_policy);
9109 if (err)
9110 return err;
9111
9112 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
9113 new_rule->delay =
9114 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
9115 if (new_rule->delay > coalesce->max_delay)
9116 return -EINVAL;
9117
9118 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
9119 new_rule->condition =
9120 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
9121 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
9122 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
9123 return -EINVAL;
9124
9125 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
9126 return -EINVAL;
9127
9128 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9129 rem)
9130 n_patterns++;
9131 if (n_patterns > coalesce->n_patterns)
9132 return -EINVAL;
9133
9134 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
9135 GFP_KERNEL);
9136 if (!new_rule->patterns)
9137 return -ENOMEM;
9138
9139 new_rule->n_patterns = n_patterns;
9140 i = 0;
9141
9142 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9143 rem) {
Johannes Berg922bd802014-05-19 17:59:50 +02009144 u8 *mask_pat;
9145
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009146 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
9147 nla_len(pat), NULL);
9148 if (!pat_tb[NL80211_PKTPAT_MASK] ||
9149 !pat_tb[NL80211_PKTPAT_PATTERN])
9150 return -EINVAL;
9151 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
9152 mask_len = DIV_ROUND_UP(pat_len, 8);
9153 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
9154 return -EINVAL;
9155 if (pat_len > coalesce->pattern_max_len ||
9156 pat_len < coalesce->pattern_min_len)
9157 return -EINVAL;
9158
9159 if (!pat_tb[NL80211_PKTPAT_OFFSET])
9160 pkt_offset = 0;
9161 else
9162 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
9163 if (pkt_offset > coalesce->max_pkt_offset)
9164 return -EINVAL;
9165 new_rule->patterns[i].pkt_offset = pkt_offset;
9166
Johannes Berg922bd802014-05-19 17:59:50 +02009167 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
9168 if (!mask_pat)
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009169 return -ENOMEM;
Johannes Berg922bd802014-05-19 17:59:50 +02009170
9171 new_rule->patterns[i].mask = mask_pat;
9172 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
9173 mask_len);
9174
9175 mask_pat += mask_len;
9176 new_rule->patterns[i].pattern = mask_pat;
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009177 new_rule->patterns[i].pattern_len = pat_len;
Johannes Berg922bd802014-05-19 17:59:50 +02009178 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
9179 pat_len);
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009180 i++;
9181 }
9182
9183 return 0;
9184}
9185
9186static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
9187{
9188 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9189 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9190 struct cfg80211_coalesce new_coalesce = {};
9191 struct cfg80211_coalesce *n_coalesce;
9192 int err, rem_rule, n_rules = 0, i, j;
9193 struct nlattr *rule;
9194 struct cfg80211_coalesce_rules *tmp_rule;
9195
9196 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
9197 return -EOPNOTSUPP;
9198
9199 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
9200 cfg80211_rdev_free_coalesce(rdev);
9201 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
9202 return 0;
9203 }
9204
9205 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9206 rem_rule)
9207 n_rules++;
9208 if (n_rules > coalesce->n_rules)
9209 return -EINVAL;
9210
9211 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
9212 GFP_KERNEL);
9213 if (!new_coalesce.rules)
9214 return -ENOMEM;
9215
9216 new_coalesce.n_rules = n_rules;
9217 i = 0;
9218
9219 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9220 rem_rule) {
9221 err = nl80211_parse_coalesce_rule(rdev, rule,
9222 &new_coalesce.rules[i]);
9223 if (err)
9224 goto error;
9225
9226 i++;
9227 }
9228
9229 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
9230 if (err)
9231 goto error;
9232
9233 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
9234 if (!n_coalesce) {
9235 err = -ENOMEM;
9236 goto error;
9237 }
9238 cfg80211_rdev_free_coalesce(rdev);
9239 rdev->coalesce = n_coalesce;
9240
9241 return 0;
9242error:
9243 for (i = 0; i < new_coalesce.n_rules; i++) {
9244 tmp_rule = &new_coalesce.rules[i];
9245 for (j = 0; j < tmp_rule->n_patterns; j++)
9246 kfree(tmp_rule->patterns[j].mask);
9247 kfree(tmp_rule->patterns);
9248 }
9249 kfree(new_coalesce.rules);
9250
9251 return err;
9252}
9253
Johannes Berge5497d72011-07-05 16:35:40 +02009254static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
9255{
9256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9257 struct net_device *dev = info->user_ptr[1];
9258 struct wireless_dev *wdev = dev->ieee80211_ptr;
9259 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
9260 struct cfg80211_gtk_rekey_data rekey_data;
9261 int err;
9262
9263 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
9264 return -EINVAL;
9265
9266 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
9267 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
9268 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
9269 nl80211_rekey_policy);
9270 if (err)
9271 return err;
9272
9273 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
9274 return -ERANGE;
9275 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
9276 return -ERANGE;
9277 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
9278 return -ERANGE;
9279
Johannes Berg78f686c2014-09-10 22:28:06 +03009280 rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
9281 rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
9282 rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
Johannes Berge5497d72011-07-05 16:35:40 +02009283
9284 wdev_lock(wdev);
9285 if (!wdev->current_bss) {
9286 err = -ENOTCONN;
9287 goto out;
9288 }
9289
9290 if (!rdev->ops->set_rekey_data) {
9291 err = -EOPNOTSUPP;
9292 goto out;
9293 }
9294
Hila Gonene35e4d22012-06-27 17:19:42 +03009295 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02009296 out:
9297 wdev_unlock(wdev);
9298 return err;
9299}
9300
Johannes Berg28946da2011-11-04 11:18:12 +01009301static int nl80211_register_unexpected_frame(struct sk_buff *skb,
9302 struct genl_info *info)
9303{
9304 struct net_device *dev = info->user_ptr[1];
9305 struct wireless_dev *wdev = dev->ieee80211_ptr;
9306
9307 if (wdev->iftype != NL80211_IFTYPE_AP &&
9308 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9309 return -EINVAL;
9310
Eric W. Biederman15e47302012-09-07 20:12:54 +00009311 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009312 return -EBUSY;
9313
Eric W. Biederman15e47302012-09-07 20:12:54 +00009314 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01009315 return 0;
9316}
9317
Johannes Berg7f6cf312011-11-04 11:18:15 +01009318static int nl80211_probe_client(struct sk_buff *skb,
9319 struct genl_info *info)
9320{
9321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9322 struct net_device *dev = info->user_ptr[1];
9323 struct wireless_dev *wdev = dev->ieee80211_ptr;
9324 struct sk_buff *msg;
9325 void *hdr;
9326 const u8 *addr;
9327 u64 cookie;
9328 int err;
9329
9330 if (wdev->iftype != NL80211_IFTYPE_AP &&
9331 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9332 return -EOPNOTSUPP;
9333
9334 if (!info->attrs[NL80211_ATTR_MAC])
9335 return -EINVAL;
9336
9337 if (!rdev->ops->probe_client)
9338 return -EOPNOTSUPP;
9339
9340 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9341 if (!msg)
9342 return -ENOMEM;
9343
Eric W. Biederman15e47302012-09-07 20:12:54 +00009344 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01009345 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03009346 if (!hdr) {
9347 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009348 goto free_msg;
9349 }
9350
9351 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9352
Hila Gonene35e4d22012-06-27 17:19:42 +03009353 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01009354 if (err)
9355 goto free_msg;
9356
David S. Miller9360ffd2012-03-29 04:41:26 -04009357 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9358 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009359
9360 genlmsg_end(msg, hdr);
9361
9362 return genlmsg_reply(msg, info);
9363
9364 nla_put_failure:
9365 err = -ENOBUFS;
9366 free_msg:
9367 nlmsg_free(msg);
9368 return err;
9369}
9370
Johannes Berg5e7602302011-11-04 11:18:17 +01009371static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
9372{
9373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07009374 struct cfg80211_beacon_registration *reg, *nreg;
9375 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01009376
9377 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
9378 return -EOPNOTSUPP;
9379
Ben Greear37c73b52012-10-26 14:49:25 -07009380 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
9381 if (!nreg)
9382 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01009383
Ben Greear37c73b52012-10-26 14:49:25 -07009384 /* First, check if already registered. */
9385 spin_lock_bh(&rdev->beacon_registrations_lock);
9386 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9387 if (reg->nlportid == info->snd_portid) {
9388 rv = -EALREADY;
9389 goto out_err;
9390 }
9391 }
9392 /* Add it to the list */
9393 nreg->nlportid = info->snd_portid;
9394 list_add(&nreg->list, &rdev->beacon_registrations);
9395
9396 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01009397
9398 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07009399out_err:
9400 spin_unlock_bh(&rdev->beacon_registrations_lock);
9401 kfree(nreg);
9402 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01009403}
9404
Johannes Berg98104fde2012-06-16 00:19:54 +02009405static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
9406{
9407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9408 struct wireless_dev *wdev = info->user_ptr[1];
9409 int err;
9410
9411 if (!rdev->ops->start_p2p_device)
9412 return -EOPNOTSUPP;
9413
9414 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9415 return -EOPNOTSUPP;
9416
9417 if (wdev->p2p_started)
9418 return 0;
9419
Luciano Coelhob6a55012014-02-27 11:07:21 +02009420 if (rfkill_blocked(rdev->rfkill))
9421 return -ERFKILL;
Johannes Berg98104fde2012-06-16 00:19:54 +02009422
Johannes Bergeeb126e2012-10-23 15:16:50 +02009423 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009424 if (err)
9425 return err;
9426
9427 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02009428 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02009429
9430 return 0;
9431}
9432
9433static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9434{
9435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9436 struct wireless_dev *wdev = info->user_ptr[1];
9437
9438 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9439 return -EOPNOTSUPP;
9440
9441 if (!rdev->ops->stop_p2p_device)
9442 return -EOPNOTSUPP;
9443
Johannes Bergf9f47522013-03-19 15:04:07 +01009444 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009445
9446 return 0;
9447}
9448
Johannes Berg3713b4e2013-02-14 16:19:38 +01009449static int nl80211_get_protocol_features(struct sk_buff *skb,
9450 struct genl_info *info)
9451{
9452 void *hdr;
9453 struct sk_buff *msg;
9454
9455 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9456 if (!msg)
9457 return -ENOMEM;
9458
9459 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9460 NL80211_CMD_GET_PROTOCOL_FEATURES);
9461 if (!hdr)
9462 goto nla_put_failure;
9463
9464 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9465 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9466 goto nla_put_failure;
9467
9468 genlmsg_end(msg, hdr);
9469 return genlmsg_reply(msg, info);
9470
9471 nla_put_failure:
9472 kfree_skb(msg);
9473 return -ENOBUFS;
9474}
9475
Jouni Malinen355199e2013-02-27 17:14:27 +02009476static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9477{
9478 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9479 struct cfg80211_update_ft_ies_params ft_params;
9480 struct net_device *dev = info->user_ptr[1];
9481
9482 if (!rdev->ops->update_ft_ies)
9483 return -EOPNOTSUPP;
9484
9485 if (!info->attrs[NL80211_ATTR_MDID] ||
9486 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9487 return -EINVAL;
9488
9489 memset(&ft_params, 0, sizeof(ft_params));
9490 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9491 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9492 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9493
9494 return rdev_update_ft_ies(rdev, dev, &ft_params);
9495}
9496
Arend van Spriel5de17982013-04-18 15:49:00 +02009497static int nl80211_crit_protocol_start(struct sk_buff *skb,
9498 struct genl_info *info)
9499{
9500 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9501 struct wireless_dev *wdev = info->user_ptr[1];
9502 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9503 u16 duration;
9504 int ret;
9505
9506 if (!rdev->ops->crit_proto_start)
9507 return -EOPNOTSUPP;
9508
9509 if (WARN_ON(!rdev->ops->crit_proto_stop))
9510 return -EINVAL;
9511
9512 if (rdev->crit_proto_nlportid)
9513 return -EBUSY;
9514
9515 /* determine protocol if provided */
9516 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9517 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9518
9519 if (proto >= NUM_NL80211_CRIT_PROTO)
9520 return -EINVAL;
9521
9522 /* timeout must be provided */
9523 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9524 return -EINVAL;
9525
9526 duration =
9527 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9528
9529 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9530 return -ERANGE;
9531
9532 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9533 if (!ret)
9534 rdev->crit_proto_nlportid = info->snd_portid;
9535
9536 return ret;
9537}
9538
9539static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9540 struct genl_info *info)
9541{
9542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9543 struct wireless_dev *wdev = info->user_ptr[1];
9544
9545 if (!rdev->ops->crit_proto_stop)
9546 return -EOPNOTSUPP;
9547
9548 if (rdev->crit_proto_nlportid) {
9549 rdev->crit_proto_nlportid = 0;
9550 rdev_crit_proto_stop(rdev, wdev);
9551 }
9552 return 0;
9553}
9554
Johannes Bergad7e7182013-11-13 13:37:47 +01009555static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9556{
9557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9558 struct wireless_dev *wdev =
9559 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9560 int i, err;
9561 u32 vid, subcmd;
9562
9563 if (!rdev->wiphy.vendor_commands)
9564 return -EOPNOTSUPP;
9565
9566 if (IS_ERR(wdev)) {
9567 err = PTR_ERR(wdev);
9568 if (err != -EINVAL)
9569 return err;
9570 wdev = NULL;
9571 } else if (wdev->wiphy != &rdev->wiphy) {
9572 return -EINVAL;
9573 }
9574
9575 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9576 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9577 return -EINVAL;
9578
9579 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9580 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9581 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9582 const struct wiphy_vendor_command *vcmd;
9583 void *data = NULL;
9584 int len = 0;
9585
9586 vcmd = &rdev->wiphy.vendor_commands[i];
9587
9588 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9589 continue;
9590
9591 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9592 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9593 if (!wdev)
9594 return -EINVAL;
9595 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9596 !wdev->netdev)
9597 return -EINVAL;
9598
9599 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9600 if (wdev->netdev &&
9601 !netif_running(wdev->netdev))
9602 return -ENETDOWN;
9603 if (!wdev->netdev && !wdev->p2p_started)
9604 return -ENETDOWN;
9605 }
9606 } else {
9607 wdev = NULL;
9608 }
9609
9610 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9611 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9612 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9613 }
9614
9615 rdev->cur_cmd_info = info;
9616 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9617 data, len);
9618 rdev->cur_cmd_info = NULL;
9619 return err;
9620 }
9621
9622 return -EOPNOTSUPP;
9623}
9624
9625struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9626 enum nl80211_commands cmd,
9627 enum nl80211_attrs attr,
9628 int approxlen)
9629{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08009630 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Bergad7e7182013-11-13 13:37:47 +01009631
9632 if (WARN_ON(!rdev->cur_cmd_info))
9633 return NULL;
9634
9635 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9636 rdev->cur_cmd_info->snd_portid,
9637 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009638 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009639}
9640EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9641
9642int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9643{
9644 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9645 void *hdr = ((void **)skb->cb)[1];
9646 struct nlattr *data = ((void **)skb->cb)[2];
9647
Johannes Bergbd8c78e2014-07-30 14:55:26 +02009648 /* clear CB data for netlink core to own from now on */
9649 memset(skb->cb, 0, sizeof(skb->cb));
9650
Johannes Bergad7e7182013-11-13 13:37:47 +01009651 if (WARN_ON(!rdev->cur_cmd_info)) {
9652 kfree_skb(skb);
9653 return -EINVAL;
9654 }
9655
9656 nla_nest_end(skb, data);
9657 genlmsg_end(skb, hdr);
9658 return genlmsg_reply(skb, rdev->cur_cmd_info);
9659}
9660EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9661
9662
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009663static int nl80211_set_qos_map(struct sk_buff *skb,
9664 struct genl_info *info)
9665{
9666 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9667 struct cfg80211_qos_map *qos_map = NULL;
9668 struct net_device *dev = info->user_ptr[1];
9669 u8 *pos, len, num_des, des_len, des;
9670 int ret;
9671
9672 if (!rdev->ops->set_qos_map)
9673 return -EOPNOTSUPP;
9674
9675 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9676 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9677 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9678
9679 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9680 len > IEEE80211_QOS_MAP_LEN_MAX)
9681 return -EINVAL;
9682
9683 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9684 if (!qos_map)
9685 return -ENOMEM;
9686
9687 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9688 if (num_des) {
9689 des_len = num_des *
9690 sizeof(struct cfg80211_dscp_exception);
9691 memcpy(qos_map->dscp_exception, pos, des_len);
9692 qos_map->num_des = num_des;
9693 for (des = 0; des < num_des; des++) {
9694 if (qos_map->dscp_exception[des].up > 7) {
9695 kfree(qos_map);
9696 return -EINVAL;
9697 }
9698 }
9699 pos += des_len;
9700 }
9701 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9702 }
9703
9704 wdev_lock(dev->ieee80211_ptr);
9705 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9706 if (!ret)
9707 ret = rdev_set_qos_map(rdev, dev, qos_map);
9708 wdev_unlock(dev->ieee80211_ptr);
9709
9710 kfree(qos_map);
9711 return ret;
9712}
9713
Johannes Berg960d01a2014-09-09 22:55:35 +03009714static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
9715{
9716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9717 struct net_device *dev = info->user_ptr[1];
9718 struct wireless_dev *wdev = dev->ieee80211_ptr;
9719 const u8 *peer;
9720 u8 tsid, up;
9721 u16 admitted_time = 0;
9722 int err;
9723
Johannes Berg723e73a2014-10-22 09:25:06 +02009724 if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
Johannes Berg960d01a2014-09-09 22:55:35 +03009725 return -EOPNOTSUPP;
9726
9727 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
9728 !info->attrs[NL80211_ATTR_USER_PRIO])
9729 return -EINVAL;
9730
9731 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
9732 if (tsid >= IEEE80211_NUM_TIDS)
9733 return -EINVAL;
9734
9735 up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
9736 if (up >= IEEE80211_NUM_UPS)
9737 return -EINVAL;
9738
9739 /* WMM uses TIDs 0-7 even for TSPEC */
Johannes Berg723e73a2014-10-22 09:25:06 +02009740 if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
Johannes Berg960d01a2014-09-09 22:55:35 +03009741 /* TODO: handle 802.11 TSPEC/admission control
Johannes Berg723e73a2014-10-22 09:25:06 +02009742 * need more attributes for that (e.g. BA session requirement);
9743 * change the WMM adminssion test above to allow both then
Johannes Berg960d01a2014-09-09 22:55:35 +03009744 */
9745 return -EINVAL;
9746 }
9747
9748 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9749
9750 if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
9751 admitted_time =
9752 nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
9753 if (!admitted_time)
9754 return -EINVAL;
9755 }
9756
9757 wdev_lock(wdev);
9758 switch (wdev->iftype) {
9759 case NL80211_IFTYPE_STATION:
9760 case NL80211_IFTYPE_P2P_CLIENT:
9761 if (wdev->current_bss)
9762 break;
9763 err = -ENOTCONN;
9764 goto out;
9765 default:
9766 err = -EOPNOTSUPP;
9767 goto out;
9768 }
9769
9770 err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
9771
9772 out:
9773 wdev_unlock(wdev);
9774 return err;
9775}
9776
9777static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
9778{
9779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9780 struct net_device *dev = info->user_ptr[1];
9781 struct wireless_dev *wdev = dev->ieee80211_ptr;
9782 const u8 *peer;
9783 u8 tsid;
9784 int err;
9785
9786 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
9787 return -EINVAL;
9788
9789 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
9790 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9791
9792 wdev_lock(wdev);
9793 err = rdev_del_tx_ts(rdev, dev, tsid, peer);
9794 wdev_unlock(wdev);
9795
9796 return err;
9797}
9798
Arik Nemtsov1057d352014-11-19 12:54:26 +02009799static int nl80211_tdls_channel_switch(struct sk_buff *skb,
9800 struct genl_info *info)
9801{
9802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9803 struct net_device *dev = info->user_ptr[1];
9804 struct wireless_dev *wdev = dev->ieee80211_ptr;
9805 struct cfg80211_chan_def chandef = {};
9806 const u8 *addr;
9807 u8 oper_class;
9808 int err;
9809
9810 if (!rdev->ops->tdls_channel_switch ||
9811 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
9812 return -EOPNOTSUPP;
9813
9814 switch (dev->ieee80211_ptr->iftype) {
9815 case NL80211_IFTYPE_STATION:
9816 case NL80211_IFTYPE_P2P_CLIENT:
9817 break;
9818 default:
9819 return -EOPNOTSUPP;
9820 }
9821
9822 if (!info->attrs[NL80211_ATTR_MAC] ||
9823 !info->attrs[NL80211_ATTR_OPER_CLASS])
9824 return -EINVAL;
9825
9826 err = nl80211_parse_chandef(rdev, info, &chandef);
9827 if (err)
9828 return err;
9829
9830 /*
9831 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
9832 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
9833 * specification is not defined for them.
9834 */
9835 if (chandef.chan->band == IEEE80211_BAND_2GHZ &&
9836 chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
9837 chandef.width != NL80211_CHAN_WIDTH_20)
9838 return -EINVAL;
9839
9840 /* we will be active on the TDLS link */
9841 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, wdev->iftype))
9842 return -EINVAL;
9843
9844 /* don't allow switching to DFS channels */
9845 if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
9846 return -EINVAL;
9847
9848 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9849 oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
9850
9851 wdev_lock(wdev);
9852 err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
9853 wdev_unlock(wdev);
9854
9855 return err;
9856}
9857
9858static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
9859 struct genl_info *info)
9860{
9861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9862 struct net_device *dev = info->user_ptr[1];
9863 struct wireless_dev *wdev = dev->ieee80211_ptr;
9864 const u8 *addr;
9865
9866 if (!rdev->ops->tdls_channel_switch ||
9867 !rdev->ops->tdls_cancel_channel_switch ||
9868 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
9869 return -EOPNOTSUPP;
9870
9871 switch (dev->ieee80211_ptr->iftype) {
9872 case NL80211_IFTYPE_STATION:
9873 case NL80211_IFTYPE_P2P_CLIENT:
9874 break;
9875 default:
9876 return -EOPNOTSUPP;
9877 }
9878
9879 if (!info->attrs[NL80211_ATTR_MAC])
9880 return -EINVAL;
9881
9882 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9883
9884 wdev_lock(wdev);
9885 rdev_tdls_cancel_channel_switch(rdev, dev, addr);
9886 wdev_unlock(wdev);
9887
9888 return 0;
9889}
9890
Johannes Berg4c476992010-10-04 21:36:35 +02009891#define NL80211_FLAG_NEED_WIPHY 0x01
9892#define NL80211_FLAG_NEED_NETDEV 0x02
9893#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009894#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9895#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9896 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009897#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009898/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009899#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9900 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg5393b912014-09-10 15:00:16 +03009901#define NL80211_FLAG_CLEAR_SKB 0x20
Johannes Berg4c476992010-10-04 21:36:35 +02009902
Johannes Bergf84f7712013-11-14 17:14:45 +01009903static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009904 struct genl_info *info)
9905{
9906 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009907 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009908 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009909 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9910
9911 if (rtnl)
9912 rtnl_lock();
9913
9914 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009915 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009916 if (IS_ERR(rdev)) {
9917 if (rtnl)
9918 rtnl_unlock();
9919 return PTR_ERR(rdev);
9920 }
9921 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009922 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9923 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009924 ASSERT_RTNL();
9925
Johannes Berg89a54e42012-06-15 14:33:17 +02009926 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9927 info->attrs);
9928 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009929 if (rtnl)
9930 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009931 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009932 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009933
Johannes Berg89a54e42012-06-15 14:33:17 +02009934 dev = wdev->netdev;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08009935 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg89a54e42012-06-15 14:33:17 +02009936
Johannes Berg1bf614e2012-06-15 15:23:36 +02009937 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9938 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009939 if (rtnl)
9940 rtnl_unlock();
9941 return -EINVAL;
9942 }
9943
9944 info->user_ptr[1] = dev;
9945 } else {
9946 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009947 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009948
Johannes Berg1bf614e2012-06-15 15:23:36 +02009949 if (dev) {
9950 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9951 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009952 if (rtnl)
9953 rtnl_unlock();
9954 return -ENETDOWN;
9955 }
9956
9957 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009958 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9959 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009960 if (rtnl)
9961 rtnl_unlock();
9962 return -ENETDOWN;
9963 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009964 }
9965
Johannes Berg4c476992010-10-04 21:36:35 +02009966 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009967 }
9968
9969 return 0;
9970}
9971
Johannes Bergf84f7712013-11-14 17:14:45 +01009972static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009973 struct genl_info *info)
9974{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009975 if (info->user_ptr[1]) {
9976 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9977 struct wireless_dev *wdev = info->user_ptr[1];
9978
9979 if (wdev->netdev)
9980 dev_put(wdev->netdev);
9981 } else {
9982 dev_put(info->user_ptr[1]);
9983 }
9984 }
Johannes Berg5393b912014-09-10 15:00:16 +03009985
Johannes Berg4c476992010-10-04 21:36:35 +02009986 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9987 rtnl_unlock();
Johannes Berg5393b912014-09-10 15:00:16 +03009988
9989 /* If needed, clear the netlink message payload from the SKB
9990 * as it might contain key data that shouldn't stick around on
9991 * the heap after the SKB is freed. The netlink message header
9992 * is still needed for further processing, so leave it intact.
9993 */
9994 if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
9995 struct nlmsghdr *nlh = nlmsg_hdr(skb);
9996
9997 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
9998 }
Johannes Berg4c476992010-10-04 21:36:35 +02009999}
10000
Johannes Berg4534de82013-11-14 17:14:46 +010010001static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -040010002 {
10003 .cmd = NL80211_CMD_GET_WIPHY,
10004 .doit = nl80211_get_wiphy,
10005 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +020010006 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -040010007 .policy = nl80211_policy,
10008 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +020010009 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10010 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010011 },
10012 {
10013 .cmd = NL80211_CMD_SET_WIPHY,
10014 .doit = nl80211_set_wiphy,
10015 .policy = nl80211_policy,
10016 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010017 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010018 },
10019 {
10020 .cmd = NL80211_CMD_GET_INTERFACE,
10021 .doit = nl80211_get_interface,
10022 .dumpit = nl80211_dump_interface,
10023 .policy = nl80211_policy,
10024 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +020010025 .internal_flags = NL80211_FLAG_NEED_WDEV |
10026 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010027 },
10028 {
10029 .cmd = NL80211_CMD_SET_INTERFACE,
10030 .doit = nl80211_set_interface,
10031 .policy = nl80211_policy,
10032 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010033 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10034 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010035 },
10036 {
10037 .cmd = NL80211_CMD_NEW_INTERFACE,
10038 .doit = nl80211_new_interface,
10039 .policy = nl80211_policy,
10040 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010041 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10042 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010043 },
10044 {
10045 .cmd = NL80211_CMD_DEL_INTERFACE,
10046 .doit = nl80211_del_interface,
10047 .policy = nl80211_policy,
10048 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +020010049 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +020010050 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010051 },
Johannes Berg41ade002007-12-19 02:03:29 +010010052 {
10053 .cmd = NL80211_CMD_GET_KEY,
10054 .doit = nl80211_get_key,
10055 .policy = nl80211_policy,
10056 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010057 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010058 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +010010059 },
10060 {
10061 .cmd = NL80211_CMD_SET_KEY,
10062 .doit = nl80211_set_key,
10063 .policy = nl80211_policy,
10064 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010065 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010066 NL80211_FLAG_NEED_RTNL |
10067 NL80211_FLAG_CLEAR_SKB,
Johannes Berg41ade002007-12-19 02:03:29 +010010068 },
10069 {
10070 .cmd = NL80211_CMD_NEW_KEY,
10071 .doit = nl80211_new_key,
10072 .policy = nl80211_policy,
10073 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010074 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010075 NL80211_FLAG_NEED_RTNL |
10076 NL80211_FLAG_CLEAR_SKB,
Johannes Berg41ade002007-12-19 02:03:29 +010010077 },
10078 {
10079 .cmd = NL80211_CMD_DEL_KEY,
10080 .doit = nl80211_del_key,
10081 .policy = nl80211_policy,
10082 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010083 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010084 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +010010085 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010010086 {
10087 .cmd = NL80211_CMD_SET_BEACON,
10088 .policy = nl80211_policy,
10089 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010010090 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010092 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010093 },
10094 {
Johannes Berg88600202012-02-13 15:17:18 +010010095 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010096 .policy = nl80211_policy,
10097 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010010098 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010099 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010100 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010101 },
10102 {
Johannes Berg88600202012-02-13 15:17:18 +010010103 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010104 .policy = nl80211_policy,
10105 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010010106 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010107 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010108 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010109 },
Johannes Berg5727ef12007-12-19 02:03:34 +010010110 {
10111 .cmd = NL80211_CMD_GET_STATION,
10112 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010113 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +010010114 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +020010115 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10116 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010117 },
10118 {
10119 .cmd = NL80211_CMD_SET_STATION,
10120 .doit = nl80211_set_station,
10121 .policy = nl80211_policy,
10122 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010123 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010124 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010125 },
10126 {
10127 .cmd = NL80211_CMD_NEW_STATION,
10128 .doit = nl80211_new_station,
10129 .policy = nl80211_policy,
10130 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010131 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010132 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010133 },
10134 {
10135 .cmd = NL80211_CMD_DEL_STATION,
10136 .doit = nl80211_del_station,
10137 .policy = nl80211_policy,
10138 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010139 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010140 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010141 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010142 {
10143 .cmd = NL80211_CMD_GET_MPATH,
10144 .doit = nl80211_get_mpath,
10145 .dumpit = nl80211_dump_mpath,
10146 .policy = nl80211_policy,
10147 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010148 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010149 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010150 },
10151 {
Henning Rogge66be7d22014-09-12 08:58:49 +020010152 .cmd = NL80211_CMD_GET_MPP,
10153 .doit = nl80211_get_mpp,
10154 .dumpit = nl80211_dump_mpp,
10155 .policy = nl80211_policy,
10156 .flags = GENL_ADMIN_PERM,
10157 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10158 NL80211_FLAG_NEED_RTNL,
10159 },
10160 {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010161 .cmd = NL80211_CMD_SET_MPATH,
10162 .doit = nl80211_set_mpath,
10163 .policy = nl80211_policy,
10164 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010165 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010166 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010167 },
10168 {
10169 .cmd = NL80211_CMD_NEW_MPATH,
10170 .doit = nl80211_new_mpath,
10171 .policy = nl80211_policy,
10172 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010173 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010174 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010175 },
10176 {
10177 .cmd = NL80211_CMD_DEL_MPATH,
10178 .doit = nl80211_del_mpath,
10179 .policy = nl80211_policy,
10180 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010181 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010182 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010183 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030010184 {
10185 .cmd = NL80211_CMD_SET_BSS,
10186 .doit = nl80211_set_bss,
10187 .policy = nl80211_policy,
10188 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010189 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010190 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +030010191 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070010192 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080010193 .cmd = NL80211_CMD_GET_REG,
10194 .doit = nl80211_get_reg,
10195 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +020010196 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080010197 /* can be retrieved by unprivileged users */
10198 },
10199 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070010200 .cmd = NL80211_CMD_SET_REG,
10201 .doit = nl80211_set_reg,
10202 .policy = nl80211_policy,
10203 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +020010204 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070010205 },
10206 {
10207 .cmd = NL80211_CMD_REQ_SET_REG,
10208 .doit = nl80211_req_set_reg,
10209 .policy = nl80211_policy,
10210 .flags = GENL_ADMIN_PERM,
10211 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010212 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -080010213 .cmd = NL80211_CMD_GET_MESH_CONFIG,
10214 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010215 .policy = nl80211_policy,
10216 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010217 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010218 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010219 },
10220 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -080010221 .cmd = NL80211_CMD_SET_MESH_CONFIG,
10222 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010223 .policy = nl80211_policy,
10224 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +010010225 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010226 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010227 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +020010228 {
Johannes Berg2a519312009-02-10 21:25:55 +010010229 .cmd = NL80211_CMD_TRIGGER_SCAN,
10230 .doit = nl80211_trigger_scan,
10231 .policy = nl80211_policy,
10232 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +020010233 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010234 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +010010235 },
10236 {
10237 .cmd = NL80211_CMD_GET_SCAN,
10238 .policy = nl80211_policy,
10239 .dumpit = nl80211_dump_scan,
10240 },
Jouni Malinen636a5d32009-03-19 13:39:22 +020010241 {
Luciano Coelho807f8a82011-05-11 17:09:35 +030010242 .cmd = NL80211_CMD_START_SCHED_SCAN,
10243 .doit = nl80211_start_sched_scan,
10244 .policy = nl80211_policy,
10245 .flags = GENL_ADMIN_PERM,
10246 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10247 NL80211_FLAG_NEED_RTNL,
10248 },
10249 {
10250 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
10251 .doit = nl80211_stop_sched_scan,
10252 .policy = nl80211_policy,
10253 .flags = GENL_ADMIN_PERM,
10254 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10255 NL80211_FLAG_NEED_RTNL,
10256 },
10257 {
Jouni Malinen636a5d32009-03-19 13:39:22 +020010258 .cmd = NL80211_CMD_AUTHENTICATE,
10259 .doit = nl80211_authenticate,
10260 .policy = nl80211_policy,
10261 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010262 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010263 NL80211_FLAG_NEED_RTNL |
10264 NL80211_FLAG_CLEAR_SKB,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010265 },
10266 {
10267 .cmd = NL80211_CMD_ASSOCIATE,
10268 .doit = nl80211_associate,
10269 .policy = nl80211_policy,
10270 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010271 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010272 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010273 },
10274 {
10275 .cmd = NL80211_CMD_DEAUTHENTICATE,
10276 .doit = nl80211_deauthenticate,
10277 .policy = nl80211_policy,
10278 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010279 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010280 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010281 },
10282 {
10283 .cmd = NL80211_CMD_DISASSOCIATE,
10284 .doit = nl80211_disassociate,
10285 .policy = nl80211_policy,
10286 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010287 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010288 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010289 },
Johannes Berg04a773a2009-04-19 21:24:32 +020010290 {
10291 .cmd = NL80211_CMD_JOIN_IBSS,
10292 .doit = nl80211_join_ibss,
10293 .policy = nl80211_policy,
10294 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010295 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010296 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +020010297 },
10298 {
10299 .cmd = NL80211_CMD_LEAVE_IBSS,
10300 .doit = nl80211_leave_ibss,
10301 .policy = nl80211_policy,
10302 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010303 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010304 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +020010305 },
Johannes Bergaff89a92009-07-01 21:26:51 +020010306#ifdef CONFIG_NL80211_TESTMODE
10307 {
10308 .cmd = NL80211_CMD_TESTMODE,
10309 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -070010310 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +020010311 .policy = nl80211_policy,
10312 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010313 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10314 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +020010315 },
10316#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +020010317 {
10318 .cmd = NL80211_CMD_CONNECT,
10319 .doit = nl80211_connect,
10320 .policy = nl80211_policy,
10321 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010322 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010323 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +020010324 },
10325 {
10326 .cmd = NL80211_CMD_DISCONNECT,
10327 .doit = nl80211_disconnect,
10328 .policy = nl80211_policy,
10329 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010330 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010331 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +020010332 },
Johannes Berg463d0182009-07-14 00:33:35 +020010333 {
10334 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
10335 .doit = nl80211_wiphy_netns,
10336 .policy = nl80211_policy,
10337 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010338 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10339 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +020010340 },
Holger Schurig61fa7132009-11-11 12:25:40 +010010341 {
10342 .cmd = NL80211_CMD_GET_SURVEY,
10343 .policy = nl80211_policy,
10344 .dumpit = nl80211_dump_survey,
10345 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010346 {
10347 .cmd = NL80211_CMD_SET_PMKSA,
10348 .doit = nl80211_setdel_pmksa,
10349 .policy = nl80211_policy,
10350 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010351 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010352 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010353 },
10354 {
10355 .cmd = NL80211_CMD_DEL_PMKSA,
10356 .doit = nl80211_setdel_pmksa,
10357 .policy = nl80211_policy,
10358 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010359 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010360 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010361 },
10362 {
10363 .cmd = NL80211_CMD_FLUSH_PMKSA,
10364 .doit = nl80211_flush_pmksa,
10365 .policy = nl80211_policy,
10366 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010368 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010369 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010370 {
10371 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
10372 .doit = nl80211_remain_on_channel,
10373 .policy = nl80211_policy,
10374 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010375 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010376 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010377 },
10378 {
10379 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
10380 .doit = nl80211_cancel_remain_on_channel,
10381 .policy = nl80211_policy,
10382 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010383 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010384 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010385 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +020010386 {
10387 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
10388 .doit = nl80211_set_tx_bitrate_mask,
10389 .policy = nl80211_policy,
10390 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010391 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10392 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +020010393 },
Jouni Malinen026331c2010-02-15 12:53:10 +020010394 {
Johannes Berg2e161f72010-08-12 15:38:38 +020010395 .cmd = NL80211_CMD_REGISTER_FRAME,
10396 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +020010397 .policy = nl80211_policy,
10398 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010399 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +020010400 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +020010401 },
10402 {
Johannes Berg2e161f72010-08-12 15:38:38 +020010403 .cmd = NL80211_CMD_FRAME,
10404 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +020010405 .policy = nl80211_policy,
10406 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010407 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010408 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +020010409 },
Kalle Valoffb9eb32010-02-17 17:58:10 +020010410 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +010010411 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
10412 .doit = nl80211_tx_mgmt_cancel_wait,
10413 .policy = nl80211_policy,
10414 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010415 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +010010416 NL80211_FLAG_NEED_RTNL,
10417 },
10418 {
Kalle Valoffb9eb32010-02-17 17:58:10 +020010419 .cmd = NL80211_CMD_SET_POWER_SAVE,
10420 .doit = nl80211_set_power_save,
10421 .policy = nl80211_policy,
10422 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010423 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10424 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +020010425 },
10426 {
10427 .cmd = NL80211_CMD_GET_POWER_SAVE,
10428 .doit = nl80211_get_power_save,
10429 .policy = nl80211_policy,
10430 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +020010431 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10432 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +020010433 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010434 {
10435 .cmd = NL80211_CMD_SET_CQM,
10436 .doit = nl80211_set_cqm,
10437 .policy = nl80211_policy,
10438 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010439 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10440 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010441 },
Johannes Bergf444de02010-05-05 15:25:02 +020010442 {
10443 .cmd = NL80211_CMD_SET_CHANNEL,
10444 .doit = nl80211_set_channel,
10445 .policy = nl80211_policy,
10446 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010447 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10448 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +020010449 },
Bill Jordane8347eb2010-10-01 13:54:28 -040010450 {
10451 .cmd = NL80211_CMD_SET_WDS_PEER,
10452 .doit = nl80211_set_wds_peer,
10453 .policy = nl80211_policy,
10454 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +020010455 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10456 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -040010457 },
Johannes Berg29cbe682010-12-03 09:20:44 +010010458 {
10459 .cmd = NL80211_CMD_JOIN_MESH,
10460 .doit = nl80211_join_mesh,
10461 .policy = nl80211_policy,
10462 .flags = GENL_ADMIN_PERM,
10463 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10464 NL80211_FLAG_NEED_RTNL,
10465 },
10466 {
10467 .cmd = NL80211_CMD_LEAVE_MESH,
10468 .doit = nl80211_leave_mesh,
10469 .policy = nl80211_policy,
10470 .flags = GENL_ADMIN_PERM,
10471 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10472 NL80211_FLAG_NEED_RTNL,
10473 },
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +010010474 {
10475 .cmd = NL80211_CMD_JOIN_OCB,
10476 .doit = nl80211_join_ocb,
10477 .policy = nl80211_policy,
10478 .flags = GENL_ADMIN_PERM,
10479 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10480 NL80211_FLAG_NEED_RTNL,
10481 },
10482 {
10483 .cmd = NL80211_CMD_LEAVE_OCB,
10484 .doit = nl80211_leave_ocb,
10485 .policy = nl80211_policy,
10486 .flags = GENL_ADMIN_PERM,
10487 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10488 NL80211_FLAG_NEED_RTNL,
10489 },
Johannes Bergdfb89c52012-06-27 09:23:48 +020010490#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +020010491 {
10492 .cmd = NL80211_CMD_GET_WOWLAN,
10493 .doit = nl80211_get_wowlan,
10494 .policy = nl80211_policy,
10495 /* can be retrieved by unprivileged users */
10496 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10497 NL80211_FLAG_NEED_RTNL,
10498 },
10499 {
10500 .cmd = NL80211_CMD_SET_WOWLAN,
10501 .doit = nl80211_set_wowlan,
10502 .policy = nl80211_policy,
10503 .flags = GENL_ADMIN_PERM,
10504 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10505 NL80211_FLAG_NEED_RTNL,
10506 },
Johannes Bergdfb89c52012-06-27 09:23:48 +020010507#endif
Johannes Berge5497d72011-07-05 16:35:40 +020010508 {
10509 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
10510 .doit = nl80211_set_rekey_data,
10511 .policy = nl80211_policy,
10512 .flags = GENL_ADMIN_PERM,
10513 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010514 NL80211_FLAG_NEED_RTNL |
10515 NL80211_FLAG_CLEAR_SKB,
Johannes Berge5497d72011-07-05 16:35:40 +020010516 },
Arik Nemtsov109086c2011-09-28 14:12:50 +030010517 {
10518 .cmd = NL80211_CMD_TDLS_MGMT,
10519 .doit = nl80211_tdls_mgmt,
10520 .policy = nl80211_policy,
10521 .flags = GENL_ADMIN_PERM,
10522 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10523 NL80211_FLAG_NEED_RTNL,
10524 },
10525 {
10526 .cmd = NL80211_CMD_TDLS_OPER,
10527 .doit = nl80211_tdls_oper,
10528 .policy = nl80211_policy,
10529 .flags = GENL_ADMIN_PERM,
10530 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10531 NL80211_FLAG_NEED_RTNL,
10532 },
Johannes Berg28946da2011-11-04 11:18:12 +010010533 {
10534 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
10535 .doit = nl80211_register_unexpected_frame,
10536 .policy = nl80211_policy,
10537 .flags = GENL_ADMIN_PERM,
10538 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10539 NL80211_FLAG_NEED_RTNL,
10540 },
Johannes Berg7f6cf312011-11-04 11:18:15 +010010541 {
10542 .cmd = NL80211_CMD_PROBE_CLIENT,
10543 .doit = nl80211_probe_client,
10544 .policy = nl80211_policy,
10545 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010546 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +010010547 NL80211_FLAG_NEED_RTNL,
10548 },
Johannes Berg5e7602302011-11-04 11:18:17 +010010549 {
10550 .cmd = NL80211_CMD_REGISTER_BEACONS,
10551 .doit = nl80211_register_beacons,
10552 .policy = nl80211_policy,
10553 .flags = GENL_ADMIN_PERM,
10554 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10555 NL80211_FLAG_NEED_RTNL,
10556 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +010010557 {
10558 .cmd = NL80211_CMD_SET_NOACK_MAP,
10559 .doit = nl80211_set_noack_map,
10560 .policy = nl80211_policy,
10561 .flags = GENL_ADMIN_PERM,
10562 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10563 NL80211_FLAG_NEED_RTNL,
10564 },
Johannes Berg98104fde2012-06-16 00:19:54 +020010565 {
10566 .cmd = NL80211_CMD_START_P2P_DEVICE,
10567 .doit = nl80211_start_p2p_device,
10568 .policy = nl80211_policy,
10569 .flags = GENL_ADMIN_PERM,
10570 .internal_flags = NL80211_FLAG_NEED_WDEV |
10571 NL80211_FLAG_NEED_RTNL,
10572 },
10573 {
10574 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
10575 .doit = nl80211_stop_p2p_device,
10576 .policy = nl80211_policy,
10577 .flags = GENL_ADMIN_PERM,
10578 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10579 NL80211_FLAG_NEED_RTNL,
10580 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +010010581 {
10582 .cmd = NL80211_CMD_SET_MCAST_RATE,
10583 .doit = nl80211_set_mcast_rate,
10584 .policy = nl80211_policy,
10585 .flags = GENL_ADMIN_PERM,
10586 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10587 NL80211_FLAG_NEED_RTNL,
10588 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +053010589 {
10590 .cmd = NL80211_CMD_SET_MAC_ACL,
10591 .doit = nl80211_set_mac_acl,
10592 .policy = nl80211_policy,
10593 .flags = GENL_ADMIN_PERM,
10594 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10595 NL80211_FLAG_NEED_RTNL,
10596 },
Simon Wunderlich04f39042013-02-08 18:16:19 +010010597 {
10598 .cmd = NL80211_CMD_RADAR_DETECT,
10599 .doit = nl80211_start_radar_detection,
10600 .policy = nl80211_policy,
10601 .flags = GENL_ADMIN_PERM,
10602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10603 NL80211_FLAG_NEED_RTNL,
10604 },
Johannes Berg3713b4e2013-02-14 16:19:38 +010010605 {
10606 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
10607 .doit = nl80211_get_protocol_features,
10608 .policy = nl80211_policy,
10609 },
Jouni Malinen355199e2013-02-27 17:14:27 +020010610 {
10611 .cmd = NL80211_CMD_UPDATE_FT_IES,
10612 .doit = nl80211_update_ft_ies,
10613 .policy = nl80211_policy,
10614 .flags = GENL_ADMIN_PERM,
10615 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10616 NL80211_FLAG_NEED_RTNL,
10617 },
Arend van Spriel5de17982013-04-18 15:49:00 +020010618 {
10619 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
10620 .doit = nl80211_crit_protocol_start,
10621 .policy = nl80211_policy,
10622 .flags = GENL_ADMIN_PERM,
10623 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10624 NL80211_FLAG_NEED_RTNL,
10625 },
10626 {
10627 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
10628 .doit = nl80211_crit_protocol_stop,
10629 .policy = nl80211_policy,
10630 .flags = GENL_ADMIN_PERM,
10631 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10632 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -070010633 },
10634 {
10635 .cmd = NL80211_CMD_GET_COALESCE,
10636 .doit = nl80211_get_coalesce,
10637 .policy = nl80211_policy,
10638 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10639 NL80211_FLAG_NEED_RTNL,
10640 },
10641 {
10642 .cmd = NL80211_CMD_SET_COALESCE,
10643 .doit = nl80211_set_coalesce,
10644 .policy = nl80211_policy,
10645 .flags = GENL_ADMIN_PERM,
10646 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10647 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020010648 },
10649 {
10650 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10651 .doit = nl80211_channel_switch,
10652 .policy = nl80211_policy,
10653 .flags = GENL_ADMIN_PERM,
10654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10655 NL80211_FLAG_NEED_RTNL,
10656 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010657 {
10658 .cmd = NL80211_CMD_VENDOR,
10659 .doit = nl80211_vendor_cmd,
10660 .policy = nl80211_policy,
10661 .flags = GENL_ADMIN_PERM,
10662 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10663 NL80211_FLAG_NEED_RTNL,
10664 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010665 {
10666 .cmd = NL80211_CMD_SET_QOS_MAP,
10667 .doit = nl80211_set_qos_map,
10668 .policy = nl80211_policy,
10669 .flags = GENL_ADMIN_PERM,
10670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10671 NL80211_FLAG_NEED_RTNL,
10672 },
Johannes Berg960d01a2014-09-09 22:55:35 +030010673 {
10674 .cmd = NL80211_CMD_ADD_TX_TS,
10675 .doit = nl80211_add_tx_ts,
10676 .policy = nl80211_policy,
10677 .flags = GENL_ADMIN_PERM,
10678 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10679 NL80211_FLAG_NEED_RTNL,
10680 },
10681 {
10682 .cmd = NL80211_CMD_DEL_TX_TS,
10683 .doit = nl80211_del_tx_ts,
10684 .policy = nl80211_policy,
10685 .flags = GENL_ADMIN_PERM,
10686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10687 NL80211_FLAG_NEED_RTNL,
10688 },
Arik Nemtsov1057d352014-11-19 12:54:26 +020010689 {
10690 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
10691 .doit = nl80211_tdls_channel_switch,
10692 .policy = nl80211_policy,
10693 .flags = GENL_ADMIN_PERM,
10694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10695 NL80211_FLAG_NEED_RTNL,
10696 },
10697 {
10698 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
10699 .doit = nl80211_tdls_cancel_channel_switch,
10700 .policy = nl80211_policy,
10701 .flags = GENL_ADMIN_PERM,
10702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10703 NL80211_FLAG_NEED_RTNL,
10704 },
Johannes Berg55682962007-09-20 13:09:35 -040010705};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010706
Johannes Berg55682962007-09-20 13:09:35 -040010707/* notification functions */
10708
Johannes Berg3bb20552014-05-26 13:52:25 +020010709void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
10710 enum nl80211_commands cmd)
Johannes Berg55682962007-09-20 13:09:35 -040010711{
10712 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010713 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010714
Johannes Berg3bb20552014-05-26 13:52:25 +020010715 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
10716 cmd != NL80211_CMD_DEL_WIPHY);
10717
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010718 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010719 if (!msg)
10720 return;
10721
Johannes Berg3bb20552014-05-26 13:52:25 +020010722 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010723 nlmsg_free(msg);
10724 return;
10725 }
10726
Johannes Berg68eb5502013-11-19 15:19:38 +010010727 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010728 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010729}
10730
Johannes Berg362a4152009-05-24 16:43:15 +020010731static int nl80211_add_scan_req(struct sk_buff *msg,
10732 struct cfg80211_registered_device *rdev)
10733{
10734 struct cfg80211_scan_request *req = rdev->scan_req;
10735 struct nlattr *nest;
10736 int i;
10737
10738 if (WARN_ON(!req))
10739 return 0;
10740
10741 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10742 if (!nest)
10743 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010744 for (i = 0; i < req->n_ssids; i++) {
10745 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10746 goto nla_put_failure;
10747 }
Johannes Berg362a4152009-05-24 16:43:15 +020010748 nla_nest_end(msg, nest);
10749
10750 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10751 if (!nest)
10752 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010753 for (i = 0; i < req->n_channels; i++) {
10754 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10755 goto nla_put_failure;
10756 }
Johannes Berg362a4152009-05-24 16:43:15 +020010757 nla_nest_end(msg, nest);
10758
David S. Miller9360ffd2012-03-29 04:41:26 -040010759 if (req->ie &&
10760 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10761 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010762
Johannes Bergae917c92013-10-25 11:05:22 +020010763 if (req->flags &&
10764 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10765 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010766
Johannes Berg362a4152009-05-24 16:43:15 +020010767 return 0;
10768 nla_put_failure:
10769 return -ENOBUFS;
10770}
10771
Johannes Berga538e2d2009-06-16 19:56:42 +020010772static int nl80211_send_scan_msg(struct sk_buff *msg,
10773 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010774 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010775 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010776 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010777{
10778 void *hdr;
10779
Eric W. Biederman15e47302012-09-07 20:12:54 +000010780 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010781 if (!hdr)
10782 return -1;
10783
David S. Miller9360ffd2012-03-29 04:41:26 -040010784 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010785 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10786 wdev->netdev->ifindex)) ||
10787 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010788 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010789
Johannes Berg362a4152009-05-24 16:43:15 +020010790 /* ignore errors and send incomplete event anyway */
10791 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010792
10793 return genlmsg_end(msg, hdr);
10794
10795 nla_put_failure:
10796 genlmsg_cancel(msg, hdr);
10797 return -EMSGSIZE;
10798}
10799
Luciano Coelho807f8a82011-05-11 17:09:35 +030010800static int
10801nl80211_send_sched_scan_msg(struct sk_buff *msg,
10802 struct cfg80211_registered_device *rdev,
10803 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010804 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010805{
10806 void *hdr;
10807
Eric W. Biederman15e47302012-09-07 20:12:54 +000010808 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010809 if (!hdr)
10810 return -1;
10811
David S. Miller9360ffd2012-03-29 04:41:26 -040010812 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10813 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10814 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010815
10816 return genlmsg_end(msg, hdr);
10817
10818 nla_put_failure:
10819 genlmsg_cancel(msg, hdr);
10820 return -EMSGSIZE;
10821}
10822
Johannes Berga538e2d2009-06-16 19:56:42 +020010823void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010824 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010825{
10826 struct sk_buff *msg;
10827
Thomas Graf58050fc2012-06-28 03:57:45 +000010828 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010829 if (!msg)
10830 return;
10831
Johannes Bergfd014282012-06-18 19:17:03 +020010832 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010833 NL80211_CMD_TRIGGER_SCAN) < 0) {
10834 nlmsg_free(msg);
10835 return;
10836 }
10837
Johannes Berg68eb5502013-11-19 15:19:38 +010010838 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010839 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010840}
10841
Johannes Bergf9d15d12014-01-22 11:14:19 +020010842struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10843 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010844{
10845 struct sk_buff *msg;
10846
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010848 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010849 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010850
Johannes Bergfd014282012-06-18 19:17:03 +020010851 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010852 aborted ? NL80211_CMD_SCAN_ABORTED :
10853 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010854 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010855 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010856 }
10857
Johannes Bergf9d15d12014-01-22 11:14:19 +020010858 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010859}
10860
Johannes Bergf9d15d12014-01-22 11:14:19 +020010861void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10862 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010863{
Johannes Berg2a519312009-02-10 21:25:55 +010010864 if (!msg)
10865 return;
10866
Johannes Berg68eb5502013-11-19 15:19:38 +010010867 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010868 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010869}
10870
Luciano Coelho807f8a82011-05-11 17:09:35 +030010871void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10872 struct net_device *netdev)
10873{
10874 struct sk_buff *msg;
10875
10876 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10877 if (!msg)
10878 return;
10879
10880 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10881 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10882 nlmsg_free(msg);
10883 return;
10884 }
10885
Johannes Berg68eb5502013-11-19 15:19:38 +010010886 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010887 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010888}
10889
10890void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10891 struct net_device *netdev, u32 cmd)
10892{
10893 struct sk_buff *msg;
10894
Thomas Graf58050fc2012-06-28 03:57:45 +000010895 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010896 if (!msg)
10897 return;
10898
10899 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10900 nlmsg_free(msg);
10901 return;
10902 }
10903
Johannes Berg68eb5502013-11-19 15:19:38 +010010904 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010905 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010906}
10907
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010908/*
10909 * This can happen on global regulatory changes or device specific settings
10910 * based on custom world regulatory domains.
10911 */
10912void nl80211_send_reg_change_event(struct regulatory_request *request)
10913{
10914 struct sk_buff *msg;
10915 void *hdr;
10916
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010917 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010918 if (!msg)
10919 return;
10920
10921 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10922 if (!hdr) {
10923 nlmsg_free(msg);
10924 return;
10925 }
10926
10927 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010928 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10929 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010930
David S. Miller9360ffd2012-03-29 04:41:26 -040010931 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10932 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10933 NL80211_REGDOM_TYPE_WORLD))
10934 goto nla_put_failure;
10935 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10936 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10937 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10938 goto nla_put_failure;
10939 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10940 request->intersect) {
10941 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10942 NL80211_REGDOM_TYPE_INTERSECTION))
10943 goto nla_put_failure;
10944 } else {
10945 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10946 NL80211_REGDOM_TYPE_COUNTRY) ||
10947 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10948 request->alpha2))
10949 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010950 }
10951
Johannes Bergf4173762012-12-03 18:23:37 +010010952 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010953 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10954 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010955
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010956 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010957
Johannes Bergbc43b282009-07-25 10:54:13 +020010958 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010959 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010960 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010961 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010962
10963 return;
10964
10965nla_put_failure:
10966 genlmsg_cancel(msg, hdr);
10967 nlmsg_free(msg);
10968}
10969
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010970static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10971 struct net_device *netdev,
10972 const u8 *buf, size_t len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030010973 enum nl80211_commands cmd, gfp_t gfp,
10974 int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010975{
10976 struct sk_buff *msg;
10977 void *hdr;
10978
Johannes Berge6d6e342009-07-01 21:26:47 +020010979 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010980 if (!msg)
10981 return;
10982
10983 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10984 if (!hdr) {
10985 nlmsg_free(msg);
10986 return;
10987 }
10988
David S. Miller9360ffd2012-03-29 04:41:26 -040010989 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10990 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10991 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10992 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010993
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030010994 if (uapsd_queues >= 0) {
10995 struct nlattr *nla_wmm =
10996 nla_nest_start(msg, NL80211_ATTR_STA_WME);
10997 if (!nla_wmm)
10998 goto nla_put_failure;
10999
11000 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
11001 uapsd_queues))
11002 goto nla_put_failure;
11003
11004 nla_nest_end(msg, nla_wmm);
11005 }
11006
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011007 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011008
Johannes Berg68eb5502013-11-19 15:19:38 +010011009 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011010 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011011 return;
11012
11013 nla_put_failure:
11014 genlmsg_cancel(msg, hdr);
11015 nlmsg_free(msg);
11016}
11017
11018void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011019 struct net_device *netdev, const u8 *buf,
11020 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011021{
11022 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011023 NL80211_CMD_AUTHENTICATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011024}
11025
11026void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
11027 struct net_device *netdev, const u8 *buf,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011028 size_t len, gfp_t gfp, int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011029{
Johannes Berge6d6e342009-07-01 21:26:47 +020011030 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011031 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011032}
11033
Jouni Malinen53b46b82009-03-27 20:53:56 +020011034void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011035 struct net_device *netdev, const u8 *buf,
11036 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011037{
11038 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011039 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011040}
11041
Jouni Malinen53b46b82009-03-27 20:53:56 +020011042void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
11043 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020011044 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011045{
11046 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011047 NL80211_CMD_DISASSOCIATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011048}
11049
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011050void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
11051 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020011052{
Johannes Berg947add32013-02-22 22:05:20 +010011053 struct wireless_dev *wdev = dev->ieee80211_ptr;
11054 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011055 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011056 const struct ieee80211_mgmt *mgmt = (void *)buf;
11057 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020011058
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011059 if (WARN_ON(len < 2))
11060 return;
11061
11062 if (ieee80211_is_deauth(mgmt->frame_control))
11063 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
11064 else
11065 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
11066
11067 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011068 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
Jouni Malinencf4e5942010-12-16 00:52:40 +020011069}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011070EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020011071
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040011072static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
11073 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020011074 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030011075{
11076 struct sk_buff *msg;
11077 void *hdr;
11078
Johannes Berge6d6e342009-07-01 21:26:47 +020011079 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011080 if (!msg)
11081 return;
11082
11083 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11084 if (!hdr) {
11085 nlmsg_free(msg);
11086 return;
11087 }
11088
David S. Miller9360ffd2012-03-29 04:41:26 -040011089 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11090 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11091 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
11092 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11093 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030011094
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011095 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030011096
Johannes Berg68eb5502013-11-19 15:19:38 +010011097 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011098 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011099 return;
11100
11101 nla_put_failure:
11102 genlmsg_cancel(msg, hdr);
11103 nlmsg_free(msg);
11104}
11105
11106void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011107 struct net_device *netdev, const u8 *addr,
11108 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030011109{
11110 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020011111 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011112}
11113
11114void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011115 struct net_device *netdev, const u8 *addr,
11116 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030011117{
Johannes Berge6d6e342009-07-01 21:26:47 +020011118 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
11119 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011120}
11121
Samuel Ortizb23aa672009-07-01 21:26:54 +020011122void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
11123 struct net_device *netdev, const u8 *bssid,
11124 const u8 *req_ie, size_t req_ie_len,
11125 const u8 *resp_ie, size_t resp_ie_len,
11126 u16 status, gfp_t gfp)
11127{
11128 struct sk_buff *msg;
11129 void *hdr;
11130
Thomas Graf58050fc2012-06-28 03:57:45 +000011131 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011132 if (!msg)
11133 return;
11134
11135 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
11136 if (!hdr) {
11137 nlmsg_free(msg);
11138 return;
11139 }
11140
David S. Miller9360ffd2012-03-29 04:41:26 -040011141 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11142 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11143 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
11144 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
11145 (req_ie &&
11146 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11147 (resp_ie &&
11148 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11149 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020011150
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011151 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011152
Johannes Berg68eb5502013-11-19 15:19:38 +010011153 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011154 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011155 return;
11156
11157 nla_put_failure:
11158 genlmsg_cancel(msg, hdr);
11159 nlmsg_free(msg);
11160
11161}
11162
11163void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
11164 struct net_device *netdev, const u8 *bssid,
11165 const u8 *req_ie, size_t req_ie_len,
11166 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
11167{
11168 struct sk_buff *msg;
11169 void *hdr;
11170
Thomas Graf58050fc2012-06-28 03:57:45 +000011171 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011172 if (!msg)
11173 return;
11174
11175 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
11176 if (!hdr) {
11177 nlmsg_free(msg);
11178 return;
11179 }
11180
David S. Miller9360ffd2012-03-29 04:41:26 -040011181 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11182 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11183 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
11184 (req_ie &&
11185 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11186 (resp_ie &&
11187 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11188 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020011189
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011190 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011191
Johannes Berg68eb5502013-11-19 15:19:38 +010011192 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011193 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011194 return;
11195
11196 nla_put_failure:
11197 genlmsg_cancel(msg, hdr);
11198 nlmsg_free(msg);
11199
11200}
11201
11202void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
11203 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020011204 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020011205{
11206 struct sk_buff *msg;
11207 void *hdr;
11208
Thomas Graf58050fc2012-06-28 03:57:45 +000011209 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011210 if (!msg)
11211 return;
11212
11213 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
11214 if (!hdr) {
11215 nlmsg_free(msg);
11216 return;
11217 }
11218
David S. Miller9360ffd2012-03-29 04:41:26 -040011219 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11220 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11221 (from_ap && reason &&
11222 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
11223 (from_ap &&
11224 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
11225 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
11226 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020011227
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011228 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011229
Johannes Berg68eb5502013-11-19 15:19:38 +010011230 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011231 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011232 return;
11233
11234 nla_put_failure:
11235 genlmsg_cancel(msg, hdr);
11236 nlmsg_free(msg);
11237
11238}
11239
Johannes Berg04a773a2009-04-19 21:24:32 +020011240void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
11241 struct net_device *netdev, const u8 *bssid,
11242 gfp_t gfp)
11243{
11244 struct sk_buff *msg;
11245 void *hdr;
11246
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070011247 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020011248 if (!msg)
11249 return;
11250
11251 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
11252 if (!hdr) {
11253 nlmsg_free(msg);
11254 return;
11255 }
11256
David S. Miller9360ffd2012-03-29 04:41:26 -040011257 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11258 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11259 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11260 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020011261
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011262 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020011263
Johannes Berg68eb5502013-11-19 15:19:38 +010011264 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011265 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020011266 return;
11267
11268 nla_put_failure:
11269 genlmsg_cancel(msg, hdr);
11270 nlmsg_free(msg);
11271}
11272
Johannes Berg947add32013-02-22 22:05:20 +010011273void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
11274 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070011275{
Johannes Berg947add32013-02-22 22:05:20 +010011276 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011277 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011278 struct sk_buff *msg;
11279 void *hdr;
11280
Johannes Berg947add32013-02-22 22:05:20 +010011281 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
11282 return;
11283
11284 trace_cfg80211_notify_new_peer_candidate(dev, addr);
11285
Javier Cardonac93b5e72011-04-07 15:08:34 -070011286 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11287 if (!msg)
11288 return;
11289
11290 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
11291 if (!hdr) {
11292 nlmsg_free(msg);
11293 return;
11294 }
11295
David S. Miller9360ffd2012-03-29 04:41:26 -040011296 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011297 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11298 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011299 (ie_len && ie &&
11300 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
11301 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070011302
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011303 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011304
Johannes Berg68eb5502013-11-19 15:19:38 +010011305 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011306 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011307 return;
11308
11309 nla_put_failure:
11310 genlmsg_cancel(msg, hdr);
11311 nlmsg_free(msg);
11312}
Johannes Berg947add32013-02-22 22:05:20 +010011313EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011314
Jouni Malinena3b8b052009-03-27 21:59:49 +020011315void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
11316 struct net_device *netdev, const u8 *addr,
11317 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020011318 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020011319{
11320 struct sk_buff *msg;
11321 void *hdr;
11322
Johannes Berge6d6e342009-07-01 21:26:47 +020011323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020011324 if (!msg)
11325 return;
11326
11327 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
11328 if (!hdr) {
11329 nlmsg_free(msg);
11330 return;
11331 }
11332
David S. Miller9360ffd2012-03-29 04:41:26 -040011333 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11334 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11335 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
11336 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
11337 (key_id != -1 &&
11338 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
11339 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
11340 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020011341
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011342 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020011343
Johannes Berg68eb5502013-11-19 15:19:38 +010011344 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011345 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020011346 return;
11347
11348 nla_put_failure:
11349 genlmsg_cancel(msg, hdr);
11350 nlmsg_free(msg);
11351}
11352
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011353void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
11354 struct ieee80211_channel *channel_before,
11355 struct ieee80211_channel *channel_after)
11356{
11357 struct sk_buff *msg;
11358 void *hdr;
11359 struct nlattr *nl_freq;
11360
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070011361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011362 if (!msg)
11363 return;
11364
11365 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
11366 if (!hdr) {
11367 nlmsg_free(msg);
11368 return;
11369 }
11370
11371 /*
11372 * Since we are applying the beacon hint to a wiphy we know its
11373 * wiphy_idx is valid
11374 */
David S. Miller9360ffd2012-03-29 04:41:26 -040011375 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
11376 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011377
11378 /* Before */
11379 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
11380 if (!nl_freq)
11381 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010011382 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011383 goto nla_put_failure;
11384 nla_nest_end(msg, nl_freq);
11385
11386 /* After */
11387 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
11388 if (!nl_freq)
11389 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010011390 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011391 goto nla_put_failure;
11392 nla_nest_end(msg, nl_freq);
11393
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011394 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011395
Johannes Berg463d0182009-07-14 00:33:35 +020011396 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010011397 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011398 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020011399 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011400
11401 return;
11402
11403nla_put_failure:
11404 genlmsg_cancel(msg, hdr);
11405 nlmsg_free(msg);
11406}
11407
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011408static void nl80211_send_remain_on_chan_event(
11409 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020011410 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011411 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011412 unsigned int duration, gfp_t gfp)
11413{
11414 struct sk_buff *msg;
11415 void *hdr;
11416
11417 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11418 if (!msg)
11419 return;
11420
11421 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11422 if (!hdr) {
11423 nlmsg_free(msg);
11424 return;
11425 }
11426
David S. Miller9360ffd2012-03-29 04:41:26 -040011427 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011428 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11429 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020011430 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011431 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010011432 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11433 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011434 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
11435 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011436
David S. Miller9360ffd2012-03-29 04:41:26 -040011437 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
11438 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
11439 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011440
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011441 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011442
Johannes Berg68eb5502013-11-19 15:19:38 +010011443 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011444 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011445 return;
11446
11447 nla_put_failure:
11448 genlmsg_cancel(msg, hdr);
11449 nlmsg_free(msg);
11450}
11451
Johannes Berg947add32013-02-22 22:05:20 +010011452void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
11453 struct ieee80211_channel *chan,
11454 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011455{
Johannes Berg947add32013-02-22 22:05:20 +010011456 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011457 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011458
11459 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011460 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020011461 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010011462 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011463}
Johannes Berg947add32013-02-22 22:05:20 +010011464EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011465
Johannes Berg947add32013-02-22 22:05:20 +010011466void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
11467 struct ieee80211_channel *chan,
11468 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011469{
Johannes Berg947add32013-02-22 22:05:20 +010011470 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011471 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011472
11473 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011474 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010011475 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011476}
Johannes Berg947add32013-02-22 22:05:20 +010011477EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011478
Johannes Berg947add32013-02-22 22:05:20 +010011479void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
11480 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010011481{
Johannes Berg947add32013-02-22 22:05:20 +010011482 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011483 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010011484 struct sk_buff *msg;
11485
Johannes Berg947add32013-02-22 22:05:20 +010011486 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
11487
Thomas Graf58050fc2012-06-28 03:57:45 +000011488 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010011489 if (!msg)
11490 return;
11491
John W. Linville66266b32012-03-15 13:25:41 -040011492 if (nl80211_send_station(msg, 0, 0, 0,
11493 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010011494 nlmsg_free(msg);
11495 return;
11496 }
11497
Johannes Berg68eb5502013-11-19 15:19:38 +010011498 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011499 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010011500}
Johannes Berg947add32013-02-22 22:05:20 +010011501EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010011502
Johannes Berg947add32013-02-22 22:05:20 +010011503void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020011504{
Johannes Berg947add32013-02-22 22:05:20 +010011505 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011506 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020011507 struct sk_buff *msg;
11508 void *hdr;
11509
Johannes Berg947add32013-02-22 22:05:20 +010011510 trace_cfg80211_del_sta(dev, mac_addr);
11511
Thomas Graf58050fc2012-06-28 03:57:45 +000011512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020011513 if (!msg)
11514 return;
11515
11516 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
11517 if (!hdr) {
11518 nlmsg_free(msg);
11519 return;
11520 }
11521
David S. Miller9360ffd2012-03-29 04:41:26 -040011522 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11523 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
11524 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020011525
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011526 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020011527
Johannes Berg68eb5502013-11-19 15:19:38 +010011528 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011529 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020011530 return;
11531
11532 nla_put_failure:
11533 genlmsg_cancel(msg, hdr);
11534 nlmsg_free(msg);
11535}
Johannes Berg947add32013-02-22 22:05:20 +010011536EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020011537
Johannes Berg947add32013-02-22 22:05:20 +010011538void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
11539 enum nl80211_connect_failed_reason reason,
11540 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011541{
Johannes Berg947add32013-02-22 22:05:20 +010011542 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011543 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011544 struct sk_buff *msg;
11545 void *hdr;
11546
11547 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11548 if (!msg)
11549 return;
11550
11551 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
11552 if (!hdr) {
11553 nlmsg_free(msg);
11554 return;
11555 }
11556
11557 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11558 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
11559 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
11560 goto nla_put_failure;
11561
11562 genlmsg_end(msg, hdr);
11563
Johannes Berg68eb5502013-11-19 15:19:38 +010011564 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011565 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011566 return;
11567
11568 nla_put_failure:
11569 genlmsg_cancel(msg, hdr);
11570 nlmsg_free(msg);
11571}
Johannes Berg947add32013-02-22 22:05:20 +010011572EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011573
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011574static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
11575 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010011576{
11577 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011578 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg28946da2011-11-04 11:18:12 +010011579 struct sk_buff *msg;
11580 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000011581 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010011582
Eric W. Biederman15e47302012-09-07 20:12:54 +000011583 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010011584 return false;
11585
11586 msg = nlmsg_new(100, gfp);
11587 if (!msg)
11588 return true;
11589
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011590 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010011591 if (!hdr) {
11592 nlmsg_free(msg);
11593 return true;
11594 }
11595
David S. Miller9360ffd2012-03-29 04:41:26 -040011596 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11597 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11598 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11599 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010011600
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011601 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000011602 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010011603 return true;
11604
11605 nla_put_failure:
11606 genlmsg_cancel(msg, hdr);
11607 nlmsg_free(msg);
11608 return true;
11609}
11610
Johannes Berg947add32013-02-22 22:05:20 +010011611bool cfg80211_rx_spurious_frame(struct net_device *dev,
11612 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011613{
Johannes Berg947add32013-02-22 22:05:20 +010011614 struct wireless_dev *wdev = dev->ieee80211_ptr;
11615 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011616
Johannes Berg947add32013-02-22 22:05:20 +010011617 trace_cfg80211_rx_spurious_frame(dev, addr);
11618
11619 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11620 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
11621 trace_cfg80211_return_bool(false);
11622 return false;
11623 }
11624 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
11625 addr, gfp);
11626 trace_cfg80211_return_bool(ret);
11627 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011628}
Johannes Berg947add32013-02-22 22:05:20 +010011629EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
11630
11631bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
11632 const u8 *addr, gfp_t gfp)
11633{
11634 struct wireless_dev *wdev = dev->ieee80211_ptr;
11635 bool ret;
11636
11637 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
11638
11639 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11640 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
11641 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
11642 trace_cfg80211_return_bool(false);
11643 return false;
11644 }
11645 ret = __nl80211_unexpected_frame(dev,
11646 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
11647 addr, gfp);
11648 trace_cfg80211_return_bool(ret);
11649 return ret;
11650}
11651EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011652
Johannes Berg2e161f72010-08-12 15:38:38 +020011653int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000011654 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010011655 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030011656 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011657{
Johannes Berg71bbc992012-06-15 15:30:18 +020011658 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011659 struct sk_buff *msg;
11660 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020011661
11662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11663 if (!msg)
11664 return -ENOMEM;
11665
Johannes Berg2e161f72010-08-12 15:38:38 +020011666 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020011667 if (!hdr) {
11668 nlmsg_free(msg);
11669 return -ENOMEM;
11670 }
11671
David S. Miller9360ffd2012-03-29 04:41:26 -040011672 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011673 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11674 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011675 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011676 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
11677 (sig_dbm &&
11678 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030011679 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11680 (flags &&
11681 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040011682 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011683
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011684 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011685
Eric W. Biederman15e47302012-09-07 20:12:54 +000011686 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020011687
11688 nla_put_failure:
11689 genlmsg_cancel(msg, hdr);
11690 nlmsg_free(msg);
11691 return -ENOBUFS;
11692}
11693
Johannes Berg947add32013-02-22 22:05:20 +010011694void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
11695 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011696{
Johannes Berg947add32013-02-22 22:05:20 +010011697 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011698 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020011699 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011700 struct sk_buff *msg;
11701 void *hdr;
11702
Johannes Berg947add32013-02-22 22:05:20 +010011703 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11704
Jouni Malinen026331c2010-02-15 12:53:10 +020011705 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11706 if (!msg)
11707 return;
11708
Johannes Berg2e161f72010-08-12 15:38:38 +020011709 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011710 if (!hdr) {
11711 nlmsg_free(msg);
11712 return;
11713 }
11714
David S. Miller9360ffd2012-03-29 04:41:26 -040011715 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011716 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11717 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011718 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011719 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11720 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11721 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11722 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011723
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011724 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011725
Johannes Berg68eb5502013-11-19 15:19:38 +010011726 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011727 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011728 return;
11729
11730 nla_put_failure:
11731 genlmsg_cancel(msg, hdr);
11732 nlmsg_free(msg);
11733}
Johannes Berg947add32013-02-22 22:05:20 +010011734EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011735
Johannes Berg947add32013-02-22 22:05:20 +010011736void cfg80211_cqm_rssi_notify(struct net_device *dev,
11737 enum nl80211_cqm_rssi_threshold_event rssi_event,
11738 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011739{
Johannes Berg947add32013-02-22 22:05:20 +010011740 struct wireless_dev *wdev = dev->ieee80211_ptr;
11741 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011742 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011743 struct sk_buff *msg;
11744 struct nlattr *pinfoattr;
11745 void *hdr;
11746
Johannes Berg947add32013-02-22 22:05:20 +010011747 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11748
Thomas Graf58050fc2012-06-28 03:57:45 +000011749 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011750 if (!msg)
11751 return;
11752
11753 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11754 if (!hdr) {
11755 nlmsg_free(msg);
11756 return;
11757 }
11758
David S. Miller9360ffd2012-03-29 04:41:26 -040011759 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011760 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011761 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011762
11763 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11764 if (!pinfoattr)
11765 goto nla_put_failure;
11766
David S. Miller9360ffd2012-03-29 04:41:26 -040011767 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11768 rssi_event))
11769 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011770
11771 nla_nest_end(msg, pinfoattr);
11772
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011773 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011774
Johannes Berg68eb5502013-11-19 15:19:38 +010011775 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011776 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011777 return;
11778
11779 nla_put_failure:
11780 genlmsg_cancel(msg, hdr);
11781 nlmsg_free(msg);
11782}
Johannes Berg947add32013-02-22 22:05:20 +010011783EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011784
Johannes Berg947add32013-02-22 22:05:20 +010011785static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11786 struct net_device *netdev, const u8 *bssid,
11787 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011788{
11789 struct sk_buff *msg;
11790 struct nlattr *rekey_attr;
11791 void *hdr;
11792
Thomas Graf58050fc2012-06-28 03:57:45 +000011793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011794 if (!msg)
11795 return;
11796
11797 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11798 if (!hdr) {
11799 nlmsg_free(msg);
11800 return;
11801 }
11802
David S. Miller9360ffd2012-03-29 04:41:26 -040011803 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11804 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11805 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11806 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011807
11808 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11809 if (!rekey_attr)
11810 goto nla_put_failure;
11811
David S. Miller9360ffd2012-03-29 04:41:26 -040011812 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11813 NL80211_REPLAY_CTR_LEN, replay_ctr))
11814 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011815
11816 nla_nest_end(msg, rekey_attr);
11817
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011818 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011819
Johannes Berg68eb5502013-11-19 15:19:38 +010011820 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011821 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011822 return;
11823
11824 nla_put_failure:
11825 genlmsg_cancel(msg, hdr);
11826 nlmsg_free(msg);
11827}
11828
Johannes Berg947add32013-02-22 22:05:20 +010011829void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11830 const u8 *replay_ctr, gfp_t gfp)
11831{
11832 struct wireless_dev *wdev = dev->ieee80211_ptr;
11833 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011834 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011835
11836 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11837 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11838}
11839EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11840
11841static void
11842nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11843 struct net_device *netdev, int index,
11844 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011845{
11846 struct sk_buff *msg;
11847 struct nlattr *attr;
11848 void *hdr;
11849
Thomas Graf58050fc2012-06-28 03:57:45 +000011850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011851 if (!msg)
11852 return;
11853
11854 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11855 if (!hdr) {
11856 nlmsg_free(msg);
11857 return;
11858 }
11859
David S. Miller9360ffd2012-03-29 04:41:26 -040011860 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11861 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11862 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011863
11864 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11865 if (!attr)
11866 goto nla_put_failure;
11867
David S. Miller9360ffd2012-03-29 04:41:26 -040011868 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11869 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11870 (preauth &&
11871 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11872 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011873
11874 nla_nest_end(msg, attr);
11875
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011876 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011877
Johannes Berg68eb5502013-11-19 15:19:38 +010011878 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011879 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011880 return;
11881
11882 nla_put_failure:
11883 genlmsg_cancel(msg, hdr);
11884 nlmsg_free(msg);
11885}
11886
Johannes Berg947add32013-02-22 22:05:20 +010011887void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11888 const u8 *bssid, bool preauth, gfp_t gfp)
11889{
11890 struct wireless_dev *wdev = dev->ieee80211_ptr;
11891 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011892 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011893
11894 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11895 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11896}
11897EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11898
11899static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11900 struct net_device *netdev,
11901 struct cfg80211_chan_def *chandef,
Luciano Coelhof8d75522014-11-07 14:31:35 +020011902 gfp_t gfp,
11903 enum nl80211_commands notif,
11904 u8 count)
Thomas Pedersen53145262012-04-06 13:35:47 -070011905{
11906 struct sk_buff *msg;
11907 void *hdr;
11908
Thomas Graf58050fc2012-06-28 03:57:45 +000011909 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011910 if (!msg)
11911 return;
11912
Luciano Coelhof8d75522014-11-07 14:31:35 +020011913 hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
Thomas Pedersen53145262012-04-06 13:35:47 -070011914 if (!hdr) {
11915 nlmsg_free(msg);
11916 return;
11917 }
11918
Johannes Berg683b6d32012-11-08 21:25:48 +010011919 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11920 goto nla_put_failure;
11921
11922 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011923 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011924
Luciano Coelhof8d75522014-11-07 14:31:35 +020011925 if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
11926 (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
11927 goto nla_put_failure;
11928
Thomas Pedersen53145262012-04-06 13:35:47 -070011929 genlmsg_end(msg, hdr);
11930
Johannes Berg68eb5502013-11-19 15:19:38 +010011931 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011932 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011933 return;
11934
11935 nla_put_failure:
11936 genlmsg_cancel(msg, hdr);
11937 nlmsg_free(msg);
11938}
11939
Johannes Berg947add32013-02-22 22:05:20 +010011940void cfg80211_ch_switch_notify(struct net_device *dev,
11941 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011942{
Johannes Berg947add32013-02-22 22:05:20 +010011943 struct wireless_dev *wdev = dev->ieee80211_ptr;
11944 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011945 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011946
Simon Wunderliche487eae2013-11-21 18:19:51 +010011947 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011948
Simon Wunderliche487eae2013-11-21 18:19:51 +010011949 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011950
Michal Kazior9e0e2962014-01-29 14:22:27 +010011951 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011952 wdev->preset_chandef = *chandef;
Luciano Coelhof8d75522014-11-07 14:31:35 +020011953 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
11954 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
Johannes Berg947add32013-02-22 22:05:20 +010011955}
11956EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11957
Luciano Coelhof8d75522014-11-07 14:31:35 +020011958void cfg80211_ch_switch_started_notify(struct net_device *dev,
11959 struct cfg80211_chan_def *chandef,
11960 u8 count)
11961{
11962 struct wireless_dev *wdev = dev->ieee80211_ptr;
11963 struct wiphy *wiphy = wdev->wiphy;
11964 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11965
11966 trace_cfg80211_ch_switch_started_notify(dev, chandef);
11967
11968 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
11969 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
11970}
11971EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
11972
Johannes Berg947add32013-02-22 22:05:20 +010011973void cfg80211_cqm_txe_notify(struct net_device *dev,
11974 const u8 *peer, u32 num_packets,
11975 u32 rate, u32 intvl, gfp_t gfp)
11976{
11977 struct wireless_dev *wdev = dev->ieee80211_ptr;
11978 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011979 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011980 struct sk_buff *msg;
11981 struct nlattr *pinfoattr;
11982 void *hdr;
11983
11984 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11985 if (!msg)
11986 return;
11987
11988 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11989 if (!hdr) {
11990 nlmsg_free(msg);
11991 return;
11992 }
11993
11994 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011995 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011996 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11997 goto nla_put_failure;
11998
11999 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
12000 if (!pinfoattr)
12001 goto nla_put_failure;
12002
12003 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
12004 goto nla_put_failure;
12005
12006 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
12007 goto nla_put_failure;
12008
12009 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
12010 goto nla_put_failure;
12011
12012 nla_nest_end(msg, pinfoattr);
12013
12014 genlmsg_end(msg, hdr);
12015
Johannes Berg68eb5502013-11-19 15:19:38 +010012016 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012017 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070012018 return;
12019
12020 nla_put_failure:
12021 genlmsg_cancel(msg, hdr);
12022 nlmsg_free(msg);
12023}
Johannes Berg947add32013-02-22 22:05:20 +010012024EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070012025
12026void
Simon Wunderlich04f39042013-02-08 18:16:19 +010012027nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010012028 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010012029 enum nl80211_radar_event event,
12030 struct net_device *netdev, gfp_t gfp)
12031{
12032 struct sk_buff *msg;
12033 void *hdr;
12034
12035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12036 if (!msg)
12037 return;
12038
12039 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
12040 if (!hdr) {
12041 nlmsg_free(msg);
12042 return;
12043 }
12044
12045 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
12046 goto nla_put_failure;
12047
12048 /* NOP and radar events don't need a netdev parameter */
12049 if (netdev) {
12050 struct wireless_dev *wdev = netdev->ieee80211_ptr;
12051
12052 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12053 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12054 goto nla_put_failure;
12055 }
12056
12057 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
12058 goto nla_put_failure;
12059
12060 if (nl80211_send_chandef(msg, chandef))
12061 goto nla_put_failure;
12062
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012063 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010012064
Johannes Berg68eb5502013-11-19 15:19:38 +010012065 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012066 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010012067 return;
12068
12069 nla_put_failure:
12070 genlmsg_cancel(msg, hdr);
12071 nlmsg_free(msg);
12072}
12073
Johannes Berg947add32013-02-22 22:05:20 +010012074void cfg80211_cqm_pktloss_notify(struct net_device *dev,
12075 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010012076{
Johannes Berg947add32013-02-22 22:05:20 +010012077 struct wireless_dev *wdev = dev->ieee80211_ptr;
12078 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012079 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010012080 struct sk_buff *msg;
12081 struct nlattr *pinfoattr;
12082 void *hdr;
12083
Johannes Berg947add32013-02-22 22:05:20 +010012084 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
12085
Thomas Graf58050fc2012-06-28 03:57:45 +000012086 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010012087 if (!msg)
12088 return;
12089
12090 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
12091 if (!hdr) {
12092 nlmsg_free(msg);
12093 return;
12094 }
12095
David S. Miller9360ffd2012-03-29 04:41:26 -040012096 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010012097 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040012098 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
12099 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010012100
12101 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
12102 if (!pinfoattr)
12103 goto nla_put_failure;
12104
David S. Miller9360ffd2012-03-29 04:41:26 -040012105 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
12106 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010012107
12108 nla_nest_end(msg, pinfoattr);
12109
Johannes Berg3b7b72e2011-10-22 19:05:51 +020012110 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010012111
Johannes Berg68eb5502013-11-19 15:19:38 +010012112 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012113 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010012114 return;
12115
12116 nla_put_failure:
12117 genlmsg_cancel(msg, hdr);
12118 nlmsg_free(msg);
12119}
Johannes Berg947add32013-02-22 22:05:20 +010012120EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010012121
Johannes Berg7f6cf312011-11-04 11:18:15 +010012122void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
12123 u64 cookie, bool acked, gfp_t gfp)
12124{
12125 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012126 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg7f6cf312011-11-04 11:18:15 +010012127 struct sk_buff *msg;
12128 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010012129
Beni Lev4ee3e062012-08-27 12:49:39 +030012130 trace_cfg80211_probe_status(dev, addr, cookie, acked);
12131
Thomas Graf58050fc2012-06-28 03:57:45 +000012132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030012133
Johannes Berg7f6cf312011-11-04 11:18:15 +010012134 if (!msg)
12135 return;
12136
12137 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
12138 if (!hdr) {
12139 nlmsg_free(msg);
12140 return;
12141 }
12142
David S. Miller9360ffd2012-03-29 04:41:26 -040012143 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12144 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12145 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12146 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
12147 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
12148 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010012149
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012150 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010012151
Johannes Berg68eb5502013-11-19 15:19:38 +010012152 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012153 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010012154 return;
12155
12156 nla_put_failure:
12157 genlmsg_cancel(msg, hdr);
12158 nlmsg_free(msg);
12159}
12160EXPORT_SYMBOL(cfg80211_probe_status);
12161
Johannes Berg5e7602302011-11-04 11:18:17 +010012162void cfg80211_report_obss_beacon(struct wiphy *wiphy,
12163 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070012164 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010012165{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012166 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg5e7602302011-11-04 11:18:17 +010012167 struct sk_buff *msg;
12168 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070012169 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010012170
Beni Lev4ee3e062012-08-27 12:49:39 +030012171 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
12172
Ben Greear37c73b52012-10-26 14:49:25 -070012173 spin_lock_bh(&rdev->beacon_registrations_lock);
12174 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12175 msg = nlmsg_new(len + 100, GFP_ATOMIC);
12176 if (!msg) {
12177 spin_unlock_bh(&rdev->beacon_registrations_lock);
12178 return;
12179 }
Johannes Berg5e7602302011-11-04 11:18:17 +010012180
Ben Greear37c73b52012-10-26 14:49:25 -070012181 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
12182 if (!hdr)
12183 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010012184
Ben Greear37c73b52012-10-26 14:49:25 -070012185 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12186 (freq &&
12187 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
12188 (sig_dbm &&
12189 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
12190 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
12191 goto nla_put_failure;
12192
12193 genlmsg_end(msg, hdr);
12194
12195 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010012196 }
Ben Greear37c73b52012-10-26 14:49:25 -070012197 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010012198 return;
12199
12200 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070012201 spin_unlock_bh(&rdev->beacon_registrations_lock);
12202 if (hdr)
12203 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010012204 nlmsg_free(msg);
12205}
12206EXPORT_SYMBOL(cfg80211_report_obss_beacon);
12207
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012208#ifdef CONFIG_PM
Luciano Coelho8cd4d452014-09-17 11:55:28 +030012209static int cfg80211_net_detect_results(struct sk_buff *msg,
12210 struct cfg80211_wowlan_wakeup *wakeup)
12211{
12212 struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
12213 struct nlattr *nl_results, *nl_match, *nl_freqs;
12214 int i, j;
12215
12216 nl_results = nla_nest_start(
12217 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
12218 if (!nl_results)
12219 return -EMSGSIZE;
12220
12221 for (i = 0; i < nd->n_matches; i++) {
12222 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
12223
12224 nl_match = nla_nest_start(msg, i);
12225 if (!nl_match)
12226 break;
12227
12228 /* The SSID attribute is optional in nl80211, but for
12229 * simplicity reasons it's always present in the
12230 * cfg80211 structure. If a driver can't pass the
12231 * SSID, that needs to be changed. A zero length SSID
12232 * is still a valid SSID (wildcard), so it cannot be
12233 * used for this purpose.
12234 */
12235 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
12236 match->ssid.ssid)) {
12237 nla_nest_cancel(msg, nl_match);
12238 goto out;
12239 }
12240
12241 if (match->n_channels) {
12242 nl_freqs = nla_nest_start(
12243 msg, NL80211_ATTR_SCAN_FREQUENCIES);
12244 if (!nl_freqs) {
12245 nla_nest_cancel(msg, nl_match);
12246 goto out;
12247 }
12248
12249 for (j = 0; j < match->n_channels; j++) {
12250 if (nla_put_u32(msg,
12251 NL80211_ATTR_WIPHY_FREQ,
12252 match->channels[j])) {
12253 nla_nest_cancel(msg, nl_freqs);
12254 nla_nest_cancel(msg, nl_match);
12255 goto out;
12256 }
12257 }
12258
12259 nla_nest_end(msg, nl_freqs);
12260 }
12261
12262 nla_nest_end(msg, nl_match);
12263 }
12264
12265out:
12266 nla_nest_end(msg, nl_results);
12267 return 0;
12268}
12269
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012270void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
12271 struct cfg80211_wowlan_wakeup *wakeup,
12272 gfp_t gfp)
12273{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012274 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012275 struct sk_buff *msg;
12276 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012277 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012278
12279 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
12280
12281 if (wakeup)
12282 size += wakeup->packet_present_len;
12283
12284 msg = nlmsg_new(size, gfp);
12285 if (!msg)
12286 return;
12287
12288 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
12289 if (!hdr)
12290 goto free_msg;
12291
12292 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12293 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12294 goto free_msg;
12295
12296 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12297 wdev->netdev->ifindex))
12298 goto free_msg;
12299
12300 if (wakeup) {
12301 struct nlattr *reasons;
12302
12303 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020012304 if (!reasons)
12305 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012306
12307 if (wakeup->disconnect &&
12308 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
12309 goto free_msg;
12310 if (wakeup->magic_pkt &&
12311 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
12312 goto free_msg;
12313 if (wakeup->gtk_rekey_failure &&
12314 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
12315 goto free_msg;
12316 if (wakeup->eap_identity_req &&
12317 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
12318 goto free_msg;
12319 if (wakeup->four_way_handshake &&
12320 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
12321 goto free_msg;
12322 if (wakeup->rfkill_release &&
12323 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
12324 goto free_msg;
12325
12326 if (wakeup->pattern_idx >= 0 &&
12327 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
12328 wakeup->pattern_idx))
12329 goto free_msg;
12330
Johannes Bergae917c92013-10-25 11:05:22 +020012331 if (wakeup->tcp_match &&
12332 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
12333 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010012334
Johannes Bergae917c92013-10-25 11:05:22 +020012335 if (wakeup->tcp_connlost &&
12336 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
12337 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010012338
Johannes Bergae917c92013-10-25 11:05:22 +020012339 if (wakeup->tcp_nomoretokens &&
12340 nla_put_flag(msg,
12341 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
12342 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010012343
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012344 if (wakeup->packet) {
12345 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
12346 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
12347
12348 if (!wakeup->packet_80211) {
12349 pkt_attr =
12350 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
12351 len_attr =
12352 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
12353 }
12354
12355 if (wakeup->packet_len &&
12356 nla_put_u32(msg, len_attr, wakeup->packet_len))
12357 goto free_msg;
12358
12359 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
12360 wakeup->packet))
12361 goto free_msg;
12362 }
12363
Luciano Coelho8cd4d452014-09-17 11:55:28 +030012364 if (wakeup->net_detect &&
12365 cfg80211_net_detect_results(msg, wakeup))
12366 goto free_msg;
12367
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012368 nla_nest_end(msg, reasons);
12369 }
12370
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012371 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012372
Johannes Berg68eb5502013-11-19 15:19:38 +010012373 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012374 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012375 return;
12376
12377 free_msg:
12378 nlmsg_free(msg);
12379}
12380EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
12381#endif
12382
Jouni Malinen3475b092012-11-16 22:49:57 +020012383void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
12384 enum nl80211_tdls_operation oper,
12385 u16 reason_code, gfp_t gfp)
12386{
12387 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012388 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinen3475b092012-11-16 22:49:57 +020012389 struct sk_buff *msg;
12390 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020012391
12392 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
12393 reason_code);
12394
12395 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12396 if (!msg)
12397 return;
12398
12399 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
12400 if (!hdr) {
12401 nlmsg_free(msg);
12402 return;
12403 }
12404
12405 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12406 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12407 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
12408 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
12409 (reason_code > 0 &&
12410 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
12411 goto nla_put_failure;
12412
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012413 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020012414
Johannes Berg68eb5502013-11-19 15:19:38 +010012415 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012416 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020012417 return;
12418
12419 nla_put_failure:
12420 genlmsg_cancel(msg, hdr);
12421 nlmsg_free(msg);
12422}
12423EXPORT_SYMBOL(cfg80211_tdls_oper_request);
12424
Jouni Malinen026331c2010-02-15 12:53:10 +020012425static int nl80211_netlink_notify(struct notifier_block * nb,
12426 unsigned long state,
12427 void *_notify)
12428{
12429 struct netlink_notify *notify = _notify;
12430 struct cfg80211_registered_device *rdev;
12431 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070012432 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020012433
12434 if (state != NETLINK_URELEASE)
12435 return NOTIFY_DONE;
12436
12437 rcu_read_lock();
12438
Johannes Berg5e7602302011-11-04 11:18:17 +010012439 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010012440 bool schedule_destroy_work = false;
12441
12442 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000012443 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070012444
Johannes Berg78f22b62014-03-24 17:57:27 +010012445 if (wdev->owner_nlportid == notify->portid)
12446 schedule_destroy_work = true;
12447 }
12448
Ben Greear37c73b52012-10-26 14:49:25 -070012449 spin_lock_bh(&rdev->beacon_registrations_lock);
12450 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
12451 list) {
12452 if (reg->nlportid == notify->portid) {
12453 list_del(&reg->list);
12454 kfree(reg);
12455 break;
12456 }
12457 }
12458 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010012459
12460 if (schedule_destroy_work) {
12461 struct cfg80211_iface_destroy *destroy;
12462
12463 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
12464 if (destroy) {
12465 destroy->nlportid = notify->portid;
12466 spin_lock(&rdev->destroy_list_lock);
12467 list_add(&destroy->list, &rdev->destroy_list);
12468 spin_unlock(&rdev->destroy_list_lock);
12469 schedule_work(&rdev->destroy_work);
12470 }
12471 }
Johannes Berg5e7602302011-11-04 11:18:17 +010012472 }
Jouni Malinen026331c2010-02-15 12:53:10 +020012473
12474 rcu_read_unlock();
12475
Zhao, Gang6784c7d2014-04-21 12:53:04 +080012476 return NOTIFY_OK;
Jouni Malinen026331c2010-02-15 12:53:10 +020012477}
12478
12479static struct notifier_block nl80211_netlink_notifier = {
12480 .notifier_call = nl80211_netlink_notify,
12481};
12482
Jouni Malinen355199e2013-02-27 17:14:27 +020012483void cfg80211_ft_event(struct net_device *netdev,
12484 struct cfg80211_ft_event_params *ft_event)
12485{
12486 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012487 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Jouni Malinen355199e2013-02-27 17:14:27 +020012488 struct sk_buff *msg;
12489 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020012490
12491 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
12492
12493 if (!ft_event->target_ap)
12494 return;
12495
12496 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12497 if (!msg)
12498 return;
12499
12500 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020012501 if (!hdr)
12502 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020012503
Johannes Bergae917c92013-10-25 11:05:22 +020012504 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12505 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12506 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
12507 goto out;
12508
12509 if (ft_event->ies &&
12510 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
12511 goto out;
12512 if (ft_event->ric_ies &&
12513 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
12514 ft_event->ric_ies))
12515 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020012516
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012517 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020012518
Johannes Berg68eb5502013-11-19 15:19:38 +010012519 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012520 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020012521 return;
12522 out:
12523 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020012524}
12525EXPORT_SYMBOL(cfg80211_ft_event);
12526
Arend van Spriel5de17982013-04-18 15:49:00 +020012527void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
12528{
12529 struct cfg80211_registered_device *rdev;
12530 struct sk_buff *msg;
12531 void *hdr;
12532 u32 nlportid;
12533
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012534 rdev = wiphy_to_rdev(wdev->wiphy);
Arend van Spriel5de17982013-04-18 15:49:00 +020012535 if (!rdev->crit_proto_nlportid)
12536 return;
12537
12538 nlportid = rdev->crit_proto_nlportid;
12539 rdev->crit_proto_nlportid = 0;
12540
12541 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12542 if (!msg)
12543 return;
12544
12545 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
12546 if (!hdr)
12547 goto nla_put_failure;
12548
12549 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12550 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12551 goto nla_put_failure;
12552
12553 genlmsg_end(msg, hdr);
12554
12555 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
12556 return;
12557
12558 nla_put_failure:
12559 if (hdr)
12560 genlmsg_cancel(msg, hdr);
12561 nlmsg_free(msg);
12562
12563}
12564EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
12565
Johannes Berg348baf02014-01-24 14:06:29 +010012566void nl80211_send_ap_stopped(struct wireless_dev *wdev)
12567{
12568 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012569 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg348baf02014-01-24 14:06:29 +010012570 struct sk_buff *msg;
12571 void *hdr;
12572
12573 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12574 if (!msg)
12575 return;
12576
12577 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
12578 if (!hdr)
12579 goto out;
12580
12581 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12582 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
12583 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12584 goto out;
12585
12586 genlmsg_end(msg, hdr);
12587
12588 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
12589 NL80211_MCGRP_MLME, GFP_KERNEL);
12590 return;
12591 out:
12592 nlmsg_free(msg);
12593}
12594
Johannes Berg55682962007-09-20 13:09:35 -040012595/* initialisation/exit functions */
12596
12597int nl80211_init(void)
12598{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000012599 int err;
Johannes Berg55682962007-09-20 13:09:35 -040012600
Johannes Berg2a94fe42013-11-19 15:19:39 +010012601 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
12602 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040012603 if (err)
12604 return err;
12605
Jouni Malinen026331c2010-02-15 12:53:10 +020012606 err = netlink_register_notifier(&nl80211_netlink_notifier);
12607 if (err)
12608 goto err_out;
12609
Johannes Berg55682962007-09-20 13:09:35 -040012610 return 0;
12611 err_out:
12612 genl_unregister_family(&nl80211_fam);
12613 return err;
12614}
12615
12616void nl80211_exit(void)
12617{
Jouni Malinen026331c2010-02-15 12:53:10 +020012618 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040012619 genl_unregister_family(&nl80211_fam);
12620}