blob: 04c1c7896cc18fda4eeb435586bb0a358e15f696 [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 Berg66cd7942017-02-07 22:40:44 +02006 * Copyright 2015-2017 Intel Deutschland GmbH
Johannes Berg55682962007-09-20 13:09:35 -04007 */
8
9#include <linux/if.h>
10#include <linux/module.h>
11#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040013#include <linux/list.h>
14#include <linux/if_ether.h>
15#include <linux/ieee80211.h>
16#include <linux/nl80211.h>
17#include <linux/rtnetlink.h>
18#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010019#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020020#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040021#include <net/genetlink.h>
22#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020023#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010024#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040025#include "core.h"
26#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070027#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030028#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040029
Jouni Malinen5fb628e2011-08-10 23:54:35 +030030static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
31 struct genl_info *info,
32 struct cfg80211_crypto_settings *settings,
33 int cipher_limit);
34
Johannes Berg55682962007-09-20 13:09:35 -040035/* the netlink family */
Johannes Berg489111e2016-10-24 14:40:03 +020036static struct genl_family nl80211_fam;
Johannes Berg55682962007-09-20 13:09:35 -040037
Johannes Berg2a94fe42013-11-19 15:19:39 +010038/* multicast groups */
39enum nl80211_multicast_groups {
40 NL80211_MCGRP_CONFIG,
41 NL80211_MCGRP_SCAN,
42 NL80211_MCGRP_REGULATORY,
43 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010044 NL80211_MCGRP_VENDOR,
Ayala Beker50bcd312016-09-20 17:31:17 +030045 NL80211_MCGRP_NAN,
Johannes Berg2a94fe42013-11-19 15:19:39 +010046 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
47};
48
49static const struct genl_multicast_group nl80211_mcgrps[] = {
Johannes Berg71b836e2014-12-23 17:17:38 +010050 [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
51 [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
52 [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
53 [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
54 [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
Ayala Beker50bcd312016-09-20 17:31:17 +030055 [NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
Johannes Berg2a94fe42013-11-19 15:19:39 +010056#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg71b836e2014-12-23 17:17:38 +010057 [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
Johannes Berg2a94fe42013-11-19 15:19:39 +010058#endif
59};
60
Johannes Berg89a54e42012-06-15 14:33:17 +020061/* returns ERR_PTR values */
62static struct wireless_dev *
63__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040064{
Johannes Berg89a54e42012-06-15 14:33:17 +020065 struct cfg80211_registered_device *rdev;
66 struct wireless_dev *result = NULL;
67 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
68 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
69 u64 wdev_id;
70 int wiphy_idx = -1;
71 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040072
Johannes Berg5fe231e2013-05-08 21:45:15 +020073 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040074
Johannes Berg89a54e42012-06-15 14:33:17 +020075 if (!have_ifidx && !have_wdev_id)
76 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040077
Johannes Berg89a54e42012-06-15 14:33:17 +020078 if (have_ifidx)
79 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
80 if (have_wdev_id) {
81 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
82 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040083 }
84
Johannes Berg89a54e42012-06-15 14:33:17 +020085 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
86 struct wireless_dev *wdev;
87
88 if (wiphy_net(&rdev->wiphy) != netns)
89 continue;
90
91 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
92 continue;
93
Johannes Berg53873f12016-05-03 16:52:04 +030094 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020095 if (have_ifidx && wdev->netdev &&
96 wdev->netdev->ifindex == ifidx) {
97 result = wdev;
98 break;
99 }
100 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
101 result = wdev;
102 break;
103 }
104 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200105
106 if (result)
107 break;
108 }
109
110 if (result)
111 return result;
112 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400113}
114
Johannes Berga9455402012-06-15 13:32:49 +0200115static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200116__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200117{
Johannes Berg7fee4772012-06-15 14:09:58 +0200118 struct cfg80211_registered_device *rdev = NULL, *tmp;
119 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200120
Johannes Berg5fe231e2013-05-08 21:45:15 +0200121 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg878d9ec2012-06-15 14:18:32 +0200123 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200124 !attrs[NL80211_ATTR_IFINDEX] &&
125 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200126 return ERR_PTR(-EINVAL);
127
Johannes Berg878d9ec2012-06-15 14:18:32 +0200128 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200129 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200130 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200131
Johannes Berg89a54e42012-06-15 14:33:17 +0200132 if (attrs[NL80211_ATTR_WDEV]) {
133 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
134 struct wireless_dev *wdev;
135 bool found = false;
136
137 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
138 if (tmp) {
139 /* make sure wdev exists */
Johannes Berg53873f12016-05-03 16:52:04 +0300140 list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +0200141 if (wdev->identifier != (u32)wdev_id)
142 continue;
143 found = true;
144 break;
145 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200146
147 if (!found)
148 tmp = NULL;
149
150 if (rdev && tmp != rdev)
151 return ERR_PTR(-EINVAL);
152 rdev = tmp;
153 }
154 }
155
Johannes Berg878d9ec2012-06-15 14:18:32 +0200156 if (attrs[NL80211_ATTR_IFINDEX]) {
157 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -0700158
Ying Xue7f2b8562014-01-15 10:23:45 +0800159 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200160 if (netdev) {
161 if (netdev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800162 tmp = wiphy_to_rdev(
163 netdev->ieee80211_ptr->wiphy);
Johannes Berg7fee4772012-06-15 14:09:58 +0200164 else
165 tmp = NULL;
166
Johannes Berg7fee4772012-06-15 14:09:58 +0200167 /* not wireless device -- return error */
168 if (!tmp)
169 return ERR_PTR(-EINVAL);
170
171 /* mismatch -- return error */
172 if (rdev && tmp != rdev)
173 return ERR_PTR(-EINVAL);
174
175 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200176 }
Johannes Berga9455402012-06-15 13:32:49 +0200177 }
178
Johannes Berg4f7eff12012-06-15 14:14:22 +0200179 if (!rdev)
180 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200181
Johannes Berg4f7eff12012-06-15 14:14:22 +0200182 if (netns != wiphy_net(&rdev->wiphy))
183 return ERR_PTR(-ENODEV);
184
185 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200186}
187
188/*
189 * This function returns a pointer to the driver
190 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200191 *
192 * The result of this can be a PTR_ERR and hence must
193 * be checked with IS_ERR() for errors.
194 */
195static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200196cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200197{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200198 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200199}
200
Johannes Berg55682962007-09-20 13:09:35 -0400201/* policy for the attributes */
Luciano Coelho8cd4d452014-09-17 11:55:28 +0300202static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
Johannes Berg55682962007-09-20 13:09:35 -0400203 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
204 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700205 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200206 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100207
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200208 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530209 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100210 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
211 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
212 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
213
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200214 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
215 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
216 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
217 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100218 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +0200219 [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400220
221 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
222 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
223 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100224
Eliad Pellere007b852011-11-24 18:13:56 +0200225 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
226 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100227
Johannes Bergb9454e82009-07-08 13:29:08 +0200228 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100229 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
230 .len = WLAN_MAX_KEY_LEN },
231 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
232 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
233 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200234 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200235 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100236
237 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
238 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
239 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
240 .len = IEEE80211_MAX_DATA_LEN },
241 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
242 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100243 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
244 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
245 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
246 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
247 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100248 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100249 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200250 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100251 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800252 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100253 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300254
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700255 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
256 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
257
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300258 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
259 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
260 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200261 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
262 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100263 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300264
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800265 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700266 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700267
Johannes Berg6c739412011-11-03 09:27:01 +0100268 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200269
270 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
271 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
272 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100273 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
274 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200275
276 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
277 .len = IEEE80211_MAX_SSID_LEN },
278 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
279 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200280 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300281 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300282 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300283 [NL80211_ATTR_STA_FLAGS2] = {
284 .len = sizeof(struct nl80211_sta_flag_update),
285 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300286 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300287 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
288 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200289 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
290 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
291 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200292 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100293 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100294 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
295 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100296 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
297 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200298 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200299 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
300 .len = IEEE80211_MAX_DATA_LEN },
301 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200302 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200303 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300304 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200305 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300306 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
307 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200308 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900309 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
310 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100311 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100312 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100313 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200314 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700315 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300316 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200317 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200318 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300319 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300320 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
321 .len = IEEE80211_MAX_DATA_LEN },
322 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
323 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530324 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300325 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530326 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300327 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
328 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
329 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
330 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
331 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Arik Nemtsov31fa97c2014-06-11 17:18:21 +0300332 [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100333 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200334 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
335 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700336 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800337 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
338 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
339 .len = NL80211_HT_CAPABILITY_LEN
340 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100341 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530342 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530343 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200344 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700345 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinen11b6b5a2016-10-27 00:41:58 +0300346 [NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000347 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700348 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100349 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
350 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530351 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
352 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200353 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
354 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100355 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100356 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
357 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
358 .len = NL80211_VHT_CAPABILITY_LEN,
359 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200360 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
361 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
362 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300363 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200364 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
365 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
366 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +0300367 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
368 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530369 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
370 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200371 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100372 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100373 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
374 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
375 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800376 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
377 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200378 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
379 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530380 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Jukka Rissanen18e5ca62014-11-13 17:25:14 +0200381 [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
Andrei Otcheretianski34d22ce2014-05-09 14:11:44 +0300382 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
Assaf Kraussbab5ab72014-09-03 15:25:01 +0300383 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
Johannes Berg960d01a2014-09-09 22:55:35 +0300384 [NL80211_ATTR_TSID] = { .type = NLA_U8 },
385 [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
386 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
Eliad Peller18998c32014-09-10 14:07:34 +0300387 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
Johannes Bergad2b26a2014-06-12 21:39:05 +0200388 [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
Arik Nemtsov1bdd7162014-12-15 19:26:01 +0200389 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
Vadim Kochan4b681c82015-01-12 16:34:05 +0200390 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
Luciano Coelho9c748932015-01-16 16:04:09 +0200391 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
Ilan peer05050752015-03-04 00:32:06 -0500392 [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
Lior David34d50512016-01-28 10:58:25 +0200393 [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
Arend van Spriel38de03d2016-03-02 20:37:18 +0100394 [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
Ayala Beker17b94242016-03-17 15:41:38 +0200395 [NL80211_ATTR_STA_SUPPORT_P2P_PS] = { .type = NLA_U8 },
Aviya Erenfeldc6e6a0c2016-07-05 15:23:08 +0300396 [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
397 .len = VHT_MUMIMO_GROUPS_DATA_LEN
398 },
399 [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = { .len = ETH_ALEN },
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300400 [NL80211_ATTR_NAN_MASTER_PREF] = { .type = NLA_U8 },
Luca Coelho85859892017-02-08 15:00:34 +0200401 [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
Ayala Bekera442b762016-09-20 17:31:15 +0300402 [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
Jouni Malinen348bd452016-10-27 00:42:03 +0300403 [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
404 .len = FILS_MAX_KEK_LEN },
405 [NL80211_ATTR_FILS_NONCES] = { .len = 2 * FILS_NONCE_LEN },
Michael Braunce0ce132016-10-10 19:12:22 +0200406 [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
Vamsi Krishna2fa436b2016-12-02 23:59:08 +0200407 [NL80211_ATTR_BSSID] = { .len = ETH_ALEN },
vamsi krishnabf95ecd2017-01-13 01:12:20 +0200408 [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
409 [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
410 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
411 },
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +0200412 [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +0300413 [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
414 .len = FILS_ERP_MAX_USERNAME_LEN },
415 [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
416 .len = FILS_ERP_MAX_REALM_LEN },
417 [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
418 [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
419 .len = FILS_ERP_MAX_RRK_LEN },
420 [NL80211_ATTR_FILS_CACHE_ID] = { .len = 2 },
421 [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400422};
423
Johannes Berge31b8212010-10-05 19:39:30 +0200424/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000425static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200426 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200427 [NL80211_KEY_IDX] = { .type = NLA_U8 },
428 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200429 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200430 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
431 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200432 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100433 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
434};
435
436/* policy for the key default flags */
437static const struct nla_policy
438nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
439 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
440 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200441};
442
Johannes Bergf83ace32016-10-17 08:04:07 +0200443#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +0200444/* policy for WoWLAN attributes */
445static const struct nla_policy
446nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
447 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
448 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
449 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
450 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200451 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
452 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
453 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
454 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100455 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
Luciano Coelho8cd4d452014-09-17 11:55:28 +0300456 [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100457};
458
459static const struct nla_policy
460nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
461 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
462 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
463 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
464 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
465 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
466 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
467 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
468 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
469 },
470 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
471 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
472 },
473 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
474 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
475 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200476};
Johannes Bergf83ace32016-10-17 08:04:07 +0200477#endif /* CONFIG_PM */
Johannes Bergff1b6e62011-05-04 15:37:28 +0200478
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -0700479/* policy for coalesce rule attributes */
480static const struct nla_policy
481nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
482 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
483 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
484 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
485};
486
Johannes Berge5497d72011-07-05 16:35:40 +0200487/* policy for GTK rekey offload attributes */
488static const struct nla_policy
489nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
490 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
491 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
492 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
493};
494
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300495static const struct nla_policy
496nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200497 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300498 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700499 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300500};
501
Avraham Stern3b06d272015-10-12 09:51:34 +0300502static const struct nla_policy
503nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
504 [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
505 [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
506};
507
Arend van Spriel38de03d2016-03-02 20:37:18 +0100508static const struct nla_policy
509nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
510 [NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
511 [NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
512 [NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
513 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
514 },
515};
516
Ayala Bekera442b762016-09-20 17:31:15 +0300517/* policy for NAN function attributes */
518static const struct nla_policy
519nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
520 [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
521 [NL80211_NAN_FUNC_SERVICE_ID] = { .type = NLA_BINARY,
522 .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
523 [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
524 [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
525 [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
526 [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
527 [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
528 [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { .len = ETH_ALEN },
529 [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
530 [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
531 [NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
532 .len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
533 [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
534 [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
535 [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
536 [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
537 [NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
538};
539
540/* policy for Service Response Filter attributes */
541static const struct nla_policy
542nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
543 [NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
544 [NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
545 .len = NL80211_NAN_FUNC_SRF_MAX_LEN },
546 [NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
547 [NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
548};
549
Johannes Berg97990a02013-04-19 01:02:55 +0200550static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
551 struct netlink_callback *cb,
552 struct cfg80211_registered_device **rdev,
553 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100554{
Johannes Berg67748892010-10-04 21:14:06 +0200555 int err;
556
Johannes Berg67748892010-10-04 21:14:06 +0200557 rtnl_lock();
558
Johannes Berg97990a02013-04-19 01:02:55 +0200559 if (!cb->args[0]) {
560 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
Johannes Bergc90c39d2016-10-24 14:40:01 +0200561 genl_family_attrbuf(&nl80211_fam),
562 nl80211_fam.maxattr, nl80211_policy);
Johannes Berg97990a02013-04-19 01:02:55 +0200563 if (err)
564 goto out_unlock;
565
Johannes Bergc90c39d2016-10-24 14:40:01 +0200566 *wdev = __cfg80211_wdev_from_attrs(
567 sock_net(skb->sk),
568 genl_family_attrbuf(&nl80211_fam));
Johannes Berg97990a02013-04-19 01:02:55 +0200569 if (IS_ERR(*wdev)) {
570 err = PTR_ERR(*wdev);
571 goto out_unlock;
572 }
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800573 *rdev = wiphy_to_rdev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200574 /* 0 is the first index - add 1 to parse only once */
575 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200576 cb->args[1] = (*wdev)->identifier;
577 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200578 /* subtract the 1 again here */
579 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200580 struct wireless_dev *tmp;
581
582 if (!wiphy) {
583 err = -ENODEV;
584 goto out_unlock;
585 }
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800586 *rdev = wiphy_to_rdev(wiphy);
Johannes Berg97990a02013-04-19 01:02:55 +0200587 *wdev = NULL;
588
Johannes Berg53873f12016-05-03 16:52:04 +0300589 list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
Johannes Berg97990a02013-04-19 01:02:55 +0200590 if (tmp->identifier == cb->args[1]) {
591 *wdev = tmp;
592 break;
593 }
594 }
Johannes Berg97990a02013-04-19 01:02:55 +0200595
596 if (!*wdev) {
597 err = -ENODEV;
598 goto out_unlock;
599 }
Johannes Berg67748892010-10-04 21:14:06 +0200600 }
601
Johannes Berg67748892010-10-04 21:14:06 +0200602 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200603 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200604 rtnl_unlock();
605 return err;
606}
607
Johannes Berg97990a02013-04-19 01:02:55 +0200608static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200609{
Johannes Berg67748892010-10-04 21:14:06 +0200610 rtnl_unlock();
611}
612
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100613/* IE validation */
614static bool is_valid_ie_attr(const struct nlattr *attr)
615{
616 const u8 *pos;
617 int len;
618
619 if (!attr)
620 return true;
621
622 pos = nla_data(attr);
623 len = nla_len(attr);
624
625 while (len) {
626 u8 elemlen;
627
628 if (len < 2)
629 return false;
630 len -= 2;
631
632 elemlen = pos[1];
633 if (elemlen > len)
634 return false;
635
636 len -= elemlen;
637 pos += 2 + elemlen;
638 }
639
640 return true;
641}
642
Johannes Berg55682962007-09-20 13:09:35 -0400643/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000644static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400645 int flags, u8 cmd)
646{
647 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000648 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400649}
650
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400651static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100652 struct ieee80211_channel *chan,
653 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400654{
Rostislav Lisovyea077c12014-04-15 14:37:55 +0200655 /* Some channels must be completely excluded from the
656 * list to protect old user-space tools from breaking
657 */
658 if (!large && chan->flags &
659 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
660 return 0;
661
David S. Miller9360ffd2012-03-29 04:41:26 -0400662 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
663 chan->center_freq))
664 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400665
David S. Miller9360ffd2012-03-29 04:41:26 -0400666 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
667 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
668 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200669 if (chan->flags & IEEE80211_CHAN_NO_IR) {
670 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
671 goto nla_put_failure;
672 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
673 goto nla_put_failure;
674 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100675 if (chan->flags & IEEE80211_CHAN_RADAR) {
676 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
677 goto nla_put_failure;
678 if (large) {
679 u32 time;
680
681 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
682
683 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
684 chan->dfs_state))
685 goto nla_put_failure;
686 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
687 time))
688 goto nla_put_failure;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100689 if (nla_put_u32(msg,
690 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
691 chan->dfs_cac_ms))
692 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100693 }
694 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400695
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100696 if (large) {
697 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
698 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
699 goto nla_put_failure;
700 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
701 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
702 goto nla_put_failure;
703 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
704 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
705 goto nla_put_failure;
706 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
707 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
708 goto nla_put_failure;
David Spinadel570dbde2014-02-23 09:12:59 +0200709 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
710 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
711 goto nla_put_failure;
Arik Nemtsov06f207f2015-05-06 16:28:31 +0300712 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
713 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
David Spinadel570dbde2014-02-23 09:12:59 +0200714 goto nla_put_failure;
Rostislav Lisovyea077c12014-04-15 14:37:55 +0200715 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
716 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
717 goto nla_put_failure;
718 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
719 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
720 goto nla_put_failure;
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100721 }
722
David S. Miller9360ffd2012-03-29 04:41:26 -0400723 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
724 DBM_TO_MBM(chan->max_power)))
725 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400726
727 return 0;
728
729 nla_put_failure:
730 return -ENOBUFS;
731}
732
Johannes Berg55682962007-09-20 13:09:35 -0400733/* netlink command implementations */
734
Johannes Bergb9454e82009-07-08 13:29:08 +0200735struct key_parse {
736 struct key_params p;
737 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200738 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200739 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100740 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200741};
742
743static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
744{
745 struct nlattr *tb[NL80211_KEY_MAX + 1];
746 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
747 nl80211_key_policy);
748 if (err)
749 return err;
750
751 k->def = !!tb[NL80211_KEY_DEFAULT];
752 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
753
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100754 if (k->def) {
755 k->def_uni = true;
756 k->def_multi = true;
757 }
758 if (k->defmgmt)
759 k->def_multi = true;
760
Johannes Bergb9454e82009-07-08 13:29:08 +0200761 if (tb[NL80211_KEY_IDX])
762 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
763
764 if (tb[NL80211_KEY_DATA]) {
765 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
766 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
767 }
768
769 if (tb[NL80211_KEY_SEQ]) {
770 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
771 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
772 }
773
774 if (tb[NL80211_KEY_CIPHER])
775 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
776
Johannes Berge31b8212010-10-05 19:39:30 +0200777 if (tb[NL80211_KEY_TYPE]) {
778 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
779 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
780 return -EINVAL;
781 }
782
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100783 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
784 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -0700785
Johannes Berg2da8f412012-01-20 13:52:37 +0100786 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
787 tb[NL80211_KEY_DEFAULT_TYPES],
788 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100789 if (err)
790 return err;
791
792 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
793 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
794 }
795
Johannes Bergb9454e82009-07-08 13:29:08 +0200796 return 0;
797}
798
799static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
800{
801 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
802 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
803 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
804 }
805
806 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
807 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
808 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
809 }
810
811 if (info->attrs[NL80211_ATTR_KEY_IDX])
812 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
813
814 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
815 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
816
817 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
818 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
819
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100820 if (k->def) {
821 k->def_uni = true;
822 k->def_multi = true;
823 }
824 if (k->defmgmt)
825 k->def_multi = true;
826
Johannes Berge31b8212010-10-05 19:39:30 +0200827 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
828 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
829 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
830 return -EINVAL;
831 }
832
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100833 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
834 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
835 int err = nla_parse_nested(
836 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
837 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
838 nl80211_key_default_policy);
839 if (err)
840 return err;
841
842 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
843 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
844 }
845
Johannes Bergb9454e82009-07-08 13:29:08 +0200846 return 0;
847}
848
849static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
850{
851 int err;
852
853 memset(k, 0, sizeof(*k));
854 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200855 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200856
857 if (info->attrs[NL80211_ATTR_KEY])
858 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
859 else
860 err = nl80211_parse_key_old(info, k);
861
862 if (err)
863 return err;
864
865 if (k->def && k->defmgmt)
866 return -EINVAL;
867
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100868 if (k->defmgmt) {
869 if (k->def_uni || !k->def_multi)
870 return -EINVAL;
871 }
872
Johannes Bergb9454e82009-07-08 13:29:08 +0200873 if (k->idx != -1) {
874 if (k->defmgmt) {
875 if (k->idx < 4 || k->idx > 5)
876 return -EINVAL;
877 } else if (k->def) {
878 if (k->idx < 0 || k->idx > 3)
879 return -EINVAL;
880 } else {
881 if (k->idx < 0 || k->idx > 5)
882 return -EINVAL;
883 }
884 }
885
886 return 0;
887}
888
Johannes Bergfffd0932009-07-08 14:22:54 +0200889static struct cfg80211_cached_keys *
890nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530891 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200892{
893 struct key_parse parse;
894 struct nlattr *key;
895 struct cfg80211_cached_keys *result;
896 int rem, err, def = 0;
Johannes Bergf1c1f172016-09-13 17:08:23 +0200897 bool have_key = false;
898
899 nla_for_each_nested(key, keys, rem) {
900 have_key = true;
901 break;
902 }
903
904 if (!have_key)
905 return NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200906
907 result = kzalloc(sizeof(*result), GFP_KERNEL);
908 if (!result)
909 return ERR_PTR(-ENOMEM);
910
911 result->def = -1;
Johannes Bergfffd0932009-07-08 14:22:54 +0200912
913 nla_for_each_nested(key, keys, rem) {
914 memset(&parse, 0, sizeof(parse));
915 parse.idx = -1;
916
917 err = nl80211_parse_key_new(key, &parse);
918 if (err)
919 goto error;
920 err = -EINVAL;
921 if (!parse.p.key)
922 goto error;
Johannes Berg42ee2312016-09-13 15:51:03 +0200923 if (parse.idx < 0 || parse.idx > 3)
Johannes Bergfffd0932009-07-08 14:22:54 +0200924 goto error;
925 if (parse.def) {
926 if (def)
927 goto error;
928 def = 1;
929 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100930 if (!parse.def_uni || !parse.def_multi)
931 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200932 } else if (parse.defmgmt)
933 goto error;
934 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200935 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200936 if (err)
937 goto error;
Johannes Berg386b1f22016-09-13 16:10:02 +0200938 if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
939 parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
940 err = -EINVAL;
941 goto error;
942 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200943 result->params[parse.idx].cipher = parse.p.cipher;
944 result->params[parse.idx].key_len = parse.p.key_len;
945 result->params[parse.idx].key = result->data[parse.idx];
946 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530947
Johannes Berg386b1f22016-09-13 16:10:02 +0200948 /* must be WEP key if we got here */
949 if (no_ht)
950 *no_ht = true;
Johannes Bergfffd0932009-07-08 14:22:54 +0200951 }
952
Johannes Bergf1c1f172016-09-13 17:08:23 +0200953 if (result->def < 0) {
954 err = -EINVAL;
955 goto error;
956 }
957
Johannes Bergfffd0932009-07-08 14:22:54 +0200958 return result;
959 error:
960 kfree(result);
961 return ERR_PTR(err);
962}
963
964static int nl80211_key_allowed(struct wireless_dev *wdev)
965{
966 ASSERT_WDEV_LOCK(wdev);
967
Johannes Bergfffd0932009-07-08 14:22:54 +0200968 switch (wdev->iftype) {
969 case NL80211_IFTYPE_AP:
970 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200971 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700972 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200973 break;
974 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200975 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200976 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200977 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200978 return -ENOLINK;
979 break;
Johannes Bergde4fcba2014-10-31 14:16:12 +0100980 case NL80211_IFTYPE_UNSPECIFIED:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100981 case NL80211_IFTYPE_OCB:
Johannes Bergde4fcba2014-10-31 14:16:12 +0100982 case NL80211_IFTYPE_MONITOR:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300983 case NL80211_IFTYPE_NAN:
Johannes Bergde4fcba2014-10-31 14:16:12 +0100984 case NL80211_IFTYPE_P2P_DEVICE:
985 case NL80211_IFTYPE_WDS:
986 case NUM_NL80211_IFTYPES:
Johannes Bergfffd0932009-07-08 14:22:54 +0200987 return -EINVAL;
988 }
989
990 return 0;
991}
992
Jouni Malinen664834d2014-01-15 00:01:44 +0200993static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
994 struct nlattr *tb)
995{
996 struct ieee80211_channel *chan;
997
998 if (tb == NULL)
999 return NULL;
1000 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1001 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1002 return NULL;
1003 return chan;
1004}
1005
Johannes Berg7527a782011-05-13 10:58:57 +02001006static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1007{
1008 struct nlattr *nl_modes = nla_nest_start(msg, attr);
1009 int i;
1010
1011 if (!nl_modes)
1012 goto nla_put_failure;
1013
1014 i = 0;
1015 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001016 if ((ifmodes & 1) && nla_put_flag(msg, i))
1017 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +02001018 ifmodes >>= 1;
1019 i++;
1020 }
1021
1022 nla_nest_end(msg, nl_modes);
1023 return 0;
1024
1025nla_put_failure:
1026 return -ENOBUFS;
1027}
1028
1029static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +01001030 struct sk_buff *msg,
1031 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +02001032{
1033 struct nlattr *nl_combis;
1034 int i, j;
1035
1036 nl_combis = nla_nest_start(msg,
1037 NL80211_ATTR_INTERFACE_COMBINATIONS);
1038 if (!nl_combis)
1039 goto nla_put_failure;
1040
1041 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1042 const struct ieee80211_iface_combination *c;
1043 struct nlattr *nl_combi, *nl_limits;
1044
1045 c = &wiphy->iface_combinations[i];
1046
1047 nl_combi = nla_nest_start(msg, i + 1);
1048 if (!nl_combi)
1049 goto nla_put_failure;
1050
1051 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
1052 if (!nl_limits)
1053 goto nla_put_failure;
1054
1055 for (j = 0; j < c->n_limits; j++) {
1056 struct nlattr *nl_limit;
1057
1058 nl_limit = nla_nest_start(msg, j + 1);
1059 if (!nl_limit)
1060 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04001061 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1062 c->limits[j].max))
1063 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +02001064 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1065 c->limits[j].types))
1066 goto nla_put_failure;
1067 nla_nest_end(msg, nl_limit);
1068 }
1069
1070 nla_nest_end(msg, nl_limits);
1071
David S. Miller9360ffd2012-03-29 04:41:26 -04001072 if (c->beacon_int_infra_match &&
1073 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1074 goto nla_put_failure;
1075 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1076 c->num_different_channels) ||
1077 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1078 c->max_interfaces))
1079 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01001080 if (large &&
Felix Fietkau8c48b502014-05-05 11:48:40 +02001081 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1082 c->radar_detect_widths) ||
1083 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1084 c->radar_detect_regions)))
Johannes Bergcdc89b92013-02-18 23:54:36 +01001085 goto nla_put_failure;
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05301086 if (c->beacon_int_min_gcd &&
1087 nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1088 c->beacon_int_min_gcd))
1089 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +02001090
1091 nla_nest_end(msg, nl_combi);
1092 }
1093
1094 nla_nest_end(msg, nl_combis);
1095
1096 return 0;
1097nla_put_failure:
1098 return -ENOBUFS;
1099}
1100
Johannes Berg3713b4e2013-02-14 16:19:38 +01001101#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001102static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1103 struct sk_buff *msg)
1104{
Johannes Berg964dc9e2013-06-03 17:25:34 +02001105 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +01001106 struct nlattr *nl_tcp;
1107
1108 if (!tcp)
1109 return 0;
1110
1111 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1112 if (!nl_tcp)
1113 return -ENOBUFS;
1114
1115 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1116 tcp->data_payload_max))
1117 return -ENOBUFS;
1118
1119 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1120 tcp->data_payload_max))
1121 return -ENOBUFS;
1122
1123 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1124 return -ENOBUFS;
1125
1126 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1127 sizeof(*tcp->tok), tcp->tok))
1128 return -ENOBUFS;
1129
1130 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1131 tcp->data_interval_max))
1132 return -ENOBUFS;
1133
1134 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1135 tcp->wake_payload_max))
1136 return -ENOBUFS;
1137
1138 nla_nest_end(msg, nl_tcp);
1139 return 0;
1140}
1141
Johannes Berg3713b4e2013-02-14 16:19:38 +01001142static int nl80211_send_wowlan(struct sk_buff *msg,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001143 struct cfg80211_registered_device *rdev,
Johannes Bergb56cf722013-02-20 01:02:38 +01001144 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001145{
1146 struct nlattr *nl_wowlan;
1147
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001148 if (!rdev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001149 return 0;
1150
1151 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1152 if (!nl_wowlan)
1153 return -ENOBUFS;
1154
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001155 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001156 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001157 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001158 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001159 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001160 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001161 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001162 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001163 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001164 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001165 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001166 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001167 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001168 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001169 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001170 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1171 return -ENOBUFS;
1172
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001173 if (rdev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001174 struct nl80211_pattern_support pat = {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001175 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1176 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1177 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1178 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001179 };
1180
1181 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1182 sizeof(pat), &pat))
1183 return -ENOBUFS;
1184 }
1185
Luciano Coelho75453cc2015-01-09 14:06:37 +02001186 if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1187 nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1188 rdev->wiphy.wowlan->max_nd_match_sets))
1189 return -ENOBUFS;
1190
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001191 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
Johannes Bergb56cf722013-02-20 01:02:38 +01001192 return -ENOBUFS;
1193
Johannes Berg3713b4e2013-02-14 16:19:38 +01001194 nla_nest_end(msg, nl_wowlan);
1195
1196 return 0;
1197}
1198#endif
1199
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001200static int nl80211_send_coalesce(struct sk_buff *msg,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001201 struct cfg80211_registered_device *rdev)
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001202{
1203 struct nl80211_coalesce_rule_support rule;
1204
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001205 if (!rdev->wiphy.coalesce)
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001206 return 0;
1207
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001208 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1209 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1210 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1211 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1212 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1213 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001214
1215 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1216 return -ENOBUFS;
1217
1218 return 0;
1219}
1220
Johannes Berg3713b4e2013-02-14 16:19:38 +01001221static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1222 struct ieee80211_supported_band *sband)
1223{
1224 struct nlattr *nl_rates, *nl_rate;
1225 struct ieee80211_rate *rate;
1226 int i;
1227
1228 /* add HT info */
1229 if (sband->ht_cap.ht_supported &&
1230 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1231 sizeof(sband->ht_cap.mcs),
1232 &sband->ht_cap.mcs) ||
1233 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1234 sband->ht_cap.cap) ||
1235 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1236 sband->ht_cap.ampdu_factor) ||
1237 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1238 sband->ht_cap.ampdu_density)))
1239 return -ENOBUFS;
1240
1241 /* add VHT info */
1242 if (sband->vht_cap.vht_supported &&
1243 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1244 sizeof(sband->vht_cap.vht_mcs),
1245 &sband->vht_cap.vht_mcs) ||
1246 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1247 sband->vht_cap.cap)))
1248 return -ENOBUFS;
1249
1250 /* add bitrates */
1251 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1252 if (!nl_rates)
1253 return -ENOBUFS;
1254
1255 for (i = 0; i < sband->n_bitrates; i++) {
1256 nl_rate = nla_nest_start(msg, i);
1257 if (!nl_rate)
1258 return -ENOBUFS;
1259
1260 rate = &sband->bitrates[i];
1261 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1262 rate->bitrate))
1263 return -ENOBUFS;
1264 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1265 nla_put_flag(msg,
1266 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1267 return -ENOBUFS;
1268
1269 nla_nest_end(msg, nl_rate);
1270 }
1271
1272 nla_nest_end(msg, nl_rates);
1273
1274 return 0;
1275}
1276
1277static int
1278nl80211_send_mgmt_stypes(struct sk_buff *msg,
1279 const struct ieee80211_txrx_stypes *mgmt_stypes)
1280{
1281 u16 stypes;
1282 struct nlattr *nl_ftypes, *nl_ifs;
1283 enum nl80211_iftype ift;
1284 int i;
1285
1286 if (!mgmt_stypes)
1287 return 0;
1288
1289 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1290 if (!nl_ifs)
1291 return -ENOBUFS;
1292
1293 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1294 nl_ftypes = nla_nest_start(msg, ift);
1295 if (!nl_ftypes)
1296 return -ENOBUFS;
1297 i = 0;
1298 stypes = mgmt_stypes[ift].tx;
1299 while (stypes) {
1300 if ((stypes & 1) &&
1301 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1302 (i << 4) | IEEE80211_FTYPE_MGMT))
1303 return -ENOBUFS;
1304 stypes >>= 1;
1305 i++;
1306 }
1307 nla_nest_end(msg, nl_ftypes);
1308 }
1309
1310 nla_nest_end(msg, nl_ifs);
1311
1312 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1313 if (!nl_ifs)
1314 return -ENOBUFS;
1315
1316 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1317 nl_ftypes = nla_nest_start(msg, ift);
1318 if (!nl_ftypes)
1319 return -ENOBUFS;
1320 i = 0;
1321 stypes = mgmt_stypes[ift].rx;
1322 while (stypes) {
1323 if ((stypes & 1) &&
1324 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1325 (i << 4) | IEEE80211_FTYPE_MGMT))
1326 return -ENOBUFS;
1327 stypes >>= 1;
1328 i++;
1329 }
1330 nla_nest_end(msg, nl_ftypes);
1331 }
1332 nla_nest_end(msg, nl_ifs);
1333
1334 return 0;
1335}
1336
Johannes Berg17948992016-10-26 11:42:04 +02001337#define CMD(op, n) \
1338 do { \
1339 if (rdev->ops->op) { \
1340 i++; \
1341 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1342 goto nla_put_failure; \
1343 } \
1344 } while (0)
1345
1346static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1347 struct sk_buff *msg)
1348{
1349 int i = 0;
1350
1351 /*
1352 * do *NOT* add anything into this function, new things need to be
1353 * advertised only to new versions of userspace that can deal with
1354 * the split (and they can't possibly care about new features...
1355 */
1356 CMD(add_virtual_intf, NEW_INTERFACE);
1357 CMD(change_virtual_intf, SET_INTERFACE);
1358 CMD(add_key, NEW_KEY);
1359 CMD(start_ap, START_AP);
1360 CMD(add_station, NEW_STATION);
1361 CMD(add_mpath, NEW_MPATH);
1362 CMD(update_mesh_config, SET_MESH_CONFIG);
1363 CMD(change_bss, SET_BSS);
1364 CMD(auth, AUTHENTICATE);
1365 CMD(assoc, ASSOCIATE);
1366 CMD(deauth, DEAUTHENTICATE);
1367 CMD(disassoc, DISASSOCIATE);
1368 CMD(join_ibss, JOIN_IBSS);
1369 CMD(join_mesh, JOIN_MESH);
1370 CMD(set_pmksa, SET_PMKSA);
1371 CMD(del_pmksa, DEL_PMKSA);
1372 CMD(flush_pmksa, FLUSH_PMKSA);
1373 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1374 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1375 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1376 CMD(mgmt_tx, FRAME);
1377 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1378 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1379 i++;
1380 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1381 goto nla_put_failure;
1382 }
1383 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1384 rdev->ops->join_mesh) {
1385 i++;
1386 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1387 goto nla_put_failure;
1388 }
1389 CMD(set_wds_peer, SET_WDS_PEER);
1390 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1391 CMD(tdls_mgmt, TDLS_MGMT);
1392 CMD(tdls_oper, TDLS_OPER);
1393 }
1394 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1395 CMD(sched_scan_start, START_SCHED_SCAN);
1396 CMD(probe_client, PROBE_CLIENT);
1397 CMD(set_noack_map, SET_NOACK_MAP);
1398 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1399 i++;
1400 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1401 goto nla_put_failure;
1402 }
1403 CMD(start_p2p_device, START_P2P_DEVICE);
1404 CMD(set_mcast_rate, SET_MCAST_RATE);
1405#ifdef CONFIG_NL80211_TESTMODE
1406 CMD(testmode_cmd, TESTMODE);
1407#endif
1408
1409 if (rdev->ops->connect || rdev->ops->auth) {
1410 i++;
1411 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1412 goto nla_put_failure;
1413 }
1414
1415 if (rdev->ops->disconnect || rdev->ops->deauth) {
1416 i++;
1417 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1418 goto nla_put_failure;
1419 }
1420
1421 return i;
1422 nla_put_failure:
1423 return -ENOBUFS;
1424}
1425
Johannes Berg86e8cf92013-06-19 10:57:22 +02001426struct nl80211_dump_wiphy_state {
1427 s64 filter_wiphy;
1428 long start;
Kanchanapally, Vidyullatha019ae3a2016-05-16 10:41:04 +05301429 long split_start, band_start, chan_start, capa_start;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001430 bool split;
1431};
1432
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001433static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
Johannes Berg3bb20552014-05-26 13:52:25 +02001434 enum nl80211_commands cmd,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001435 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001436 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001437{
1438 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001439 struct nlattr *nl_bands, *nl_band;
1440 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001441 struct nlattr *nl_cmds;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001442 enum nl80211_band band;
Johannes Bergee688b002008-01-24 19:38:39 +01001443 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001444 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001445 const struct ieee80211_txrx_stypes *mgmt_stypes =
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001446 rdev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001447 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001448
Johannes Berg3bb20552014-05-26 13:52:25 +02001449 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -04001450 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001451 return -ENOBUFS;
1452
Johannes Berg86e8cf92013-06-19 10:57:22 +02001453 if (WARN_ON(!state))
1454 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001455
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001456 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001457 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001458 wiphy_name(&rdev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001459 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001460 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001461 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001462
Johannes Berg3bb20552014-05-26 13:52:25 +02001463 if (cmd != NL80211_CMD_NEW_WIPHY)
1464 goto finish;
1465
Johannes Berg86e8cf92013-06-19 10:57:22 +02001466 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001467 case 0:
1468 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001469 rdev->wiphy.retry_short) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001470 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001471 rdev->wiphy.retry_long) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001472 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001473 rdev->wiphy.frag_threshold) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001474 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001475 rdev->wiphy.rts_threshold) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001476 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001477 rdev->wiphy.coverage_class) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001478 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001479 rdev->wiphy.max_scan_ssids) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001480 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001481 rdev->wiphy.max_sched_scan_ssids) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001482 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001483 rdev->wiphy.max_scan_ie_len) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001484 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001485 rdev->wiphy.max_sched_scan_ie_len) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001486 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
Avraham Stern3b06d272015-10-12 09:51:34 +03001487 rdev->wiphy.max_match_sets) ||
1488 nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1489 rdev->wiphy.max_sched_scan_plans) ||
1490 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1491 rdev->wiphy.max_sched_scan_plan_interval) ||
1492 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1493 rdev->wiphy.max_sched_scan_plan_iterations))
Johannes Bergee688b002008-01-24 19:38:39 +01001494 goto nla_put_failure;
1495
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001496 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001497 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1498 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001499 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001500 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1501 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001502 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001503 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1504 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001505 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001506 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1507 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001508 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001509 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1510 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001511 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001512 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001513 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001514 state->split_start++;
1515 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001516 break;
1517 case 1:
1518 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001519 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1520 rdev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001521 goto nla_put_failure;
1522
Johannes Berg3713b4e2013-02-14 16:19:38 +01001523 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001524 rdev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001525 goto nla_put_failure;
1526
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001527 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001528 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1529 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001530
Johannes Berg3713b4e2013-02-14 16:19:38 +01001531 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001532 rdev->wiphy.available_antennas_tx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001534 rdev->wiphy.available_antennas_rx))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001535 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001536
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001537 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001538 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001539 rdev->wiphy.probe_resp_offload))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001540 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001541
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001542 if ((rdev->wiphy.available_antennas_tx ||
1543 rdev->wiphy.available_antennas_rx) &&
1544 rdev->ops->get_antenna) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001545 u32 tx_ant = 0, rx_ant = 0;
1546 int res;
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07001547
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001548 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001549 if (!res) {
1550 if (nla_put_u32(msg,
1551 NL80211_ATTR_WIPHY_ANTENNA_TX,
1552 tx_ant) ||
1553 nla_put_u32(msg,
1554 NL80211_ATTR_WIPHY_ANTENNA_RX,
1555 rx_ant))
1556 goto nla_put_failure;
1557 }
Johannes Bergee688b002008-01-24 19:38:39 +01001558 }
1559
Johannes Berg86e8cf92013-06-19 10:57:22 +02001560 state->split_start++;
1561 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001562 break;
1563 case 2:
1564 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001565 rdev->wiphy.interface_modes))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001566 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001567 state->split_start++;
1568 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001569 break;
1570 case 3:
1571 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1572 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001573 goto nla_put_failure;
1574
Johannes Berg86e8cf92013-06-19 10:57:22 +02001575 for (band = state->band_start;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001576 band < NUM_NL80211_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001577 struct ieee80211_supported_band *sband;
1578
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001579 sband = rdev->wiphy.bands[band];
Johannes Berg3713b4e2013-02-14 16:19:38 +01001580
1581 if (!sband)
1582 continue;
1583
1584 nl_band = nla_nest_start(msg, band);
1585 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001586 goto nla_put_failure;
1587
Johannes Berg86e8cf92013-06-19 10:57:22 +02001588 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001589 case 0:
1590 if (nl80211_send_band_rateinfo(msg, sband))
1591 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001592 state->chan_start++;
1593 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001594 break;
1595 default:
1596 /* add frequencies */
1597 nl_freqs = nla_nest_start(
1598 msg, NL80211_BAND_ATTR_FREQS);
1599 if (!nl_freqs)
1600 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001601
Johannes Berg86e8cf92013-06-19 10:57:22 +02001602 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001603 i < sband->n_channels;
1604 i++) {
1605 nl_freq = nla_nest_start(msg, i);
1606 if (!nl_freq)
1607 goto nla_put_failure;
1608
1609 chan = &sband->channels[i];
1610
Johannes Berg86e8cf92013-06-19 10:57:22 +02001611 if (nl80211_msg_put_channel(
1612 msg, chan,
1613 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001614 goto nla_put_failure;
1615
1616 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001617 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001618 break;
1619 }
1620 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001621 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001622 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001623 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001624 nla_nest_end(msg, nl_freqs);
1625 }
1626
1627 nla_nest_end(msg, nl_band);
1628
Johannes Berg86e8cf92013-06-19 10:57:22 +02001629 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001630 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001631 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001632 band--;
1633 break;
1634 }
Johannes Bergee688b002008-01-24 19:38:39 +01001635 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001636 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001637
Johannes Berg57fbcce2016-04-12 15:56:15 +02001638 if (band < NUM_NL80211_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001639 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001640 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001641 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001642
Johannes Berg3713b4e2013-02-14 16:19:38 +01001643 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001644 if (state->band_start == 0 && state->chan_start == 0)
1645 state->split_start++;
1646 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001647 break;
1648 case 4:
1649 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1650 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001651 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001652
Johannes Berg17948992016-10-26 11:42:04 +02001653 i = nl80211_add_commands_unsplit(rdev, msg);
1654 if (i < 0)
1655 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001656 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001657 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1658 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001659 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001660 CMD(channel_switch, CHANNEL_SWITCH);
Johannes Berg02df00e2014-06-10 14:06:25 +02001661 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg723e73a2014-10-22 09:25:06 +02001662 if (rdev->wiphy.features &
1663 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
Johannes Berg960d01a2014-09-09 22:55:35 +03001664 CMD(add_tx_ts, ADD_TX_TS);
Michael Braunce0ce132016-10-10 19:12:22 +02001665 CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
vamsi krishna088e8df2016-10-27 16:51:11 +03001666 CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
Arend van Spriel5de17982013-04-18 15:49:00 +02001667 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001668#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001669
Johannes Berg3713b4e2013-02-14 16:19:38 +01001670 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001671 state->split_start++;
1672 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001673 break;
1674 case 5:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001675 if (rdev->ops->remain_on_channel &&
1676 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001677 nla_put_u32(msg,
1678 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001679 rdev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001680 goto nla_put_failure;
1681
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001682 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001683 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1684 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001685
Johannes Berg3713b4e2013-02-14 16:19:38 +01001686 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1687 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001688 state->split_start++;
1689 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001690 break;
1691 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001692#ifdef CONFIG_PM
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001693 if (nl80211_send_wowlan(msg, rdev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001694 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001695 state->split_start++;
1696 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001697 break;
1698#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001700#endif
1701 case 7:
1702 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001703 rdev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001704 goto nla_put_failure;
1705
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001706 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001707 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001708 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001709
Johannes Berg86e8cf92013-06-19 10:57:22 +02001710 state->split_start++;
1711 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001712 break;
1713 case 8:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001714 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001716 rdev->wiphy.ap_sme_capa))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001717 goto nla_put_failure;
1718
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001719 features = rdev->wiphy.features;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001720 /*
1721 * We can only add the per-channel limit information if the
1722 * dump is split, otherwise it makes it too big. Therefore
1723 * only advertise it in that case.
1724 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001725 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001726 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1727 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001728 goto nla_put_failure;
1729
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001730 if (rdev->wiphy.ht_capa_mod_mask &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001731 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001732 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1733 rdev->wiphy.ht_capa_mod_mask))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001734 goto nla_put_failure;
1735
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001736 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1737 rdev->wiphy.max_acl_mac_addrs &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001738 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001739 rdev->wiphy.max_acl_mac_addrs))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001740 goto nla_put_failure;
1741
1742 /*
1743 * Any information below this point is only available to
1744 * applications that can deal with it being split. This
1745 * helps ensure that newly added capabilities don't break
1746 * older tools by overrunning their buffers.
1747 *
1748 * We still increment split_start so that in the split
1749 * case we'll continue with more data in the next round,
1750 * but break unconditionally so unsplit data stops here.
1751 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001752 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001753 break;
1754 case 9:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001755 if (rdev->wiphy.extended_capabilities &&
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001756 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001757 rdev->wiphy.extended_capabilities_len,
1758 rdev->wiphy.extended_capabilities) ||
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001759 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001760 rdev->wiphy.extended_capabilities_len,
1761 rdev->wiphy.extended_capabilities_mask)))
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001762 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001763
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001764 if (rdev->wiphy.vht_capa_mod_mask &&
Johannes Bergee2aca32013-02-21 17:36:01 +01001765 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001766 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1767 rdev->wiphy.vht_capa_mod_mask))
Johannes Bergee2aca32013-02-21 17:36:01 +01001768 goto nla_put_failure;
1769
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001770 state->split_start++;
1771 break;
1772 case 10:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001773 if (nl80211_send_coalesce(msg, rdev))
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -07001774 goto nla_put_failure;
1775
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001776 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001777 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1778 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1779 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001780
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001781 if (rdev->wiphy.max_ap_assoc_sta &&
Jouni Malinenb43504c2014-01-15 00:01:08 +02001782 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001783 rdev->wiphy.max_ap_assoc_sta))
Jouni Malinenb43504c2014-01-15 00:01:08 +02001784 goto nla_put_failure;
1785
Johannes Bergad7e7182013-11-13 13:37:47 +01001786 state->split_start++;
1787 break;
1788 case 11:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001789 if (rdev->wiphy.n_vendor_commands) {
Johannes Berg567ffc32013-12-18 14:43:31 +01001790 const struct nl80211_vendor_cmd_info *info;
1791 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001792
Johannes Berg567ffc32013-12-18 14:43:31 +01001793 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1794 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001795 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001796
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001797 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1798 info = &rdev->wiphy.vendor_commands[i].info;
Johannes Berg567ffc32013-12-18 14:43:31 +01001799 if (nla_put(msg, i + 1, sizeof(*info), info))
1800 goto nla_put_failure;
1801 }
1802 nla_nest_end(msg, nested);
1803 }
1804
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001805 if (rdev->wiphy.n_vendor_events) {
Johannes Berg567ffc32013-12-18 14:43:31 +01001806 const struct nl80211_vendor_cmd_info *info;
1807 struct nlattr *nested;
1808
1809 nested = nla_nest_start(msg,
1810 NL80211_ATTR_VENDOR_EVENTS);
1811 if (!nested)
1812 goto nla_put_failure;
1813
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001814 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1815 info = &rdev->wiphy.vendor_events[i];
Johannes Berg567ffc32013-12-18 14:43:31 +01001816 if (nla_put(msg, i + 1, sizeof(*info), info))
1817 goto nla_put_failure;
1818 }
1819 nla_nest_end(msg, nested);
1820 }
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03001821 state->split_start++;
1822 break;
1823 case 12:
1824 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1825 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1826 rdev->wiphy.max_num_csa_counters))
1827 goto nla_put_failure;
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001828
Arik Nemtsov1bdd7162014-12-15 19:26:01 +02001829 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
1830 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
1831 goto nla_put_failure;
1832
Gautam Kumar Shuklad75bb062014-12-23 16:55:19 +01001833 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
1834 sizeof(rdev->wiphy.ext_features),
1835 rdev->wiphy.ext_features))
1836 goto nla_put_failure;
1837
Arend van Spriel38de03d2016-03-02 20:37:18 +01001838 if (rdev->wiphy.bss_select_support) {
1839 struct nlattr *nested;
1840 u32 bss_select_support = rdev->wiphy.bss_select_support;
1841
1842 nested = nla_nest_start(msg, NL80211_ATTR_BSS_SELECT);
1843 if (!nested)
1844 goto nla_put_failure;
1845
1846 i = 0;
1847 while (bss_select_support) {
1848 if ((bss_select_support & 1) &&
1849 nla_put_flag(msg, i))
1850 goto nla_put_failure;
1851 i++;
1852 bss_select_support >>= 1;
1853 }
1854 nla_nest_end(msg, nested);
1855 }
1856
Kanchanapally, Vidyullatha019ae3a2016-05-16 10:41:04 +05301857 state->split_start++;
1858 break;
1859 case 13:
1860 if (rdev->wiphy.num_iftype_ext_capab &&
1861 rdev->wiphy.iftype_ext_capab) {
1862 struct nlattr *nested_ext_capab, *nested;
1863
1864 nested = nla_nest_start(msg,
1865 NL80211_ATTR_IFTYPE_EXT_CAPA);
1866 if (!nested)
1867 goto nla_put_failure;
1868
1869 for (i = state->capa_start;
1870 i < rdev->wiphy.num_iftype_ext_capab; i++) {
1871 const struct wiphy_iftype_ext_capab *capab;
1872
1873 capab = &rdev->wiphy.iftype_ext_capab[i];
1874
1875 nested_ext_capab = nla_nest_start(msg, i);
1876 if (!nested_ext_capab ||
1877 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1878 capab->iftype) ||
1879 nla_put(msg, NL80211_ATTR_EXT_CAPA,
1880 capab->extended_capabilities_len,
1881 capab->extended_capabilities) ||
1882 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1883 capab->extended_capabilities_len,
1884 capab->extended_capabilities_mask))
1885 goto nla_put_failure;
1886
1887 nla_nest_end(msg, nested_ext_capab);
1888 if (state->split)
1889 break;
1890 }
1891 nla_nest_end(msg, nested);
1892 if (i < rdev->wiphy.num_iftype_ext_capab) {
1893 state->capa_start = i + 1;
1894 break;
1895 }
1896 }
1897
Luca Coelho85859892017-02-08 15:00:34 +02001898 if (nla_put_u32(msg, NL80211_ATTR_BANDS,
1899 rdev->wiphy.nan_supported_bands))
1900 goto nla_put_failure;
1901
Johannes Berg3713b4e2013-02-14 16:19:38 +01001902 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001903 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001904 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001905 }
Johannes Berg3bb20552014-05-26 13:52:25 +02001906 finish:
Johannes Berg053c0952015-01-16 22:09:00 +01001907 genlmsg_end(msg, hdr);
1908 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001909
1910 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001911 genlmsg_cancel(msg, hdr);
1912 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001913}
1914
Johannes Berg86e8cf92013-06-19 10:57:22 +02001915static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1916 struct netlink_callback *cb,
1917 struct nl80211_dump_wiphy_state *state)
1918{
Johannes Bergc90c39d2016-10-24 14:40:01 +02001919 struct nlattr **tb = genl_family_attrbuf(&nl80211_fam);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001920 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1921 tb, nl80211_fam.maxattr, nl80211_policy);
1922 /* ignore parse errors for backward compatibility */
1923 if (ret)
1924 return 0;
1925
1926 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1927 if (tb[NL80211_ATTR_WIPHY])
1928 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1929 if (tb[NL80211_ATTR_WDEV])
1930 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1931 if (tb[NL80211_ATTR_IFINDEX]) {
1932 struct net_device *netdev;
1933 struct cfg80211_registered_device *rdev;
1934 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1935
Ying Xue7f2b8562014-01-15 10:23:45 +08001936 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001937 if (!netdev)
1938 return -ENODEV;
1939 if (netdev->ieee80211_ptr) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001940 rdev = wiphy_to_rdev(
Johannes Berg86e8cf92013-06-19 10:57:22 +02001941 netdev->ieee80211_ptr->wiphy);
1942 state->filter_wiphy = rdev->wiphy_idx;
1943 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001944 }
1945
1946 return 0;
1947}
1948
Johannes Berg55682962007-09-20 13:09:35 -04001949static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1950{
Johannes Berg645e77d2013-03-01 14:03:49 +01001951 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001952 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001953 struct cfg80211_registered_device *rdev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001954
Johannes Berg5fe231e2013-05-08 21:45:15 +02001955 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001956 if (!state) {
1957 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001958 if (!state) {
1959 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001960 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001961 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001962 state->filter_wiphy = -1;
1963 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1964 if (ret) {
1965 kfree(state);
1966 rtnl_unlock();
1967 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001968 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001969 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001970 }
1971
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001972 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1973 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001974 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001975 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001976 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001977 if (state->filter_wiphy != -1 &&
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001978 state->filter_wiphy != rdev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001979 continue;
1980 /* attempt to fit multiple wiphy data chunks into the skb */
1981 do {
Johannes Berg3bb20552014-05-26 13:52:25 +02001982 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
1983 skb,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001984 NETLINK_CB(cb->skb).portid,
1985 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001986 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001987 if (ret < 0) {
1988 /*
1989 * If sending the wiphy data didn't fit (ENOBUFS
1990 * or EMSGSIZE returned), this SKB is still
1991 * empty (so it's not too big because another
1992 * wiphy dataset is already in the skb) and
1993 * we've not tried to adjust the dump allocation
1994 * yet ... then adjust the alloc size to be
1995 * bigger, and return 1 but with the empty skb.
1996 * This results in an empty message being RX'ed
1997 * in userspace, but that is ignored.
1998 *
1999 * We can then retry with the larger buffer.
2000 */
2001 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01002002 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01002003 cb->min_dump_alloc < 4096) {
2004 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01002005 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07002006 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01002007 return 1;
2008 }
2009 idx--;
2010 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01002011 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02002012 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01002013 break;
Johannes Berg55682962007-09-20 13:09:35 -04002014 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02002015 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002016
Johannes Berg86e8cf92013-06-19 10:57:22 +02002017 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04002018
2019 return skb->len;
2020}
2021
Johannes Berg86e8cf92013-06-19 10:57:22 +02002022static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2023{
2024 kfree((void *)cb->args[0]);
2025 return 0;
2026}
2027
Johannes Berg55682962007-09-20 13:09:35 -04002028static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2029{
2030 struct sk_buff *msg;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02002032 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04002033
Johannes Berg645e77d2013-03-01 14:03:49 +01002034 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002035 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002036 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002037
Johannes Berg3bb20552014-05-26 13:52:25 +02002038 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2039 info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02002040 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002041 nlmsg_free(msg);
2042 return -ENOBUFS;
2043 }
Johannes Berg55682962007-09-20 13:09:35 -04002044
Johannes Berg134e6372009-07-10 09:51:34 +00002045 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002046}
2047
Jouni Malinen31888482008-10-30 16:59:24 +02002048static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2049 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
2050 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
2051 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
2052 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
2053 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
2054};
2055
2056static int parse_txq_params(struct nlattr *tb[],
2057 struct ieee80211_txq_params *txq_params)
2058{
Johannes Berga3304b02012-03-28 11:04:24 +02002059 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02002060 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2061 !tb[NL80211_TXQ_ATTR_AIFS])
2062 return -EINVAL;
2063
Johannes Berga3304b02012-03-28 11:04:24 +02002064 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02002065 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2066 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2067 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2068 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2069
Johannes Berga3304b02012-03-28 11:04:24 +02002070 if (txq_params->ac >= NL80211_NUM_ACS)
2071 return -EINVAL;
2072
Jouni Malinen31888482008-10-30 16:59:24 +02002073 return 0;
2074}
2075
Johannes Bergf444de02010-05-05 15:25:02 +02002076static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2077{
2078 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02002079 * You can only set the channel explicitly for WDS interfaces,
2080 * all others have their channel managed via their respective
2081 * "establish a connection" command (connect, join, ...)
2082 *
2083 * For AP/GO and mesh mode, the channel can be set with the
2084 * channel userspace API, but is only stored and passed to the
2085 * low-level driver when the AP starts or the mesh is joined.
2086 * This is for backward compatibility, userspace can also give
2087 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02002088 *
2089 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02002090 * whatever else is going on, so they have their own special
2091 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02002092 */
2093 return !wdev ||
2094 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02002095 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02002096 wdev->iftype == NL80211_IFTYPE_MONITOR ||
2097 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02002098}
2099
Johannes Berg683b6d32012-11-08 21:25:48 +01002100static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2101 struct genl_info *info,
2102 struct cfg80211_chan_def *chandef)
2103{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05302104 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01002105
2106 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
2107 return -EINVAL;
2108
2109 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
2110
2111 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002112 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2113 chandef->center_freq1 = control_freq;
2114 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01002115
2116 /* Primary channel not allowed */
2117 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
2118 return -EINVAL;
2119
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002120 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2121 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01002122
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002123 chantype = nla_get_u32(
2124 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2125
2126 switch (chantype) {
2127 case NL80211_CHAN_NO_HT:
2128 case NL80211_CHAN_HT20:
2129 case NL80211_CHAN_HT40PLUS:
2130 case NL80211_CHAN_HT40MINUS:
2131 cfg80211_chandef_create(chandef, chandef->chan,
2132 chantype);
2133 break;
2134 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01002135 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01002136 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002137 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2138 chandef->width =
2139 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2140 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
2141 chandef->center_freq1 =
2142 nla_get_u32(
2143 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
2144 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
2145 chandef->center_freq2 =
2146 nla_get_u32(
2147 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
2148 }
2149
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002150 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002151 return -EINVAL;
2152
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002153 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2154 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002155 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002156
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02002157 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2158 chandef->width == NL80211_CHAN_WIDTH_10) &&
2159 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
2160 return -EINVAL;
2161
Johannes Berg683b6d32012-11-08 21:25:48 +01002162 return 0;
2163}
2164
Johannes Bergf444de02010-05-05 15:25:02 +02002165static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
Jouni Malinene16821b2014-04-28 11:22:08 +03002166 struct net_device *dev,
Johannes Bergf444de02010-05-05 15:25:02 +02002167 struct genl_info *info)
2168{
Johannes Berg683b6d32012-11-08 21:25:48 +01002169 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02002170 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02002171 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
Jouni Malinene16821b2014-04-28 11:22:08 +03002172 struct wireless_dev *wdev = NULL;
Johannes Berge8c9bd52012-06-06 08:18:22 +02002173
Jouni Malinene16821b2014-04-28 11:22:08 +03002174 if (dev)
2175 wdev = dev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002176 if (!nl80211_can_set_dev_channel(wdev))
2177 return -EOPNOTSUPP;
Jouni Malinene16821b2014-04-28 11:22:08 +03002178 if (wdev)
2179 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02002180
Johannes Berg683b6d32012-11-08 21:25:48 +01002181 result = nl80211_parse_chandef(rdev, info, &chandef);
2182 if (result)
2183 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02002184
Johannes Berge8c9bd52012-06-06 08:18:22 +02002185 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02002186 case NL80211_IFTYPE_AP:
2187 case NL80211_IFTYPE_P2P_GO:
Arik Nemtsov923b3522015-07-08 15:41:44 +03002188 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2189 iftype)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02002190 result = -EINVAL;
2191 break;
2192 }
Jouni Malinene16821b2014-04-28 11:22:08 +03002193 if (wdev->beacon_interval) {
2194 if (!dev || !rdev->ops->set_ap_chanwidth ||
2195 !(rdev->wiphy.features &
2196 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2197 result = -EBUSY;
2198 break;
2199 }
2200
2201 /* Only allow dynamic channel width changes */
2202 if (chandef.chan != wdev->preset_chandef.chan) {
2203 result = -EBUSY;
2204 break;
2205 }
2206 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2207 if (result)
2208 break;
2209 }
Johannes Berg683b6d32012-11-08 21:25:48 +01002210 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02002211 result = 0;
2212 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02002213 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01002214 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02002215 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02002216 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01002217 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02002218 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02002219 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02002220 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02002221 }
Johannes Bergf444de02010-05-05 15:25:02 +02002222
2223 return result;
2224}
2225
2226static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2227{
Johannes Berg4c476992010-10-04 21:36:35 +02002228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2229 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02002230
Jouni Malinene16821b2014-04-28 11:22:08 +03002231 return __nl80211_set_channel(rdev, netdev, info);
Johannes Bergf444de02010-05-05 15:25:02 +02002232}
2233
Bill Jordane8347eb2010-10-01 13:54:28 -04002234static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2235{
Johannes Berg43b19952010-10-07 13:10:30 +02002236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2237 struct net_device *dev = info->user_ptr[1];
2238 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02002239 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04002240
2241 if (!info->attrs[NL80211_ATTR_MAC])
2242 return -EINVAL;
2243
Johannes Berg43b19952010-10-07 13:10:30 +02002244 if (netif_running(dev))
2245 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04002246
Johannes Berg43b19952010-10-07 13:10:30 +02002247 if (!rdev->ops->set_wds_peer)
2248 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04002249
Johannes Berg43b19952010-10-07 13:10:30 +02002250 if (wdev->iftype != NL80211_IFTYPE_WDS)
2251 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04002252
2253 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03002254 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04002255}
2256
Johannes Berg55682962007-09-20 13:09:35 -04002257static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2258{
2259 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02002260 struct net_device *netdev = NULL;
2261 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04002262 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02002263 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002264 u32 changed;
2265 u8 retry_short = 0, retry_long = 0;
2266 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01002267 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04002268
Johannes Berg5fe231e2013-05-08 21:45:15 +02002269 ASSERT_RTNL();
2270
Johannes Bergf444de02010-05-05 15:25:02 +02002271 /*
2272 * Try to find the wiphy and netdev. Normally this
2273 * function shouldn't need the netdev, but this is
2274 * done for backward compatibility -- previously
2275 * setting the channel was done per wiphy, but now
2276 * it is per netdev. Previous userland like hostapd
2277 * also passed a netdev to set_wiphy, so that it is
2278 * possible to let that go to the right netdev!
2279 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002280
Johannes Bergf444de02010-05-05 15:25:02 +02002281 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2282 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2283
Ying Xue7f2b8562014-01-15 10:23:45 +08002284 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002285 if (netdev && netdev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002286 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002287 else
Johannes Bergf444de02010-05-05 15:25:02 +02002288 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002289 }
2290
Johannes Bergf444de02010-05-05 15:25:02 +02002291 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002292 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2293 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002294 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002295 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002296 wdev = NULL;
2297 netdev = NULL;
2298 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002299 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002300 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002301
2302 /*
2303 * end workaround code, by now the rdev is available
2304 * and locked, and wdev may or may not be NULL.
2305 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002306
2307 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002308 result = cfg80211_dev_rename(
2309 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002310
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002311 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002312 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002313
Jouni Malinen31888482008-10-30 16:59:24 +02002314 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2315 struct ieee80211_txq_params txq_params;
2316 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2317
Ying Xue7f2b8562014-01-15 10:23:45 +08002318 if (!rdev->ops->set_txq_params)
2319 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002320
Ying Xue7f2b8562014-01-15 10:23:45 +08002321 if (!netdev)
2322 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002323
Johannes Berg133a3ff2011-11-03 14:50:13 +01002324 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002325 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2326 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002327
Ying Xue7f2b8562014-01-15 10:23:45 +08002328 if (!netif_running(netdev))
2329 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002330
Jouni Malinen31888482008-10-30 16:59:24 +02002331 nla_for_each_nested(nl_txq_params,
2332 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2333 rem_txq_params) {
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02002334 result = nla_parse_nested(tb, NL80211_TXQ_ATTR_MAX,
2335 nl_txq_params,
2336 txq_params_policy);
Johannes Bergae811e22014-01-24 10:17:47 +01002337 if (result)
2338 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002339 result = parse_txq_params(tb, &txq_params);
2340 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002341 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002342
Hila Gonene35e4d22012-06-27 17:19:42 +03002343 result = rdev_set_txq_params(rdev, netdev,
2344 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002345 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002346 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002347 }
2348 }
2349
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002350 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinene16821b2014-04-28 11:22:08 +03002351 result = __nl80211_set_channel(
2352 rdev,
2353 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2354 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002355 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002356 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002357 }
2358
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002359 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002360 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002361 enum nl80211_tx_power_setting type;
2362 int idx, mbm = 0;
2363
Johannes Bergc8442112012-10-24 10:17:18 +02002364 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2365 txp_wdev = NULL;
2366
Ying Xue7f2b8562014-01-15 10:23:45 +08002367 if (!rdev->ops->set_tx_power)
2368 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002369
2370 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2371 type = nla_get_u32(info->attrs[idx]);
2372
2373 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002374 (type != NL80211_TX_POWER_AUTOMATIC))
2375 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002376
2377 if (type != NL80211_TX_POWER_AUTOMATIC) {
2378 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2379 mbm = nla_get_u32(info->attrs[idx]);
2380 }
2381
Johannes Bergc8442112012-10-24 10:17:18 +02002382 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002383 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002384 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002385 }
2386
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002387 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2388 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2389 u32 tx_ant, rx_ant;
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07002390
Bruno Randolf7f531e02010-12-16 11:30:22 +09002391 if ((!rdev->wiphy.available_antennas_tx &&
2392 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002393 !rdev->ops->set_antenna)
2394 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002395
2396 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2397 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2398
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002399 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002400 * available antenna masks, except for the "all" mask */
2401 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002402 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2403 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002404
Bruno Randolf7f531e02010-12-16 11:30:22 +09002405 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2406 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002407
Hila Gonene35e4d22012-06-27 17:19:42 +03002408 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002409 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002410 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002411 }
2412
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002413 changed = 0;
2414
2415 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2416 retry_short = nla_get_u8(
2417 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002418 if (retry_short == 0)
2419 return -EINVAL;
2420
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002421 changed |= WIPHY_PARAM_RETRY_SHORT;
2422 }
2423
2424 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2425 retry_long = nla_get_u8(
2426 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002427 if (retry_long == 0)
2428 return -EINVAL;
2429
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002430 changed |= WIPHY_PARAM_RETRY_LONG;
2431 }
2432
2433 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2434 frag_threshold = nla_get_u32(
2435 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002436 if (frag_threshold < 256)
2437 return -EINVAL;
2438
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002439 if (frag_threshold != (u32) -1) {
2440 /*
2441 * Fragments (apart from the last one) are required to
2442 * have even length. Make the fragmentation code
2443 * simpler by stripping LSB should someone try to use
2444 * odd threshold value.
2445 */
2446 frag_threshold &= ~0x1;
2447 }
2448 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2449 }
2450
2451 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2452 rts_threshold = nla_get_u32(
2453 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2454 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2455 }
2456
Lukáš Turek81077e82009-12-21 22:50:47 +01002457 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +02002458 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2459 return -EINVAL;
2460
Lukáš Turek81077e82009-12-21 22:50:47 +01002461 coverage_class = nla_get_u8(
2462 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2463 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2464 }
2465
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +02002466 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2467 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2468 return -EOPNOTSUPP;
2469
2470 changed |= WIPHY_PARAM_DYN_ACK;
2471 }
2472
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002473 if (changed) {
2474 u8 old_retry_short, old_retry_long;
2475 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002476 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002477
Ying Xue7f2b8562014-01-15 10:23:45 +08002478 if (!rdev->ops->set_wiphy_params)
2479 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002480
2481 old_retry_short = rdev->wiphy.retry_short;
2482 old_retry_long = rdev->wiphy.retry_long;
2483 old_frag_threshold = rdev->wiphy.frag_threshold;
2484 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002485 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002486
2487 if (changed & WIPHY_PARAM_RETRY_SHORT)
2488 rdev->wiphy.retry_short = retry_short;
2489 if (changed & WIPHY_PARAM_RETRY_LONG)
2490 rdev->wiphy.retry_long = retry_long;
2491 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2492 rdev->wiphy.frag_threshold = frag_threshold;
2493 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2494 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002495 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2496 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002497
Hila Gonene35e4d22012-06-27 17:19:42 +03002498 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002499 if (result) {
2500 rdev->wiphy.retry_short = old_retry_short;
2501 rdev->wiphy.retry_long = old_retry_long;
2502 rdev->wiphy.frag_threshold = old_frag_threshold;
2503 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002504 rdev->wiphy.coverage_class = old_coverage_class;
Michal Kazior9189ee32015-08-03 10:55:24 +02002505 return result;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002506 }
2507 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002508 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002509}
2510
Johannes Berg71bbc992012-06-15 15:30:18 +02002511static inline u64 wdev_id(struct wireless_dev *wdev)
2512{
2513 return (u64)wdev->identifier |
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002514 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
Johannes Berg71bbc992012-06-15 15:30:18 +02002515}
Johannes Berg55682962007-09-20 13:09:35 -04002516
Johannes Berg683b6d32012-11-08 21:25:48 +01002517static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002518 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002519{
Johannes Berg601555c2014-11-27 17:26:56 +01002520 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2521 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002522
Johannes Berg683b6d32012-11-08 21:25:48 +01002523 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2524 chandef->chan->center_freq))
2525 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002526 switch (chandef->width) {
2527 case NL80211_CHAN_WIDTH_20_NOHT:
2528 case NL80211_CHAN_WIDTH_20:
2529 case NL80211_CHAN_WIDTH_40:
2530 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2531 cfg80211_get_chandef_type(chandef)))
2532 return -ENOBUFS;
2533 break;
2534 default:
2535 break;
2536 }
2537 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2538 return -ENOBUFS;
2539 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2540 return -ENOBUFS;
2541 if (chandef->center_freq2 &&
2542 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002543 return -ENOBUFS;
2544 return 0;
2545}
2546
Eric W. Biederman15e47302012-09-07 20:12:54 +00002547static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002548 struct cfg80211_registered_device *rdev,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002549 struct wireless_dev *wdev, bool removal)
Johannes Berg55682962007-09-20 13:09:35 -04002550{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002551 struct net_device *dev = wdev->netdev;
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002552 u8 cmd = NL80211_CMD_NEW_INTERFACE;
Johannes Berg55682962007-09-20 13:09:35 -04002553 void *hdr;
2554
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002555 if (removal)
2556 cmd = NL80211_CMD_DEL_INTERFACE;
2557
2558 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -04002559 if (!hdr)
2560 return -1;
2561
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002562 if (dev &&
2563 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002564 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002565 goto nla_put_failure;
2566
2567 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2568 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02002569 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
2570 NL80211_ATTR_PAD) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002571 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002572 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2573 rdev->devlist_generation ^
2574 (cfg80211_rdev_list_generation << 2)))
2575 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002576
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002577 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002578 int ret;
2579 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002580
Johannes Berg683b6d32012-11-08 21:25:48 +01002581 ret = rdev_get_channel(rdev, wdev, &chandef);
2582 if (ret == 0) {
2583 if (nl80211_send_chandef(msg, &chandef))
2584 goto nla_put_failure;
2585 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002586 }
2587
Rafał Miłeckid55d0d52015-08-31 22:59:38 +02002588 if (rdev->ops->get_tx_power) {
2589 int dbm, ret;
2590
2591 ret = rdev_get_tx_power(rdev, wdev, &dbm);
2592 if (ret == 0 &&
2593 nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2594 DBM_TO_MBM(dbm)))
2595 goto nla_put_failure;
2596 }
2597
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002598 if (wdev->ssid_len) {
2599 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2600 goto nla_put_failure;
2601 }
2602
Johannes Berg053c0952015-01-16 22:09:00 +01002603 genlmsg_end(msg, hdr);
2604 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002605
2606 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002607 genlmsg_cancel(msg, hdr);
2608 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002609}
2610
2611static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2612{
2613 int wp_idx = 0;
2614 int if_idx = 0;
2615 int wp_start = cb->args[0];
2616 int if_start = cb->args[1];
Denis Kenziorb7fb44d2016-08-03 17:02:15 -05002617 int filter_wiphy = -1;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002618 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002619 struct wireless_dev *wdev;
2620
Johannes Berg5fe231e2013-05-08 21:45:15 +02002621 rtnl_lock();
Denis Kenziorb7fb44d2016-08-03 17:02:15 -05002622 if (!cb->args[2]) {
2623 struct nl80211_dump_wiphy_state state = {
2624 .filter_wiphy = -1,
2625 };
2626 int ret;
2627
2628 ret = nl80211_dump_wiphy_parse(skb, cb, &state);
2629 if (ret)
2630 return ret;
2631
2632 filter_wiphy = state.filter_wiphy;
2633
2634 /*
2635 * if filtering, set cb->args[2] to +1 since 0 is the default
2636 * value needed to determine that parsing is necessary.
2637 */
2638 if (filter_wiphy >= 0)
2639 cb->args[2] = filter_wiphy + 1;
2640 else
2641 cb->args[2] = -1;
2642 } else if (cb->args[2] > 0) {
2643 filter_wiphy = cb->args[2] - 1;
2644 }
2645
Johannes Bergf5ea9122009-08-07 16:17:38 +02002646 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2647 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002648 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002649 if (wp_idx < wp_start) {
2650 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002651 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002652 }
Denis Kenziorb7fb44d2016-08-03 17:02:15 -05002653
2654 if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
2655 continue;
2656
Johannes Berg55682962007-09-20 13:09:35 -04002657 if_idx = 0;
2658
Johannes Berg53873f12016-05-03 16:52:04 +03002659 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002660 if (if_idx < if_start) {
2661 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002662 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002663 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002664 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002665 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002666 rdev, wdev, false) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002667 goto out;
2668 }
2669 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002670 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002671
2672 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002673 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002674 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002675 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002676
2677 cb->args[0] = wp_idx;
2678 cb->args[1] = if_idx;
2679
2680 return skb->len;
2681}
2682
2683static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2684{
2685 struct sk_buff *msg;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002686 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002687 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002688
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002689 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002690 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002691 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002692
Eric W. Biederman15e47302012-09-07 20:12:54 +00002693 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002694 rdev, wdev, false) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002695 nlmsg_free(msg);
2696 return -ENOBUFS;
2697 }
Johannes Berg55682962007-09-20 13:09:35 -04002698
Johannes Berg134e6372009-07-10 09:51:34 +00002699 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002700}
2701
Michael Wu66f7ac52008-01-31 19:48:22 +01002702static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2703 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2704 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2705 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2706 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2707 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002708 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002709};
2710
2711static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2712{
2713 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2714 int flag;
2715
2716 *mntrflags = 0;
2717
2718 if (!nla)
2719 return -EINVAL;
2720
2721 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2722 nla, mntr_flags_policy))
2723 return -EINVAL;
2724
2725 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2726 if (flags[flag])
2727 *mntrflags |= (1<<flag);
2728
Johannes Berg818a9862017-04-12 11:23:28 +02002729 *mntrflags |= MONITOR_FLAG_CHANGED;
2730
Michael Wu66f7ac52008-01-31 19:48:22 +01002731 return 0;
2732}
2733
Johannes Berg1db77592017-04-12 11:36:31 +02002734static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
2735 enum nl80211_iftype type,
2736 struct genl_info *info,
2737 struct vif_params *params)
2738{
2739 bool change = false;
2740 int err;
2741
2742 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2743 if (type != NL80211_IFTYPE_MONITOR)
2744 return -EINVAL;
2745
2746 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2747 &params->flags);
2748 if (err)
2749 return err;
2750
2751 change = true;
2752 }
2753
2754 if (params->flags & MONITOR_FLAG_ACTIVE &&
2755 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2756 return -EOPNOTSUPP;
2757
2758 if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
2759 const u8 *mumimo_groups;
2760 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2761
2762 if (type != NL80211_IFTYPE_MONITOR)
2763 return -EINVAL;
2764
2765 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2766 return -EOPNOTSUPP;
2767
2768 mumimo_groups =
2769 nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
2770
2771 /* bits 0 and 63 are reserved and must be zero */
2772 if ((mumimo_groups[0] & BIT(7)) ||
2773 (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(0)))
2774 return -EINVAL;
2775
2776 params->vht_mumimo_groups = mumimo_groups;
2777 change = true;
2778 }
2779
2780 if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
2781 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2782
2783 if (type != NL80211_IFTYPE_MONITOR)
2784 return -EINVAL;
2785
2786 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2787 return -EOPNOTSUPP;
2788
2789 params->vht_mumimo_follow_addr =
2790 nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
2791 change = true;
2792 }
2793
2794 return change ? 1 : 0;
2795}
2796
Johannes Berg9bc383d2009-11-19 11:55:19 +01002797static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002798 struct net_device *netdev, u8 use_4addr,
2799 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002800{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002801 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002802 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002803 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002804 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002805 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002806
2807 switch (iftype) {
2808 case NL80211_IFTYPE_AP_VLAN:
2809 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2810 return 0;
2811 break;
2812 case NL80211_IFTYPE_STATION:
2813 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2814 return 0;
2815 break;
2816 default:
2817 break;
2818 }
2819
2820 return -EOPNOTSUPP;
2821}
2822
Johannes Berg55682962007-09-20 13:09:35 -04002823static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2824{
Johannes Berg4c476992010-10-04 21:36:35 +02002825 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002826 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002827 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002828 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002829 struct net_device *dev = info->user_ptr[1];
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002830 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002831
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002832 memset(&params, 0, sizeof(params));
2833
Johannes Berg04a773a2009-04-19 21:24:32 +02002834 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002835
Johannes Berg723b0382008-09-16 20:22:09 +02002836 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002837 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002838 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002839 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002840 if (ntype > NL80211_IFTYPE_MAX)
2841 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002842 }
2843
Johannes Berg92ffe052008-09-16 20:39:36 +02002844 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002845 struct wireless_dev *wdev = dev->ieee80211_ptr;
2846
Johannes Berg4c476992010-10-04 21:36:35 +02002847 if (ntype != NL80211_IFTYPE_MESH_POINT)
2848 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002849 if (netif_running(dev))
2850 return -EBUSY;
2851
2852 wdev_lock(wdev);
2853 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2854 IEEE80211_MAX_MESH_ID_LEN);
2855 wdev->mesh_id_up_len =
2856 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2857 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2858 wdev->mesh_id_up_len);
2859 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002860 }
2861
Felix Fietkau8b787642009-11-10 18:53:10 +01002862 if (info->attrs[NL80211_ATTR_4ADDR]) {
2863 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2864 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002865 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002866 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002867 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002868 } else {
2869 params.use_4addr = -1;
2870 }
2871
Johannes Berg1db77592017-04-12 11:36:31 +02002872 err = nl80211_parse_mon_options(rdev, ntype, info, &params);
2873 if (err < 0)
2874 return err;
2875 if (err > 0)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002876 change = true;
Aviya Erenfeldc6e6a0c2016-07-05 15:23:08 +03002877
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002878 if (change)
Johannes Berg818a9862017-04-12 11:23:28 +02002879 err = cfg80211_change_iface(rdev, dev, ntype, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002880 else
2881 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002882
Johannes Berg9bc383d2009-11-19 11:55:19 +01002883 if (!err && params.use_4addr != -1)
2884 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2885
Johannes Berg55682962007-09-20 13:09:35 -04002886 return err;
2887}
2888
2889static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2890{
Johannes Berg4c476992010-10-04 21:36:35 +02002891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002892 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002893 struct wireless_dev *wdev;
Denis Kenzior896ff062016-08-03 16:58:33 -05002894 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002895 int err;
2896 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2897
Johannes Berg78f22b62014-03-24 17:57:27 +01002898 /* to avoid failing a new interface creation due to pending removal */
2899 cfg80211_destroy_ifaces(rdev);
2900
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002901 memset(&params, 0, sizeof(params));
2902
Johannes Berg55682962007-09-20 13:09:35 -04002903 if (!info->attrs[NL80211_ATTR_IFNAME])
2904 return -EINVAL;
2905
2906 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2907 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2908 if (type > NL80211_IFTYPE_MAX)
2909 return -EINVAL;
2910 }
2911
Johannes Berg79c97e92009-07-07 03:56:12 +02002912 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002913 !(rdev->wiphy.interface_modes & (1 << type)))
2914 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002915
Ayala Bekercb3b7d82016-09-20 17:31:13 +03002916 if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
Ben Greeare8f479b2014-10-22 12:23:05 -07002917 rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2918 info->attrs[NL80211_ATTR_MAC]) {
Arend van Spriel1c18f142013-01-08 10:17:27 +01002919 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2920 ETH_ALEN);
2921 if (!is_valid_ether_addr(params.macaddr))
2922 return -EADDRNOTAVAIL;
2923 }
2924
Johannes Berg9bc383d2009-11-19 11:55:19 +01002925 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002926 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002927 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002928 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002929 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002930 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002931
Johannes Berg1db77592017-04-12 11:36:31 +02002932 err = nl80211_parse_mon_options(rdev, type, info, &params);
2933 if (err < 0)
2934 return err;
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002935
Johannes Berga18c7192015-02-24 10:56:42 +01002936 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2937 if (!msg)
2938 return -ENOMEM;
2939
Hila Gonene35e4d22012-06-27 17:19:42 +03002940 wdev = rdev_add_virtual_intf(rdev,
2941 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Johannes Berg818a9862017-04-12 11:23:28 +02002942 NET_NAME_USER, type, &params);
Rafał Miłeckid687cbb2014-11-14 18:43:28 +01002943 if (WARN_ON(!wdev)) {
2944 nlmsg_free(msg);
2945 return -EPROTO;
2946 } else if (IS_ERR(wdev)) {
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002947 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002948 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002949 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002950
Jukka Rissanen18e5ca62014-11-13 17:25:14 +02002951 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
Johannes Berg78f22b62014-03-24 17:57:27 +01002952 wdev->owner_nlportid = info->snd_portid;
2953
Johannes Berg98104fde2012-06-16 00:19:54 +02002954 switch (type) {
2955 case NL80211_IFTYPE_MESH_POINT:
2956 if (!info->attrs[NL80211_ATTR_MESH_ID])
2957 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002958 wdev_lock(wdev);
2959 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2960 IEEE80211_MAX_MESH_ID_LEN);
2961 wdev->mesh_id_up_len =
2962 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2963 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2964 wdev->mesh_id_up_len);
2965 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002966 break;
Ayala Bekercb3b7d82016-09-20 17:31:13 +03002967 case NL80211_IFTYPE_NAN:
Johannes Berg98104fde2012-06-16 00:19:54 +02002968 case NL80211_IFTYPE_P2P_DEVICE:
2969 /*
Ayala Bekercb3b7d82016-09-20 17:31:13 +03002970 * P2P Device and NAN do not have a netdev, so don't go
Johannes Berg98104fde2012-06-16 00:19:54 +02002971 * through the netdev notifier and must be added here
2972 */
2973 mutex_init(&wdev->mtx);
2974 INIT_LIST_HEAD(&wdev->event_list);
2975 spin_lock_init(&wdev->event_lock);
2976 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2977 spin_lock_init(&wdev->mgmt_registrations_lock);
2978
Johannes Berg98104fde2012-06-16 00:19:54 +02002979 wdev->identifier = ++rdev->wdev_id;
Johannes Berg53873f12016-05-03 16:52:04 +03002980 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
Johannes Berg98104fde2012-06-16 00:19:54 +02002981 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002982 break;
2983 default:
2984 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002985 }
2986
Eric W. Biederman15e47302012-09-07 20:12:54 +00002987 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002988 rdev, wdev, false) < 0) {
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002989 nlmsg_free(msg);
2990 return -ENOBUFS;
2991 }
2992
Denis Kenzior896ff062016-08-03 16:58:33 -05002993 /*
2994 * For wdevs which have no associated netdev object (e.g. of type
2995 * NL80211_IFTYPE_P2P_DEVICE), emit the NEW_INTERFACE event here.
2996 * For all other types, the event will be generated from the
2997 * netdev notifier
2998 */
2999 if (!wdev->netdev)
3000 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02003001
Johannes Berg1c90f9d2012-06-16 00:05:37 +02003002 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04003003}
3004
3005static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3006{
Johannes Berg4c476992010-10-04 21:36:35 +02003007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02003008 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04003009
Johannes Berg4c476992010-10-04 21:36:35 +02003010 if (!rdev->ops->del_virtual_intf)
3011 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003012
Johannes Berg84efbb82012-06-16 00:00:26 +02003013 /*
3014 * If we remove a wireless device without a netdev then clear
3015 * user_ptr[1] so that nl80211_post_doit won't dereference it
3016 * to check if it needs to do dev_put(). Otherwise it crashes
3017 * since the wdev has been freed, unlike with a netdev where
3018 * we need the dev_put() for the netdev to really be freed.
3019 */
3020 if (!wdev->netdev)
3021 info->user_ptr[1] = NULL;
3022
Denis Kenzior7f8ed012016-08-03 16:58:35 -05003023 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04003024}
3025
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01003026static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3027{
3028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3029 struct net_device *dev = info->user_ptr[1];
3030 u16 noack_map;
3031
3032 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3033 return -EINVAL;
3034
3035 if (!rdev->ops->set_noack_map)
3036 return -EOPNOTSUPP;
3037
3038 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3039
Hila Gonene35e4d22012-06-27 17:19:42 +03003040 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01003041}
3042
Johannes Berg41ade002007-12-19 02:03:29 +01003043struct get_key_cookie {
3044 struct sk_buff *msg;
3045 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02003046 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01003047};
3048
3049static void get_key_callback(void *c, struct key_params *params)
3050{
Johannes Bergb9454e82009-07-08 13:29:08 +02003051 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01003052 struct get_key_cookie *cookie = c;
3053
David S. Miller9360ffd2012-03-29 04:41:26 -04003054 if ((params->key &&
3055 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3056 params->key_len, params->key)) ||
3057 (params->seq &&
3058 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3059 params->seq_len, params->seq)) ||
3060 (params->cipher &&
3061 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3062 params->cipher)))
3063 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01003064
Johannes Bergb9454e82009-07-08 13:29:08 +02003065 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
3066 if (!key)
3067 goto nla_put_failure;
3068
David S. Miller9360ffd2012-03-29 04:41:26 -04003069 if ((params->key &&
3070 nla_put(cookie->msg, NL80211_KEY_DATA,
3071 params->key_len, params->key)) ||
3072 (params->seq &&
3073 nla_put(cookie->msg, NL80211_KEY_SEQ,
3074 params->seq_len, params->seq)) ||
3075 (params->cipher &&
3076 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3077 params->cipher)))
3078 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02003079
David S. Miller9360ffd2012-03-29 04:41:26 -04003080 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
3081 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02003082
3083 nla_nest_end(cookie->msg, key);
3084
Johannes Berg41ade002007-12-19 02:03:29 +01003085 return;
3086 nla_put_failure:
3087 cookie->error = 1;
3088}
3089
3090static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3091{
Johannes Berg4c476992010-10-04 21:36:35 +02003092 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01003093 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003094 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01003095 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02003096 const u8 *mac_addr = NULL;
3097 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01003098 struct get_key_cookie cookie = {
3099 .error = 0,
3100 };
3101 void *hdr;
3102 struct sk_buff *msg;
3103
3104 if (info->attrs[NL80211_ATTR_KEY_IDX])
3105 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3106
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02003107 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01003108 return -EINVAL;
3109
3110 if (info->attrs[NL80211_ATTR_MAC])
3111 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3112
Johannes Berge31b8212010-10-05 19:39:30 +02003113 pairwise = !!mac_addr;
3114 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3115 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07003116
Johannes Berge31b8212010-10-05 19:39:30 +02003117 if (kt >= NUM_NL80211_KEYTYPES)
3118 return -EINVAL;
3119 if (kt != NL80211_KEYTYPE_GROUP &&
3120 kt != NL80211_KEYTYPE_PAIRWISE)
3121 return -EINVAL;
3122 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3123 }
3124
Johannes Berg4c476992010-10-04 21:36:35 +02003125 if (!rdev->ops->get_key)
3126 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01003127
Johannes Berg0fa7b392015-01-23 11:10:12 +01003128 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3129 return -ENOENT;
3130
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003131 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003132 if (!msg)
3133 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01003134
Eric W. Biederman15e47302012-09-07 20:12:54 +00003135 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01003136 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03003137 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02003138 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01003139
3140 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02003141 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01003142
David S. Miller9360ffd2012-03-29 04:41:26 -04003143 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3144 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3145 goto nla_put_failure;
3146 if (mac_addr &&
3147 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3148 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01003149
Hila Gonene35e4d22012-06-27 17:19:42 +03003150 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3151 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01003152
3153 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03003154 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01003155
3156 if (cookie.error)
3157 goto nla_put_failure;
3158
3159 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003160 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01003161
3162 nla_put_failure:
3163 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03003164 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01003165 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01003166 return err;
3167}
3168
3169static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3170{
Johannes Berg4c476992010-10-04 21:36:35 +02003171 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02003172 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01003173 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003174 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01003175
Johannes Bergb9454e82009-07-08 13:29:08 +02003176 err = nl80211_parse_key(info, &key);
3177 if (err)
3178 return err;
3179
3180 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01003181 return -EINVAL;
3182
Johannes Bergb9454e82009-07-08 13:29:08 +02003183 /* only support setting default key */
3184 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01003185 return -EINVAL;
3186
Johannes Bergfffd0932009-07-08 14:22:54 +02003187 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01003188
3189 if (key.def) {
3190 if (!rdev->ops->set_default_key) {
3191 err = -EOPNOTSUPP;
3192 goto out;
3193 }
3194
3195 err = nl80211_key_allowed(dev->ieee80211_ptr);
3196 if (err)
3197 goto out;
3198
Hila Gonene35e4d22012-06-27 17:19:42 +03003199 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01003200 key.def_uni, key.def_multi);
3201
3202 if (err)
3203 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02003204
Johannes Berg3d23e342009-09-29 23:27:28 +02003205#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01003206 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02003207#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01003208 } else {
3209 if (key.def_uni || !key.def_multi) {
3210 err = -EINVAL;
3211 goto out;
3212 }
3213
3214 if (!rdev->ops->set_default_mgmt_key) {
3215 err = -EOPNOTSUPP;
3216 goto out;
3217 }
3218
3219 err = nl80211_key_allowed(dev->ieee80211_ptr);
3220 if (err)
3221 goto out;
3222
Hila Gonene35e4d22012-06-27 17:19:42 +03003223 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01003224 if (err)
3225 goto out;
3226
3227#ifdef CONFIG_CFG80211_WEXT
3228 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3229#endif
3230 }
3231
3232 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02003233 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01003234
Johannes Berg41ade002007-12-19 02:03:29 +01003235 return err;
3236}
3237
3238static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3239{
Johannes Berg4c476992010-10-04 21:36:35 +02003240 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02003241 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003242 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02003243 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02003244 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01003245
Johannes Bergb9454e82009-07-08 13:29:08 +02003246 err = nl80211_parse_key(info, &key);
3247 if (err)
3248 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01003249
Johannes Bergb9454e82009-07-08 13:29:08 +02003250 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01003251 return -EINVAL;
3252
Johannes Berg41ade002007-12-19 02:03:29 +01003253 if (info->attrs[NL80211_ATTR_MAC])
3254 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3255
Johannes Berge31b8212010-10-05 19:39:30 +02003256 if (key.type == -1) {
3257 if (mac_addr)
3258 key.type = NL80211_KEYTYPE_PAIRWISE;
3259 else
3260 key.type = NL80211_KEYTYPE_GROUP;
3261 }
3262
3263 /* for now */
3264 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3265 key.type != NL80211_KEYTYPE_GROUP)
3266 return -EINVAL;
3267
Johannes Berg4c476992010-10-04 21:36:35 +02003268 if (!rdev->ops->add_key)
3269 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003270
Johannes Berge31b8212010-10-05 19:39:30 +02003271 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3272 key.type == NL80211_KEYTYPE_PAIRWISE,
3273 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02003274 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003275
3276 wdev_lock(dev->ieee80211_ptr);
3277 err = nl80211_key_allowed(dev->ieee80211_ptr);
3278 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03003279 err = rdev_add_key(rdev, dev, key.idx,
3280 key.type == NL80211_KEYTYPE_PAIRWISE,
3281 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02003282 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01003283
Johannes Berg41ade002007-12-19 02:03:29 +01003284 return err;
3285}
3286
3287static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3288{
Johannes Berg4c476992010-10-04 21:36:35 +02003289 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01003290 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003291 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01003292 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02003293 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01003294
Johannes Bergb9454e82009-07-08 13:29:08 +02003295 err = nl80211_parse_key(info, &key);
3296 if (err)
3297 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01003298
3299 if (info->attrs[NL80211_ATTR_MAC])
3300 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3301
Johannes Berge31b8212010-10-05 19:39:30 +02003302 if (key.type == -1) {
3303 if (mac_addr)
3304 key.type = NL80211_KEYTYPE_PAIRWISE;
3305 else
3306 key.type = NL80211_KEYTYPE_GROUP;
3307 }
3308
3309 /* for now */
3310 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3311 key.type != NL80211_KEYTYPE_GROUP)
3312 return -EINVAL;
3313
Johannes Berg4c476992010-10-04 21:36:35 +02003314 if (!rdev->ops->del_key)
3315 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01003316
Johannes Bergfffd0932009-07-08 14:22:54 +02003317 wdev_lock(dev->ieee80211_ptr);
3318 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02003319
Johannes Berg0fa7b392015-01-23 11:10:12 +01003320 if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
Johannes Berge31b8212010-10-05 19:39:30 +02003321 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3322 err = -ENOENT;
3323
Johannes Bergfffd0932009-07-08 14:22:54 +02003324 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03003325 err = rdev_del_key(rdev, dev, key.idx,
3326 key.type == NL80211_KEYTYPE_PAIRWISE,
3327 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01003328
Johannes Berg3d23e342009-09-29 23:27:28 +02003329#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02003330 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02003331 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02003332 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02003333 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02003334 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3335 }
3336#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02003337 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02003338
Johannes Berg41ade002007-12-19 02:03:29 +01003339 return err;
3340}
3341
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303342/* This function returns an error or the number of nested attributes */
3343static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3344{
3345 struct nlattr *attr;
3346 int n_entries = 0, tmp;
3347
3348 nla_for_each_nested(attr, nl_attr, tmp) {
3349 if (nla_len(attr) != ETH_ALEN)
3350 return -EINVAL;
3351
3352 n_entries++;
3353 }
3354
3355 return n_entries;
3356}
3357
3358/*
3359 * This function parses ACL information and allocates memory for ACL data.
3360 * On successful return, the calling function is responsible to free the
3361 * ACL buffer returned by this function.
3362 */
3363static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3364 struct genl_info *info)
3365{
3366 enum nl80211_acl_policy acl_policy;
3367 struct nlattr *attr;
3368 struct cfg80211_acl_data *acl;
3369 int i = 0, n_entries, tmp;
3370
3371 if (!wiphy->max_acl_mac_addrs)
3372 return ERR_PTR(-EOPNOTSUPP);
3373
3374 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3375 return ERR_PTR(-EINVAL);
3376
3377 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3378 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3379 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3380 return ERR_PTR(-EINVAL);
3381
3382 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3383 return ERR_PTR(-EINVAL);
3384
3385 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3386 if (n_entries < 0)
3387 return ERR_PTR(n_entries);
3388
3389 if (n_entries > wiphy->max_acl_mac_addrs)
3390 return ERR_PTR(-ENOTSUPP);
3391
3392 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3393 GFP_KERNEL);
3394 if (!acl)
3395 return ERR_PTR(-ENOMEM);
3396
3397 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3398 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3399 i++;
3400 }
3401
3402 acl->n_acl_entries = n_entries;
3403 acl->acl_policy = acl_policy;
3404
3405 return acl;
3406}
3407
3408static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3409{
3410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3411 struct net_device *dev = info->user_ptr[1];
3412 struct cfg80211_acl_data *acl;
3413 int err;
3414
3415 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3416 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3417 return -EOPNOTSUPP;
3418
3419 if (!dev->ieee80211_ptr->beacon_interval)
3420 return -EINVAL;
3421
3422 acl = parse_acl_data(&rdev->wiphy, info);
3423 if (IS_ERR(acl))
3424 return PTR_ERR(acl);
3425
3426 err = rdev_set_mac_acl(rdev, dev, acl);
3427
3428 kfree(acl);
3429
3430 return err;
3431}
3432
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303433static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
3434 u8 *rates, u8 rates_len)
3435{
3436 u8 i;
3437 u32 mask = 0;
3438
3439 for (i = 0; i < rates_len; i++) {
3440 int rate = (rates[i] & 0x7f) * 5;
3441 int ridx;
3442
3443 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
3444 struct ieee80211_rate *srate =
3445 &sband->bitrates[ridx];
3446 if (rate == srate->bitrate) {
3447 mask |= 1 << ridx;
3448 break;
3449 }
3450 }
3451 if (ridx == sband->n_bitrates)
3452 return 0; /* rate not found */
3453 }
3454
3455 return mask;
3456}
3457
3458static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
3459 u8 *rates, u8 rates_len,
3460 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
3461{
3462 u8 i;
3463
3464 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
3465
3466 for (i = 0; i < rates_len; i++) {
3467 int ridx, rbit;
3468
3469 ridx = rates[i] / 8;
3470 rbit = BIT(rates[i] % 8);
3471
3472 /* check validity */
3473 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
3474 return false;
3475
3476 /* check availability */
3477 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
3478 mcs[ridx] |= rbit;
3479 else
3480 return false;
3481 }
3482
3483 return true;
3484}
3485
3486static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
3487{
3488 u16 mcs_mask = 0;
3489
3490 switch (vht_mcs_map) {
3491 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
3492 break;
3493 case IEEE80211_VHT_MCS_SUPPORT_0_7:
3494 mcs_mask = 0x00FF;
3495 break;
3496 case IEEE80211_VHT_MCS_SUPPORT_0_8:
3497 mcs_mask = 0x01FF;
3498 break;
3499 case IEEE80211_VHT_MCS_SUPPORT_0_9:
3500 mcs_mask = 0x03FF;
3501 break;
3502 default:
3503 break;
3504 }
3505
3506 return mcs_mask;
3507}
3508
3509static void vht_build_mcs_mask(u16 vht_mcs_map,
3510 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
3511{
3512 u8 nss;
3513
3514 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
3515 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
3516 vht_mcs_map >>= 2;
3517 }
3518}
3519
3520static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
3521 struct nl80211_txrate_vht *txrate,
3522 u16 mcs[NL80211_VHT_NSS_MAX])
3523{
3524 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3525 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
3526 u8 i;
3527
3528 if (!sband->vht_cap.vht_supported)
3529 return false;
3530
3531 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
3532
3533 /* Build vht_mcs_mask from VHT capabilities */
3534 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
3535
3536 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3537 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
3538 mcs[i] = txrate->mcs[i];
3539 else
3540 return false;
3541 }
3542
3543 return true;
3544}
3545
3546static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
3547 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
3548 .len = NL80211_MAX_SUPP_RATES },
3549 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
3550 .len = NL80211_MAX_SUPP_HT_RATES },
3551 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
3552 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
3553};
3554
3555static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
3556 struct cfg80211_bitrate_mask *mask)
3557{
3558 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
3559 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3560 int rem, i;
3561 struct nlattr *tx_rates;
3562 struct ieee80211_supported_band *sband;
3563 u16 vht_tx_mcs_map;
3564
3565 memset(mask, 0, sizeof(*mask));
3566 /* Default to all rates enabled */
3567 for (i = 0; i < NUM_NL80211_BANDS; i++) {
3568 sband = rdev->wiphy.bands[i];
3569
3570 if (!sband)
3571 continue;
3572
3573 mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
3574 memcpy(mask->control[i].ht_mcs,
3575 sband->ht_cap.mcs.rx_mask,
3576 sizeof(mask->control[i].ht_mcs));
3577
3578 if (!sband->vht_cap.vht_supported)
3579 continue;
3580
3581 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3582 vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
3583 }
3584
3585 /* if no rates are given set it back to the defaults */
3586 if (!info->attrs[NL80211_ATTR_TX_RATES])
3587 goto out;
3588
3589 /* The nested attribute uses enum nl80211_band as the index. This maps
3590 * directly to the enum nl80211_band values used in cfg80211.
3591 */
3592 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
3593 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
3594 enum nl80211_band band = nla_type(tx_rates);
3595 int err;
3596
3597 if (band < 0 || band >= NUM_NL80211_BANDS)
3598 return -EINVAL;
3599 sband = rdev->wiphy.bands[band];
3600 if (sband == NULL)
3601 return -EINVAL;
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02003602 err = nla_parse_nested(tb, NL80211_TXRATE_MAX, tx_rates,
3603 nl80211_txattr_policy);
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303604 if (err)
3605 return err;
3606 if (tb[NL80211_TXRATE_LEGACY]) {
3607 mask->control[band].legacy = rateset_to_mask(
3608 sband,
3609 nla_data(tb[NL80211_TXRATE_LEGACY]),
3610 nla_len(tb[NL80211_TXRATE_LEGACY]));
3611 if ((mask->control[band].legacy == 0) &&
3612 nla_len(tb[NL80211_TXRATE_LEGACY]))
3613 return -EINVAL;
3614 }
3615 if (tb[NL80211_TXRATE_HT]) {
3616 if (!ht_rateset_to_mask(
3617 sband,
3618 nla_data(tb[NL80211_TXRATE_HT]),
3619 nla_len(tb[NL80211_TXRATE_HT]),
3620 mask->control[band].ht_mcs))
3621 return -EINVAL;
3622 }
3623 if (tb[NL80211_TXRATE_VHT]) {
3624 if (!vht_set_mcs_mask(
3625 sband,
3626 nla_data(tb[NL80211_TXRATE_VHT]),
3627 mask->control[band].vht_mcs))
3628 return -EINVAL;
3629 }
3630 if (tb[NL80211_TXRATE_GI]) {
3631 mask->control[band].gi =
3632 nla_get_u8(tb[NL80211_TXRATE_GI]);
3633 if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
3634 return -EINVAL;
3635 }
3636
3637 if (mask->control[band].legacy == 0) {
3638 /* don't allow empty legacy rates if HT or VHT
3639 * are not even supported.
3640 */
3641 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
3642 rdev->wiphy.bands[band]->vht_cap.vht_supported))
3643 return -EINVAL;
3644
3645 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3646 if (mask->control[band].ht_mcs[i])
3647 goto out;
3648
3649 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3650 if (mask->control[band].vht_mcs[i])
3651 goto out;
3652
3653 /* legacy and mcs rates may not be both empty */
3654 return -EINVAL;
3655 }
3656 }
3657
3658out:
3659 return 0;
3660}
3661
Johannes Berg8564e382016-09-19 09:44:44 +02003662static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
3663 enum nl80211_band band,
3664 struct cfg80211_bitrate_mask *beacon_rate)
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303665{
Johannes Berg8564e382016-09-19 09:44:44 +02003666 u32 count_ht, count_vht, i;
3667 u32 rate = beacon_rate->control[band].legacy;
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303668
3669 /* Allow only one rate */
3670 if (hweight32(rate) > 1)
3671 return -EINVAL;
3672
3673 count_ht = 0;
3674 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
Johannes Berg8564e382016-09-19 09:44:44 +02003675 if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303676 return -EINVAL;
Johannes Berg8564e382016-09-19 09:44:44 +02003677 } else if (beacon_rate->control[band].ht_mcs[i]) {
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303678 count_ht++;
3679 if (count_ht > 1)
3680 return -EINVAL;
3681 }
3682 if (count_ht && rate)
3683 return -EINVAL;
3684 }
3685
3686 count_vht = 0;
3687 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
Johannes Berg8564e382016-09-19 09:44:44 +02003688 if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303689 return -EINVAL;
Johannes Berg8564e382016-09-19 09:44:44 +02003690 } else if (beacon_rate->control[band].vht_mcs[i]) {
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303691 count_vht++;
3692 if (count_vht > 1)
3693 return -EINVAL;
3694 }
3695 if (count_vht && rate)
3696 return -EINVAL;
3697 }
3698
3699 if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
3700 return -EINVAL;
3701
Johannes Berg8564e382016-09-19 09:44:44 +02003702 if (rate &&
3703 !wiphy_ext_feature_isset(&rdev->wiphy,
3704 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
3705 return -EINVAL;
3706 if (count_ht &&
3707 !wiphy_ext_feature_isset(&rdev->wiphy,
3708 NL80211_EXT_FEATURE_BEACON_RATE_HT))
3709 return -EINVAL;
3710 if (count_vht &&
3711 !wiphy_ext_feature_isset(&rdev->wiphy,
3712 NL80211_EXT_FEATURE_BEACON_RATE_VHT))
3713 return -EINVAL;
3714
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05303715 return 0;
3716}
3717
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003718static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003719 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003720{
Johannes Berg88600202012-02-13 15:17:18 +01003721 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003722
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003723 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3724 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3725 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3726 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003727 return -EINVAL;
3728
Johannes Berg88600202012-02-13 15:17:18 +01003729 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003730
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003731 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3732 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3733 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003734 if (!bcn->head_len)
3735 return -EINVAL;
3736 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003737 }
3738
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003739 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3740 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3741 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003742 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003743 }
3744
Johannes Berg4c476992010-10-04 21:36:35 +02003745 if (!haveinfo)
3746 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003747
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003748 if (attrs[NL80211_ATTR_IE]) {
3749 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3750 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003751 }
3752
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003753 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003754 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003755 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003756 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003757 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003758 }
3759
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003760 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003761 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003762 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003763 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003764 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003765 }
3766
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003767 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3768 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3769 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003770 }
3771
Johannes Berg88600202012-02-13 15:17:18 +01003772 return 0;
3773}
3774
Johannes Berg66cd7942017-02-07 22:40:44 +02003775static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
3776 const u8 *rates)
3777{
3778 int i;
3779
3780 if (!rates)
3781 return;
3782
3783 for (i = 0; i < rates[1]; i++) {
3784 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
3785 params->ht_required = true;
3786 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
3787 params->vht_required = true;
3788 }
3789}
3790
3791/*
3792 * Since the nl80211 API didn't include, from the beginning, attributes about
3793 * HT/VHT requirements/capabilities, we parse them out of the IEs for the
3794 * benefit of drivers that rebuild IEs in the firmware.
3795 */
3796static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
3797{
3798 const struct cfg80211_beacon_data *bcn = &params->beacon;
3799 size_t ies_len = bcn->beacon_ies_len;
3800 const u8 *ies = bcn->beacon_ies;
3801 const u8 *rates;
3802 const u8 *cap;
3803
3804 rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
3805 nl80211_check_ap_rate_selectors(params, rates);
3806
3807 rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
3808 nl80211_check_ap_rate_selectors(params, rates);
3809
3810 cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
3811 if (cap && cap[1] >= sizeof(*params->ht_cap))
3812 params->ht_cap = (void *)(cap + 2);
3813 cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
3814 if (cap && cap[1] >= sizeof(*params->vht_cap))
3815 params->vht_cap = (void *)(cap + 2);
3816}
3817
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003818static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3819 struct cfg80211_ap_settings *params)
3820{
3821 struct wireless_dev *wdev;
3822 bool ret = false;
3823
Johannes Berg53873f12016-05-03 16:52:04 +03003824 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003825 if (wdev->iftype != NL80211_IFTYPE_AP &&
3826 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3827 continue;
3828
Johannes Berg683b6d32012-11-08 21:25:48 +01003829 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003830 continue;
3831
Johannes Berg683b6d32012-11-08 21:25:48 +01003832 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003833 ret = true;
3834 break;
3835 }
3836
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003837 return ret;
3838}
3839
Jouni Malinene39e5b52012-09-30 19:29:39 +03003840static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3841 enum nl80211_auth_type auth_type,
3842 enum nl80211_commands cmd)
3843{
3844 if (auth_type > NL80211_AUTHTYPE_MAX)
3845 return false;
3846
3847 switch (cmd) {
3848 case NL80211_CMD_AUTHENTICATE:
3849 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3850 auth_type == NL80211_AUTHTYPE_SAE)
3851 return false;
Jouni Malinen63181062016-10-27 00:42:02 +03003852 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3853 NL80211_EXT_FEATURE_FILS_STA) &&
3854 (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3855 auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3856 auth_type == NL80211_AUTHTYPE_FILS_PK))
3857 return false;
Jouni Malinene39e5b52012-09-30 19:29:39 +03003858 return true;
3859 case NL80211_CMD_CONNECT:
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +03003860 /* SAE not supported yet */
3861 if (auth_type == NL80211_AUTHTYPE_SAE)
3862 return false;
3863 /* FILS with SK PFS or PK not supported yet */
3864 if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3865 auth_type == NL80211_AUTHTYPE_FILS_PK)
3866 return false;
3867 if (!wiphy_ext_feature_isset(
3868 &rdev->wiphy,
3869 NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
3870 auth_type == NL80211_AUTHTYPE_FILS_SK)
3871 return false;
3872 return true;
Jouni Malinene39e5b52012-09-30 19:29:39 +03003873 case NL80211_CMD_START_AP:
3874 /* SAE not supported yet */
3875 if (auth_type == NL80211_AUTHTYPE_SAE)
3876 return false;
Jouni Malinen63181062016-10-27 00:42:02 +03003877 /* FILS not supported yet */
3878 if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3879 auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3880 auth_type == NL80211_AUTHTYPE_FILS_PK)
3881 return false;
Jouni Malinene39e5b52012-09-30 19:29:39 +03003882 return true;
3883 default:
3884 return false;
3885 }
3886}
3887
Johannes Berg88600202012-02-13 15:17:18 +01003888static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3889{
3890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3891 struct net_device *dev = info->user_ptr[1];
3892 struct wireless_dev *wdev = dev->ieee80211_ptr;
3893 struct cfg80211_ap_settings params;
3894 int err;
3895
3896 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3897 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3898 return -EOPNOTSUPP;
3899
3900 if (!rdev->ops->start_ap)
3901 return -EOPNOTSUPP;
3902
3903 if (wdev->beacon_interval)
3904 return -EALREADY;
3905
3906 memset(&params, 0, sizeof(params));
3907
3908 /* these are required for START_AP */
3909 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3910 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3911 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3912 return -EINVAL;
3913
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003914 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003915 if (err)
3916 return err;
3917
3918 params.beacon_interval =
3919 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3920 params.dtim_period =
3921 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3922
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05303923 err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
3924 params.beacon_interval);
Johannes Berg88600202012-02-13 15:17:18 +01003925 if (err)
3926 return err;
3927
3928 /*
3929 * In theory, some of these attributes should be required here
3930 * but since they were not used when the command was originally
3931 * added, keep them optional for old user space programs to let
3932 * them continue to work with drivers that do not need the
3933 * additional information -- drivers must check!
3934 */
3935 if (info->attrs[NL80211_ATTR_SSID]) {
3936 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3937 params.ssid_len =
3938 nla_len(info->attrs[NL80211_ATTR_SSID]);
3939 if (params.ssid_len == 0 ||
3940 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3941 return -EINVAL;
3942 }
3943
3944 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3945 params.hidden_ssid = nla_get_u32(
3946 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3947 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3948 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3949 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3950 return -EINVAL;
3951 }
3952
3953 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3954
3955 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3956 params.auth_type = nla_get_u32(
3957 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003958 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3959 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003960 return -EINVAL;
3961 } else
3962 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3963
3964 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3965 NL80211_MAX_NR_CIPHER_SUITES);
3966 if (err)
3967 return err;
3968
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303969 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3970 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3971 return -EOPNOTSUPP;
3972 params.inactivity_timeout = nla_get_u16(
3973 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3974 }
3975
Johannes Berg53cabad2012-11-14 15:17:28 +01003976 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3977 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3978 return -EINVAL;
3979 params.p2p_ctwindow =
3980 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3981 if (params.p2p_ctwindow > 127)
3982 return -EINVAL;
3983 if (params.p2p_ctwindow != 0 &&
3984 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3985 return -EINVAL;
3986 }
3987
3988 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3989 u8 tmp;
3990
3991 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3992 return -EINVAL;
3993 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3994 if (tmp > 1)
3995 return -EINVAL;
3996 params.p2p_opp_ps = tmp;
3997 if (params.p2p_opp_ps != 0 &&
3998 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3999 return -EINVAL;
4000 }
4001
Johannes Bergaa430da2012-05-16 23:50:18 +02004002 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01004003 err = nl80211_parse_chandef(rdev, info, &params.chandef);
4004 if (err)
4005 return err;
4006 } else if (wdev->preset_chandef.chan) {
4007 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02004008 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02004009 return -EINVAL;
4010
Arik Nemtsov923b3522015-07-08 15:41:44 +03004011 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4012 wdev->iftype))
Johannes Bergaa430da2012-05-16 23:50:18 +02004013 return -EINVAL;
4014
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05304015 if (info->attrs[NL80211_ATTR_TX_RATES]) {
4016 err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4017 if (err)
4018 return err;
4019
Johannes Berg8564e382016-09-19 09:44:44 +02004020 err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4021 &params.beacon_rate);
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05304022 if (err)
4023 return err;
4024 }
4025
Eliad Peller18998c32014-09-10 14:07:34 +03004026 if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4027 params.smps_mode =
4028 nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4029 switch (params.smps_mode) {
4030 case NL80211_SMPS_OFF:
4031 break;
4032 case NL80211_SMPS_STATIC:
4033 if (!(rdev->wiphy.features &
4034 NL80211_FEATURE_STATIC_SMPS))
4035 return -EINVAL;
4036 break;
4037 case NL80211_SMPS_DYNAMIC:
4038 if (!(rdev->wiphy.features &
4039 NL80211_FEATURE_DYNAMIC_SMPS))
4040 return -EINVAL;
4041 break;
4042 default:
4043 return -EINVAL;
4044 }
4045 } else {
4046 params.smps_mode = NL80211_SMPS_OFF;
4047 }
4048
Purushottam Kushwaha6e8ef842016-07-05 13:44:51 +05304049 params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4050 if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4051 return -EOPNOTSUPP;
4052
Ola Olsson4baf6be2015-10-29 07:04:58 +01004053 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4054 params.acl = parse_acl_data(&rdev->wiphy, info);
4055 if (IS_ERR(params.acl))
4056 return PTR_ERR(params.acl);
4057 }
4058
Johannes Berg66cd7942017-02-07 22:40:44 +02004059 nl80211_calculate_ap_params(&params);
4060
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004061 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03004062 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02004063 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01004064 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01004065 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01004066 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01004067 wdev->ssid_len = params.ssid_len;
4068 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02004069 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004070 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05304071
4072 kfree(params.acl);
4073
Johannes Berg56d18932011-05-09 18:41:15 +02004074 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01004075}
4076
Johannes Berg88600202012-02-13 15:17:18 +01004077static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4078{
4079 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4080 struct net_device *dev = info->user_ptr[1];
4081 struct wireless_dev *wdev = dev->ieee80211_ptr;
4082 struct cfg80211_beacon_data params;
4083 int err;
4084
4085 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4086 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4087 return -EOPNOTSUPP;
4088
4089 if (!rdev->ops->change_beacon)
4090 return -EOPNOTSUPP;
4091
4092 if (!wdev->beacon_interval)
4093 return -EINVAL;
4094
Simon Wunderlicha1193be2013-06-14 14:15:19 +02004095 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01004096 if (err)
4097 return err;
4098
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004099 wdev_lock(wdev);
4100 err = rdev_change_beacon(rdev, dev, &params);
4101 wdev_unlock(wdev);
4102
4103 return err;
Johannes Berg88600202012-02-13 15:17:18 +01004104}
4105
4106static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01004107{
Johannes Berg4c476992010-10-04 21:36:35 +02004108 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4109 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01004110
Ilan Peer7c8d5e02014-02-25 15:33:38 +02004111 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01004112}
4113
Johannes Berg5727ef12007-12-19 02:03:34 +01004114static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4115 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4116 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4117 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03004118 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07004119 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01004120 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01004121};
4122
Johannes Bergeccb8e82009-05-11 21:57:56 +03004123static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004124 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03004125 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01004126{
4127 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03004128 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01004129 int flag;
4130
Johannes Bergeccb8e82009-05-11 21:57:56 +03004131 /*
4132 * Try parsing the new attribute first so userspace
4133 * can specify both for older kernels.
4134 */
4135 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4136 if (nla) {
4137 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01004138
Johannes Bergeccb8e82009-05-11 21:57:56 +03004139 sta_flags = nla_data(nla);
4140 params->sta_flags_mask = sta_flags->mask;
4141 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004142 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03004143 if ((params->sta_flags_mask |
4144 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4145 return -EINVAL;
4146 return 0;
4147 }
4148
4149 /* if present, parse the old attribute */
4150
4151 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01004152 if (!nla)
4153 return 0;
4154
4155 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
4156 nla, sta_flags_policy))
4157 return -EINVAL;
4158
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004159 /*
4160 * Only allow certain flags for interface types so that
4161 * other attributes are silently ignored. Remember that
4162 * this is backward compatibility code with old userspace
4163 * and shouldn't be hit in other cases anyway.
4164 */
4165 switch (iftype) {
4166 case NL80211_IFTYPE_AP:
4167 case NL80211_IFTYPE_AP_VLAN:
4168 case NL80211_IFTYPE_P2P_GO:
4169 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4170 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4171 BIT(NL80211_STA_FLAG_WME) |
4172 BIT(NL80211_STA_FLAG_MFP);
4173 break;
4174 case NL80211_IFTYPE_P2P_CLIENT:
4175 case NL80211_IFTYPE_STATION:
4176 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4177 BIT(NL80211_STA_FLAG_TDLS_PEER);
4178 break;
4179 case NL80211_IFTYPE_MESH_POINT:
4180 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4181 BIT(NL80211_STA_FLAG_MFP) |
4182 BIT(NL80211_STA_FLAG_AUTHORIZED);
4183 default:
4184 return -EINVAL;
4185 }
Johannes Berg5727ef12007-12-19 02:03:34 +01004186
Johannes Berg3383b5a2012-05-10 20:14:43 +02004187 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4188 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03004189 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01004190
Johannes Berg3383b5a2012-05-10 20:14:43 +02004191 /* no longer support new API additions in old API */
4192 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4193 return -EINVAL;
4194 }
4195 }
4196
Johannes Berg5727ef12007-12-19 02:03:34 +01004197 return 0;
4198}
4199
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004200static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
4201 int attr)
4202{
4203 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03004204 u32 bitrate;
4205 u16 bitrate_compat;
Matthias Kaehlckebbf67e42017-04-17 15:59:52 -07004206 enum nl80211_rate_info rate_flg;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004207
4208 rate = nla_nest_start(msg, attr);
4209 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01004210 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004211
4212 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4213 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03004214 /* report 16-bit bitrate only if we can */
4215 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01004216 if (bitrate > 0 &&
4217 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4218 return false;
4219 if (bitrate_compat > 0 &&
4220 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4221 return false;
4222
Johannes Bergb51f3be2015-01-15 16:14:02 +01004223 switch (info->bw) {
4224 case RATE_INFO_BW_5:
4225 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4226 break;
4227 case RATE_INFO_BW_10:
4228 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4229 break;
4230 default:
4231 WARN_ON(1);
4232 /* fall through */
4233 case RATE_INFO_BW_20:
4234 rate_flg = 0;
4235 break;
4236 case RATE_INFO_BW_40:
4237 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4238 break;
4239 case RATE_INFO_BW_80:
4240 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4241 break;
4242 case RATE_INFO_BW_160:
4243 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4244 break;
4245 }
4246
4247 if (rate_flg && nla_put_flag(msg, rate_flg))
4248 return false;
4249
Johannes Bergdb9c64c2012-11-09 14:56:41 +01004250 if (info->flags & RATE_INFO_FLAGS_MCS) {
4251 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
4252 return false;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01004253 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4254 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4255 return false;
4256 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
4257 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
4258 return false;
4259 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
4260 return false;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01004261 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4262 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4263 return false;
4264 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004265
4266 nla_nest_end(msg, rate);
4267 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004268}
4269
Felix Fietkau119363c2013-04-22 16:29:30 +02004270static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
4271 int id)
4272{
4273 void *attr;
4274 int i = 0;
4275
4276 if (!mask)
4277 return true;
4278
4279 attr = nla_nest_start(msg, id);
4280 if (!attr)
4281 return false;
4282
4283 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
4284 if (!(mask & BIT(i)))
4285 continue;
4286
4287 if (nla_put_u8(msg, i, signal[i]))
4288 return false;
4289 }
4290
4291 nla_nest_end(msg, attr);
4292
4293 return true;
4294}
4295
Johannes Bergcf5ead82014-11-14 17:14:00 +01004296static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
4297 u32 seq, int flags,
John W. Linville66266b32012-03-15 13:25:41 -04004298 struct cfg80211_registered_device *rdev,
4299 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01004300 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004301{
4302 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07004303 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004304
Johannes Bergcf5ead82014-11-14 17:14:00 +01004305 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004306 if (!hdr)
4307 return -1;
4308
David S. Miller9360ffd2012-03-29 04:41:26 -04004309 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4310 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
4311 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
4312 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004313
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004314 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
4315 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004316 goto nla_put_failure;
Johannes Berg319090b2014-11-17 14:08:11 +01004317
4318#define PUT_SINFO(attr, memb, type) do { \
Johannes Bergd686b922016-04-26 09:54:11 +02004319 BUILD_BUG_ON(sizeof(type) == sizeof(u64)); \
Mohammed Shafi Shajakhan739960f2016-04-07 19:59:34 +05304320 if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) && \
Johannes Berg319090b2014-11-17 14:08:11 +01004321 nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr, \
4322 sinfo->memb)) \
4323 goto nla_put_failure; \
4324 } while (0)
Johannes Bergd686b922016-04-26 09:54:11 +02004325#define PUT_SINFO_U64(attr, memb) do { \
4326 if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) && \
4327 nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr, \
4328 sinfo->memb, NL80211_STA_INFO_PAD)) \
4329 goto nla_put_failure; \
4330 } while (0)
Johannes Berg319090b2014-11-17 14:08:11 +01004331
4332 PUT_SINFO(CONNECTED_TIME, connected_time, u32);
4333 PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
4334
4335 if (sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES) |
4336 BIT(NL80211_STA_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04004337 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02004338 (u32)sinfo->rx_bytes))
4339 goto nla_put_failure;
Johannes Berg319090b2014-11-17 14:08:11 +01004340
4341 if (sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES) |
4342 BIT(NL80211_STA_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02004343 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
4344 (u32)sinfo->tx_bytes))
4345 goto nla_put_failure;
Johannes Berg319090b2014-11-17 14:08:11 +01004346
Johannes Bergd686b922016-04-26 09:54:11 +02004347 PUT_SINFO_U64(RX_BYTES64, rx_bytes);
4348 PUT_SINFO_U64(TX_BYTES64, tx_bytes);
Johannes Berg319090b2014-11-17 14:08:11 +01004349 PUT_SINFO(LLID, llid, u16);
4350 PUT_SINFO(PLID, plid, u16);
4351 PUT_SINFO(PLINK_STATE, plink_state, u8);
Johannes Bergd686b922016-04-26 09:54:11 +02004352 PUT_SINFO_U64(RX_DURATION, rx_duration);
Johannes Berg319090b2014-11-17 14:08:11 +01004353
John W. Linville66266b32012-03-15 13:25:41 -04004354 switch (rdev->wiphy.signal_type) {
4355 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berg319090b2014-11-17 14:08:11 +01004356 PUT_SINFO(SIGNAL, signal, u8);
4357 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
John W. Linville66266b32012-03-15 13:25:41 -04004358 break;
4359 default:
4360 break;
4361 }
Johannes Berg319090b2014-11-17 14:08:11 +01004362 if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL)) {
Felix Fietkau119363c2013-04-22 16:29:30 +02004363 if (!nl80211_put_signal(msg, sinfo->chains,
4364 sinfo->chain_signal,
4365 NL80211_STA_INFO_CHAIN_SIGNAL))
4366 goto nla_put_failure;
4367 }
Johannes Berg319090b2014-11-17 14:08:11 +01004368 if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
Felix Fietkau119363c2013-04-22 16:29:30 +02004369 if (!nl80211_put_signal(msg, sinfo->chains,
4370 sinfo->chain_signal_avg,
4371 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
4372 goto nla_put_failure;
4373 }
Johannes Berg319090b2014-11-17 14:08:11 +01004374 if (sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004375 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
4376 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01004377 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004378 }
Johannes Berg319090b2014-11-17 14:08:11 +01004379 if (sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE)) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01004380 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
4381 NL80211_STA_INFO_RX_BITRATE))
4382 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01004383 }
Johannes Berg319090b2014-11-17 14:08:11 +01004384
4385 PUT_SINFO(RX_PACKETS, rx_packets, u32);
4386 PUT_SINFO(TX_PACKETS, tx_packets, u32);
4387 PUT_SINFO(TX_RETRIES, tx_retries, u32);
4388 PUT_SINFO(TX_FAILED, tx_failed, u32);
4389 PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
4390 PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
4391 PUT_SINFO(LOCAL_PM, local_pm, u32);
4392 PUT_SINFO(PEER_PM, peer_pm, u32);
4393 PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
4394
4395 if (sinfo->filled & BIT(NL80211_STA_INFO_BSS_PARAM)) {
Paul Stewartf4263c92011-03-31 09:25:41 -07004396 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
4397 if (!bss_param)
4398 goto nla_put_failure;
4399
David S. Miller9360ffd2012-03-29 04:41:26 -04004400 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
4401 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
4402 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
4403 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
4404 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
4405 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
4406 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
4407 sinfo->bss_param.dtim_period) ||
4408 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
4409 sinfo->bss_param.beacon_interval))
4410 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07004411
4412 nla_nest_end(msg, bss_param);
4413 }
Johannes Berg319090b2014-11-17 14:08:11 +01004414 if ((sinfo->filled & BIT(NL80211_STA_INFO_STA_FLAGS)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04004415 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
4416 sizeof(struct nl80211_sta_flag_update),
4417 &sinfo->sta_flags))
4418 goto nla_put_failure;
Johannes Berg319090b2014-11-17 14:08:11 +01004419
Johannes Bergd686b922016-04-26 09:54:11 +02004420 PUT_SINFO_U64(T_OFFSET, t_offset);
4421 PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
4422 PUT_SINFO_U64(BEACON_RX, rx_beacon);
Johannes Berga76b1942014-11-17 14:12:22 +01004423 PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
Johannes Berg319090b2014-11-17 14:08:11 +01004424
4425#undef PUT_SINFO
Johannes Bergd686b922016-04-26 09:54:11 +02004426#undef PUT_SINFO_U64
Johannes Berg6de39802014-12-19 12:34:00 +01004427
4428 if (sinfo->filled & BIT(NL80211_STA_INFO_TID_STATS)) {
4429 struct nlattr *tidsattr;
4430 int tid;
4431
4432 tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
4433 if (!tidsattr)
4434 goto nla_put_failure;
4435
4436 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
4437 struct cfg80211_tid_stats *tidstats;
4438 struct nlattr *tidattr;
4439
4440 tidstats = &sinfo->pertid[tid];
4441
4442 if (!tidstats->filled)
4443 continue;
4444
4445 tidattr = nla_nest_start(msg, tid + 1);
4446 if (!tidattr)
4447 goto nla_put_failure;
4448
Johannes Bergd686b922016-04-26 09:54:11 +02004449#define PUT_TIDVAL_U64(attr, memb) do { \
Johannes Berg6de39802014-12-19 12:34:00 +01004450 if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) && \
Johannes Bergd686b922016-04-26 09:54:11 +02004451 nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr, \
4452 tidstats->memb, NL80211_TID_STATS_PAD)) \
Johannes Berg6de39802014-12-19 12:34:00 +01004453 goto nla_put_failure; \
4454 } while (0)
4455
Johannes Bergd686b922016-04-26 09:54:11 +02004456 PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
4457 PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
4458 PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
4459 PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
Johannes Berg6de39802014-12-19 12:34:00 +01004460
Johannes Bergd686b922016-04-26 09:54:11 +02004461#undef PUT_TIDVAL_U64
Johannes Berg6de39802014-12-19 12:34:00 +01004462 nla_nest_end(msg, tidattr);
4463 }
4464
4465 nla_nest_end(msg, tidsattr);
4466 }
4467
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004468 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004469
Johannes Berg319090b2014-11-17 14:08:11 +01004470 if (sinfo->assoc_req_ies_len &&
David S. Miller9360ffd2012-03-29 04:41:26 -04004471 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
4472 sinfo->assoc_req_ies))
4473 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03004474
Johannes Berg053c0952015-01-16 22:09:00 +01004475 genlmsg_end(msg, hdr);
4476 return 0;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004477
4478 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004479 genlmsg_cancel(msg, hdr);
4480 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004481}
4482
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004483static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004484 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004485{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004486 struct station_info sinfo;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004487 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02004488 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004489 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004490 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004492
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004493 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004494 if (err)
4495 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004496
Johannes Berg97990a02013-04-19 01:02:55 +02004497 if (!wdev->netdev) {
4498 err = -EINVAL;
4499 goto out_err;
4500 }
4501
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004502 if (!rdev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004503 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004504 goto out_err;
4505 }
4506
Johannes Bergbba95fe2008-07-29 13:22:51 +02004507 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03004508 memset(&sinfo, 0, sizeof(sinfo));
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004509 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03004510 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004511 if (err == -ENOENT)
4512 break;
4513 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004514 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004515
Johannes Bergcf5ead82014-11-14 17:14:00 +01004516 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004517 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004519 rdev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004520 &sinfo) < 0)
4521 goto out;
4522
4523 sta_idx++;
4524 }
4525
Johannes Bergbba95fe2008-07-29 13:22:51 +02004526 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004527 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004528 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004529 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004530 nl80211_finish_wdev_dump(rdev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004531
4532 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004533}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004534
Johannes Berg5727ef12007-12-19 02:03:34 +01004535static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
4536{
Johannes Berg4c476992010-10-04 21:36:35 +02004537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4538 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004539 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004540 struct sk_buff *msg;
4541 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004542 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004543
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004544 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004545
4546 if (!info->attrs[NL80211_ATTR_MAC])
4547 return -EINVAL;
4548
4549 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4550
Johannes Berg4c476992010-10-04 21:36:35 +02004551 if (!rdev->ops->get_station)
4552 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004553
Hila Gonene35e4d22012-06-27 17:19:42 +03004554 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004555 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004556 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004557
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004558 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004559 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004560 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004561
Johannes Bergcf5ead82014-11-14 17:14:00 +01004562 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
4563 info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04004564 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02004565 nlmsg_free(msg);
4566 return -ENOBUFS;
4567 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01004568
Johannes Berg4c476992010-10-04 21:36:35 +02004569 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01004570}
4571
Johannes Berg77ee7c82013-02-15 00:48:33 +01004572int cfg80211_check_station_change(struct wiphy *wiphy,
4573 struct station_parameters *params,
4574 enum cfg80211_station_type statype)
4575{
Ayala Bekere4208422015-10-23 11:20:06 +03004576 if (params->listen_interval != -1 &&
4577 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
Johannes Berg77ee7c82013-02-15 00:48:33 +01004578 return -EINVAL;
Ayala Bekere4208422015-10-23 11:20:06 +03004579
Ayala Beker17b94242016-03-17 15:41:38 +02004580 if (params->support_p2p_ps != -1 &&
4581 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4582 return -EINVAL;
4583
Arik Nemtsovc72e1142014-07-17 17:14:29 +03004584 if (params->aid &&
Ayala Bekere4208422015-10-23 11:20:06 +03004585 !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
4586 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
Johannes Berg77ee7c82013-02-15 00:48:33 +01004587 return -EINVAL;
4588
4589 /* When you run into this, adjust the code below for the new flag */
4590 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4591
4592 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08004593 case CFG80211_STA_MESH_PEER_KERNEL:
4594 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004595 /*
4596 * No ignoring the TDLS flag here -- the userspace mesh
4597 * code doesn't have the bug of including TDLS in the
4598 * mask everywhere.
4599 */
4600 if (params->sta_flags_mask &
4601 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4602 BIT(NL80211_STA_FLAG_MFP) |
4603 BIT(NL80211_STA_FLAG_AUTHORIZED)))
4604 return -EINVAL;
4605 break;
4606 case CFG80211_STA_TDLS_PEER_SETUP:
4607 case CFG80211_STA_TDLS_PEER_ACTIVE:
4608 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4609 return -EINVAL;
4610 /* ignore since it can't change */
4611 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4612 break;
4613 default:
4614 /* disallow mesh-specific things */
4615 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4616 return -EINVAL;
4617 if (params->local_pm)
4618 return -EINVAL;
4619 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4620 return -EINVAL;
4621 }
4622
4623 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4624 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4625 /* TDLS can't be set, ... */
4626 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4627 return -EINVAL;
4628 /*
4629 * ... but don't bother the driver with it. This works around
4630 * a hostapd/wpa_supplicant issue -- it always includes the
4631 * TLDS_PEER flag in the mask even for AP mode.
4632 */
4633 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4634 }
4635
Ayala Beker47edb112015-09-21 15:49:53 +03004636 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4637 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
Johannes Berg77ee7c82013-02-15 00:48:33 +01004638 /* reject other things that can't change */
4639 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
4640 return -EINVAL;
4641 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
4642 return -EINVAL;
4643 if (params->supported_rates)
4644 return -EINVAL;
4645 if (params->ext_capab || params->ht_capa || params->vht_capa)
4646 return -EINVAL;
4647 }
4648
Ayala Beker47edb112015-09-21 15:49:53 +03004649 if (statype != CFG80211_STA_AP_CLIENT &&
4650 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
Johannes Berg77ee7c82013-02-15 00:48:33 +01004651 if (params->vlan)
4652 return -EINVAL;
4653 }
4654
4655 switch (statype) {
4656 case CFG80211_STA_AP_MLME_CLIENT:
4657 /* Use this only for authorizing/unauthorizing a station */
4658 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4659 return -EOPNOTSUPP;
4660 break;
4661 case CFG80211_STA_AP_CLIENT:
Ayala Beker47edb112015-09-21 15:49:53 +03004662 case CFG80211_STA_AP_CLIENT_UNASSOC:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004663 /* accept only the listed bits */
4664 if (params->sta_flags_mask &
4665 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4666 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4667 BIT(NL80211_STA_FLAG_ASSOCIATED) |
4668 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4669 BIT(NL80211_STA_FLAG_WME) |
4670 BIT(NL80211_STA_FLAG_MFP)))
4671 return -EINVAL;
4672
4673 /* but authenticated/associated only if driver handles it */
4674 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4675 params->sta_flags_mask &
4676 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4677 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4678 return -EINVAL;
4679 break;
4680 case CFG80211_STA_IBSS:
4681 case CFG80211_STA_AP_STA:
4682 /* reject any changes other than AUTHORIZED */
4683 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4684 return -EINVAL;
4685 break;
4686 case CFG80211_STA_TDLS_PEER_SETUP:
4687 /* reject any changes other than AUTHORIZED or WME */
4688 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4689 BIT(NL80211_STA_FLAG_WME)))
4690 return -EINVAL;
4691 /* force (at least) rates when authorizing */
4692 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4693 !params->supported_rates)
4694 return -EINVAL;
4695 break;
4696 case CFG80211_STA_TDLS_PEER_ACTIVE:
4697 /* reject any changes */
4698 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08004699 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004700 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4701 return -EINVAL;
4702 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08004703 case CFG80211_STA_MESH_PEER_USER:
Chun-Yeow Yeoh42925042015-04-18 01:30:02 +08004704 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
4705 params->plink_action != NL80211_PLINK_ACTION_BLOCK)
Johannes Berg77ee7c82013-02-15 00:48:33 +01004706 return -EINVAL;
4707 break;
4708 }
4709
Beni Lev06f7c882016-07-19 19:28:56 +03004710 /*
4711 * Older kernel versions ignored this attribute entirely, so don't
4712 * reject attempts to update it but mark it as unused instead so the
4713 * driver won't look at the data.
4714 */
4715 if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
4716 statype != CFG80211_STA_TDLS_PEER_SETUP)
4717 params->opmode_notif_used = false;
4718
Johannes Berg77ee7c82013-02-15 00:48:33 +01004719 return 0;
4720}
4721EXPORT_SYMBOL(cfg80211_check_station_change);
4722
Johannes Berg5727ef12007-12-19 02:03:34 +01004723/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01004724 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01004725 */
Johannes Berg80b99892011-11-18 16:23:01 +01004726static struct net_device *get_vlan(struct genl_info *info,
4727 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01004728{
Johannes Berg463d0182009-07-14 00:33:35 +02004729 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01004730 struct net_device *v;
4731 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01004732
Johannes Berg80b99892011-11-18 16:23:01 +01004733 if (!vlanattr)
4734 return NULL;
4735
4736 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4737 if (!v)
4738 return ERR_PTR(-ENODEV);
4739
4740 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4741 ret = -EINVAL;
4742 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01004743 }
Johannes Berg80b99892011-11-18 16:23:01 +01004744
Johannes Berg77ee7c82013-02-15 00:48:33 +01004745 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4746 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4747 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4748 ret = -EINVAL;
4749 goto error;
4750 }
4751
Johannes Berg80b99892011-11-18 16:23:01 +01004752 if (!netif_running(v)) {
4753 ret = -ENETDOWN;
4754 goto error;
4755 }
4756
4757 return v;
4758 error:
4759 dev_put(v);
4760 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01004761}
4762
Johannes Berg94e860f2014-01-20 23:58:15 +01004763static const struct nla_policy
4764nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02004765 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4766 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4767};
4768
Johannes Bergff276692013-02-15 00:09:01 +01004769static int nl80211_parse_sta_wme(struct genl_info *info,
4770 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02004771{
Jouni Malinendf881292013-02-14 21:10:54 +02004772 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4773 struct nlattr *nla;
4774 int err;
4775
Jouni Malinendf881292013-02-14 21:10:54 +02004776 /* parse WME attributes if present */
4777 if (!info->attrs[NL80211_ATTR_STA_WME])
4778 return 0;
4779
4780 nla = info->attrs[NL80211_ATTR_STA_WME];
4781 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4782 nl80211_sta_wme_policy);
4783 if (err)
4784 return err;
4785
4786 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4787 params->uapsd_queues = nla_get_u8(
4788 tb[NL80211_STA_WME_UAPSD_QUEUES]);
4789 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4790 return -EINVAL;
4791
4792 if (tb[NL80211_STA_WME_MAX_SP])
4793 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4794
4795 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4796 return -EINVAL;
4797
4798 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4799
4800 return 0;
4801}
4802
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304803static int nl80211_parse_sta_channel_info(struct genl_info *info,
4804 struct station_parameters *params)
4805{
4806 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4807 params->supported_channels =
4808 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4809 params->supported_channels_len =
4810 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4811 /*
4812 * Need to include at least one (first channel, number of
4813 * channels) tuple for each subband, and must have proper
4814 * tuples for the rest of the data as well.
4815 */
4816 if (params->supported_channels_len < 2)
4817 return -EINVAL;
4818 if (params->supported_channels_len % 2)
4819 return -EINVAL;
4820 }
4821
4822 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4823 params->supported_oper_classes =
4824 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4825 params->supported_oper_classes_len =
4826 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4827 /*
4828 * The value of the Length field of the Supported Operating
4829 * Classes element is between 2 and 253.
4830 */
4831 if (params->supported_oper_classes_len < 2 ||
4832 params->supported_oper_classes_len > 253)
4833 return -EINVAL;
4834 }
4835 return 0;
4836}
4837
Johannes Bergff276692013-02-15 00:09:01 +01004838static int nl80211_set_station_tdls(struct genl_info *info,
4839 struct station_parameters *params)
4840{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304841 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004842 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004843 if (info->attrs[NL80211_ATTR_PEER_AID])
4844 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004845 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4846 params->ht_capa =
4847 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4848 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4849 params->vht_capa =
4850 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4851
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304852 err = nl80211_parse_sta_channel_info(info, params);
4853 if (err)
4854 return err;
4855
Johannes Bergff276692013-02-15 00:09:01 +01004856 return nl80211_parse_sta_wme(info, params);
4857}
4858
Johannes Berg5727ef12007-12-19 02:03:34 +01004859static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4860{
Johannes Berg4c476992010-10-04 21:36:35 +02004861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004862 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004863 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004864 u8 *mac_addr;
4865 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004866
4867 memset(&params, 0, sizeof(params));
4868
Johannes Berg77ee7c82013-02-15 00:48:33 +01004869 if (!rdev->ops->change_station)
4870 return -EOPNOTSUPP;
4871
Ayala Bekere4208422015-10-23 11:20:06 +03004872 /*
4873 * AID and listen_interval properties can be set only for unassociated
4874 * station. Include these parameters here and will check them in
4875 * cfg80211_check_station_change().
4876 */
Ayala Bekera9bc31e2015-11-26 16:26:12 +01004877 if (info->attrs[NL80211_ATTR_STA_AID])
4878 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Ayala Bekere4208422015-10-23 11:20:06 +03004879
4880 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4881 params.listen_interval =
4882 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4883 else
4884 params.listen_interval = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01004885
Ayala Beker17b94242016-03-17 15:41:38 +02004886 if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
4887 u8 tmp;
4888
4889 tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
4890 if (tmp >= NUM_NL80211_P2P_PS_STATUS)
4891 return -EINVAL;
4892
4893 params.support_p2p_ps = tmp;
4894 } else {
4895 params.support_p2p_ps = -1;
4896 }
4897
Johannes Berg5727ef12007-12-19 02:03:34 +01004898 if (!info->attrs[NL80211_ATTR_MAC])
4899 return -EINVAL;
4900
4901 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4902
4903 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4904 params.supported_rates =
4905 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4906 params.supported_rates_len =
4907 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4908 }
4909
Jouni Malinen9d62a982013-02-14 21:10:13 +02004910 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4911 params.capability =
4912 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4913 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4914 }
4915
4916 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4917 params.ext_capab =
4918 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4919 params.ext_capab_len =
4920 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4921 }
4922
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004923 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004924 return -EINVAL;
4925
Johannes Bergf8bacc22013-02-14 23:27:01 +01004926 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004927 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004928 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4929 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4930 return -EINVAL;
4931 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004932
Johannes Bergf8bacc22013-02-14 23:27:01 +01004933 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004934 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004935 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4936 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4937 return -EINVAL;
Masashi Honma7d27a0b2016-07-01 10:19:34 +09004938 if (info->attrs[NL80211_ATTR_MESH_PEER_AID]) {
4939 params.peer_aid = nla_get_u16(
4940 info->attrs[NL80211_ATTR_MESH_PEER_AID]);
4941 if (params.peer_aid > IEEE80211_MAX_AID)
4942 return -EINVAL;
4943 }
Johannes Bergf8bacc22013-02-14 23:27:01 +01004944 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4945 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004946
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004947 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4948 enum nl80211_mesh_power_mode pm = nla_get_u32(
4949 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4950
4951 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4952 pm > NL80211_MESH_POWER_MAX)
4953 return -EINVAL;
4954
4955 params.local_pm = pm;
4956 }
4957
Beni Lev06f7c882016-07-19 19:28:56 +03004958 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4959 params.opmode_notif_used = true;
4960 params.opmode_notif =
4961 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4962 }
4963
Johannes Berg77ee7c82013-02-15 00:48:33 +01004964 /* Include parameters for TDLS peer (will check later) */
4965 err = nl80211_set_station_tdls(info, &params);
4966 if (err)
4967 return err;
4968
4969 params.vlan = get_vlan(info, rdev);
4970 if (IS_ERR(params.vlan))
4971 return PTR_ERR(params.vlan);
4972
Johannes Berga97f4422009-06-18 17:23:43 +02004973 switch (dev->ieee80211_ptr->iftype) {
4974 case NL80211_IFTYPE_AP:
4975 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004976 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004977 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004978 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004979 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004980 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004981 break;
4982 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004983 err = -EOPNOTSUPP;
4984 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004985 }
4986
Johannes Berg77ee7c82013-02-15 00:48:33 +01004987 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004988 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004989
Johannes Berg77ee7c82013-02-15 00:48:33 +01004990 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004991 if (params.vlan)
4992 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004993
Johannes Berg5727ef12007-12-19 02:03:34 +01004994 return err;
4995}
4996
4997static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4998{
Johannes Berg4c476992010-10-04 21:36:35 +02004999 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01005000 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02005001 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01005002 struct station_parameters params;
5003 u8 *mac_addr = NULL;
Johannes Bergbda95eb2015-11-26 16:26:13 +01005004 u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5005 BIT(NL80211_STA_FLAG_ASSOCIATED);
Johannes Berg5727ef12007-12-19 02:03:34 +01005006
5007 memset(&params, 0, sizeof(params));
5008
Johannes Berg984c3112013-02-14 23:43:25 +01005009 if (!rdev->ops->add_station)
5010 return -EOPNOTSUPP;
5011
Johannes Berg5727ef12007-12-19 02:03:34 +01005012 if (!info->attrs[NL80211_ATTR_MAC])
5013 return -EINVAL;
5014
Johannes Berg5727ef12007-12-19 02:03:34 +01005015 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5016 return -EINVAL;
5017
5018 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5019 return -EINVAL;
5020
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03005021 if (!info->attrs[NL80211_ATTR_STA_AID] &&
5022 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02005023 return -EINVAL;
5024
Johannes Berg5727ef12007-12-19 02:03:34 +01005025 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5026 params.supported_rates =
5027 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5028 params.supported_rates_len =
5029 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5030 params.listen_interval =
5031 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02005032
Ayala Beker17b94242016-03-17 15:41:38 +02005033 if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5034 u8 tmp;
5035
5036 tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5037 if (tmp >= NUM_NL80211_P2P_PS_STATUS)
5038 return -EINVAL;
5039
5040 params.support_p2p_ps = tmp;
5041 } else {
5042 /*
5043 * if not specified, assume it's supported for P2P GO interface,
5044 * and is NOT supported for AP interface
5045 */
5046 params.support_p2p_ps =
5047 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5048 }
5049
Jouni Malinen3d124ea2013-05-27 18:24:02 +03005050 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03005051 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03005052 else
5053 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02005054 if (!params.aid || params.aid > IEEE80211_MAX_AID)
5055 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02005056
Jouni Malinen9d62a982013-02-14 21:10:13 +02005057 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5058 params.capability =
5059 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5060 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5061 }
5062
5063 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5064 params.ext_capab =
5065 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5066 params.ext_capab_len =
5067 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5068 }
5069
Jouni Malinen36aedc92008-08-25 11:58:58 +03005070 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5071 params.ht_capa =
5072 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01005073
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00005074 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5075 params.vht_capa =
5076 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5077
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01005078 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5079 params.opmode_notif_used = true;
5080 params.opmode_notif =
5081 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5082 }
5083
Johannes Bergf8bacc22013-02-14 23:27:01 +01005084 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07005085 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01005086 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5087 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5088 return -EINVAL;
5089 }
Javier Cardona96b78df2011-04-07 15:08:33 -07005090
Sunil Duttc01fc9a2013-10-09 20:45:21 +05305091 err = nl80211_parse_sta_channel_info(info, &params);
5092 if (err)
5093 return err;
5094
Johannes Bergff276692013-02-15 00:09:01 +01005095 err = nl80211_parse_sta_wme(info, &params);
5096 if (err)
5097 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01005098
Johannes Bergbdd3ae32012-01-02 13:30:03 +01005099 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01005100 return -EINVAL;
5101
Johannes Berg496fcc22015-03-12 08:53:27 +02005102 /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5103 * as userspace might just pass through the capabilities from the IEs
5104 * directly, rather than enforcing this restriction and returning an
5105 * error in this case.
5106 */
5107 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5108 params.ht_capa = NULL;
5109 params.vht_capa = NULL;
5110 }
5111
Johannes Berg77ee7c82013-02-15 00:48:33 +01005112 /* When you run into this, adjust the code below for the new flag */
5113 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5114
Johannes Bergbdd90d52011-12-14 12:20:27 +01005115 switch (dev->ieee80211_ptr->iftype) {
5116 case NL80211_IFTYPE_AP:
5117 case NL80211_IFTYPE_AP_VLAN:
5118 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01005119 /* ignore WME attributes if iface/sta is not capable */
5120 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5121 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5122 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03005123
Johannes Bergbdd90d52011-12-14 12:20:27 +01005124 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03005125 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5126 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02005127 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01005128 /* but don't bother the driver with it */
5129 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03005130
Johannes Bergd582cff2012-10-26 17:53:44 +02005131 /* allow authenticated/associated only if driver handles it */
5132 if (!(rdev->wiphy.features &
5133 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
Johannes Bergbda95eb2015-11-26 16:26:13 +01005134 params.sta_flags_mask & auth_assoc)
Johannes Bergd582cff2012-10-26 17:53:44 +02005135 return -EINVAL;
5136
Johannes Bergbda95eb2015-11-26 16:26:13 +01005137 /* Older userspace, or userspace wanting to be compatible with
5138 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5139 * and assoc flags in the mask, but assumes the station will be
5140 * added as associated anyway since this was the required driver
5141 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5142 * introduced.
5143 * In order to not bother drivers with this quirk in the API
5144 * set the flags in both the mask and set for new stations in
5145 * this case.
5146 */
5147 if (!(params.sta_flags_mask & auth_assoc)) {
5148 params.sta_flags_mask |= auth_assoc;
5149 params.sta_flags_set |= auth_assoc;
5150 }
5151
Johannes Bergbdd90d52011-12-14 12:20:27 +01005152 /* must be last in here for error handling */
5153 params.vlan = get_vlan(info, rdev);
5154 if (IS_ERR(params.vlan))
5155 return PTR_ERR(params.vlan);
5156 break;
5157 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01005158 /* ignore uAPSD data */
5159 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5160
Johannes Bergd582cff2012-10-26 17:53:44 +02005161 /* associated is disallowed */
5162 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
5163 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01005164 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03005165 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5166 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02005167 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01005168 break;
5169 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01005170 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01005171 /* ignore uAPSD data */
5172 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5173
Johannes Berg77ee7c82013-02-15 00:48:33 +01005174 /* these are disallowed */
5175 if (params.sta_flags_mask &
5176 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
5177 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02005178 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01005179 /* Only TDLS peers can be added */
5180 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5181 return -EINVAL;
5182 /* Can only add if TDLS ... */
5183 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
5184 return -EOPNOTSUPP;
5185 /* ... with external setup is supported */
5186 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
5187 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01005188 /*
5189 * Older wpa_supplicant versions always mark the TDLS peer
5190 * as authorized, but it shouldn't yet be.
5191 */
5192 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01005193 break;
5194 default:
5195 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03005196 }
5197
Johannes Bergbdd90d52011-12-14 12:20:27 +01005198 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01005199
Hila Gonene35e4d22012-06-27 17:19:42 +03005200 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01005201
Johannes Berg5727ef12007-12-19 02:03:34 +01005202 if (params.vlan)
5203 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01005204 return err;
5205}
5206
5207static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
5208{
Johannes Berg4c476992010-10-04 21:36:35 +02005209 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5210 struct net_device *dev = info->user_ptr[1];
Jouni Malinen89c771e2014-10-10 20:52:40 +03005211 struct station_del_parameters params;
5212
5213 memset(&params, 0, sizeof(params));
Johannes Berg5727ef12007-12-19 02:03:34 +01005214
5215 if (info->attrs[NL80211_ATTR_MAC])
Jouni Malinen89c771e2014-10-10 20:52:40 +03005216 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg5727ef12007-12-19 02:03:34 +01005217
Johannes Berge80cf852009-05-11 14:43:13 +02005218 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02005219 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005220 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005221 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5222 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02005223
Johannes Berg4c476992010-10-04 21:36:35 +02005224 if (!rdev->ops->del_station)
5225 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01005226
Jouni Malinen98856862014-10-20 13:20:45 +03005227 if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
5228 params.subtype =
5229 nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
5230 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
5231 params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
5232 return -EINVAL;
5233 } else {
5234 /* Default to Deauthentication frame */
5235 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
5236 }
5237
5238 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
5239 params.reason_code =
5240 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5241 if (params.reason_code == 0)
5242 return -EINVAL; /* 0 is reserved */
5243 } else {
5244 /* Default to reason code 2 */
5245 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
5246 }
5247
Jouni Malinen89c771e2014-10-10 20:52:40 +03005248 return rdev_del_station(rdev, dev, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01005249}
5250
Eric W. Biederman15e47302012-09-07 20:12:54 +00005251static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005252 int flags, struct net_device *dev,
5253 u8 *dst, u8 *next_hop,
5254 struct mpath_info *pinfo)
5255{
5256 void *hdr;
5257 struct nlattr *pinfoattr;
5258
Henning Rogge1ef4c852014-11-04 16:14:58 +01005259 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005260 if (!hdr)
5261 return -1;
5262
David S. Miller9360ffd2012-03-29 04:41:26 -04005263 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5264 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
5265 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
5266 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
5267 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02005268
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005269 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
5270 if (!pinfoattr)
5271 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005272 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
5273 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
5274 pinfo->frame_qlen))
5275 goto nla_put_failure;
5276 if (((pinfo->filled & MPATH_INFO_SN) &&
5277 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
5278 ((pinfo->filled & MPATH_INFO_METRIC) &&
5279 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
5280 pinfo->metric)) ||
5281 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
5282 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
5283 pinfo->exptime)) ||
5284 ((pinfo->filled & MPATH_INFO_FLAGS) &&
5285 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
5286 pinfo->flags)) ||
5287 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
5288 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
5289 pinfo->discovery_timeout)) ||
5290 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
5291 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
5292 pinfo->discovery_retries)))
5293 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005294
5295 nla_nest_end(msg, pinfoattr);
5296
Johannes Berg053c0952015-01-16 22:09:00 +01005297 genlmsg_end(msg, hdr);
5298 return 0;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005299
5300 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07005301 genlmsg_cancel(msg, hdr);
5302 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005303}
5304
5305static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02005306 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005307{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005308 struct mpath_info pinfo;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08005309 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005310 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005311 u8 dst[ETH_ALEN];
5312 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02005313 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005314 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005315
Zhao, Gang1b8ec872014-04-21 12:53:02 +08005316 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005317 if (err)
5318 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02005319
Zhao, Gang1b8ec872014-04-21 12:53:02 +08005320 if (!rdev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02005321 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02005322 goto out_err;
5323 }
5324
Johannes Berg97990a02013-04-19 01:02:55 +02005325 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02005326 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02005327 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02005328 }
5329
Johannes Bergbba95fe2008-07-29 13:22:51 +02005330 while (1) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08005331 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
Johannes Berg97990a02013-04-19 01:02:55 +02005332 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02005333 if (err == -ENOENT)
5334 break;
5335 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01005336 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02005337
Eric W. Biederman15e47302012-09-07 20:12:54 +00005338 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02005339 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005340 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02005341 &pinfo) < 0)
5342 goto out;
5343
5344 path_idx++;
5345 }
5346
Johannes Bergbba95fe2008-07-29 13:22:51 +02005347 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005348 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02005349 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02005350 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08005351 nl80211_finish_wdev_dump(rdev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02005352 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005353}
5354
5355static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
5356{
Johannes Berg4c476992010-10-04 21:36:35 +02005357 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005358 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02005359 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005360 struct mpath_info pinfo;
5361 struct sk_buff *msg;
5362 u8 *dst = NULL;
5363 u8 next_hop[ETH_ALEN];
5364
5365 memset(&pinfo, 0, sizeof(pinfo));
5366
5367 if (!info->attrs[NL80211_ATTR_MAC])
5368 return -EINVAL;
5369
5370 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5371
Johannes Berg4c476992010-10-04 21:36:35 +02005372 if (!rdev->ops->get_mpath)
5373 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01005374
Johannes Berg4c476992010-10-04 21:36:35 +02005375 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5376 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005377
Hila Gonene35e4d22012-06-27 17:19:42 +03005378 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005379 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02005380 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005381
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005382 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005383 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02005384 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005385
Eric W. Biederman15e47302012-09-07 20:12:54 +00005386 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02005387 dev, dst, next_hop, &pinfo) < 0) {
5388 nlmsg_free(msg);
5389 return -ENOBUFS;
5390 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005391
Johannes Berg4c476992010-10-04 21:36:35 +02005392 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005393}
5394
5395static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
5396{
Johannes Berg4c476992010-10-04 21:36:35 +02005397 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5398 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005399 u8 *dst = NULL;
5400 u8 *next_hop = NULL;
5401
5402 if (!info->attrs[NL80211_ATTR_MAC])
5403 return -EINVAL;
5404
5405 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5406 return -EINVAL;
5407
5408 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5409 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5410
Johannes Berg4c476992010-10-04 21:36:35 +02005411 if (!rdev->ops->change_mpath)
5412 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01005413
Johannes Berg4c476992010-10-04 21:36:35 +02005414 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5415 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005416
Hila Gonene35e4d22012-06-27 17:19:42 +03005417 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005418}
Johannes Berg4c476992010-10-04 21:36:35 +02005419
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005420static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
5421{
Johannes Berg4c476992010-10-04 21:36:35 +02005422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5423 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005424 u8 *dst = NULL;
5425 u8 *next_hop = NULL;
5426
5427 if (!info->attrs[NL80211_ATTR_MAC])
5428 return -EINVAL;
5429
5430 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5431 return -EINVAL;
5432
5433 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5434 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5435
Johannes Berg4c476992010-10-04 21:36:35 +02005436 if (!rdev->ops->add_mpath)
5437 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01005438
Johannes Berg4c476992010-10-04 21:36:35 +02005439 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5440 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005441
Hila Gonene35e4d22012-06-27 17:19:42 +03005442 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005443}
5444
5445static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
5446{
Johannes Berg4c476992010-10-04 21:36:35 +02005447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5448 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005449 u8 *dst = NULL;
5450
5451 if (info->attrs[NL80211_ATTR_MAC])
5452 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5453
Johannes Berg4c476992010-10-04 21:36:35 +02005454 if (!rdev->ops->del_mpath)
5455 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01005456
Hila Gonene35e4d22012-06-27 17:19:42 +03005457 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005458}
5459
Henning Rogge66be7d22014-09-12 08:58:49 +02005460static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
5461{
5462 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5463 int err;
5464 struct net_device *dev = info->user_ptr[1];
5465 struct mpath_info pinfo;
5466 struct sk_buff *msg;
5467 u8 *dst = NULL;
5468 u8 mpp[ETH_ALEN];
5469
5470 memset(&pinfo, 0, sizeof(pinfo));
5471
5472 if (!info->attrs[NL80211_ATTR_MAC])
5473 return -EINVAL;
5474
5475 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5476
5477 if (!rdev->ops->get_mpp)
5478 return -EOPNOTSUPP;
5479
5480 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5481 return -EOPNOTSUPP;
5482
5483 err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
5484 if (err)
5485 return err;
5486
5487 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5488 if (!msg)
5489 return -ENOMEM;
5490
5491 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5492 dev, dst, mpp, &pinfo) < 0) {
5493 nlmsg_free(msg);
5494 return -ENOBUFS;
5495 }
5496
5497 return genlmsg_reply(msg, info);
5498}
5499
5500static int nl80211_dump_mpp(struct sk_buff *skb,
5501 struct netlink_callback *cb)
5502{
5503 struct mpath_info pinfo;
5504 struct cfg80211_registered_device *rdev;
5505 struct wireless_dev *wdev;
5506 u8 dst[ETH_ALEN];
5507 u8 mpp[ETH_ALEN];
5508 int path_idx = cb->args[2];
5509 int err;
5510
5511 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5512 if (err)
5513 return err;
5514
5515 if (!rdev->ops->dump_mpp) {
5516 err = -EOPNOTSUPP;
5517 goto out_err;
5518 }
5519
5520 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5521 err = -EOPNOTSUPP;
5522 goto out_err;
5523 }
5524
5525 while (1) {
5526 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
5527 mpp, &pinfo);
5528 if (err == -ENOENT)
5529 break;
5530 if (err)
5531 goto out_err;
5532
5533 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5534 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5535 wdev->netdev, dst, mpp,
5536 &pinfo) < 0)
5537 goto out;
5538
5539 path_idx++;
5540 }
5541
5542 out:
5543 cb->args[2] = path_idx;
5544 err = skb->len;
5545 out_err:
5546 nl80211_finish_wdev_dump(rdev);
5547 return err;
5548}
5549
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005550static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
5551{
Johannes Berg4c476992010-10-04 21:36:35 +02005552 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5553 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005554 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005555 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005556 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005557
5558 memset(&params, 0, sizeof(params));
5559 /* default to not changing parameters */
5560 params.use_cts_prot = -1;
5561 params.use_short_preamble = -1;
5562 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02005563 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01005564 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01005565 params.p2p_ctwindow = -1;
5566 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005567
5568 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
5569 params.use_cts_prot =
5570 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
5571 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
5572 params.use_short_preamble =
5573 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
5574 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
5575 params.use_short_slot_time =
5576 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02005577 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5578 params.basic_rates =
5579 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5580 params.basic_rates_len =
5581 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5582 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02005583 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
5584 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01005585 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
5586 params.ht_opmode =
5587 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005588
Johannes Berg53cabad2012-11-14 15:17:28 +01005589 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5590 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5591 return -EINVAL;
5592 params.p2p_ctwindow =
5593 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5594 if (params.p2p_ctwindow < 0)
5595 return -EINVAL;
5596 if (params.p2p_ctwindow != 0 &&
5597 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5598 return -EINVAL;
5599 }
5600
5601 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5602 u8 tmp;
5603
5604 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5605 return -EINVAL;
5606 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5607 if (tmp > 1)
5608 return -EINVAL;
5609 params.p2p_opp_ps = tmp;
5610 if (params.p2p_opp_ps &&
5611 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
5612 return -EINVAL;
5613 }
5614
Johannes Berg4c476992010-10-04 21:36:35 +02005615 if (!rdev->ops->change_bss)
5616 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005617
Johannes Berg074ac8d2010-09-16 14:58:22 +02005618 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02005619 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5620 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005621
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005622 wdev_lock(wdev);
5623 err = rdev_change_bss(rdev, dev, &params);
5624 wdev_unlock(wdev);
5625
5626 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005627}
5628
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005629static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
5630{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005631 char *data = NULL;
Ilan peer05050752015-03-04 00:32:06 -05005632 bool is_indoor;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005633 enum nl80211_user_reg_hint_type user_reg_hint_type;
Ilan peer05050752015-03-04 00:32:06 -05005634 u32 owner_nlportid;
5635
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05005636 /*
5637 * You should only get this when cfg80211 hasn't yet initialized
5638 * completely when built-in to the kernel right between the time
5639 * window between nl80211_init() and regulatory_init(), if that is
5640 * even possible.
5641 */
Johannes Berg458f4f92012-12-06 15:47:38 +01005642 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05005643 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05005644
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005645 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
5646 user_reg_hint_type =
5647 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
5648 else
5649 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
5650
5651 switch (user_reg_hint_type) {
5652 case NL80211_USER_REG_HINT_USER:
5653 case NL80211_USER_REG_HINT_CELL_BASE:
Ilan Peer52616f22014-02-25 16:26:00 +02005654 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5655 return -EINVAL;
5656
5657 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5658 return regulatory_hint_user(data, user_reg_hint_type);
5659 case NL80211_USER_REG_HINT_INDOOR:
Ilan peer05050752015-03-04 00:32:06 -05005660 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5661 owner_nlportid = info->snd_portid;
5662 is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
5663 } else {
5664 owner_nlportid = 0;
5665 is_indoor = true;
5666 }
5667
5668 return regulatory_hint_indoor(is_indoor, owner_nlportid);
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005669 default:
5670 return -EINVAL;
5671 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005672}
5673
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005674static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005675 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005676{
Johannes Berg4c476992010-10-04 21:36:35 +02005677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02005678 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005679 struct wireless_dev *wdev = dev->ieee80211_ptr;
5680 struct mesh_config cur_params;
5681 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005682 void *hdr;
5683 struct nlattr *pinfoattr;
5684 struct sk_buff *msg;
5685
Johannes Berg29cbe682010-12-03 09:20:44 +01005686 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5687 return -EOPNOTSUPP;
5688
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005689 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02005690 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02005691
Johannes Berg29cbe682010-12-03 09:20:44 +01005692 wdev_lock(wdev);
5693 /* If not connected, get default parameters */
5694 if (!wdev->mesh_id_len)
5695 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
5696 else
Hila Gonene35e4d22012-06-27 17:19:42 +03005697 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01005698 wdev_unlock(wdev);
5699
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005700 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02005701 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005702
5703 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005704 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005705 if (!msg)
5706 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00005707 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005708 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005709 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005710 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005711 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005712 if (!pinfoattr)
5713 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005714 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5715 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
5716 cur_params.dot11MeshRetryTimeout) ||
5717 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5718 cur_params.dot11MeshConfirmTimeout) ||
5719 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
5720 cur_params.dot11MeshHoldingTimeout) ||
5721 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5722 cur_params.dot11MeshMaxPeerLinks) ||
5723 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5724 cur_params.dot11MeshMaxRetries) ||
5725 nla_put_u8(msg, NL80211_MESHCONF_TTL,
5726 cur_params.dot11MeshTTL) ||
5727 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5728 cur_params.element_ttl) ||
5729 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5730 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04005731 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5732 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005733 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5734 cur_params.dot11MeshHWMPmaxPREQretries) ||
5735 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5736 cur_params.path_refresh_time) ||
5737 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5738 cur_params.min_discovery_timeout) ||
5739 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5740 cur_params.dot11MeshHWMPactivePathTimeout) ||
5741 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5742 cur_params.dot11MeshHWMPpreqMinInterval) ||
5743 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5744 cur_params.dot11MeshHWMPperrMinInterval) ||
5745 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5746 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5747 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5748 cur_params.dot11MeshHWMPRootMode) ||
5749 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5750 cur_params.dot11MeshHWMPRannInterval) ||
5751 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5752 cur_params.dot11MeshGateAnnouncementProtocol) ||
5753 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5754 cur_params.dot11MeshForwarding) ||
Masashi Honma335d5342017-03-16 10:57:17 +09005755 nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07005756 cur_params.rssi_threshold) ||
5757 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005758 cur_params.ht_opmode) ||
5759 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5760 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5761 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005762 cur_params.dot11MeshHWMProotInterval) ||
5763 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005764 cur_params.dot11MeshHWMPconfirmationInterval) ||
5765 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5766 cur_params.power_mode) ||
5767 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005768 cur_params.dot11MeshAwakeWindowDuration) ||
5769 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5770 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04005771 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005772 nla_nest_end(msg, pinfoattr);
5773 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005774 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005775
Johannes Berg3b858752009-03-12 09:55:09 +01005776 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005777 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005778 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04005779 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02005780 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005781}
5782
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005783static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005784 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5785 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5786 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5787 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5788 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5789 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01005790 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005791 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07005792 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005793 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5794 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5795 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5796 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5797 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08005798 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005799 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07005800 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07005801 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07005802 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08005803 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005804 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5805 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005806 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5807 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005808 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005809 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5810 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005811 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005812};
5813
Javier Cardonac80d5452010-12-16 17:37:49 -08005814static const struct nla_policy
5815 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07005816 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08005817 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5818 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07005819 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07005820 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005821 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07005822 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005823 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07005824 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08005825};
5826
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005827static int nl80211_check_bool(const struct nlattr *nla, u8 min, u8 max, bool *out)
5828{
5829 u8 val = nla_get_u8(nla);
5830 if (val < min || val > max)
5831 return -EINVAL;
5832 *out = val;
5833 return 0;
5834}
5835
5836static int nl80211_check_u8(const struct nlattr *nla, u8 min, u8 max, u8 *out)
5837{
5838 u8 val = nla_get_u8(nla);
5839 if (val < min || val > max)
5840 return -EINVAL;
5841 *out = val;
5842 return 0;
5843}
5844
5845static int nl80211_check_u16(const struct nlattr *nla, u16 min, u16 max, u16 *out)
5846{
5847 u16 val = nla_get_u16(nla);
5848 if (val < min || val > max)
5849 return -EINVAL;
5850 *out = val;
5851 return 0;
5852}
5853
5854static int nl80211_check_u32(const struct nlattr *nla, u32 min, u32 max, u32 *out)
5855{
5856 u32 val = nla_get_u32(nla);
5857 if (val < min || val > max)
5858 return -EINVAL;
5859 *out = val;
5860 return 0;
5861}
5862
5863static int nl80211_check_s32(const struct nlattr *nla, s32 min, s32 max, s32 *out)
5864{
5865 s32 val = nla_get_s32(nla);
5866 if (val < min || val > max)
5867 return -EINVAL;
5868 *out = val;
5869 return 0;
5870}
5871
Johannes Bergff9a71a2016-08-11 14:59:53 +02005872static int nl80211_check_power_mode(const struct nlattr *nla,
5873 enum nl80211_mesh_power_mode min,
5874 enum nl80211_mesh_power_mode max,
5875 enum nl80211_mesh_power_mode *out)
5876{
5877 u32 val = nla_get_u32(nla);
5878 if (val < min || val > max)
5879 return -EINVAL;
5880 *out = val;
5881 return 0;
5882}
5883
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005884static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005885 struct mesh_config *cfg,
5886 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005887{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005888 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005889 u32 mask = 0;
Masashi Honma97572352016-08-03 10:07:44 +09005890 u16 ht_opmode;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005891
Marco Porschea54fba2013-01-07 16:04:48 +01005892#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5893do { \
5894 if (tb[attr]) { \
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005895 if (fn(tb[attr], min, max, &cfg->param)) \
Marco Porschea54fba2013-01-07 16:04:48 +01005896 return -EINVAL; \
Marco Porschea54fba2013-01-07 16:04:48 +01005897 mask |= (1 << (attr - 1)); \
5898 } \
5899} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005900
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005901 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005902 return -EINVAL;
5903 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005904 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005905 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005906 return -EINVAL;
5907
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005908 /* This makes sure that there aren't more than 32 mesh config
5909 * parameters (otherwise our bitfield scheme would not work.) */
5910 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5911
5912 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01005913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005914 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005915 nl80211_check_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005917 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005918 nl80211_check_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005920 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005921 nl80211_check_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005923 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005924 nl80211_check_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005926 mask, NL80211_MESHCONF_MAX_RETRIES,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005927 nl80211_check_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005928 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005929 mask, NL80211_MESHCONF_TTL, nl80211_check_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005930 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005931 mask, NL80211_MESHCONF_ELEMENT_TTL,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005932 nl80211_check_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005933 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005934 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005935 nl80211_check_bool);
Marco Porschea54fba2013-01-07 16:04:48 +01005936 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
5937 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005938 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005939 nl80211_check_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005941 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005942 nl80211_check_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005944 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005945 nl80211_check_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005946 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005947 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005948 nl80211_check_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005949 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
5950 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005951 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005952 nl80211_check_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005953 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01005954 1, 65535, mask,
5955 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005956 nl80211_check_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08005957 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01005958 1, 65535, mask,
5959 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005960 nl80211_check_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005961 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005962 dot11MeshHWMPnetDiameterTraversalTime,
5963 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005964 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005965 nl80211_check_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005966 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
5967 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005968 nl80211_check_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005969 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
5970 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005971 nl80211_check_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00005972 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005973 dot11MeshGateAnnouncementProtocol, 0, 1,
5974 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005975 nl80211_check_bool);
Marco Porschea54fba2013-01-07 16:04:48 +01005976 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005977 mask, NL80211_MESHCONF_FORWARDING,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005978 nl80211_check_bool);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08005979 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005980 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02005981 nl80211_check_s32);
Masashi Honma97572352016-08-03 10:07:44 +09005982 /*
5983 * Check HT operation mode based on
5984 * IEEE 802.11 2012 8.4.2.59 HT Operation element.
5985 */
5986 if (tb[NL80211_MESHCONF_HT_OPMODE]) {
5987 ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
5988
5989 if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
5990 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
5991 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
5992 return -EINVAL;
5993
5994 if ((ht_opmode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT) &&
5995 (ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
5996 return -EINVAL;
5997
5998 switch (ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION) {
5999 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
6000 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
6001 if (ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT)
6002 return -EINVAL;
6003 break;
6004 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
6005 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
6006 if (!(ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6007 return -EINVAL;
6008 break;
6009 }
6010 cfg->ht_opmode = ht_opmode;
Masashi Honmafd551ba2017-01-26 08:56:13 +09006011 mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
Masashi Honma97572352016-08-03 10:07:44 +09006012 }
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08006013 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01006014 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08006015 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02006016 nl80211_check_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01006017 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08006018 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02006019 nl80211_check_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08006020 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01006021 dot11MeshHWMPconfirmationInterval,
6022 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08006023 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02006024 nl80211_check_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01006025 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
6026 NL80211_MESH_POWER_ACTIVE,
6027 NL80211_MESH_POWER_MAX,
6028 mask, NL80211_MESHCONF_POWER_MODE,
Johannes Bergff9a71a2016-08-11 14:59:53 +02006029 nl80211_check_power_mode);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01006030 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
6031 0, 65535, mask,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02006032 NL80211_MESHCONF_AWAKE_WINDOW, nl80211_check_u16);
Masashi Honma31f909a2015-02-24 22:42:16 +09006033 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07006034 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
Arnd Bergmannf151d9d2016-06-15 22:29:41 +02006035 nl80211_check_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01006036 if (mask_out)
6037 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08006038
Johannes Bergbd90fdc2010-12-03 09:20:43 +01006039 return 0;
6040
6041#undef FILL_IN_MESH_PARAM_IF_SET
6042}
6043
Javier Cardonac80d5452010-12-16 17:37:49 -08006044static int nl80211_parse_mesh_setup(struct genl_info *info,
6045 struct mesh_setup *setup)
6046{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08006047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08006048 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6049
6050 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6051 return -EINVAL;
6052 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
6053 info->attrs[NL80211_ATTR_MESH_SETUP],
6054 nl80211_mesh_setup_params_policy))
6055 return -EINVAL;
6056
Javier Cardonad299a1f2012-03-31 11:31:33 -07006057 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6058 setup->sync_method =
6059 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6060 IEEE80211_SYNC_METHOD_VENDOR :
6061 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6062
Javier Cardonac80d5452010-12-16 17:37:49 -08006063 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6064 setup->path_sel_proto =
6065 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6066 IEEE80211_PATH_PROTOCOL_VENDOR :
6067 IEEE80211_PATH_PROTOCOL_HWMP;
6068
6069 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6070 setup->path_metric =
6071 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6072 IEEE80211_PATH_METRIC_VENDOR :
6073 IEEE80211_PATH_METRIC_AIRTIME;
6074
Javier Cardona581a8b02011-04-07 15:08:27 -07006075 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08006076 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07006077 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08006078 if (!is_valid_ie_attr(ieattr))
6079 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07006080 setup->ie = nla_data(ieattr);
6081 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08006082 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08006083 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6084 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6085 return -EINVAL;
6086 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07006087 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6088 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08006089 if (setup->is_secure)
6090 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08006091
Colleen Twitty6e16d902013-05-08 11:45:59 -07006092 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6093 if (!setup->user_mpm)
6094 return -EINVAL;
6095 setup->auth_id =
6096 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6097 }
6098
Javier Cardonac80d5452010-12-16 17:37:49 -08006099 return 0;
6100}
6101
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006102static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01006103 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01006104{
6105 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6106 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01006107 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01006108 struct mesh_config cfg;
6109 u32 mask;
6110 int err;
6111
Johannes Berg29cbe682010-12-03 09:20:44 +01006112 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6113 return -EOPNOTSUPP;
6114
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006115 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01006116 return -EOPNOTSUPP;
6117
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006118 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01006119 if (err)
6120 return err;
6121
Johannes Berg29cbe682010-12-03 09:20:44 +01006122 wdev_lock(wdev);
6123 if (!wdev->mesh_id_len)
6124 err = -ENOLINK;
6125
6126 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03006127 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006128
6129 wdev_unlock(wdev);
6130
6131 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07006132}
6133
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006134static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6135 struct sk_buff *msg)
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006136{
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006137 struct nlattr *nl_reg_rules;
6138 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006139
Johannes Berg458f4f92012-12-06 15:47:38 +01006140 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6141 (regdom->dfs_region &&
6142 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006143 goto nla_put_failure;
Johannes Berg458f4f92012-12-06 15:47:38 +01006144
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006145 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
6146 if (!nl_reg_rules)
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006147 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006148
Johannes Berg458f4f92012-12-06 15:47:38 +01006149 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006150 struct nlattr *nl_reg_rule;
6151 const struct ieee80211_reg_rule *reg_rule;
6152 const struct ieee80211_freq_range *freq_range;
6153 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01006154 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006155
Johannes Berg458f4f92012-12-06 15:47:38 +01006156 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006157 freq_range = &reg_rule->freq_range;
6158 power_rule = &reg_rule->power_rule;
6159
6160 nl_reg_rule = nla_nest_start(msg, i);
6161 if (!nl_reg_rule)
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006162 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006163
Janusz Dziedzic97524822014-01-30 09:52:20 +01006164 max_bandwidth_khz = freq_range->max_bandwidth_khz;
6165 if (!max_bandwidth_khz)
6166 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6167 reg_rule);
6168
David S. Miller9360ffd2012-03-29 04:41:26 -04006169 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6170 reg_rule->flags) ||
6171 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6172 freq_range->start_freq_khz) ||
6173 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6174 freq_range->end_freq_khz) ||
6175 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01006176 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006177 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6178 power_rule->max_antenna_gain) ||
6179 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01006180 power_rule->max_eirp) ||
6181 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6182 reg_rule->dfs_cac_ms))
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006183 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006184
6185 nla_nest_end(msg, nl_reg_rule);
6186 }
6187
6188 nla_nest_end(msg, nl_reg_rules);
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006189 return 0;
6190
6191nla_put_failure:
6192 return -EMSGSIZE;
6193}
6194
6195static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6196{
6197 const struct ieee80211_regdomain *regdom = NULL;
6198 struct cfg80211_registered_device *rdev;
6199 struct wiphy *wiphy = NULL;
6200 struct sk_buff *msg;
6201 void *hdr;
6202
6203 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6204 if (!msg)
6205 return -ENOBUFS;
6206
6207 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6208 NL80211_CMD_GET_REG);
6209 if (!hdr)
6210 goto put_failure;
6211
6212 if (info->attrs[NL80211_ATTR_WIPHY]) {
Arik Nemtsov1bdd7162014-12-15 19:26:01 +02006213 bool self_managed;
6214
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006215 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
6216 if (IS_ERR(rdev)) {
6217 nlmsg_free(msg);
6218 return PTR_ERR(rdev);
6219 }
6220
6221 wiphy = &rdev->wiphy;
Arik Nemtsov1bdd7162014-12-15 19:26:01 +02006222 self_managed = wiphy->regulatory_flags &
6223 REGULATORY_WIPHY_SELF_MANAGED;
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006224 regdom = get_wiphy_regdom(wiphy);
6225
Arik Nemtsov1bdd7162014-12-15 19:26:01 +02006226 /* a self-managed-reg device must have a private regdom */
6227 if (WARN_ON(!regdom && self_managed)) {
6228 nlmsg_free(msg);
6229 return -EINVAL;
6230 }
6231
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006232 if (regdom &&
6233 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6234 goto nla_put_failure;
6235 }
6236
6237 if (!wiphy && reg_last_request_cell_base() &&
6238 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6239 NL80211_USER_REG_HINT_CELL_BASE))
6240 goto nla_put_failure;
6241
6242 rcu_read_lock();
6243
6244 if (!regdom)
6245 regdom = rcu_dereference(cfg80211_regdomain);
6246
6247 if (nl80211_put_regdom(regdom, msg))
6248 goto nla_put_failure_rcu;
6249
6250 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006251
6252 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02006253 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006254
Johannes Berg458f4f92012-12-06 15:47:38 +01006255nla_put_failure_rcu:
6256 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006257nla_put_failure:
6258 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01006259put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04006260 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02006261 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006262}
6263
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006264static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
6265 u32 seq, int flags, struct wiphy *wiphy,
6266 const struct ieee80211_regdomain *regdom)
6267{
6268 void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6269 NL80211_CMD_GET_REG);
6270
6271 if (!hdr)
6272 return -1;
6273
6274 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6275
6276 if (nl80211_put_regdom(regdom, msg))
6277 goto nla_put_failure;
6278
6279 if (!wiphy && reg_last_request_cell_base() &&
6280 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6281 NL80211_USER_REG_HINT_CELL_BASE))
6282 goto nla_put_failure;
6283
6284 if (wiphy &&
6285 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6286 goto nla_put_failure;
6287
Arik Nemtsov1bdd7162014-12-15 19:26:01 +02006288 if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
6289 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
6290 goto nla_put_failure;
6291
Johannes Berg053c0952015-01-16 22:09:00 +01006292 genlmsg_end(msg, hdr);
6293 return 0;
Arik Nemtsovad30ca22014-12-15 19:25:59 +02006294
6295nla_put_failure:
6296 genlmsg_cancel(msg, hdr);
6297 return -EMSGSIZE;
6298}
6299
6300static int nl80211_get_reg_dump(struct sk_buff *skb,
6301 struct netlink_callback *cb)
6302{
6303 const struct ieee80211_regdomain *regdom = NULL;
6304 struct cfg80211_registered_device *rdev;
6305 int err, reg_idx, start = cb->args[2];
6306
6307 rtnl_lock();
6308
6309 if (cfg80211_regdomain && start == 0) {
6310 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6311 NLM_F_MULTI, NULL,
6312 rtnl_dereference(cfg80211_regdomain));
6313 if (err < 0)
6314 goto out_err;
6315 }
6316
6317 /* the global regdom is idx 0 */
6318 reg_idx = 1;
6319 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
6320 regdom = get_wiphy_regdom(&rdev->wiphy);
6321 if (!regdom)
6322 continue;
6323
6324 if (++reg_idx <= start)
6325 continue;
6326
6327 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6328 NLM_F_MULTI, &rdev->wiphy, regdom);
6329 if (err < 0) {
6330 reg_idx--;
6331 break;
6332 }
6333 }
6334
6335 cb->args[2] = reg_idx;
6336 err = skb->len;
6337out_err:
6338 rtnl_unlock();
6339 return err;
6340}
6341
Johannes Bergb6863032015-10-15 09:25:18 +02006342#ifdef CONFIG_CFG80211_CRDA_SUPPORT
6343static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
6344 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6345 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6346 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6347 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6348 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6349 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6350 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
6351};
6352
6353static int parse_reg_rule(struct nlattr *tb[],
6354 struct ieee80211_reg_rule *reg_rule)
6355{
6356 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
6357 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
6358
6359 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
6360 return -EINVAL;
6361 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
6362 return -EINVAL;
6363 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
6364 return -EINVAL;
6365 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6366 return -EINVAL;
6367 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6368 return -EINVAL;
6369
6370 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
6371
6372 freq_range->start_freq_khz =
6373 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
6374 freq_range->end_freq_khz =
6375 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
6376 freq_range->max_bandwidth_khz =
6377 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
6378
6379 power_rule->max_eirp =
6380 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
6381
6382 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
6383 power_rule->max_antenna_gain =
6384 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
6385
6386 if (tb[NL80211_ATTR_DFS_CAC_TIME])
6387 reg_rule->dfs_cac_ms =
6388 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
6389
6390 return 0;
6391}
6392
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006393static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
6394{
6395 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
6396 struct nlattr *nl_reg_rule;
Johannes Bergea372c52014-11-28 14:54:31 +01006397 char *alpha2;
6398 int rem_reg_rules, r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006399 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01006400 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Johannes Bergea372c52014-11-28 14:54:31 +01006401 struct ieee80211_regdomain *rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006402
6403 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6404 return -EINVAL;
6405
6406 if (!info->attrs[NL80211_ATTR_REG_RULES])
6407 return -EINVAL;
6408
6409 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6410
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07006411 if (info->attrs[NL80211_ATTR_DFS_REGION])
6412 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
6413
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006414 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01006415 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006416 num_rules++;
6417 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04006418 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006419 }
6420
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08006421 if (!reg_is_valid_request(alpha2))
6422 return -EINVAL;
6423
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006424 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01006425 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006426
6427 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01006428 if (!rd)
6429 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006430
6431 rd->n_reg_rules = num_rules;
6432 rd->alpha2[0] = alpha2[0];
6433 rd->alpha2[1] = alpha2[1];
6434
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07006435 /*
6436 * Disable DFS master mode if the DFS region was
6437 * not supported or known on this kernel.
6438 */
6439 if (reg_supported_dfs_region(dfs_region))
6440 rd->dfs_region = dfs_region;
6441
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006442 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01006443 rem_reg_rules) {
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02006444 r = nla_parse_nested(tb, NL80211_REG_RULE_ATTR_MAX,
6445 nl_reg_rule, reg_rule_policy);
Johannes Bergae811e22014-01-24 10:17:47 +01006446 if (r)
6447 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006448 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
6449 if (r)
6450 goto bad_reg;
6451
6452 rule_idx++;
6453
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04006454 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
6455 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006456 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04006457 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006458 }
6459
Johannes Berg06627992016-06-09 10:40:09 +02006460 /* set_regdom takes ownership of rd */
6461 return set_regdom(rd, REGD_SOURCE_CRDA);
Johannes Bergd2372b32008-10-24 20:32:20 +02006462 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006463 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04006464 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006465}
Johannes Bergb6863032015-10-15 09:25:18 +02006466#endif /* CONFIG_CFG80211_CRDA_SUPPORT */
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006467
Johannes Berg83f5e2c2009-06-17 17:41:49 +02006468static int validate_scan_freqs(struct nlattr *freqs)
6469{
6470 struct nlattr *attr1, *attr2;
6471 int n_channels = 0, tmp1, tmp2;
6472
6473 nla_for_each_nested(attr1, freqs, tmp1) {
6474 n_channels++;
6475 /*
6476 * Some hardware has a limited channel list for
6477 * scanning, and it is pretty much nonsensical
6478 * to scan for a channel twice, so disallow that
6479 * and don't require drivers to check that the
6480 * channel list they get isn't longer than what
6481 * they can scan, as long as they can scan all
6482 * the channels they registered at once.
6483 */
6484 nla_for_each_nested(attr2, freqs, tmp2)
6485 if (attr1 != attr2 &&
6486 nla_get_u32(attr1) == nla_get_u32(attr2))
6487 return 0;
6488 }
6489
6490 return n_channels;
6491}
6492
Johannes Berg57fbcce2016-04-12 15:56:15 +02006493static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
Arend van Spriel38de03d2016-03-02 20:37:18 +01006494{
Johannes Berg57fbcce2016-04-12 15:56:15 +02006495 return b < NUM_NL80211_BANDS && wiphy->bands[b];
Arend van Spriel38de03d2016-03-02 20:37:18 +01006496}
6497
6498static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
6499 struct cfg80211_bss_selection *bss_select)
6500{
6501 struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
6502 struct nlattr *nest;
6503 int err;
6504 bool found = false;
6505 int i;
6506
6507 /* only process one nested attribute */
6508 nest = nla_data(nla);
6509 if (!nla_ok(nest, nla_len(nest)))
6510 return -EINVAL;
6511
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02006512 err = nla_parse_nested(attr, NL80211_BSS_SELECT_ATTR_MAX, nest,
6513 nl80211_bss_select_policy);
Arend van Spriel38de03d2016-03-02 20:37:18 +01006514 if (err)
6515 return err;
6516
6517 /* only one attribute may be given */
6518 for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
6519 if (attr[i]) {
6520 if (found)
6521 return -EINVAL;
6522 found = true;
6523 }
6524 }
6525
6526 bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
6527
6528 if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
6529 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
6530
6531 if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
6532 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
6533 bss_select->param.band_pref =
6534 nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
6535 if (!is_band_valid(wiphy, bss_select->param.band_pref))
6536 return -EINVAL;
6537 }
6538
6539 if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
6540 struct nl80211_bss_select_rssi_adjust *adj_param;
6541
6542 adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
6543 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
6544 bss_select->param.adjust.band = adj_param->band;
6545 bss_select->param.adjust.delta = adj_param->delta;
6546 if (!is_band_valid(wiphy, bss_select->param.adjust.band))
6547 return -EINVAL;
6548 }
6549
6550 /* user-space did not provide behaviour attribute */
6551 if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
6552 return -EINVAL;
6553
6554 if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
6555 return -EINVAL;
6556
6557 return 0;
6558}
6559
Johannes Bergad2b26a2014-06-12 21:39:05 +02006560static int nl80211_parse_random_mac(struct nlattr **attrs,
6561 u8 *mac_addr, u8 *mac_addr_mask)
6562{
6563 int i;
6564
6565 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
Joe Perchesd2beae12015-03-02 19:54:58 -08006566 eth_zero_addr(mac_addr);
6567 eth_zero_addr(mac_addr_mask);
Johannes Bergad2b26a2014-06-12 21:39:05 +02006568 mac_addr[0] = 0x2;
6569 mac_addr_mask[0] = 0x3;
6570
6571 return 0;
6572 }
6573
6574 /* need both or none */
6575 if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
6576 return -EINVAL;
6577
6578 memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
6579 memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
6580
6581 /* don't allow or configure an mcast address */
6582 if (!is_multicast_ether_addr(mac_addr_mask) ||
6583 is_multicast_ether_addr(mac_addr))
6584 return -EINVAL;
6585
6586 /*
6587 * allow users to pass a MAC address that has bits set outside
6588 * of the mask, but don't bother drivers with having to deal
6589 * with such bits
6590 */
6591 for (i = 0; i < ETH_ALEN; i++)
6592 mac_addr[i] &= mac_addr_mask[i];
6593
6594 return 0;
6595}
6596
Vasanthakumar Thiagarajan34373d12017-02-27 17:04:34 +05306597static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
6598{
6599 ASSERT_WDEV_LOCK(wdev);
6600
6601 if (!cfg80211_beaconing_iface_active(wdev))
6602 return true;
6603
6604 if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
6605 return true;
6606
6607 return regulatory_pre_cac_allowed(wdev->wiphy);
6608}
6609
Johannes Berg2a519312009-02-10 21:25:55 +01006610static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
6611{
Johannes Berg4c476992010-10-04 21:36:35 +02006612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02006613 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01006614 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01006615 struct nlattr *attr;
6616 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02006617 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02006618 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01006619
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006620 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6621 return -EINVAL;
6622
Johannes Berg79c97e92009-07-07 03:56:12 +02006623 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01006624
Ayala Bekercb3b7d82016-09-20 17:31:13 +03006625 if (wdev->iftype == NL80211_IFTYPE_NAN)
6626 return -EOPNOTSUPP;
6627
Johannes Berg4c476992010-10-04 21:36:35 +02006628 if (!rdev->ops->scan)
6629 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01006630
Johannes Bergf9d15d12014-01-22 11:14:19 +02006631 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01006632 err = -EBUSY;
6633 goto unlock;
6634 }
Johannes Berg2a519312009-02-10 21:25:55 +01006635
6636 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02006637 n_channels = validate_scan_freqs(
6638 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01006639 if (!n_channels) {
6640 err = -EINVAL;
6641 goto unlock;
6642 }
Johannes Berg2a519312009-02-10 21:25:55 +01006643 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02006644 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01006645 }
6646
6647 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
6648 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
6649 n_ssids++;
6650
Johannes Bergf9f47522013-03-19 15:04:07 +01006651 if (n_ssids > wiphy->max_scan_ssids) {
6652 err = -EINVAL;
6653 goto unlock;
6654 }
Johannes Berg2a519312009-02-10 21:25:55 +01006655
Jouni Malinen70692ad2009-02-16 19:39:13 +02006656 if (info->attrs[NL80211_ATTR_IE])
6657 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6658 else
6659 ie_len = 0;
6660
Johannes Bergf9f47522013-03-19 15:04:07 +01006661 if (ie_len > wiphy->max_scan_ie_len) {
6662 err = -EINVAL;
6663 goto unlock;
6664 }
Johannes Berg18a83652009-03-31 12:12:05 +02006665
Johannes Berg2a519312009-02-10 21:25:55 +01006666 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03006667 + sizeof(*request->ssids) * n_ssids
6668 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02006669 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01006670 if (!request) {
6671 err = -ENOMEM;
6672 goto unlock;
6673 }
Johannes Berg2a519312009-02-10 21:25:55 +01006674
Johannes Berg2a519312009-02-10 21:25:55 +01006675 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02006676 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01006677 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02006678 if (ie_len) {
Johannes Berg13874e42015-01-23 11:25:20 +01006679 if (n_ssids)
Jouni Malinen70692ad2009-02-16 19:39:13 +02006680 request->ie = (void *)(request->ssids + n_ssids);
6681 else
6682 request->ie = (void *)(request->channels + n_channels);
6683 }
Johannes Berg2a519312009-02-10 21:25:55 +01006684
Johannes Berg584991d2009-11-02 13:32:03 +01006685 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006686 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6687 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01006688 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01006689 struct ieee80211_channel *chan;
6690
6691 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
6692
6693 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01006694 err = -EINVAL;
6695 goto out_free;
6696 }
Johannes Berg584991d2009-11-02 13:32:03 +01006697
6698 /* ignore disabled channels */
6699 if (chan->flags & IEEE80211_CHAN_DISABLED)
6700 continue;
6701
6702 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01006703 i++;
6704 }
6705 } else {
Johannes Berg57fbcce2016-04-12 15:56:15 +02006706 enum nl80211_band band;
Johannes Berg34850ab2011-07-18 18:08:35 +02006707
Johannes Berg2a519312009-02-10 21:25:55 +01006708 /* all channels */
Johannes Berg57fbcce2016-04-12 15:56:15 +02006709 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg2a519312009-02-10 21:25:55 +01006710 int j;
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07006711
Johannes Berg2a519312009-02-10 21:25:55 +01006712 if (!wiphy->bands[band])
6713 continue;
6714 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01006715 struct ieee80211_channel *chan;
6716
6717 chan = &wiphy->bands[band]->channels[j];
6718
6719 if (chan->flags & IEEE80211_CHAN_DISABLED)
6720 continue;
6721
6722 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01006723 i++;
6724 }
6725 }
6726 }
6727
Johannes Berg584991d2009-11-02 13:32:03 +01006728 if (!i) {
6729 err = -EINVAL;
6730 goto out_free;
6731 }
6732
6733 request->n_channels = i;
6734
Vasanthakumar Thiagarajan34373d12017-02-27 17:04:34 +05306735 wdev_lock(wdev);
6736 if (!cfg80211_off_channel_oper_allowed(wdev)) {
6737 struct ieee80211_channel *chan;
6738
6739 if (request->n_channels != 1) {
6740 wdev_unlock(wdev);
6741 err = -EBUSY;
6742 goto out_free;
6743 }
6744
6745 chan = request->channels[0];
6746 if (chan->center_freq != wdev->chandef.chan->center_freq) {
6747 wdev_unlock(wdev);
6748 err = -EBUSY;
6749 goto out_free;
6750 }
6751 }
6752 wdev_unlock(wdev);
6753
Johannes Berg2a519312009-02-10 21:25:55 +01006754 i = 0;
Johannes Berg13874e42015-01-23 11:25:20 +01006755 if (n_ssids) {
Johannes Berg2a519312009-02-10 21:25:55 +01006756 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03006757 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01006758 err = -EINVAL;
6759 goto out_free;
6760 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03006761 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01006762 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01006763 i++;
6764 }
6765 }
6766
Jouni Malinen70692ad2009-02-16 19:39:13 +02006767 if (info->attrs[NL80211_ATTR_IE]) {
6768 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02006769 memcpy((void *)request->ie,
6770 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02006771 request->ie_len);
6772 }
6773
Johannes Berg57fbcce2016-04-12 15:56:15 +02006774 for (i = 0; i < NUM_NL80211_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02006775 if (wiphy->bands[i])
6776 request->rates[i] =
6777 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02006778
6779 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
6780 nla_for_each_nested(attr,
6781 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
6782 tmp) {
Johannes Berg57fbcce2016-04-12 15:56:15 +02006783 enum nl80211_band band = nla_type(attr);
Johannes Berg34850ab2011-07-18 18:08:35 +02006784
Johannes Berg57fbcce2016-04-12 15:56:15 +02006785 if (band < 0 || band >= NUM_NL80211_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02006786 err = -EINVAL;
6787 goto out_free;
6788 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01006789
6790 if (!wiphy->bands[band])
6791 continue;
6792
Johannes Berg34850ab2011-07-18 18:08:35 +02006793 err = ieee80211_get_ratemask(wiphy->bands[band],
6794 nla_data(attr),
6795 nla_len(attr),
6796 &request->rates[band]);
6797 if (err)
6798 goto out_free;
6799 }
6800 }
6801
Avraham Stern1d762502016-07-05 17:10:13 +03006802 if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
6803 if (!wiphy_ext_feature_isset(wiphy,
6804 NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
6805 err = -EOPNOTSUPP;
6806 goto out_free;
6807 }
6808
6809 request->duration =
6810 nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
6811 request->duration_mandatory =
6812 nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
6813 }
6814
Sam Leffler46856bb2012-10-11 21:03:32 -07006815 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07006816 request->flags = nla_get_u32(
6817 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02006818 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6819 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07006820 err = -EOPNOTSUPP;
6821 goto out_free;
6822 }
Johannes Bergad2b26a2014-06-12 21:39:05 +02006823
6824 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6825 if (!(wiphy->features &
6826 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)) {
6827 err = -EOPNOTSUPP;
6828 goto out_free;
6829 }
6830
6831 if (wdev->current_bss) {
6832 err = -EOPNOTSUPP;
6833 goto out_free;
6834 }
6835
6836 err = nl80211_parse_random_mac(info->attrs,
6837 request->mac_addr,
6838 request->mac_addr_mask);
6839 if (err)
6840 goto out_free;
6841 }
Sam Leffler46856bb2012-10-11 21:03:32 -07006842 }
Sam Lefflered4737712012-10-11 21:03:31 -07006843
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306844 request->no_cck =
6845 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6846
Vamsi Krishna2fa436b2016-12-02 23:59:08 +02006847 /* Initial implementation used NL80211_ATTR_MAC to set the specific
6848 * BSSID to scan for. This was problematic because that same attribute
6849 * was already used for another purpose (local random MAC address). The
6850 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
6851 * compatibility with older userspace components, also use the
6852 * NL80211_ATTR_MAC value here if it can be determined to be used for
6853 * the specific BSSID use case instead of the random MAC address
6854 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
6855 */
6856 if (info->attrs[NL80211_ATTR_BSSID])
6857 memcpy(request->bssid,
6858 nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
6859 else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
6860 info->attrs[NL80211_ATTR_MAC])
Jouni Malinen818965d2016-02-26 22:12:47 +02006861 memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
6862 ETH_ALEN);
6863 else
6864 eth_broadcast_addr(request->bssid);
6865
Johannes Bergfd014282012-06-18 19:17:03 +02006866 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02006867 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07006868 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01006869
Johannes Berg79c97e92009-07-07 03:56:12 +02006870 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03006871 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01006872
Johannes Berg463d0182009-07-14 00:33:35 +02006873 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02006874 nl80211_send_scan_start(rdev, wdev);
6875 if (wdev->netdev)
6876 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02006877 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01006878 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02006879 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01006880 kfree(request);
6881 }
Johannes Berg3b858752009-03-12 09:55:09 +01006882
Johannes Bergf9f47522013-03-19 15:04:07 +01006883 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01006884 return err;
6885}
6886
Vidyullatha Kanchanapally91d3ab42015-10-30 19:14:49 +05306887static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
6888{
6889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6890 struct wireless_dev *wdev = info->user_ptr[1];
6891
6892 if (!rdev->ops->abort_scan)
6893 return -EOPNOTSUPP;
6894
6895 if (rdev->scan_msg)
6896 return 0;
6897
6898 if (!rdev->scan_req)
6899 return -ENOENT;
6900
6901 rdev_abort_scan(rdev, wdev);
6902 return 0;
6903}
6904
Avraham Stern3b06d272015-10-12 09:51:34 +03006905static int
6906nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
6907 struct cfg80211_sched_scan_request *request,
6908 struct nlattr **attrs)
6909{
6910 int tmp, err, i = 0;
6911 struct nlattr *attr;
6912
6913 if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
6914 u32 interval;
6915
6916 /*
6917 * If scan plans are not specified,
Arend Van Spriel5a88de52016-11-17 09:02:40 +00006918 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
Avraham Stern3b06d272015-10-12 09:51:34 +03006919 * case one scan plan will be set with the specified scan
6920 * interval and infinite number of iterations.
6921 */
Avraham Stern3b06d272015-10-12 09:51:34 +03006922 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
6923 if (!interval)
6924 return -EINVAL;
6925
6926 request->scan_plans[0].interval =
6927 DIV_ROUND_UP(interval, MSEC_PER_SEC);
6928 if (!request->scan_plans[0].interval)
6929 return -EINVAL;
6930
6931 if (request->scan_plans[0].interval >
6932 wiphy->max_sched_scan_plan_interval)
6933 request->scan_plans[0].interval =
6934 wiphy->max_sched_scan_plan_interval;
6935
6936 return 0;
6937 }
6938
6939 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
6940 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
6941
6942 if (WARN_ON(i >= n_plans))
6943 return -EINVAL;
6944
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02006945 err = nla_parse_nested(plan, NL80211_SCHED_SCAN_PLAN_MAX,
6946 attr, nl80211_plan_policy);
Avraham Stern3b06d272015-10-12 09:51:34 +03006947 if (err)
6948 return err;
6949
6950 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
6951 return -EINVAL;
6952
6953 request->scan_plans[i].interval =
6954 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
6955 if (!request->scan_plans[i].interval ||
6956 request->scan_plans[i].interval >
6957 wiphy->max_sched_scan_plan_interval)
6958 return -EINVAL;
6959
6960 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
6961 request->scan_plans[i].iterations =
6962 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
6963 if (!request->scan_plans[i].iterations ||
6964 (request->scan_plans[i].iterations >
6965 wiphy->max_sched_scan_plan_iterations))
6966 return -EINVAL;
6967 } else if (i < n_plans - 1) {
6968 /*
6969 * All scan plans but the last one must specify
6970 * a finite number of iterations
6971 */
6972 return -EINVAL;
6973 }
6974
6975 i++;
6976 }
6977
6978 /*
6979 * The last scan plan must not specify the number of
6980 * iterations, it is supposed to run infinitely
6981 */
6982 if (request->scan_plans[n_plans - 1].iterations)
6983 return -EINVAL;
6984
6985 return 0;
6986}
6987
Luciano Coelho256da022014-11-10 16:13:46 +02006988static struct cfg80211_sched_scan_request *
Johannes Bergad2b26a2014-06-12 21:39:05 +02006989nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
Arend Van Sprielaad1e812017-01-27 12:27:44 +00006990 struct nlattr **attrs, int max_match_sets)
Luciano Coelho807f8a82011-05-11 17:09:35 +03006991{
6992 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006993 struct nlattr *attr;
Avraham Stern3b06d272015-10-12 09:51:34 +03006994 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02006995 enum nl80211_band band;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006996 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03006997 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01006998 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006999
Luciano Coelho256da022014-11-10 16:13:46 +02007000 if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
7001 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007002
Luciano Coelho256da022014-11-10 16:13:46 +02007003 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007004 n_channels = validate_scan_freqs(
Luciano Coelho256da022014-11-10 16:13:46 +02007005 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007006 if (!n_channels)
Luciano Coelho256da022014-11-10 16:13:46 +02007007 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007008 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02007009 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007010 }
7011
Luciano Coelho256da022014-11-10 16:13:46 +02007012 if (attrs[NL80211_ATTR_SCAN_SSIDS])
7013 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
Luciano Coelho807f8a82011-05-11 17:09:35 +03007014 tmp)
7015 n_ssids++;
7016
Luciano Coelho93b6aa62011-07-13 14:57:28 +03007017 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho256da022014-11-10 16:13:46 +02007018 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007019
Johannes Bergea73cbc2014-01-24 10:53:53 +01007020 /*
7021 * First, count the number of 'real' matchsets. Due to an issue with
7022 * the old implementation, matchsets containing only the RSSI attribute
7023 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7024 * RSSI for all matchsets, rather than their own matchset for reporting
7025 * all APs with a strong RSSI. This is needed to be compatible with
7026 * older userspace that treated a matchset with only the RSSI as the
7027 * global RSSI for all other matchsets - if there are other matchsets.
7028 */
Luciano Coelho256da022014-11-10 16:13:46 +02007029 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007030 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02007031 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01007032 tmp) {
7033 struct nlattr *rssi;
7034
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02007035 err = nla_parse_nested(tb,
7036 NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7037 attr, nl80211_match_policy);
Johannes Bergea73cbc2014-01-24 10:53:53 +01007038 if (err)
Luciano Coelho256da022014-11-10 16:13:46 +02007039 return ERR_PTR(err);
Johannes Bergea73cbc2014-01-24 10:53:53 +01007040 /* add other standalone attributes here */
7041 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
7042 n_match_sets++;
7043 continue;
7044 }
7045 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7046 if (rssi)
7047 default_match_rssi = nla_get_s32(rssi);
7048 }
7049 }
7050
7051 /* However, if there's no other matchset, add the RSSI one */
7052 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7053 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007054
Arend Van Sprielaad1e812017-01-27 12:27:44 +00007055 if (n_match_sets > max_match_sets)
Luciano Coelho256da022014-11-10 16:13:46 +02007056 return ERR_PTR(-EINVAL);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007057
Luciano Coelho256da022014-11-10 16:13:46 +02007058 if (attrs[NL80211_ATTR_IE])
7059 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007060 else
7061 ie_len = 0;
7062
Luciano Coelho5a865ba2011-07-13 14:57:29 +03007063 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho256da022014-11-10 16:13:46 +02007064 return ERR_PTR(-EINVAL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03007065
Avraham Stern3b06d272015-10-12 09:51:34 +03007066 if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7067 /*
7068 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7069 * each scan plan already specifies its own interval
7070 */
7071 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7072 return ERR_PTR(-EINVAL);
7073
7074 nla_for_each_nested(attr,
7075 attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7076 n_plans++;
7077 } else {
7078 /*
7079 * The scan interval attribute is kept for backward
7080 * compatibility. If no scan plans are specified and sched scan
7081 * interval is specified, one scan plan will be set with this
7082 * scan interval and infinite number of iterations.
7083 */
7084 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7085 return ERR_PTR(-EINVAL);
7086
7087 n_plans = 1;
7088 }
7089
7090 if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7091 return ERR_PTR(-EINVAL);
7092
vamsi krishnabf95ecd2017-01-13 01:12:20 +02007093 if (!wiphy_ext_feature_isset(
7094 wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7095 (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7096 attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
7097 return ERR_PTR(-EINVAL);
7098
Luciano Coelho807f8a82011-05-11 17:09:35 +03007099 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03007100 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007101 + sizeof(*request->match_sets) * n_match_sets
Avraham Stern3b06d272015-10-12 09:51:34 +03007102 + sizeof(*request->scan_plans) * n_plans
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03007103 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03007104 + ie_len, GFP_KERNEL);
Luciano Coelho256da022014-11-10 16:13:46 +02007105 if (!request)
7106 return ERR_PTR(-ENOMEM);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007107
7108 if (n_ssids)
7109 request->ssids = (void *)&request->channels[n_channels];
7110 request->n_ssids = n_ssids;
7111 if (ie_len) {
Johannes Berg13874e42015-01-23 11:25:20 +01007112 if (n_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007113 request->ie = (void *)(request->ssids + n_ssids);
7114 else
7115 request->ie = (void *)(request->channels + n_channels);
7116 }
7117
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007118 if (n_match_sets) {
7119 if (request->ie)
7120 request->match_sets = (void *)(request->ie + ie_len);
Johannes Berg13874e42015-01-23 11:25:20 +01007121 else if (n_ssids)
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007122 request->match_sets =
7123 (void *)(request->ssids + n_ssids);
7124 else
7125 request->match_sets =
7126 (void *)(request->channels + n_channels);
7127 }
7128 request->n_match_sets = n_match_sets;
7129
Avraham Stern3b06d272015-10-12 09:51:34 +03007130 if (n_match_sets)
7131 request->scan_plans = (void *)(request->match_sets +
7132 n_match_sets);
7133 else if (request->ie)
7134 request->scan_plans = (void *)(request->ie + ie_len);
7135 else if (n_ssids)
7136 request->scan_plans = (void *)(request->ssids + n_ssids);
7137 else
7138 request->scan_plans = (void *)(request->channels + n_channels);
7139
7140 request->n_scan_plans = n_plans;
7141
Luciano Coelho807f8a82011-05-11 17:09:35 +03007142 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02007143 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007144 /* user specified, bail out if channel not found */
7145 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02007146 attrs[NL80211_ATTR_SCAN_FREQUENCIES],
Luciano Coelho807f8a82011-05-11 17:09:35 +03007147 tmp) {
7148 struct ieee80211_channel *chan;
7149
7150 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7151
7152 if (!chan) {
7153 err = -EINVAL;
7154 goto out_free;
7155 }
7156
7157 /* ignore disabled channels */
7158 if (chan->flags & IEEE80211_CHAN_DISABLED)
7159 continue;
7160
7161 request->channels[i] = chan;
7162 i++;
7163 }
7164 } else {
7165 /* all channels */
Johannes Berg57fbcce2016-04-12 15:56:15 +02007166 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007167 int j;
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07007168
Luciano Coelho807f8a82011-05-11 17:09:35 +03007169 if (!wiphy->bands[band])
7170 continue;
7171 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7172 struct ieee80211_channel *chan;
7173
7174 chan = &wiphy->bands[band]->channels[j];
7175
7176 if (chan->flags & IEEE80211_CHAN_DISABLED)
7177 continue;
7178
7179 request->channels[i] = chan;
7180 i++;
7181 }
7182 }
7183 }
7184
7185 if (!i) {
7186 err = -EINVAL;
7187 goto out_free;
7188 }
7189
7190 request->n_channels = i;
7191
7192 i = 0;
Johannes Berg13874e42015-01-23 11:25:20 +01007193 if (n_ssids) {
Luciano Coelho256da022014-11-10 16:13:46 +02007194 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
Luciano Coelho807f8a82011-05-11 17:09:35 +03007195 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03007196 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007197 err = -EINVAL;
7198 goto out_free;
7199 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03007200 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007201 memcpy(request->ssids[i].ssid, nla_data(attr),
7202 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03007203 i++;
7204 }
7205 }
7206
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007207 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02007208 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007209 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02007210 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007211 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07007212 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007213
Johannes Bergbfe2c7b2016-10-26 14:42:21 +02007214 err = nla_parse_nested(tb,
7215 NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7216 attr, nl80211_match_policy);
Johannes Bergae811e22014-01-24 10:17:47 +01007217 if (err)
7218 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02007219 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007220 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01007221 if (WARN_ON(i >= n_match_sets)) {
7222 /* this indicates a programming error,
7223 * the loop above should have verified
7224 * things properly
7225 */
7226 err = -EINVAL;
7227 goto out_free;
7228 }
7229
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007230 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
7231 err = -EINVAL;
7232 goto out_free;
7233 }
7234 memcpy(request->match_sets[i].ssid.ssid,
7235 nla_data(ssid), nla_len(ssid));
7236 request->match_sets[i].ssid.ssid_len =
7237 nla_len(ssid);
Kirtika Ruchandani56ab3642016-05-29 19:54:10 -07007238 /* special attribute - old implementation w/a */
Johannes Bergea73cbc2014-01-24 10:53:53 +01007239 request->match_sets[i].rssi_thold =
7240 default_match_rssi;
7241 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7242 if (rssi)
7243 request->match_sets[i].rssi_thold =
7244 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007245 }
7246 i++;
7247 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01007248
7249 /* there was no other matchset, so the RSSI one is alone */
Luciano Coelhof89f46c2014-12-01 11:32:09 +02007250 if (i == 0 && n_match_sets)
Johannes Bergea73cbc2014-01-24 10:53:53 +01007251 request->match_sets[0].rssi_thold = default_match_rssi;
7252
7253 request->min_rssi_thold = INT_MAX;
7254 for (i = 0; i < n_match_sets; i++)
7255 request->min_rssi_thold =
7256 min(request->match_sets[i].rssi_thold,
7257 request->min_rssi_thold);
7258 } else {
7259 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03007260 }
7261
Johannes Berg9900e482014-02-04 21:01:25 +01007262 if (ie_len) {
7263 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007264 memcpy((void *)request->ie,
Luciano Coelho256da022014-11-10 16:13:46 +02007265 nla_data(attrs[NL80211_ATTR_IE]),
Luciano Coelho807f8a82011-05-11 17:09:35 +03007266 request->ie_len);
7267 }
7268
Luciano Coelho256da022014-11-10 16:13:46 +02007269 if (attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07007270 request->flags = nla_get_u32(
Luciano Coelho256da022014-11-10 16:13:46 +02007271 attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02007272 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7273 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07007274 err = -EOPNOTSUPP;
7275 goto out_free;
7276 }
Johannes Bergad2b26a2014-06-12 21:39:05 +02007277
7278 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7279 u32 flg = NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
7280
7281 if (!wdev) /* must be net-detect */
7282 flg = NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7283
7284 if (!(wiphy->features & flg)) {
7285 err = -EOPNOTSUPP;
7286 goto out_free;
7287 }
7288
7289 if (wdev && wdev->current_bss) {
7290 err = -EOPNOTSUPP;
7291 goto out_free;
7292 }
7293
7294 err = nl80211_parse_random_mac(attrs, request->mac_addr,
7295 request->mac_addr_mask);
7296 if (err)
7297 goto out_free;
7298 }
Sam Leffler46856bb2012-10-11 21:03:32 -07007299 }
Sam Lefflered4737712012-10-11 21:03:31 -07007300
Luciano Coelho9c748932015-01-16 16:04:09 +02007301 if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
7302 request->delay =
7303 nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
7304
vamsi krishnabf95ecd2017-01-13 01:12:20 +02007305 if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
7306 request->relative_rssi = nla_get_s8(
7307 attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
7308 request->relative_rssi_set = true;
7309 }
7310
7311 if (request->relative_rssi_set &&
7312 attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
7313 struct nl80211_bss_select_rssi_adjust *rssi_adjust;
7314
7315 rssi_adjust = nla_data(
7316 attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
7317 request->rssi_adjust.band = rssi_adjust->band;
7318 request->rssi_adjust.delta = rssi_adjust->delta;
7319 if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
7320 err = -EINVAL;
7321 goto out_free;
7322 }
7323 }
7324
Avraham Stern3b06d272015-10-12 09:51:34 +03007325 err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
7326 if (err)
7327 goto out_free;
7328
Sam Leffler15d60302012-10-11 21:03:34 -07007329 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007330
Luciano Coelho256da022014-11-10 16:13:46 +02007331 return request;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007332
7333out_free:
7334 kfree(request);
Luciano Coelho256da022014-11-10 16:13:46 +02007335 return ERR_PTR(err);
7336}
7337
7338static int nl80211_start_sched_scan(struct sk_buff *skb,
7339 struct genl_info *info)
7340{
7341 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7342 struct net_device *dev = info->user_ptr[1];
Johannes Bergad2b26a2014-06-12 21:39:05 +02007343 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007344 struct cfg80211_sched_scan_request *sched_scan_req;
Luciano Coelho256da022014-11-10 16:13:46 +02007345 int err;
7346
7347 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
7348 !rdev->ops->sched_scan_start)
7349 return -EOPNOTSUPP;
7350
7351 if (rdev->sched_scan_req)
7352 return -EINPROGRESS;
7353
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007354 sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
Arend Van Sprielaad1e812017-01-27 12:27:44 +00007355 info->attrs,
7356 rdev->wiphy.max_match_sets);
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007357
7358 err = PTR_ERR_OR_ZERO(sched_scan_req);
Luciano Coelho256da022014-11-10 16:13:46 +02007359 if (err)
7360 goto out_err;
7361
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007362 err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
Luciano Coelho256da022014-11-10 16:13:46 +02007363 if (err)
7364 goto out_free;
7365
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007366 sched_scan_req->dev = dev;
7367 sched_scan_req->wiphy = &rdev->wiphy;
7368
Jukka Rissanen93a1e862014-12-15 13:25:39 +02007369 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
7370 sched_scan_req->owner_nlportid = info->snd_portid;
7371
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007372 rcu_assign_pointer(rdev->sched_scan_req, sched_scan_req);
Luciano Coelho256da022014-11-10 16:13:46 +02007373
Arend Van Spriel96b08fd2017-04-13 13:06:27 +01007374 nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
Luciano Coelho256da022014-11-10 16:13:46 +02007375 return 0;
7376
7377out_free:
Jukka Rissanen31a60ed2014-12-15 13:25:38 +02007378 kfree(sched_scan_req);
Luciano Coelho256da022014-11-10 16:13:46 +02007379out_err:
Luciano Coelho807f8a82011-05-11 17:09:35 +03007380 return err;
7381}
7382
7383static int nl80211_stop_sched_scan(struct sk_buff *skb,
7384 struct genl_info *info)
7385{
7386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7387
7388 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
7389 !rdev->ops->sched_scan_stop)
7390 return -EOPNOTSUPP;
7391
Johannes Berg5fe231e2013-05-08 21:45:15 +02007392 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007393}
7394
Simon Wunderlich04f39042013-02-08 18:16:19 +01007395static int nl80211_start_radar_detection(struct sk_buff *skb,
7396 struct genl_info *info)
7397{
7398 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7399 struct net_device *dev = info->user_ptr[1];
7400 struct wireless_dev *wdev = dev->ieee80211_ptr;
7401 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01007402 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01007403 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01007404 int err;
7405
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01007406 dfs_region = reg_get_dfs_region(wdev->wiphy);
7407 if (dfs_region == NL80211_DFS_UNSET)
7408 return -EINVAL;
7409
Simon Wunderlich04f39042013-02-08 18:16:19 +01007410 err = nl80211_parse_chandef(rdev, info, &chandef);
7411 if (err)
7412 return err;
7413
Simon Wunderlichff311bc2013-09-03 19:43:18 +02007414 if (netif_carrier_ok(dev))
7415 return -EBUSY;
7416
Simon Wunderlich04f39042013-02-08 18:16:19 +01007417 if (wdev->cac_started)
7418 return -EBUSY;
7419
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02007420 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
Luciano Coelho00ec75f2014-05-15 13:05:39 +03007421 wdev->iftype);
Simon Wunderlich04f39042013-02-08 18:16:19 +01007422 if (err < 0)
7423 return err;
7424
7425 if (err == 0)
7426 return -EINVAL;
7427
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01007428 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01007429 return -EINVAL;
7430
7431 if (!rdev->ops->start_radar_detection)
7432 return -EOPNOTSUPP;
7433
Janusz Dziedzic31559f32014-02-21 19:46:13 +01007434 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
7435 if (WARN_ON(!cac_time_ms))
7436 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
7437
Ilan Peera1056b1b2015-10-22 22:27:46 +03007438 err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01007439 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01007440 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01007441 wdev->cac_started = true;
7442 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01007443 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01007444 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01007445 return err;
7446}
7447
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007448static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
7449{
7450 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7451 struct net_device *dev = info->user_ptr[1];
7452 struct wireless_dev *wdev = dev->ieee80211_ptr;
7453 struct cfg80211_csa_settings params;
7454 /* csa_attrs is defined static to avoid waste of stack size - this
7455 * function is called under RTNL lock, so this should not be a problem.
7456 */
7457 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007458 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007459 bool need_new_beacon = false;
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007460 int len, i;
Luciano Coelho252e07c2014-10-08 09:48:34 +03007461 u32 cs_count;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007462
7463 if (!rdev->ops->channel_switch ||
7464 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
7465 return -EOPNOTSUPP;
7466
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007467 switch (dev->ieee80211_ptr->iftype) {
7468 case NL80211_IFTYPE_AP:
7469 case NL80211_IFTYPE_P2P_GO:
7470 need_new_beacon = true;
7471
7472 /* useless if AP is not running */
7473 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01007474 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007475 break;
7476 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01007477 if (!wdev->ssid_len)
7478 return -ENOTCONN;
7479 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07007480 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01007481 if (!wdev->mesh_id_len)
7482 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007483 break;
7484 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007485 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007486 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007487
7488 memset(&params, 0, sizeof(params));
7489
7490 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7491 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
7492 return -EINVAL;
7493
7494 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02007495 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007496 return -EINVAL;
7497
Luciano Coelho252e07c2014-10-08 09:48:34 +03007498 /* Even though the attribute is u32, the specification says
7499 * u8, so let's make sure we don't overflow.
7500 */
7501 cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
7502 if (cs_count > 255)
7503 return -EINVAL;
7504
7505 params.count = cs_count;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007506
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007507 if (!need_new_beacon)
7508 goto skip_beacons;
7509
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007510 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
7511 if (err)
7512 return err;
7513
7514 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
7515 info->attrs[NL80211_ATTR_CSA_IES],
7516 nl80211_policy);
7517 if (err)
7518 return err;
7519
7520 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
7521 if (err)
7522 return err;
7523
7524 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
7525 return -EINVAL;
7526
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007527 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7528 if (!len || (len % sizeof(u16)))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007529 return -EINVAL;
7530
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007531 params.n_counter_offsets_beacon = len / sizeof(u16);
7532 if (rdev->wiphy.max_num_csa_counters &&
7533 (params.n_counter_offsets_beacon >
7534 rdev->wiphy.max_num_csa_counters))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007535 return -EINVAL;
7536
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007537 params.counter_offsets_beacon =
7538 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7539
7540 /* sanity checks - counters should fit and be the same */
7541 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
7542 u16 offset = params.counter_offsets_beacon[i];
7543
7544 if (offset >= params.beacon_csa.tail_len)
7545 return -EINVAL;
7546
7547 if (params.beacon_csa.tail[offset] != params.count)
7548 return -EINVAL;
7549 }
7550
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007551 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007552 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7553 if (!len || (len % sizeof(u16)))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007554 return -EINVAL;
7555
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007556 params.n_counter_offsets_presp = len / sizeof(u16);
7557 if (rdev->wiphy.max_num_csa_counters &&
Johannes Bergad5987b2016-09-13 15:53:55 +02007558 (params.n_counter_offsets_presp >
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007559 rdev->wiphy.max_num_csa_counters))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007560 return -EINVAL;
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03007561
7562 params.counter_offsets_presp =
7563 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7564
7565 /* sanity checks - counters should fit and be the same */
7566 for (i = 0; i < params.n_counter_offsets_presp; i++) {
7567 u16 offset = params.counter_offsets_presp[i];
7568
7569 if (offset >= params.beacon_csa.probe_resp_len)
7570 return -EINVAL;
7571
7572 if (params.beacon_csa.probe_resp[offset] !=
7573 params.count)
7574 return -EINVAL;
7575 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007576 }
7577
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02007578skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007579 err = nl80211_parse_chandef(rdev, info, &params.chandef);
7580 if (err)
7581 return err;
7582
Arik Nemtsov923b3522015-07-08 15:41:44 +03007583 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
7584 wdev->iftype))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007585 return -EINVAL;
7586
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02007587 err = cfg80211_chandef_dfs_required(wdev->wiphy,
7588 &params.chandef,
7589 wdev->iftype);
7590 if (err < 0)
7591 return err;
7592
Fabian Frederickdcc6c2f2014-10-25 17:57:35 +02007593 if (err > 0)
Luciano Coelho2beb6dab2014-02-18 11:40:36 +02007594 params.radar_required = true;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007595
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007596 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
7597 params.block_tx = true;
7598
Simon Wunderlichc56589e2013-11-21 18:19:49 +01007599 wdev_lock(wdev);
7600 err = rdev_channel_switch(rdev, dev, &params);
7601 wdev_unlock(wdev);
7602
7603 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02007604}
7605
Johannes Berg9720bb32011-06-21 09:45:33 +02007606static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
7607 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01007608 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02007609 struct wireless_dev *wdev,
7610 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01007611{
Johannes Berg48ab9052009-07-10 18:42:31 +02007612 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01007613 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01007614 void *hdr;
7615 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02007616
7617 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007618
Eric W. Biederman15e47302012-09-07 20:12:54 +00007619 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01007620 NL80211_CMD_NEW_SCAN_RESULTS);
7621 if (!hdr)
7622 return -1;
7623
Johannes Berg9720bb32011-06-21 09:45:33 +02007624 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
7625
Johannes Berg97990a02013-04-19 01:02:55 +02007626 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
7627 goto nla_put_failure;
7628 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007629 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
7630 goto nla_put_failure;
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007631 if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
7632 NL80211_ATTR_PAD))
Johannes Berg97990a02013-04-19 01:02:55 +02007633 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007634
7635 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
7636 if (!bss)
7637 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007638 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01007639 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007640 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01007641
7642 rcu_read_lock();
Johannes Berg0e227082014-08-12 20:34:30 +02007643 /* indicate whether we have probe response data or not */
7644 if (rcu_access_pointer(res->proberesp_ies) &&
7645 nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
7646 goto fail_unlock_rcu;
7647
7648 /* this pointer prefers to be pointed to probe response data
7649 * but is always valid
7650 */
Johannes Berg9caf0362012-11-29 01:25:20 +01007651 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01007652 if (ies) {
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007653 if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
7654 NL80211_BSS_PAD))
Johannes Berg8cef2c92013-02-05 16:54:31 +01007655 goto fail_unlock_rcu;
Johannes Berg8cef2c92013-02-05 16:54:31 +01007656 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
7657 ies->len, ies->data))
7658 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01007659 }
Johannes Berg0e227082014-08-12 20:34:30 +02007660
7661 /* and this pointer is always (unless driver didn't know) beacon data */
Johannes Berg9caf0362012-11-29 01:25:20 +01007662 ies = rcu_dereference(res->beacon_ies);
Johannes Berg0e227082014-08-12 20:34:30 +02007663 if (ies && ies->from_beacon) {
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007664 if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
7665 NL80211_BSS_PAD))
Johannes Berg8cef2c92013-02-05 16:54:31 +01007666 goto fail_unlock_rcu;
7667 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
7668 ies->len, ies->data))
7669 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01007670 }
7671 rcu_read_unlock();
7672
David S. Miller9360ffd2012-03-29 04:41:26 -04007673 if (res->beacon_interval &&
7674 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
7675 goto nla_put_failure;
7676 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
7677 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02007678 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04007679 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
7680 jiffies_to_msecs(jiffies - intbss->ts)))
7681 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007682
Avraham Stern1d762502016-07-05 17:10:13 +03007683 if (intbss->parent_tsf &&
7684 (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
7685 intbss->parent_tsf, NL80211_BSS_PAD) ||
7686 nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
7687 intbss->parent_bssid)))
7688 goto nla_put_failure;
7689
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02007690 if (intbss->ts_boottime &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007691 nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
7692 intbss->ts_boottime, NL80211_BSS_PAD))
Dmitry Shmidt6e19bc42015-10-07 11:32:53 +02007693 goto nla_put_failure;
7694
Johannes Berg77965c92009-02-18 18:45:06 +01007695 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01007696 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04007697 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
7698 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007699 break;
7700 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04007701 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
7702 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007703 break;
7704 default:
7705 break;
7706 }
7707
Johannes Berg48ab9052009-07-10 18:42:31 +02007708 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02007709 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02007710 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04007711 if (intbss == wdev->current_bss &&
7712 nla_put_u32(msg, NL80211_BSS_STATUS,
7713 NL80211_BSS_STATUS_ASSOCIATED))
7714 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02007715 break;
7716 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04007717 if (intbss == wdev->current_bss &&
7718 nla_put_u32(msg, NL80211_BSS_STATUS,
7719 NL80211_BSS_STATUS_IBSS_JOINED))
7720 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02007721 break;
7722 default:
7723 break;
7724 }
7725
Johannes Berg2a519312009-02-10 21:25:55 +01007726 nla_nest_end(msg, bss);
7727
Johannes Berg053c0952015-01-16 22:09:00 +01007728 genlmsg_end(msg, hdr);
7729 return 0;
Johannes Berg2a519312009-02-10 21:25:55 +01007730
Johannes Berg8cef2c92013-02-05 16:54:31 +01007731 fail_unlock_rcu:
7732 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01007733 nla_put_failure:
7734 genlmsg_cancel(msg, hdr);
7735 return -EMSGSIZE;
7736}
7737
Johannes Berg97990a02013-04-19 01:02:55 +02007738static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01007739{
Johannes Berg48ab9052009-07-10 18:42:31 +02007740 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01007741 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02007742 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02007743 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01007744 int err;
7745
Johannes Berg97990a02013-04-19 01:02:55 +02007746 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02007747 if (err)
7748 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01007749
Johannes Berg48ab9052009-07-10 18:42:31 +02007750 wdev_lock(wdev);
7751 spin_lock_bh(&rdev->bss_lock);
7752 cfg80211_bss_expire(rdev);
7753
Johannes Berg9720bb32011-06-21 09:45:33 +02007754 cb->seq = rdev->bss_generation;
7755
Johannes Berg48ab9052009-07-10 18:42:31 +02007756 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01007757 if (++idx <= start)
7758 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02007759 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01007760 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02007761 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007762 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02007763 break;
Johannes Berg2a519312009-02-10 21:25:55 +01007764 }
7765 }
7766
Johannes Berg48ab9052009-07-10 18:42:31 +02007767 spin_unlock_bh(&rdev->bss_lock);
7768 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007769
Johannes Berg97990a02013-04-19 01:02:55 +02007770 cb->args[2] = idx;
7771 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007772
Johannes Berg67748892010-10-04 21:14:06 +02007773 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01007774}
7775
Eric W. Biederman15e47302012-09-07 20:12:54 +00007776static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg11f78ac2014-11-14 16:43:50 +01007777 int flags, struct net_device *dev,
7778 bool allow_radio_stats,
7779 struct survey_info *survey)
Holger Schurig61fa7132009-11-11 12:25:40 +01007780{
7781 void *hdr;
7782 struct nlattr *infoattr;
7783
Johannes Berg11f78ac2014-11-14 16:43:50 +01007784 /* skip radio stats if userspace didn't request them */
7785 if (!survey->channel && !allow_radio_stats)
7786 return 0;
7787
Eric W. Biederman15e47302012-09-07 20:12:54 +00007788 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01007789 NL80211_CMD_NEW_SURVEY_RESULTS);
7790 if (!hdr)
7791 return -ENOMEM;
7792
David S. Miller9360ffd2012-03-29 04:41:26 -04007793 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
7794 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01007795
7796 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
7797 if (!infoattr)
7798 goto nla_put_failure;
7799
Johannes Berg11f78ac2014-11-14 16:43:50 +01007800 if (survey->channel &&
7801 nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
David S. Miller9360ffd2012-03-29 04:41:26 -04007802 survey->channel->center_freq))
7803 goto nla_put_failure;
7804
7805 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
7806 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
7807 goto nla_put_failure;
7808 if ((survey->filled & SURVEY_INFO_IN_USE) &&
7809 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
7810 goto nla_put_failure;
Johannes Berg4ed20be2014-11-14 16:35:34 +01007811 if ((survey->filled & SURVEY_INFO_TIME) &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007812 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
7813 survey->time, NL80211_SURVEY_INFO_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04007814 goto nla_put_failure;
Johannes Berg4ed20be2014-11-14 16:35:34 +01007815 if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007816 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
7817 survey->time_busy, NL80211_SURVEY_INFO_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04007818 goto nla_put_failure;
Johannes Berg4ed20be2014-11-14 16:35:34 +01007819 if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007820 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
7821 survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04007822 goto nla_put_failure;
Johannes Berg4ed20be2014-11-14 16:35:34 +01007823 if ((survey->filled & SURVEY_INFO_TIME_RX) &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007824 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
7825 survey->time_rx, NL80211_SURVEY_INFO_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04007826 goto nla_put_failure;
Johannes Berg4ed20be2014-11-14 16:35:34 +01007827 if ((survey->filled & SURVEY_INFO_TIME_TX) &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007828 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
7829 survey->time_tx, NL80211_SURVEY_INFO_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04007830 goto nla_put_failure;
Johannes Berg052536a2014-11-14 16:44:11 +01007831 if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02007832 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
7833 survey->time_scan, NL80211_SURVEY_INFO_PAD))
Johannes Berg052536a2014-11-14 16:44:11 +01007834 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01007835
7836 nla_nest_end(msg, infoattr);
7837
Johannes Berg053c0952015-01-16 22:09:00 +01007838 genlmsg_end(msg, hdr);
7839 return 0;
Holger Schurig61fa7132009-11-11 12:25:40 +01007840
7841 nla_put_failure:
7842 genlmsg_cancel(msg, hdr);
7843 return -EMSGSIZE;
7844}
7845
Johannes Berg11f78ac2014-11-14 16:43:50 +01007846static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
Holger Schurig61fa7132009-11-11 12:25:40 +01007847{
Johannes Bergc90c39d2016-10-24 14:40:01 +02007848 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
Holger Schurig61fa7132009-11-11 12:25:40 +01007849 struct survey_info survey;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08007850 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02007851 struct wireless_dev *wdev;
7852 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01007853 int res;
Johannes Berg11f78ac2014-11-14 16:43:50 +01007854 bool radio_stats;
Holger Schurig61fa7132009-11-11 12:25:40 +01007855
Zhao, Gang1b8ec872014-04-21 12:53:02 +08007856 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02007857 if (res)
7858 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01007859
Johannes Berg11f78ac2014-11-14 16:43:50 +01007860 /* prepare_wdev_dump parsed the attributes */
Johannes Bergc90c39d2016-10-24 14:40:01 +02007861 radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
Johannes Berg11f78ac2014-11-14 16:43:50 +01007862
Johannes Berg97990a02013-04-19 01:02:55 +02007863 if (!wdev->netdev) {
7864 res = -EINVAL;
7865 goto out_err;
7866 }
7867
Zhao, Gang1b8ec872014-04-21 12:53:02 +08007868 if (!rdev->ops->dump_survey) {
Holger Schurig61fa7132009-11-11 12:25:40 +01007869 res = -EOPNOTSUPP;
7870 goto out_err;
7871 }
7872
7873 while (1) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08007874 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01007875 if (res == -ENOENT)
7876 break;
7877 if (res)
7878 goto out_err;
7879
Johannes Berg11f78ac2014-11-14 16:43:50 +01007880 /* don't send disabled channels, but do send non-channel data */
7881 if (survey.channel &&
7882 survey.channel->flags & IEEE80211_CHAN_DISABLED) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07007883 survey_idx++;
7884 continue;
7885 }
7886
Holger Schurig61fa7132009-11-11 12:25:40 +01007887 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007888 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01007889 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg11f78ac2014-11-14 16:43:50 +01007890 wdev->netdev, radio_stats, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01007891 goto out;
7892 survey_idx++;
7893 }
7894
7895 out:
Johannes Berg97990a02013-04-19 01:02:55 +02007896 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01007897 res = skb->len;
7898 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08007899 nl80211_finish_wdev_dump(rdev);
Holger Schurig61fa7132009-11-11 12:25:40 +01007900 return res;
7901}
7902
Samuel Ortizb23aa672009-07-01 21:26:54 +02007903static bool nl80211_valid_wpa_versions(u32 wpa_versions)
7904{
7905 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
7906 NL80211_WPA_VERSION_2));
7907}
7908
Jouni Malinen636a5d32009-03-19 13:39:22 +02007909static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
7910{
Johannes Berg4c476992010-10-04 21:36:35 +02007911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7912 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02007913 struct ieee80211_channel *chan;
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03007914 const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
7915 int err, ssid_len, ie_len = 0, auth_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02007916 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02007917 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03007918 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02007919
Johannes Bergf4a11bb2009-03-27 12:40:28 +01007920 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7921 return -EINVAL;
7922
7923 if (!info->attrs[NL80211_ATTR_MAC])
7924 return -EINVAL;
7925
Jouni Malinen17780922009-03-27 20:52:47 +02007926 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
7927 return -EINVAL;
7928
Johannes Berg19957bb2009-07-02 17:20:43 +02007929 if (!info->attrs[NL80211_ATTR_SSID])
7930 return -EINVAL;
7931
7932 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7933 return -EINVAL;
7934
Johannes Bergfffd0932009-07-08 14:22:54 +02007935 err = nl80211_parse_key(info, &key);
7936 if (err)
7937 return err;
7938
7939 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02007940 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
7941 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02007942 if (!key.p.key || !key.p.key_len)
7943 return -EINVAL;
7944 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
7945 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
7946 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
7947 key.p.key_len != WLAN_KEY_LEN_WEP104))
7948 return -EINVAL;
Johannes Bergb6b55552016-09-13 16:25:58 +02007949 if (key.idx > 3)
Johannes Bergfffd0932009-07-08 14:22:54 +02007950 return -EINVAL;
7951 } else {
7952 key.p.key_len = 0;
7953 key.p.key = NULL;
7954 }
7955
Johannes Bergafea0b72010-08-10 09:46:42 +02007956 if (key.idx >= 0) {
7957 int i;
7958 bool ok = false;
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07007959
Johannes Bergafea0b72010-08-10 09:46:42 +02007960 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
7961 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
7962 ok = true;
7963 break;
7964 }
7965 }
Johannes Berg4c476992010-10-04 21:36:35 +02007966 if (!ok)
7967 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02007968 }
7969
Johannes Berg4c476992010-10-04 21:36:35 +02007970 if (!rdev->ops->auth)
7971 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02007972
Johannes Berg074ac8d2010-09-16 14:58:22 +02007973 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007974 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7975 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02007976
Johannes Berg19957bb2009-07-02 17:20:43 +02007977 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02007978 chan = nl80211_get_valid_chan(&rdev->wiphy,
7979 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7980 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02007981 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02007982
Johannes Berg19957bb2009-07-02 17:20:43 +02007983 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7984 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7985
7986 if (info->attrs[NL80211_ATTR_IE]) {
7987 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7988 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7989 }
7990
7991 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007992 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02007993 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02007994
Jouni Malinen63181062016-10-27 00:42:02 +03007995 if ((auth_type == NL80211_AUTHTYPE_SAE ||
7996 auth_type == NL80211_AUTHTYPE_FILS_SK ||
7997 auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
7998 auth_type == NL80211_AUTHTYPE_FILS_PK) &&
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03007999 !info->attrs[NL80211_ATTR_AUTH_DATA])
Jouni Malinene39e5b52012-09-30 19:29:39 +03008000 return -EINVAL;
8001
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03008002 if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
Jouni Malinen63181062016-10-27 00:42:02 +03008003 if (auth_type != NL80211_AUTHTYPE_SAE &&
8004 auth_type != NL80211_AUTHTYPE_FILS_SK &&
8005 auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
8006 auth_type != NL80211_AUTHTYPE_FILS_PK)
Jouni Malinene39e5b52012-09-30 19:29:39 +03008007 return -EINVAL;
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03008008 auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
8009 auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03008010 /* need to include at least Auth Transaction and Status Code */
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03008011 if (auth_data_len < 4)
Jouni Malinene39e5b52012-09-30 19:29:39 +03008012 return -EINVAL;
8013 }
8014
Jouni Malinend5cdfac2010-04-04 09:37:19 +03008015 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8016
Johannes Berg95de8172012-01-20 13:55:25 +01008017 /*
8018 * Since we no longer track auth state, ignore
8019 * requests to only change local state.
8020 */
8021 if (local_state_change)
8022 return 0;
8023
Johannes Berg91bf9b22013-05-15 17:44:01 +02008024 wdev_lock(dev->ieee80211_ptr);
8025 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
8026 ssid, ssid_len, ie, ie_len,
8027 key.p.key, key.p.key_len, key.idx,
Jouni Malinen11b6b5a2016-10-27 00:41:58 +03008028 auth_data, auth_data_len);
Johannes Berg91bf9b22013-05-15 17:44:01 +02008029 wdev_unlock(dev->ieee80211_ptr);
8030 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008031}
8032
Johannes Bergc0692b82010-08-27 14:26:53 +03008033static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
8034 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02008035 struct cfg80211_crypto_settings *settings,
8036 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008037{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02008038 memset(settings, 0, sizeof(*settings));
8039
Samuel Ortizb23aa672009-07-01 21:26:54 +02008040 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
8041
Johannes Bergc0692b82010-08-27 14:26:53 +03008042 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
8043 u16 proto;
Kirtika Ruchandani7a087e72016-05-29 19:51:23 -07008044
Johannes Bergc0692b82010-08-27 14:26:53 +03008045 proto = nla_get_u16(
8046 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
8047 settings->control_port_ethertype = cpu_to_be16(proto);
8048 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
8049 proto != ETH_P_PAE)
8050 return -EINVAL;
8051 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
8052 settings->control_port_no_encrypt = true;
8053 } else
8054 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
8055
Samuel Ortizb23aa672009-07-01 21:26:54 +02008056 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
8057 void *data;
8058 int len, i;
8059
8060 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8061 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8062 settings->n_ciphers_pairwise = len / sizeof(u32);
8063
8064 if (len % sizeof(u32))
8065 return -EINVAL;
8066
Johannes Berg3dc27d22009-07-02 21:36:37 +02008067 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008068 return -EINVAL;
8069
8070 memcpy(settings->ciphers_pairwise, data, len);
8071
8072 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03008073 if (!cfg80211_supported_cipher_suite(
8074 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008075 settings->ciphers_pairwise[i]))
8076 return -EINVAL;
8077 }
8078
8079 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
8080 settings->cipher_group =
8081 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03008082 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
8083 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02008084 return -EINVAL;
8085 }
8086
8087 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
8088 settings->wpa_versions =
8089 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
8090 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
8091 return -EINVAL;
8092 }
8093
8094 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
8095 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03008096 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008097
8098 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
8099 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
8100 settings->n_akm_suites = len / sizeof(u32);
8101
8102 if (len % sizeof(u32))
8103 return -EINVAL;
8104
Jouni Malinen1b9ca022011-09-21 16:13:07 +03008105 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
8106 return -EINVAL;
8107
Samuel Ortizb23aa672009-07-01 21:26:54 +02008108 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008109 }
8110
8111 return 0;
8112}
8113
Jouni Malinen636a5d32009-03-19 13:39:22 +02008114static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
8115{
Johannes Berg4c476992010-10-04 21:36:35 +02008116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8117 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02008118 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01008119 struct cfg80211_assoc_request req = {};
8120 const u8 *bssid, *ssid;
8121 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008122
Johannes Bergf4a11bb2009-03-27 12:40:28 +01008123 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8124 return -EINVAL;
8125
8126 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02008127 !info->attrs[NL80211_ATTR_SSID] ||
8128 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01008129 return -EINVAL;
8130
Johannes Berg4c476992010-10-04 21:36:35 +02008131 if (!rdev->ops->assoc)
8132 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008133
Johannes Berg074ac8d2010-09-16 14:58:22 +02008134 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02008135 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8136 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02008137
Johannes Berg19957bb2009-07-02 17:20:43 +02008138 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008139
Jouni Malinen664834d2014-01-15 00:01:44 +02008140 chan = nl80211_get_valid_chan(&rdev->wiphy,
8141 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8142 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02008143 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008144
Johannes Berg19957bb2009-07-02 17:20:43 +02008145 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8146 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008147
8148 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01008149 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8150 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008151 }
8152
Jouni Malinendc6382ce2009-05-06 22:09:37 +03008153 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02008154 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03008155 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02008156 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01008157 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02008158 else if (mfp != NL80211_MFP_NO)
8159 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03008160 }
8161
Johannes Berg3e5d7642009-07-07 14:37:26 +02008162 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01008163 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02008164
Ben Greear7e7c8922011-11-18 11:31:59 -08008165 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01008166 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08008167
8168 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01008169 memcpy(&req.ht_capa_mask,
8170 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8171 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08008172
8173 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01008174 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08008175 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01008176 memcpy(&req.ht_capa,
8177 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8178 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08008179 }
8180
Johannes Bergee2aca32013-02-21 17:36:01 +01008181 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01008182 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01008183
8184 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01008185 memcpy(&req.vht_capa_mask,
8186 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
8187 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01008188
8189 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01008190 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01008191 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01008192 memcpy(&req.vht_capa,
8193 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
8194 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01008195 }
8196
Assaf Kraussbab5ab72014-09-03 15:25:01 +03008197 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
Beni Lev0c9ca112016-02-17 20:30:00 +02008198 if (!((rdev->wiphy.features &
8199 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
8200 (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
8201 !wiphy_ext_feature_isset(&rdev->wiphy,
8202 NL80211_EXT_FEATURE_RRM))
Assaf Kraussbab5ab72014-09-03 15:25:01 +03008203 return -EINVAL;
8204 req.flags |= ASSOC_REQ_USE_RRM;
8205 }
8206
Jouni Malinen348bd452016-10-27 00:42:03 +03008207 if (info->attrs[NL80211_ATTR_FILS_KEK]) {
8208 req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
8209 req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
8210 if (!info->attrs[NL80211_ATTR_FILS_NONCES])
8211 return -EINVAL;
8212 req.fils_nonces =
8213 nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
8214 }
8215
Johannes Bergf62fab72013-02-21 20:09:09 +01008216 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02008217 if (!err) {
8218 wdev_lock(dev->ieee80211_ptr);
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05008219
Johannes Bergf62fab72013-02-21 20:09:09 +01008220 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
8221 ssid, ssid_len, &req);
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05008222
8223 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8224 dev->ieee80211_ptr->conn_owner_nlportid =
8225 info->snd_portid;
8226 memcpy(dev->ieee80211_ptr->disconnect_bssid,
8227 bssid, ETH_ALEN);
8228 }
8229
Johannes Berg91bf9b22013-05-15 17:44:01 +02008230 wdev_unlock(dev->ieee80211_ptr);
8231 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02008232
Jouni Malinen636a5d32009-03-19 13:39:22 +02008233 return err;
8234}
8235
8236static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
8237{
Johannes Berg4c476992010-10-04 21:36:35 +02008238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8239 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02008240 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02008241 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02008242 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03008243 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008244
Johannes Bergf4a11bb2009-03-27 12:40:28 +01008245 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8246 return -EINVAL;
8247
8248 if (!info->attrs[NL80211_ATTR_MAC])
8249 return -EINVAL;
8250
8251 if (!info->attrs[NL80211_ATTR_REASON_CODE])
8252 return -EINVAL;
8253
Johannes Berg4c476992010-10-04 21:36:35 +02008254 if (!rdev->ops->deauth)
8255 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008256
Johannes Berg074ac8d2010-09-16 14:58:22 +02008257 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02008258 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8259 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02008260
Johannes Berg19957bb2009-07-02 17:20:43 +02008261 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008262
Johannes Berg19957bb2009-07-02 17:20:43 +02008263 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8264 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01008265 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02008266 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02008267 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02008268
8269 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02008270 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8271 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008272 }
8273
Jouni Malinend5cdfac2010-04-04 09:37:19 +03008274 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8275
Johannes Berg91bf9b22013-05-15 17:44:01 +02008276 wdev_lock(dev->ieee80211_ptr);
8277 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
8278 local_state_change);
8279 wdev_unlock(dev->ieee80211_ptr);
8280 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008281}
8282
8283static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
8284{
Johannes Berg4c476992010-10-04 21:36:35 +02008285 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8286 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02008287 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02008288 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02008289 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03008290 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008291
Johannes Bergf4a11bb2009-03-27 12:40:28 +01008292 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8293 return -EINVAL;
8294
8295 if (!info->attrs[NL80211_ATTR_MAC])
8296 return -EINVAL;
8297
8298 if (!info->attrs[NL80211_ATTR_REASON_CODE])
8299 return -EINVAL;
8300
Johannes Berg4c476992010-10-04 21:36:35 +02008301 if (!rdev->ops->disassoc)
8302 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008303
Johannes Berg074ac8d2010-09-16 14:58:22 +02008304 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02008305 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8306 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02008307
Johannes Berg19957bb2009-07-02 17:20:43 +02008308 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008309
Johannes Berg19957bb2009-07-02 17:20:43 +02008310 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8311 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01008312 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02008313 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02008314 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02008315
8316 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02008317 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8318 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02008319 }
8320
Jouni Malinend5cdfac2010-04-04 09:37:19 +03008321 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8322
Johannes Berg91bf9b22013-05-15 17:44:01 +02008323 wdev_lock(dev->ieee80211_ptr);
8324 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
8325 local_state_change);
8326 wdev_unlock(dev->ieee80211_ptr);
8327 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02008328}
8329
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01008330static bool
8331nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
Johannes Berg57fbcce2016-04-12 15:56:15 +02008332 int mcast_rate[NUM_NL80211_BANDS],
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01008333 int rateval)
8334{
8335 struct wiphy *wiphy = &rdev->wiphy;
8336 bool found = false;
8337 int band, i;
8338
Johannes Berg57fbcce2016-04-12 15:56:15 +02008339 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01008340 struct ieee80211_supported_band *sband;
8341
8342 sband = wiphy->bands[band];
8343 if (!sband)
8344 continue;
8345
8346 for (i = 0; i < sband->n_bitrates; i++) {
8347 if (sband->bitrates[i].bitrate == rateval) {
8348 mcast_rate[band] = i + 1;
8349 found = true;
8350 break;
8351 }
8352 }
8353 }
8354
8355 return found;
8356}
8357
Johannes Berg04a773a2009-04-19 21:24:32 +02008358static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
8359{
Johannes Berg4c476992010-10-04 21:36:35 +02008360 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8361 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02008362 struct cfg80211_ibss_params ibss;
8363 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02008364 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02008365 int err;
8366
Johannes Berg8e30bc52009-04-22 17:45:38 +02008367 memset(&ibss, 0, sizeof(ibss));
8368
Johannes Berg04a773a2009-04-19 21:24:32 +02008369 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8370 return -EINVAL;
8371
Johannes Berg683b6d32012-11-08 21:25:48 +01008372 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02008373 !nla_len(info->attrs[NL80211_ATTR_SSID]))
8374 return -EINVAL;
8375
Johannes Berg8e30bc52009-04-22 17:45:38 +02008376 ibss.beacon_interval = 100;
8377
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05308378 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
Johannes Berg8e30bc52009-04-22 17:45:38 +02008379 ibss.beacon_interval =
8380 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05308381
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05308382 err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
8383 ibss.beacon_interval);
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05308384 if (err)
8385 return err;
Johannes Berg8e30bc52009-04-22 17:45:38 +02008386
Johannes Berg4c476992010-10-04 21:36:35 +02008387 if (!rdev->ops->join_ibss)
8388 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02008389
Johannes Berg4c476992010-10-04 21:36:35 +02008390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8391 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02008392
Johannes Berg79c97e92009-07-07 03:56:12 +02008393 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02008394
Johannes Berg39193492011-09-16 13:45:25 +02008395 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02008396 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02008397
8398 if (!is_valid_ether_addr(ibss.bssid))
8399 return -EINVAL;
8400 }
Johannes Berg04a773a2009-04-19 21:24:32 +02008401 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8402 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8403
8404 if (info->attrs[NL80211_ATTR_IE]) {
8405 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8406 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8407 }
8408
Johannes Berg683b6d32012-11-08 21:25:48 +01008409 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
8410 if (err)
8411 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01008412
Ilan Peer174e0cd2014-02-23 09:13:01 +02008413 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
8414 NL80211_IFTYPE_ADHOC))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01008415 return -EINVAL;
8416
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02008417 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02008418 case NL80211_CHAN_WIDTH_5:
8419 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02008420 case NL80211_CHAN_WIDTH_20_NOHT:
8421 break;
8422 case NL80211_CHAN_WIDTH_20:
8423 case NL80211_CHAN_WIDTH_40:
Janusz.Dziedzic@tieto.comffc11992015-02-21 16:52:39 +01008424 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8425 return -EINVAL;
8426 break;
8427 case NL80211_CHAN_WIDTH_80:
8428 case NL80211_CHAN_WIDTH_80P80:
8429 case NL80211_CHAN_WIDTH_160:
8430 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8431 return -EINVAL;
8432 if (!wiphy_ext_feature_isset(&rdev->wiphy,
8433 NL80211_EXT_FEATURE_VHT_IBSS))
8434 return -EINVAL;
8435 break;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02008436 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01008437 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02008438 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01008439
Johannes Berg04a773a2009-04-19 21:24:32 +02008440 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02008441 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02008442
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03008443 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8444 u8 *rates =
8445 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8446 int n_rates =
8447 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8448 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01008449 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03008450
Johannes Berg34850ab2011-07-18 18:08:35 +02008451 err = ieee80211_get_ratemask(sband, rates, n_rates,
8452 &ibss.basic_rates);
8453 if (err)
8454 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03008455 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01008456
Simon Wunderlich803768f2013-06-28 10:39:58 +02008457 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8458 memcpy(&ibss.ht_capa_mask,
8459 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8460 sizeof(ibss.ht_capa_mask));
8461
8462 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8463 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8464 return -EINVAL;
8465 memcpy(&ibss.ht_capa,
8466 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8467 sizeof(ibss.ht_capa));
8468 }
8469
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01008470 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8471 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
8472 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8473 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03008474
Johannes Berg4c476992010-10-04 21:36:35 +02008475 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05308476 bool no_ht = false;
8477
Johannes Berg4c476992010-10-04 21:36:35 +02008478 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05308479 info->attrs[NL80211_ATTR_KEYS],
8480 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02008481 if (IS_ERR(connkeys))
8482 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05308483
Johannes Berg3d9d1d62012-11-08 23:14:50 +01008484 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
8485 no_ht) {
Ola Olsson5e950a72016-02-11 01:00:22 +01008486 kzfree(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05308487 return -EINVAL;
8488 }
Johannes Berg4c476992010-10-04 21:36:35 +02008489 }
Johannes Berg04a773a2009-04-19 21:24:32 +02008490
Antonio Quartulli267335d2012-01-31 20:25:47 +01008491 ibss.control_port =
8492 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
8493
Simon Wunderlich5336fa82013-10-07 18:41:05 +02008494 ibss.userspace_handles_dfs =
8495 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
8496
Johannes Berg4c476992010-10-04 21:36:35 +02008497 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02008498 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +03008499 kzfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02008500 return err;
8501}
8502
8503static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
8504{
Johannes Berg4c476992010-10-04 21:36:35 +02008505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8506 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02008507
Johannes Berg4c476992010-10-04 21:36:35 +02008508 if (!rdev->ops->leave_ibss)
8509 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02008510
Johannes Berg4c476992010-10-04 21:36:35 +02008511 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8512 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02008513
Johannes Berg4c476992010-10-04 21:36:35 +02008514 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02008515}
8516
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008517static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
8518{
8519 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8520 struct net_device *dev = info->user_ptr[1];
Johannes Berg57fbcce2016-04-12 15:56:15 +02008521 int mcast_rate[NUM_NL80211_BANDS];
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008522 u32 nla_rate;
8523 int err;
8524
8525 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Bertold Van den Bergh876dc932015-08-05 16:02:21 +02008526 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
8527 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008528 return -EOPNOTSUPP;
8529
8530 if (!rdev->ops->set_mcast_rate)
8531 return -EOPNOTSUPP;
8532
8533 memset(mcast_rate, 0, sizeof(mcast_rate));
8534
8535 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
8536 return -EINVAL;
8537
8538 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
8539 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
8540 return -EINVAL;
8541
Ilan Peera1056b1b2015-10-22 22:27:46 +03008542 err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008543
8544 return err;
8545}
8546
Johannes Bergad7e7182013-11-13 13:37:47 +01008547static struct sk_buff *
8548__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
Ahmad Kholaif6c09e792015-02-26 15:26:53 +02008549 struct wireless_dev *wdev, int approxlen,
8550 u32 portid, u32 seq, enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01008551 enum nl80211_attrs attr,
8552 const struct nl80211_vendor_cmd_info *info,
8553 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01008554{
8555 struct sk_buff *skb;
8556 void *hdr;
8557 struct nlattr *data;
8558
8559 skb = nlmsg_new(approxlen + 100, gfp);
8560 if (!skb)
8561 return NULL;
8562
8563 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
8564 if (!hdr) {
8565 kfree_skb(skb);
8566 return NULL;
8567 }
8568
8569 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
8570 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01008571
8572 if (info) {
8573 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
8574 info->vendor_id))
8575 goto nla_put_failure;
8576 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
8577 info->subcmd))
8578 goto nla_put_failure;
8579 }
8580
Ahmad Kholaif6c09e792015-02-26 15:26:53 +02008581 if (wdev) {
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02008582 if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
8583 wdev_id(wdev), NL80211_ATTR_PAD))
Ahmad Kholaif6c09e792015-02-26 15:26:53 +02008584 goto nla_put_failure;
8585 if (wdev->netdev &&
8586 nla_put_u32(skb, NL80211_ATTR_IFINDEX,
8587 wdev->netdev->ifindex))
8588 goto nla_put_failure;
8589 }
8590
Johannes Bergad7e7182013-11-13 13:37:47 +01008591 data = nla_nest_start(skb, attr);
Johannes Berg76e1fb42016-09-14 09:55:57 +02008592 if (!data)
8593 goto nla_put_failure;
Johannes Bergad7e7182013-11-13 13:37:47 +01008594
8595 ((void **)skb->cb)[0] = rdev;
8596 ((void **)skb->cb)[1] = hdr;
8597 ((void **)skb->cb)[2] = data;
8598
8599 return skb;
8600
8601 nla_put_failure:
8602 kfree_skb(skb);
8603 return NULL;
8604}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008605
Johannes Berge03ad6e2014-01-01 17:22:30 +01008606struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
Ahmad Kholaif6c09e792015-02-26 15:26:53 +02008607 struct wireless_dev *wdev,
Johannes Berge03ad6e2014-01-01 17:22:30 +01008608 enum nl80211_commands cmd,
8609 enum nl80211_attrs attr,
8610 int vendor_event_idx,
8611 int approxlen, gfp_t gfp)
8612{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08008613 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berge03ad6e2014-01-01 17:22:30 +01008614 const struct nl80211_vendor_cmd_info *info;
8615
8616 switch (cmd) {
8617 case NL80211_CMD_TESTMODE:
8618 if (WARN_ON(vendor_event_idx != -1))
8619 return NULL;
8620 info = NULL;
8621 break;
8622 case NL80211_CMD_VENDOR:
8623 if (WARN_ON(vendor_event_idx < 0 ||
8624 vendor_event_idx >= wiphy->n_vendor_events))
8625 return NULL;
8626 info = &wiphy->vendor_events[vendor_event_idx];
8627 break;
8628 default:
8629 WARN_ON(1);
8630 return NULL;
8631 }
8632
Ahmad Kholaif6c09e792015-02-26 15:26:53 +02008633 return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
Johannes Berge03ad6e2014-01-01 17:22:30 +01008634 cmd, attr, info, gfp);
8635}
8636EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
8637
8638void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
8639{
8640 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
8641 void *hdr = ((void **)skb->cb)[1];
8642 struct nlattr *data = ((void **)skb->cb)[2];
8643 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
8644
Johannes Bergbd8c78e2014-07-30 14:55:26 +02008645 /* clear CB data for netlink core to own from now on */
8646 memset(skb->cb, 0, sizeof(skb->cb));
8647
Johannes Berge03ad6e2014-01-01 17:22:30 +01008648 nla_nest_end(skb, data);
8649 genlmsg_end(skb, hdr);
8650
8651 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
8652 mcgrp = NL80211_MCGRP_VENDOR;
8653
8654 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
8655 mcgrp, gfp);
8656}
8657EXPORT_SYMBOL(__cfg80211_send_event_skb);
8658
Johannes Bergaff89a92009-07-01 21:26:51 +02008659#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02008660static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
8661{
Johannes Berg4c476992010-10-04 21:36:35 +02008662 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03008663 struct wireless_dev *wdev =
8664 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02008665 int err;
8666
David Spinadelfc73f112013-07-31 18:04:15 +03008667 if (!rdev->ops->testmode_cmd)
8668 return -EOPNOTSUPP;
8669
8670 if (IS_ERR(wdev)) {
8671 err = PTR_ERR(wdev);
8672 if (err != -EINVAL)
8673 return err;
8674 wdev = NULL;
8675 } else if (wdev->wiphy != &rdev->wiphy) {
8676 return -EINVAL;
8677 }
8678
Johannes Bergaff89a92009-07-01 21:26:51 +02008679 if (!info->attrs[NL80211_ATTR_TESTDATA])
8680 return -EINVAL;
8681
Johannes Bergad7e7182013-11-13 13:37:47 +01008682 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03008683 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02008684 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
8685 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01008686 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02008687
Johannes Bergaff89a92009-07-01 21:26:51 +02008688 return err;
8689}
8690
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008691static int nl80211_testmode_dump(struct sk_buff *skb,
8692 struct netlink_callback *cb)
8693{
Johannes Berg00918d32011-12-13 17:22:05 +01008694 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008695 int err;
8696 long phy_idx;
8697 void *data = NULL;
8698 int data_len = 0;
8699
Johannes Berg5fe231e2013-05-08 21:45:15 +02008700 rtnl_lock();
8701
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008702 if (cb->args[0]) {
8703 /*
8704 * 0 is a valid index, but not valid for args[0],
8705 * so we need to offset by 1.
8706 */
8707 phy_idx = cb->args[0] - 1;
Luca Coelhoa4956dc2017-02-07 22:13:56 +02008708
8709 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
8710 if (!rdev) {
8711 err = -ENOENT;
8712 goto out_err;
8713 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008714 } else {
Johannes Bergc90c39d2016-10-24 14:40:01 +02008715 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8716
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008717 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
Johannes Bergc90c39d2016-10-24 14:40:01 +02008718 attrbuf, nl80211_fam.maxattr, nl80211_policy);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008719 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02008720 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01008721
Johannes Bergc90c39d2016-10-24 14:40:01 +02008722 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
Johannes Berg2bd7e352012-06-15 14:23:16 +02008723 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02008724 err = PTR_ERR(rdev);
8725 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01008726 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02008727 phy_idx = rdev->wiphy_idx;
Johannes Berg2bd7e352012-06-15 14:23:16 +02008728
Johannes Bergc90c39d2016-10-24 14:40:01 +02008729 if (attrbuf[NL80211_ATTR_TESTDATA])
8730 cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008731 }
8732
8733 if (cb->args[1]) {
8734 data = nla_data((void *)cb->args[1]);
8735 data_len = nla_len((void *)cb->args[1]);
8736 }
8737
Johannes Berg00918d32011-12-13 17:22:05 +01008738 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008739 err = -EOPNOTSUPP;
8740 goto out_err;
8741 }
8742
8743 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00008744 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008745 cb->nlh->nlmsg_seq, NLM_F_MULTI,
8746 NL80211_CMD_TESTMODE);
8747 struct nlattr *tmdata;
8748
Dan Carpentercb35fba2013-08-14 14:50:01 +03008749 if (!hdr)
8750 break;
8751
David S. Miller9360ffd2012-03-29 04:41:26 -04008752 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008753 genlmsg_cancel(skb, hdr);
8754 break;
8755 }
8756
8757 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
8758 if (!tmdata) {
8759 genlmsg_cancel(skb, hdr);
8760 break;
8761 }
Hila Gonene35e4d22012-06-27 17:19:42 +03008762 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008763 nla_nest_end(skb, tmdata);
8764
8765 if (err == -ENOBUFS || err == -ENOENT) {
8766 genlmsg_cancel(skb, hdr);
8767 break;
8768 } else if (err) {
8769 genlmsg_cancel(skb, hdr);
8770 goto out_err;
8771 }
8772
8773 genlmsg_end(skb, hdr);
8774 }
8775
8776 err = skb->len;
8777 /* see above */
8778 cb->args[0] = phy_idx + 1;
8779 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02008780 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008781 return err;
8782}
Johannes Bergaff89a92009-07-01 21:26:51 +02008783#endif
8784
Samuel Ortizb23aa672009-07-01 21:26:54 +02008785static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
8786{
Johannes Berg4c476992010-10-04 21:36:35 +02008787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8788 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02008789 struct cfg80211_connect_params connect;
8790 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02008791 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008792 int err;
8793
8794 memset(&connect, 0, sizeof(connect));
8795
8796 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8797 return -EINVAL;
8798
8799 if (!info->attrs[NL80211_ATTR_SSID] ||
8800 !nla_len(info->attrs[NL80211_ATTR_SSID]))
8801 return -EINVAL;
8802
8803 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
8804 connect.auth_type =
8805 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03008806 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
8807 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02008808 return -EINVAL;
8809 } else
8810 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
8811
8812 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
8813
Johannes Bergc0692b82010-08-27 14:26:53 +03008814 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02008815 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008816 if (err)
8817 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008818
Johannes Berg074ac8d2010-09-16 14:58:22 +02008819 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02008820 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8821 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008822
Johannes Berg79c97e92009-07-07 03:56:12 +02008823 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008824
Bala Shanmugam4486ea92012-03-07 17:27:12 +05308825 connect.bg_scan_period = -1;
8826 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
8827 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
8828 connect.bg_scan_period =
8829 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
8830 }
8831
Samuel Ortizb23aa672009-07-01 21:26:54 +02008832 if (info->attrs[NL80211_ATTR_MAC])
8833 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02008834 else if (info->attrs[NL80211_ATTR_MAC_HINT])
8835 connect.bssid_hint =
8836 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008837 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8838 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8839
8840 if (info->attrs[NL80211_ATTR_IE]) {
8841 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8842 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8843 }
8844
Jouni Malinencee00a92013-01-15 17:15:57 +02008845 if (info->attrs[NL80211_ATTR_USE_MFP]) {
8846 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
8847 if (connect.mfp != NL80211_MFP_REQUIRED &&
8848 connect.mfp != NL80211_MFP_NO)
8849 return -EINVAL;
8850 } else {
8851 connect.mfp = NL80211_MFP_NO;
8852 }
8853
Jouni Malinenba6fbac2016-03-29 13:53:27 +03008854 if (info->attrs[NL80211_ATTR_PREV_BSSID])
8855 connect.prev_bssid =
8856 nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
8857
Samuel Ortizb23aa672009-07-01 21:26:54 +02008858 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02008859 connect.channel = nl80211_get_valid_chan(
8860 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8861 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02008862 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02008863 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02008864 connect.channel_hint = nl80211_get_valid_chan(
8865 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
8866 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02008867 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008868 }
8869
Johannes Bergfffd0932009-07-08 14:22:54 +02008870 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
8871 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05308872 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02008873 if (IS_ERR(connkeys))
8874 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02008875 }
8876
Ben Greear7e7c8922011-11-18 11:31:59 -08008877 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
8878 connect.flags |= ASSOC_REQ_DISABLE_HT;
8879
8880 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8881 memcpy(&connect.ht_capa_mask,
8882 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8883 sizeof(connect.ht_capa_mask));
8884
8885 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08008886 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
Johannes Bergb47f6102014-09-10 13:39:54 +03008887 kzfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08008888 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08008889 }
Ben Greear7e7c8922011-11-18 11:31:59 -08008890 memcpy(&connect.ht_capa,
8891 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8892 sizeof(connect.ht_capa));
8893 }
8894
Johannes Bergee2aca32013-02-21 17:36:01 +01008895 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
8896 connect.flags |= ASSOC_REQ_DISABLE_VHT;
8897
8898 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8899 memcpy(&connect.vht_capa_mask,
8900 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
8901 sizeof(connect.vht_capa_mask));
8902
8903 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
8904 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
Johannes Bergb47f6102014-09-10 13:39:54 +03008905 kzfree(connkeys);
Johannes Bergee2aca32013-02-21 17:36:01 +01008906 return -EINVAL;
8907 }
8908 memcpy(&connect.vht_capa,
8909 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
8910 sizeof(connect.vht_capa));
8911 }
8912
Assaf Kraussbab5ab72014-09-03 15:25:01 +03008913 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
Beni Lev0c9ca112016-02-17 20:30:00 +02008914 if (!((rdev->wiphy.features &
8915 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
8916 (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
8917 !wiphy_ext_feature_isset(&rdev->wiphy,
8918 NL80211_EXT_FEATURE_RRM)) {
Ola Olsson707554b2015-12-11 21:04:52 +01008919 kzfree(connkeys);
Assaf Kraussbab5ab72014-09-03 15:25:01 +03008920 return -EINVAL;
Ola Olsson707554b2015-12-11 21:04:52 +01008921 }
Assaf Kraussbab5ab72014-09-03 15:25:01 +03008922 connect.flags |= ASSOC_REQ_USE_RRM;
8923 }
8924
Lior David34d50512016-01-28 10:58:25 +02008925 connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
Johannes Berg57fbcce2016-04-12 15:56:15 +02008926 if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
Lior David34d50512016-01-28 10:58:25 +02008927 kzfree(connkeys);
8928 return -EOPNOTSUPP;
8929 }
8930
Arend van Spriel38de03d2016-03-02 20:37:18 +01008931 if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
8932 /* bss selection makes no sense if bssid is set */
8933 if (connect.bssid) {
8934 kzfree(connkeys);
8935 return -EINVAL;
8936 }
8937
8938 err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
8939 wiphy, &connect.bss_select);
8940 if (err) {
8941 kzfree(connkeys);
8942 return err;
8943 }
8944 }
8945
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +03008946 if (wiphy_ext_feature_isset(&rdev->wiphy,
8947 NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
8948 info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
8949 info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
8950 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
8951 info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
8952 connect.fils_erp_username =
8953 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
8954 connect.fils_erp_username_len =
8955 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
8956 connect.fils_erp_realm =
8957 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
8958 connect.fils_erp_realm_len =
8959 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
8960 connect.fils_erp_next_seq_num =
8961 nla_get_u16(
8962 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
8963 connect.fils_erp_rrk =
8964 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
8965 connect.fils_erp_rrk_len =
8966 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
8967 } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
8968 info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
8969 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
8970 info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
8971 kzfree(connkeys);
8972 return -EINVAL;
8973 }
8974
Johannes Berg83739b02013-05-15 17:44:01 +02008975 wdev_lock(dev->ieee80211_ptr);
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05008976
Jouni Malinen4ce2bd9c42016-03-29 13:53:28 +03008977 err = cfg80211_connect(rdev, dev, &connect, connkeys,
8978 connect.prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +02008979 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +03008980 kzfree(connkeys);
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -05008981
8982 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8983 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
8984 if (connect.bssid)
8985 memcpy(dev->ieee80211_ptr->disconnect_bssid,
8986 connect.bssid, ETH_ALEN);
8987 else
8988 memset(dev->ieee80211_ptr->disconnect_bssid,
8989 0, ETH_ALEN);
8990 }
8991
8992 wdev_unlock(dev->ieee80211_ptr);
8993
Samuel Ortizb23aa672009-07-01 21:26:54 +02008994 return err;
8995}
8996
vamsi krishna088e8df2016-10-27 16:51:11 +03008997static int nl80211_update_connect_params(struct sk_buff *skb,
8998 struct genl_info *info)
8999{
9000 struct cfg80211_connect_params connect = {};
9001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9002 struct net_device *dev = info->user_ptr[1];
9003 struct wireless_dev *wdev = dev->ieee80211_ptr;
9004 u32 changed = 0;
9005 int ret;
9006
9007 if (!rdev->ops->update_connect_params)
9008 return -EOPNOTSUPP;
9009
9010 if (info->attrs[NL80211_ATTR_IE]) {
9011 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9012 return -EINVAL;
9013 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9014 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9015 changed |= UPDATE_ASSOC_IES;
9016 }
9017
9018 wdev_lock(dev->ieee80211_ptr);
9019 if (!wdev->current_bss)
9020 ret = -ENOLINK;
9021 else
9022 ret = rdev_update_connect_params(rdev, dev, &connect, changed);
9023 wdev_unlock(dev->ieee80211_ptr);
9024
9025 return ret;
9026}
9027
Samuel Ortizb23aa672009-07-01 21:26:54 +02009028static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
9029{
Johannes Berg4c476992010-10-04 21:36:35 +02009030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9031 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02009032 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02009033 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009034
9035 if (!info->attrs[NL80211_ATTR_REASON_CODE])
9036 reason = WLAN_REASON_DEAUTH_LEAVING;
9037 else
9038 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9039
9040 if (reason == 0)
9041 return -EINVAL;
9042
Johannes Berg074ac8d2010-09-16 14:58:22 +02009043 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02009044 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9045 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009046
Johannes Berg83739b02013-05-15 17:44:01 +02009047 wdev_lock(dev->ieee80211_ptr);
9048 ret = cfg80211_disconnect(rdev, dev, reason, true);
9049 wdev_unlock(dev->ieee80211_ptr);
9050 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009051}
9052
Johannes Berg463d0182009-07-14 00:33:35 +02009053static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
9054{
Johannes Berg4c476992010-10-04 21:36:35 +02009055 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02009056 struct net *net;
9057 int err;
Johannes Berg463d0182009-07-14 00:33:35 +02009058
Vadim Kochan4b681c82015-01-12 16:34:05 +02009059 if (info->attrs[NL80211_ATTR_PID]) {
9060 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
9061
9062 net = get_net_ns_by_pid(pid);
9063 } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
9064 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
9065
9066 net = get_net_ns_by_fd(fd);
9067 } else {
Johannes Berg463d0182009-07-14 00:33:35 +02009068 return -EINVAL;
Vadim Kochan4b681c82015-01-12 16:34:05 +02009069 }
Johannes Berg463d0182009-07-14 00:33:35 +02009070
Johannes Berg4c476992010-10-04 21:36:35 +02009071 if (IS_ERR(net))
9072 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02009073
9074 err = 0;
9075
9076 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02009077 if (!net_eq(wiphy_net(&rdev->wiphy), net))
9078 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02009079
Johannes Berg463d0182009-07-14 00:33:35 +02009080 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02009081 return err;
9082}
9083
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009084static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
9085{
Johannes Berg4c476992010-10-04 21:36:35 +02009086 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009087 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
9088 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02009089 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009090 struct cfg80211_pmksa pmksa;
9091
9092 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
9093
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009094 if (!info->attrs[NL80211_ATTR_PMKID])
9095 return -EINVAL;
9096
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009097 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +03009098
9099 if (info->attrs[NL80211_ATTR_MAC]) {
9100 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9101 } else if (info->attrs[NL80211_ATTR_SSID] &&
9102 info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
9103 (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
9104 info->attrs[NL80211_ATTR_PMK])) {
9105 pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9106 pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9107 pmksa.cache_id =
9108 nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
9109 } else {
9110 return -EINVAL;
9111 }
9112 if (info->attrs[NL80211_ATTR_PMK]) {
9113 pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9114 pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
9115 }
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009116
Johannes Berg074ac8d2010-09-16 14:58:22 +02009117 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02009118 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9119 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009120
9121 switch (info->genlhdr->cmd) {
9122 case NL80211_CMD_SET_PMKSA:
9123 rdev_ops = rdev->ops->set_pmksa;
9124 break;
9125 case NL80211_CMD_DEL_PMKSA:
9126 rdev_ops = rdev->ops->del_pmksa;
9127 break;
9128 default:
9129 WARN_ON(1);
9130 break;
9131 }
9132
Johannes Berg4c476992010-10-04 21:36:35 +02009133 if (!rdev_ops)
9134 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009135
Johannes Berg4c476992010-10-04 21:36:35 +02009136 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009137}
9138
9139static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
9140{
Johannes Berg4c476992010-10-04 21:36:35 +02009141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9142 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009143
Johannes Berg074ac8d2010-09-16 14:58:22 +02009144 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02009145 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9146 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009147
Johannes Berg4c476992010-10-04 21:36:35 +02009148 if (!rdev->ops->flush_pmksa)
9149 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009150
Hila Gonene35e4d22012-06-27 17:19:42 +03009151 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009152}
9153
Arik Nemtsov109086c2011-09-28 14:12:50 +03009154static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
9155{
9156 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9157 struct net_device *dev = info->user_ptr[1];
9158 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05309159 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03009160 u16 status_code;
9161 u8 *peer;
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03009162 bool initiator;
Arik Nemtsov109086c2011-09-28 14:12:50 +03009163
9164 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9165 !rdev->ops->tdls_mgmt)
9166 return -EOPNOTSUPP;
9167
9168 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
9169 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
9170 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
9171 !info->attrs[NL80211_ATTR_IE] ||
9172 !info->attrs[NL80211_ATTR_MAC])
9173 return -EINVAL;
9174
9175 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9176 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
9177 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
9178 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03009179 initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05309180 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
9181 peer_capability =
9182 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03009183
Hila Gonene35e4d22012-06-27 17:19:42 +03009184 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05309185 dialog_token, status_code, peer_capability,
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03009186 initiator,
Hila Gonene35e4d22012-06-27 17:19:42 +03009187 nla_data(info->attrs[NL80211_ATTR_IE]),
9188 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03009189}
9190
9191static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
9192{
9193 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9194 struct net_device *dev = info->user_ptr[1];
9195 enum nl80211_tdls_operation operation;
9196 u8 *peer;
9197
9198 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9199 !rdev->ops->tdls_oper)
9200 return -EOPNOTSUPP;
9201
9202 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
9203 !info->attrs[NL80211_ATTR_MAC])
9204 return -EINVAL;
9205
9206 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
9207 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9208
Hila Gonene35e4d22012-06-27 17:19:42 +03009209 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03009210}
9211
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009212static int nl80211_remain_on_channel(struct sk_buff *skb,
9213 struct genl_info *info)
9214{
Johannes Berg4c476992010-10-04 21:36:35 +02009215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02009216 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01009217 struct cfg80211_chan_def chandef;
Vasanthakumar Thiagarajan34373d12017-02-27 17:04:34 +05309218 const struct cfg80211_chan_def *compat_chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009219 struct sk_buff *msg;
9220 void *hdr;
9221 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01009222 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009223 int err;
9224
9225 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9226 !info->attrs[NL80211_ATTR_DURATION])
9227 return -EINVAL;
9228
9229 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9230
Johannes Berg7c4ef712011-11-18 15:33:48 +01009231 if (!rdev->ops->remain_on_channel ||
9232 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02009233 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009234
Johannes Bergebf348f2012-06-01 12:50:54 +02009235 /*
9236 * We should be on that channel for at least a minimum amount of
9237 * time (10ms) but no longer than the driver supports.
9238 */
9239 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9240 duration > rdev->wiphy.max_remain_on_channel_duration)
9241 return -EINVAL;
9242
Johannes Berg683b6d32012-11-08 21:25:48 +01009243 err = nl80211_parse_chandef(rdev, info, &chandef);
9244 if (err)
9245 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009246
Vasanthakumar Thiagarajan34373d12017-02-27 17:04:34 +05309247 wdev_lock(wdev);
9248 if (!cfg80211_off_channel_oper_allowed(wdev) &&
9249 !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
9250 compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
9251 &chandef);
9252 if (compat_chandef != &chandef) {
9253 wdev_unlock(wdev);
9254 return -EBUSY;
9255 }
9256 }
9257 wdev_unlock(wdev);
9258
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009259 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02009260 if (!msg)
9261 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009262
Eric W. Biederman15e47302012-09-07 20:12:54 +00009263 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009264 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03009265 if (!hdr) {
9266 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009267 goto free_msg;
9268 }
9269
Johannes Berg683b6d32012-11-08 21:25:48 +01009270 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
9271 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009272
9273 if (err)
9274 goto free_msg;
9275
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02009276 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9277 NL80211_ATTR_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04009278 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009279
9280 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02009281
9282 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009283
9284 nla_put_failure:
9285 err = -ENOBUFS;
9286 free_msg:
9287 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009288 return err;
9289}
9290
9291static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
9292 struct genl_info *info)
9293{
Johannes Berg4c476992010-10-04 21:36:35 +02009294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02009295 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009296 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009297
9298 if (!info->attrs[NL80211_ATTR_COOKIE])
9299 return -EINVAL;
9300
Johannes Berg4c476992010-10-04 21:36:35 +02009301 if (!rdev->ops->cancel_remain_on_channel)
9302 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009303
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009304 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9305
Hila Gonene35e4d22012-06-27 17:19:42 +03009306 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009307}
9308
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009309static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
9310 struct genl_info *info)
9311{
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009312 struct cfg80211_bitrate_mask mask;
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05309313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02009314 struct net_device *dev = info->user_ptr[1];
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05309315 int err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009316
Johannes Berg4c476992010-10-04 21:36:35 +02009317 if (!rdev->ops->set_bitrate_mask)
9318 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009319
Purushottam Kushwahaa7c7fbf2016-09-14 17:38:44 +05309320 err = nl80211_parse_tx_bitrate_mask(info, &mask);
9321 if (err)
9322 return err;
Janusz Dziedzic78693032013-12-03 09:50:44 +01009323
Hila Gonene35e4d22012-06-27 17:19:42 +03009324 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009325}
9326
Johannes Berg2e161f72010-08-12 15:38:38 +02009327static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02009328{
Johannes Berg4c476992010-10-04 21:36:35 +02009329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02009330 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02009331 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02009332
9333 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
9334 return -EINVAL;
9335
Johannes Berg2e161f72010-08-12 15:38:38 +02009336 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
9337 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02009338
Johannes Berg71bbc992012-06-15 15:30:18 +02009339 switch (wdev->iftype) {
9340 case NL80211_IFTYPE_STATION:
9341 case NL80211_IFTYPE_ADHOC:
9342 case NL80211_IFTYPE_P2P_CLIENT:
9343 case NL80211_IFTYPE_AP:
9344 case NL80211_IFTYPE_AP_VLAN:
9345 case NL80211_IFTYPE_MESH_POINT:
9346 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02009347 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02009348 break;
Ayala Bekercb3b7d82016-09-20 17:31:13 +03009349 case NL80211_IFTYPE_NAN:
Johannes Berg71bbc992012-06-15 15:30:18 +02009350 default:
Johannes Berg4c476992010-10-04 21:36:35 +02009351 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02009352 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009353
9354 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02009355 if (!rdev->ops->mgmt_tx)
9356 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02009357
Eric W. Biederman15e47302012-09-07 20:12:54 +00009358 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02009359 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
9360 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02009361}
9362
Johannes Berg2e161f72010-08-12 15:38:38 +02009363static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02009364{
Johannes Berg4c476992010-10-04 21:36:35 +02009365 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02009366 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01009367 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02009368 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01009369 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02009370 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01009371 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009372 struct cfg80211_mgmt_tx_params params = {
9373 .dont_wait_for_ack =
9374 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
9375 };
Jouni Malinen026331c2010-02-15 12:53:10 +02009376
Johannes Berg683b6d32012-11-08 21:25:48 +01009377 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02009378 return -EINVAL;
9379
Johannes Berg4c476992010-10-04 21:36:35 +02009380 if (!rdev->ops->mgmt_tx)
9381 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02009382
Johannes Berg71bbc992012-06-15 15:30:18 +02009383 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02009384 case NL80211_IFTYPE_P2P_DEVICE:
9385 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9386 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02009387 case NL80211_IFTYPE_STATION:
9388 case NL80211_IFTYPE_ADHOC:
9389 case NL80211_IFTYPE_P2P_CLIENT:
9390 case NL80211_IFTYPE_AP:
9391 case NL80211_IFTYPE_AP_VLAN:
9392 case NL80211_IFTYPE_MESH_POINT:
9393 case NL80211_IFTYPE_P2P_GO:
9394 break;
Ayala Bekercb3b7d82016-09-20 17:31:13 +03009395 case NL80211_IFTYPE_NAN:
Johannes Berg71bbc992012-06-15 15:30:18 +02009396 default:
Johannes Berg4c476992010-10-04 21:36:35 +02009397 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02009398 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009399
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009400 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01009401 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009402 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009403 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02009404
9405 /*
9406 * We should wait on the channel for at least a minimum amount
9407 * of time (10ms) but no longer than the driver supports.
9408 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009409 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9410 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02009411 return -EINVAL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009412 }
9413
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009414 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009415
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009416 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01009417 return -EINVAL;
9418
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009419 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05309420
Antonio Quartulliea141b752013-06-11 14:20:03 +02009421 /* get the channel if any has been specified, otherwise pass NULL to
9422 * the driver. The latter will use the current one
9423 */
9424 chandef.chan = NULL;
9425 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9426 err = nl80211_parse_chandef(rdev, info, &chandef);
9427 if (err)
9428 return err;
9429 }
9430
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009431 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02009432 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02009433
Vasanthakumar Thiagarajan34373d12017-02-27 17:04:34 +05309434 wdev_lock(wdev);
9435 if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
9436 wdev_unlock(wdev);
9437 return -EBUSY;
9438 }
9439 wdev_unlock(wdev);
9440
Andrei Otcheretianski34d22ce2014-05-09 14:11:44 +03009441 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
9442 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
9443
9444 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
9445 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9446 int i;
9447
9448 if (len % sizeof(u16))
9449 return -EINVAL;
9450
9451 params.n_csa_offsets = len / sizeof(u16);
9452 params.csa_offsets =
9453 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9454
9455 /* check that all the offsets fit the frame */
9456 for (i = 0; i < params.n_csa_offsets; i++) {
9457 if (params.csa_offsets[i] >= params.len)
9458 return -EINVAL;
9459 }
9460 }
9461
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009462 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01009463 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9464 if (!msg)
9465 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02009466
Eric W. Biederman15e47302012-09-07 20:12:54 +00009467 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01009468 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03009469 if (!hdr) {
9470 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01009471 goto free_msg;
9472 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009473 }
Johannes Berge247bd902011-11-04 11:18:21 +01009474
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02009475 params.chan = chandef.chan;
9476 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02009477 if (err)
9478 goto free_msg;
9479
Johannes Berge247bd902011-11-04 11:18:21 +01009480 if (msg) {
Nicolas Dichtel2dad6242016-04-25 10:25:22 +02009481 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9482 NL80211_ATTR_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -04009483 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009484
Johannes Berge247bd902011-11-04 11:18:21 +01009485 genlmsg_end(msg, hdr);
9486 return genlmsg_reply(msg, info);
9487 }
9488
9489 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02009490
9491 nla_put_failure:
9492 err = -ENOBUFS;
9493 free_msg:
9494 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02009495 return err;
9496}
9497
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009498static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
9499{
9500 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02009501 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009502 u64 cookie;
9503
9504 if (!info->attrs[NL80211_ATTR_COOKIE])
9505 return -EINVAL;
9506
9507 if (!rdev->ops->mgmt_tx_cancel_wait)
9508 return -EOPNOTSUPP;
9509
Johannes Berg71bbc992012-06-15 15:30:18 +02009510 switch (wdev->iftype) {
9511 case NL80211_IFTYPE_STATION:
9512 case NL80211_IFTYPE_ADHOC:
9513 case NL80211_IFTYPE_P2P_CLIENT:
9514 case NL80211_IFTYPE_AP:
9515 case NL80211_IFTYPE_AP_VLAN:
9516 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02009517 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02009518 break;
Ayala Bekercb3b7d82016-09-20 17:31:13 +03009519 case NL80211_IFTYPE_NAN:
Johannes Berg71bbc992012-06-15 15:30:18 +02009520 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009521 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02009522 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009523
9524 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9525
Hila Gonene35e4d22012-06-27 17:19:42 +03009526 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009527}
9528
Kalle Valoffb9eb32010-02-17 17:58:10 +02009529static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
9530{
Johannes Berg4c476992010-10-04 21:36:35 +02009531 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02009532 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009533 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02009534 u8 ps_state;
9535 bool state;
9536 int err;
9537
Johannes Berg4c476992010-10-04 21:36:35 +02009538 if (!info->attrs[NL80211_ATTR_PS_STATE])
9539 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009540
9541 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
9542
Johannes Berg4c476992010-10-04 21:36:35 +02009543 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
9544 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009545
9546 wdev = dev->ieee80211_ptr;
9547
Johannes Berg4c476992010-10-04 21:36:35 +02009548 if (!rdev->ops->set_power_mgmt)
9549 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009550
9551 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
9552
9553 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02009554 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009555
Hila Gonene35e4d22012-06-27 17:19:42 +03009556 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02009557 if (!err)
9558 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009559 return err;
9560}
9561
9562static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
9563{
Johannes Berg4c476992010-10-04 21:36:35 +02009564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02009565 enum nl80211_ps_state ps_state;
9566 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009567 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02009568 struct sk_buff *msg;
9569 void *hdr;
9570 int err;
9571
Kalle Valoffb9eb32010-02-17 17:58:10 +02009572 wdev = dev->ieee80211_ptr;
9573
Johannes Berg4c476992010-10-04 21:36:35 +02009574 if (!rdev->ops->set_power_mgmt)
9575 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009576
9577 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02009578 if (!msg)
9579 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009580
Eric W. Biederman15e47302012-09-07 20:12:54 +00009581 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009582 NL80211_CMD_GET_POWER_SAVE);
9583 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02009584 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009585 goto free_msg;
9586 }
9587
9588 if (wdev->ps)
9589 ps_state = NL80211_PS_ENABLED;
9590 else
9591 ps_state = NL80211_PS_DISABLED;
9592
David S. Miller9360ffd2012-03-29 04:41:26 -04009593 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
9594 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02009595
9596 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02009597 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02009598
Johannes Berg4c476992010-10-04 21:36:35 +02009599 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02009600 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02009601 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02009602 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02009603 return err;
9604}
9605
Johannes Berg94e860f2014-01-20 23:58:15 +01009606static const struct nla_policy
9607nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009608 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009609 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
9610 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07009611 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
9612 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
9613 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +01009614 [NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009615};
9616
Thomas Pedersen84f10702012-07-12 16:17:33 -07009617static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01009618 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07009619{
9620 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07009621 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02009622 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07009623
Johannes Bergd9d8b012012-11-26 12:51:52 +01009624 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07009625 return -EINVAL;
9626
Thomas Pedersen84f10702012-07-12 16:17:33 -07009627 if (!rdev->ops->set_cqm_txe_config)
9628 return -EOPNOTSUPP;
9629
9630 if (wdev->iftype != NL80211_IFTYPE_STATION &&
9631 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9632 return -EOPNOTSUPP;
9633
Hila Gonene35e4d22012-06-27 17:19:42 +03009634 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07009635}
9636
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009637static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
9638 struct net_device *dev)
9639{
9640 struct wireless_dev *wdev = dev->ieee80211_ptr;
9641 s32 last, low, high;
9642 u32 hyst;
9643 int i, n;
9644 int err;
9645
9646 /* RSSI reporting disabled? */
9647 if (!wdev->cqm_config)
9648 return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
9649
9650 /*
9651 * Obtain current RSSI value if possible, if not and no RSSI threshold
9652 * event has been received yet, we should receive an event after a
9653 * connection is established and enough beacons received to calculate
9654 * the average.
9655 */
9656 if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
9657 rdev->ops->get_station) {
9658 struct station_info sinfo;
9659 u8 *mac_addr;
9660
9661 mac_addr = wdev->current_bss->pub.bssid;
9662
9663 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
9664 if (err)
9665 return err;
9666
9667 if (sinfo.filled & BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
9668 wdev->cqm_config->last_rssi_event_value =
9669 (s8) sinfo.rx_beacon_signal_avg;
9670 }
9671
9672 last = wdev->cqm_config->last_rssi_event_value;
9673 hyst = wdev->cqm_config->rssi_hyst;
9674 n = wdev->cqm_config->n_rssi_thresholds;
9675
9676 for (i = 0; i < n; i++)
9677 if (last < wdev->cqm_config->rssi_thresholds[i])
9678 break;
9679
9680 low = i > 0 ?
9681 (wdev->cqm_config->rssi_thresholds[i - 1] - hyst) : S32_MIN;
9682 high = i < n ?
9683 (wdev->cqm_config->rssi_thresholds[i] + hyst - 1) : S32_MAX;
9684
9685 return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
9686}
9687
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009688static int nl80211_set_cqm_rssi(struct genl_info *info,
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009689 const s32 *thresholds, int n_thresholds,
9690 u32 hysteresis)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009691{
Johannes Berg4c476992010-10-04 21:36:35 +02009692 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02009693 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02009694 struct wireless_dev *wdev = dev->ieee80211_ptr;
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009695 int i, err;
9696 s32 prev = S32_MIN;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009697
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009698 /* Check all values negative and sorted */
9699 for (i = 0; i < n_thresholds; i++) {
9700 if (thresholds[i] > 0 || thresholds[i] <= prev)
9701 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009702
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009703 prev = thresholds[i];
9704 }
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009705
Johannes Berg074ac8d2010-09-16 14:58:22 +02009706 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02009707 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9708 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009709
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009710 wdev_lock(wdev);
9711 cfg80211_cqm_config_free(wdev);
9712 wdev_unlock(wdev);
9713
9714 if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
9715 if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
9716 return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
9717
9718 return rdev_set_cqm_rssi_config(rdev, dev,
9719 thresholds[0], hysteresis);
9720 }
9721
9722 if (!wiphy_ext_feature_isset(&rdev->wiphy,
9723 NL80211_EXT_FEATURE_CQM_RSSI_LIST))
9724 return -EOPNOTSUPP;
9725
9726 if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
9727 n_thresholds = 0;
9728
9729 wdev_lock(wdev);
9730 if (n_thresholds) {
9731 struct cfg80211_cqm_config *cqm_config;
9732
9733 cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
9734 n_thresholds * sizeof(s32), GFP_KERNEL);
9735 if (!cqm_config) {
9736 err = -ENOMEM;
9737 goto unlock;
9738 }
9739
9740 cqm_config->rssi_hyst = hysteresis;
9741 cqm_config->n_rssi_thresholds = n_thresholds;
9742 memcpy(cqm_config->rssi_thresholds, thresholds,
9743 n_thresholds * sizeof(s32));
9744
9745 wdev->cqm_config = cqm_config;
9746 }
9747
9748 err = cfg80211_cqm_rssi_update(rdev, dev);
9749
9750unlock:
9751 wdev_unlock(wdev);
9752
9753 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009754}
9755
9756static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
9757{
9758 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
9759 struct nlattr *cqm;
9760 int err;
9761
9762 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02009763 if (!cqm)
9764 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009765
9766 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
9767 nl80211_attr_cqm_policy);
9768 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02009769 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009770
9771 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
9772 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009773 const s32 *thresholds =
9774 nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
9775 int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
Johannes Berg1da5fcc2013-08-06 14:10:48 +02009776 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009777
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +01009778 if (len % 4)
9779 return -EINVAL;
9780
9781 return nl80211_set_cqm_rssi(info, thresholds, len / 4,
9782 hysteresis);
Johannes Berg1da5fcc2013-08-06 14:10:48 +02009783 }
9784
9785 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
9786 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
9787 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
9788 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
9789 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
9790 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
9791
9792 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
9793 }
9794
9795 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009796}
9797
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +01009798static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
9799{
9800 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9801 struct net_device *dev = info->user_ptr[1];
9802 struct ocb_setup setup = {};
9803 int err;
9804
9805 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
9806 if (err)
9807 return err;
9808
9809 return cfg80211_join_ocb(rdev, dev, &setup);
9810}
9811
9812static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
9813{
9814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9815 struct net_device *dev = info->user_ptr[1];
9816
9817 return cfg80211_leave_ocb(rdev, dev);
9818}
9819
Johannes Berg29cbe682010-12-03 09:20:44 +01009820static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
9821{
9822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9823 struct net_device *dev = info->user_ptr[1];
9824 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08009825 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01009826 int err;
9827
9828 /* start with default */
9829 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08009830 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01009831
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009832 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01009833 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009834 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01009835 if (err)
9836 return err;
9837 }
9838
9839 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
9840 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
9841 return -EINVAL;
9842
Javier Cardonac80d5452010-12-16 17:37:49 -08009843 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
9844 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
9845
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08009846 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9847 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
9848 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9849 return -EINVAL;
9850
Marco Porsch9bdbf042013-01-07 16:04:51 +01009851 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
9852 setup.beacon_interval =
9853 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05309854
Purushottam Kushwaha0c317a02016-10-12 18:26:51 +05309855 err = cfg80211_validate_beacon_int(rdev,
9856 NL80211_IFTYPE_MESH_POINT,
9857 setup.beacon_interval);
Purushottam Kushwaha12d20fc92016-08-11 15:14:02 +05309858 if (err)
9859 return err;
Marco Porsch9bdbf042013-01-07 16:04:51 +01009860 }
9861
9862 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
9863 setup.dtim_period =
9864 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
9865 if (setup.dtim_period < 1 || setup.dtim_period > 100)
9866 return -EINVAL;
9867 }
9868
Javier Cardonac80d5452010-12-16 17:37:49 -08009869 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
9870 /* parse additional setup parameters if given */
9871 err = nl80211_parse_mesh_setup(info, &setup);
9872 if (err)
9873 return err;
9874 }
9875
Thomas Pedersend37bb182013-03-04 13:06:13 -08009876 if (setup.user_mpm)
9877 cfg.auto_open_plinks = false;
9878
Johannes Bergcc1d2802012-05-16 23:50:20 +02009879 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01009880 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
9881 if (err)
9882 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02009883 } else {
9884 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01009885 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02009886 }
9887
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07009888 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9889 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9890 int n_rates =
9891 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9892 struct ieee80211_supported_band *sband;
9893
9894 if (!setup.chandef.chan)
9895 return -EINVAL;
9896
9897 sband = rdev->wiphy.bands[setup.chandef.chan->band];
9898
9899 err = ieee80211_get_ratemask(sband, rates, n_rates,
9900 &setup.basic_rates);
9901 if (err)
9902 return err;
9903 }
9904
Johannes Berg8564e382016-09-19 09:44:44 +02009905 if (info->attrs[NL80211_ATTR_TX_RATES]) {
9906 err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
9907 if (err)
9908 return err;
9909
9910 err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
9911 &setup.beacon_rate);
9912 if (err)
9913 return err;
9914 }
9915
Javier Cardonac80d5452010-12-16 17:37:49 -08009916 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01009917}
9918
9919static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
9920{
9921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9922 struct net_device *dev = info->user_ptr[1];
9923
9924 return cfg80211_leave_mesh(rdev, dev);
9925}
9926
Johannes Bergdfb89c52012-06-27 09:23:48 +02009927#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08009928static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
9929 struct cfg80211_registered_device *rdev)
9930{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009931 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08009932 struct nlattr *nl_pats, *nl_pat;
9933 int i, pat_len;
9934
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009935 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08009936 return 0;
9937
9938 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
9939 if (!nl_pats)
9940 return -ENOBUFS;
9941
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009942 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08009943 nl_pat = nla_nest_start(msg, i + 1);
9944 if (!nl_pat)
9945 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009946 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07009947 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009948 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07009949 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
9950 wowlan->patterns[i].pattern) ||
9951 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009952 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08009953 return -ENOBUFS;
9954 nla_nest_end(msg, nl_pat);
9955 }
9956 nla_nest_end(msg, nl_pats);
9957
9958 return 0;
9959}
9960
Johannes Berg2a0e0472013-01-23 22:57:40 +01009961static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
9962 struct cfg80211_wowlan_tcp *tcp)
9963{
9964 struct nlattr *nl_tcp;
9965
9966 if (!tcp)
9967 return 0;
9968
9969 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
9970 if (!nl_tcp)
9971 return -ENOBUFS;
9972
Jiri Benc930345e2015-03-29 16:59:25 +02009973 if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
9974 nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
Johannes Berg2a0e0472013-01-23 22:57:40 +01009975 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
9976 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
9977 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
9978 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
9979 tcp->payload_len, tcp->payload) ||
9980 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
9981 tcp->data_interval) ||
9982 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
9983 tcp->wake_len, tcp->wake_data) ||
9984 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
9985 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
9986 return -ENOBUFS;
9987
9988 if (tcp->payload_seq.len &&
9989 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
9990 sizeof(tcp->payload_seq), &tcp->payload_seq))
9991 return -ENOBUFS;
9992
9993 if (tcp->payload_tok.len &&
9994 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
9995 sizeof(tcp->payload_tok) + tcp->tokens_size,
9996 &tcp->payload_tok))
9997 return -ENOBUFS;
9998
Johannes Berge248ad32013-05-16 10:24:28 +02009999 nla_nest_end(msg, nl_tcp);
10000
Johannes Berg2a0e0472013-01-23 22:57:40 +010010001 return 0;
10002}
10003
Luciano Coelho75453cc2015-01-09 14:06:37 +020010004static int nl80211_send_wowlan_nd(struct sk_buff *msg,
10005 struct cfg80211_sched_scan_request *req)
10006{
Avraham Stern3b06d272015-10-12 09:51:34 +030010007 struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
Luciano Coelho75453cc2015-01-09 14:06:37 +020010008 int i;
10009
10010 if (!req)
10011 return 0;
10012
10013 nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
10014 if (!nd)
10015 return -ENOBUFS;
10016
Avraham Stern3b06d272015-10-12 09:51:34 +030010017 if (req->n_scan_plans == 1 &&
10018 nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
10019 req->scan_plans[0].interval * 1000))
Luciano Coelho75453cc2015-01-09 14:06:37 +020010020 return -ENOBUFS;
10021
Luciano Coelho21fea562015-03-17 16:36:01 +020010022 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
10023 return -ENOBUFS;
10024
vamsi krishnabf95ecd2017-01-13 01:12:20 +020010025 if (req->relative_rssi_set) {
10026 struct nl80211_bss_select_rssi_adjust rssi_adjust;
10027
10028 if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
10029 req->relative_rssi))
10030 return -ENOBUFS;
10031
10032 rssi_adjust.band = req->rssi_adjust.band;
10033 rssi_adjust.delta = req->rssi_adjust.delta;
10034 if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
10035 sizeof(rssi_adjust), &rssi_adjust))
10036 return -ENOBUFS;
10037 }
10038
Luciano Coelho75453cc2015-01-09 14:06:37 +020010039 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10040 if (!freqs)
10041 return -ENOBUFS;
10042
Johannes Berg53b18982016-09-14 09:59:21 +020010043 for (i = 0; i < req->n_channels; i++) {
10044 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10045 return -ENOBUFS;
10046 }
Luciano Coelho75453cc2015-01-09 14:06:37 +020010047
10048 nla_nest_end(msg, freqs);
10049
10050 if (req->n_match_sets) {
10051 matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
Johannes Berg76e1fb42016-09-14 09:55:57 +020010052 if (!matches)
10053 return -ENOBUFS;
10054
Luciano Coelho75453cc2015-01-09 14:06:37 +020010055 for (i = 0; i < req->n_match_sets; i++) {
10056 match = nla_nest_start(msg, i);
Johannes Berg76e1fb42016-09-14 09:55:57 +020010057 if (!match)
10058 return -ENOBUFS;
10059
Johannes Berg53b18982016-09-14 09:59:21 +020010060 if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
10061 req->match_sets[i].ssid.ssid_len,
10062 req->match_sets[i].ssid.ssid))
10063 return -ENOBUFS;
Luciano Coelho75453cc2015-01-09 14:06:37 +020010064 nla_nest_end(msg, match);
10065 }
10066 nla_nest_end(msg, matches);
10067 }
10068
Avraham Stern3b06d272015-10-12 09:51:34 +030010069 scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
10070 if (!scan_plans)
10071 return -ENOBUFS;
10072
10073 for (i = 0; i < req->n_scan_plans; i++) {
10074 scan_plan = nla_nest_start(msg, i + 1);
Johannes Berg76e1fb42016-09-14 09:55:57 +020010075 if (!scan_plan)
10076 return -ENOBUFS;
10077
Avraham Stern3b06d272015-10-12 09:51:34 +030010078 if (!scan_plan ||
10079 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
10080 req->scan_plans[i].interval) ||
10081 (req->scan_plans[i].iterations &&
10082 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
10083 req->scan_plans[i].iterations)))
10084 return -ENOBUFS;
10085 nla_nest_end(msg, scan_plan);
10086 }
10087 nla_nest_end(msg, scan_plans);
10088
Luciano Coelho75453cc2015-01-09 14:06:37 +020010089 nla_nest_end(msg, nd);
10090
10091 return 0;
10092}
10093
Johannes Bergff1b6e62011-05-04 15:37:28 +020010094static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
10095{
10096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10097 struct sk_buff *msg;
10098 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +010010099 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010100
Johannes Berg964dc9e2013-06-03 17:25:34 +020010101 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +020010102 return -EOPNOTSUPP;
10103
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010104 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +010010105 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010106 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
10107 rdev->wiphy.wowlan_config->tcp->payload_len +
10108 rdev->wiphy.wowlan_config->tcp->wake_len +
10109 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +010010110 }
10111
10112 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +020010113 if (!msg)
10114 return -ENOMEM;
10115
Eric W. Biederman15e47302012-09-07 20:12:54 +000010116 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +020010117 NL80211_CMD_GET_WOWLAN);
10118 if (!hdr)
10119 goto nla_put_failure;
10120
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010121 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +020010122 struct nlattr *nl_wowlan;
10123
10124 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10125 if (!nl_wowlan)
10126 goto nla_put_failure;
10127
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010128 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010129 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010130 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010131 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010132 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010133 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010134 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010135 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010136 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010137 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010138 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010139 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010140 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010141 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
10142 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +010010143
Amitkumar Karwarbb92d192013-02-12 12:16:26 -080010144 if (nl80211_send_wowlan_patterns(msg, rdev))
10145 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +010010146
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010147 if (nl80211_send_wowlan_tcp(msg,
10148 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +010010149 goto nla_put_failure;
10150
Luciano Coelho75453cc2015-01-09 14:06:37 +020010151 if (nl80211_send_wowlan_nd(
10152 msg,
10153 rdev->wiphy.wowlan_config->nd_config))
10154 goto nla_put_failure;
10155
Johannes Bergff1b6e62011-05-04 15:37:28 +020010156 nla_nest_end(msg, nl_wowlan);
10157 }
10158
10159 genlmsg_end(msg, hdr);
10160 return genlmsg_reply(msg, info);
10161
10162nla_put_failure:
10163 nlmsg_free(msg);
10164 return -ENOBUFS;
10165}
10166
Johannes Berg2a0e0472013-01-23 22:57:40 +010010167static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
10168 struct nlattr *attr,
10169 struct cfg80211_wowlan *trig)
10170{
10171 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
10172 struct cfg80211_wowlan_tcp *cfg;
10173 struct nl80211_wowlan_tcp_data_token *tok = NULL;
10174 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
10175 u32 size;
10176 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
10177 int err, port;
10178
Johannes Berg964dc9e2013-06-03 17:25:34 +020010179 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010180 return -EINVAL;
10181
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010182 err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TCP, attr,
10183 nl80211_wowlan_tcp_policy);
Johannes Berg2a0e0472013-01-23 22:57:40 +010010184 if (err)
10185 return err;
10186
10187 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
10188 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
10189 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
10190 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
10191 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
10192 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
10193 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
10194 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
10195 return -EINVAL;
10196
10197 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +020010198 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010199 return -EINVAL;
10200
10201 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +020010202 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +010010203 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010204 return -EINVAL;
10205
10206 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +020010207 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010208 return -EINVAL;
10209
10210 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
10211 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
10212 return -EINVAL;
10213
10214 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
10215 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10216
10217 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10218 tokens_size = tokln - sizeof(*tok);
10219
10220 if (!tok->len || tokens_size % tok->len)
10221 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +020010222 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010223 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +020010224 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010225 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +020010226 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010227 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +020010228 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010229 return -EINVAL;
10230 if (tok->offset + tok->len > data_size)
10231 return -EINVAL;
10232 }
10233
10234 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
10235 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +020010236 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +010010237 return -EINVAL;
10238 if (seq->len == 0 || seq->len > 4)
10239 return -EINVAL;
10240 if (seq->len + seq->offset > data_size)
10241 return -EINVAL;
10242 }
10243
10244 size = sizeof(*cfg);
10245 size += data_size;
10246 size += wake_size + wake_mask_size;
10247 size += tokens_size;
10248
10249 cfg = kzalloc(size, GFP_KERNEL);
10250 if (!cfg)
10251 return -ENOMEM;
Jiri Benc67b61f62015-03-29 16:59:26 +020010252 cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
10253 cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
Johannes Berg2a0e0472013-01-23 22:57:40 +010010254 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
10255 ETH_ALEN);
10256 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
10257 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
10258 else
10259 port = 0;
10260#ifdef CONFIG_INET
10261 /* allocate a socket and port for it and use it */
10262 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
10263 IPPROTO_TCP, &cfg->sock, 1);
10264 if (err) {
10265 kfree(cfg);
10266 return err;
10267 }
10268 if (inet_csk_get_port(cfg->sock->sk, port)) {
10269 sock_release(cfg->sock);
10270 kfree(cfg);
10271 return -EADDRINUSE;
10272 }
10273 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
10274#else
10275 if (!port) {
10276 kfree(cfg);
10277 return -EINVAL;
10278 }
10279 cfg->src_port = port;
10280#endif
10281
10282 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
10283 cfg->payload_len = data_size;
10284 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
10285 memcpy((void *)cfg->payload,
10286 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
10287 data_size);
10288 if (seq)
10289 cfg->payload_seq = *seq;
10290 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
10291 cfg->wake_len = wake_size;
10292 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
10293 memcpy((void *)cfg->wake_data,
10294 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
10295 wake_size);
10296 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
10297 data_size + wake_size;
10298 memcpy((void *)cfg->wake_mask,
10299 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
10300 wake_mask_size);
10301 if (tok) {
10302 cfg->tokens_size = tokens_size;
10303 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
10304 }
10305
10306 trig->tcp = cfg;
10307
10308 return 0;
10309}
10310
Luciano Coelho8cd4d452014-09-17 11:55:28 +030010311static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
10312 const struct wiphy_wowlan_support *wowlan,
10313 struct nlattr *attr,
10314 struct cfg80211_wowlan *trig)
10315{
10316 struct nlattr **tb;
10317 int err;
10318
10319 tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
10320 if (!tb)
10321 return -ENOMEM;
10322
10323 if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
10324 err = -EOPNOTSUPP;
10325 goto out;
10326 }
10327
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010328 err = nla_parse_nested(tb, NL80211_ATTR_MAX, attr, nl80211_policy);
Luciano Coelho8cd4d452014-09-17 11:55:28 +030010329 if (err)
10330 goto out;
10331
Arend Van Sprielaad1e812017-01-27 12:27:44 +000010332 trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
10333 wowlan->max_nd_match_sets);
Luciano Coelho8cd4d452014-09-17 11:55:28 +030010334 err = PTR_ERR_OR_ZERO(trig->nd_config);
10335 if (err)
10336 trig->nd_config = NULL;
10337
10338out:
10339 kfree(tb);
10340 return err;
10341}
10342
Johannes Bergff1b6e62011-05-04 15:37:28 +020010343static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
10344{
10345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10346 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +020010347 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +020010348 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +020010349 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010350 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010351 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Berg98fc4382015-03-01 09:10:13 +020010352 bool regular = false;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010353
Johannes Berg964dc9e2013-06-03 17:25:34 +020010354 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +020010355 return -EOPNOTSUPP;
10356
Johannes Bergae33bd82012-07-12 16:25:02 +020010357 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
10358 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010359 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +020010360 goto set_wakeup;
10361 }
Johannes Bergff1b6e62011-05-04 15:37:28 +020010362
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010363 err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TRIG,
10364 info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
10365 nl80211_wowlan_policy);
Johannes Bergff1b6e62011-05-04 15:37:28 +020010366 if (err)
10367 return err;
10368
10369 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
10370 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
10371 return -EINVAL;
10372 new_triggers.any = true;
10373 }
10374
10375 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
10376 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
10377 return -EINVAL;
10378 new_triggers.disconnect = true;
Johannes Berg98fc4382015-03-01 09:10:13 +020010379 regular = true;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010380 }
10381
10382 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
10383 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
10384 return -EINVAL;
10385 new_triggers.magic_pkt = true;
Johannes Berg98fc4382015-03-01 09:10:13 +020010386 regular = true;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010387 }
10388
Johannes Berg77dbbb12011-07-13 10:48:55 +020010389 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
10390 return -EINVAL;
10391
10392 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
10393 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
10394 return -EINVAL;
10395 new_triggers.gtk_rekey_failure = true;
Johannes Berg98fc4382015-03-01 09:10:13 +020010396 regular = true;
Johannes Berg77dbbb12011-07-13 10:48:55 +020010397 }
10398
10399 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
10400 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
10401 return -EINVAL;
10402 new_triggers.eap_identity_req = true;
Johannes Berg98fc4382015-03-01 09:10:13 +020010403 regular = true;
Johannes Berg77dbbb12011-07-13 10:48:55 +020010404 }
10405
10406 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
10407 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
10408 return -EINVAL;
10409 new_triggers.four_way_handshake = true;
Johannes Berg98fc4382015-03-01 09:10:13 +020010410 regular = true;
Johannes Berg77dbbb12011-07-13 10:48:55 +020010411 }
10412
10413 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
10414 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
10415 return -EINVAL;
10416 new_triggers.rfkill_release = true;
Johannes Berg98fc4382015-03-01 09:10:13 +020010417 regular = true;
Johannes Berg77dbbb12011-07-13 10:48:55 +020010418 }
10419
Johannes Bergff1b6e62011-05-04 15:37:28 +020010420 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
10421 struct nlattr *pat;
10422 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -080010423 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010424 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +020010425
Johannes Berg98fc4382015-03-01 09:10:13 +020010426 regular = true;
10427
Johannes Bergff1b6e62011-05-04 15:37:28 +020010428 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10429 rem)
10430 n_patterns++;
10431 if (n_patterns > wowlan->n_patterns)
10432 return -EINVAL;
10433
10434 new_triggers.patterns = kcalloc(n_patterns,
10435 sizeof(new_triggers.patterns[0]),
10436 GFP_KERNEL);
10437 if (!new_triggers.patterns)
10438 return -ENOMEM;
10439
10440 new_triggers.n_patterns = n_patterns;
10441 i = 0;
10442
10443 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10444 rem) {
Johannes Berg922bd802014-05-19 17:59:50 +020010445 u8 *mask_pat;
10446
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010447 nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10448 NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +020010449 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010450 if (!pat_tb[NL80211_PKTPAT_MASK] ||
10451 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +020010452 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010453 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +020010454 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010455 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +020010456 goto error;
10457 if (pat_len > wowlan->pattern_max_len ||
10458 pat_len < wowlan->pattern_min_len)
10459 goto error;
10460
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010461 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -080010462 pkt_offset = 0;
10463 else
10464 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010465 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -080010466 if (pkt_offset > wowlan->max_pkt_offset)
10467 goto error;
10468 new_triggers.patterns[i].pkt_offset = pkt_offset;
10469
Johannes Berg922bd802014-05-19 17:59:50 +020010470 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10471 if (!mask_pat) {
Johannes Bergff1b6e62011-05-04 15:37:28 +020010472 err = -ENOMEM;
10473 goto error;
10474 }
Johannes Berg922bd802014-05-19 17:59:50 +020010475 new_triggers.patterns[i].mask = mask_pat;
10476 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +020010477 mask_len);
Johannes Berg922bd802014-05-19 17:59:50 +020010478 mask_pat += mask_len;
10479 new_triggers.patterns[i].pattern = mask_pat;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010480 new_triggers.patterns[i].pattern_len = pat_len;
Johannes Berg922bd802014-05-19 17:59:50 +020010481 memcpy(mask_pat,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -070010482 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +020010483 pat_len);
10484 i++;
10485 }
10486 }
10487
Johannes Berg2a0e0472013-01-23 22:57:40 +010010488 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
Johannes Berg98fc4382015-03-01 09:10:13 +020010489 regular = true;
Johannes Berg2a0e0472013-01-23 22:57:40 +010010490 err = nl80211_parse_wowlan_tcp(
10491 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
10492 &new_triggers);
10493 if (err)
10494 goto error;
10495 }
10496
Luciano Coelho8cd4d452014-09-17 11:55:28 +030010497 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
Johannes Berg98fc4382015-03-01 09:10:13 +020010498 regular = true;
Luciano Coelho8cd4d452014-09-17 11:55:28 +030010499 err = nl80211_parse_wowlan_nd(
10500 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
10501 &new_triggers);
10502 if (err)
10503 goto error;
10504 }
10505
Johannes Berg98fc4382015-03-01 09:10:13 +020010506 /* The 'any' trigger means the device continues operating more or less
10507 * as in its normal operation mode and wakes up the host on most of the
10508 * normal interrupts (like packet RX, ...)
10509 * It therefore makes little sense to combine with the more constrained
10510 * wakeup trigger modes.
10511 */
10512 if (new_triggers.any && regular) {
10513 err = -EINVAL;
10514 goto error;
10515 }
10516
Johannes Bergae33bd82012-07-12 16:25:02 +020010517 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
10518 if (!ntrig) {
10519 err = -ENOMEM;
10520 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010521 }
Johannes Bergae33bd82012-07-12 16:25:02 +020010522 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010523 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +020010524
Johannes Bergae33bd82012-07-12 16:25:02 +020010525 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +020010526 if (rdev->ops->set_wakeup &&
10527 prev_enabled != !!rdev->wiphy.wowlan_config)
10528 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +020010529
Johannes Bergff1b6e62011-05-04 15:37:28 +020010530 return 0;
10531 error:
10532 for (i = 0; i < new_triggers.n_patterns; i++)
10533 kfree(new_triggers.patterns[i].mask);
10534 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +010010535 if (new_triggers.tcp && new_triggers.tcp->sock)
10536 sock_release(new_triggers.tcp->sock);
10537 kfree(new_triggers.tcp);
Ola Olssone5dbe072015-12-12 23:17:17 +010010538 kfree(new_triggers.nd_config);
Johannes Bergff1b6e62011-05-04 15:37:28 +020010539 return err;
10540}
Johannes Bergdfb89c52012-06-27 09:23:48 +020010541#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +020010542
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010543static int nl80211_send_coalesce_rules(struct sk_buff *msg,
10544 struct cfg80211_registered_device *rdev)
10545{
10546 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
10547 int i, j, pat_len;
10548 struct cfg80211_coalesce_rules *rule;
10549
10550 if (!rdev->coalesce->n_rules)
10551 return 0;
10552
10553 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
10554 if (!nl_rules)
10555 return -ENOBUFS;
10556
10557 for (i = 0; i < rdev->coalesce->n_rules; i++) {
10558 nl_rule = nla_nest_start(msg, i + 1);
10559 if (!nl_rule)
10560 return -ENOBUFS;
10561
10562 rule = &rdev->coalesce->rules[i];
10563 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
10564 rule->delay))
10565 return -ENOBUFS;
10566
10567 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
10568 rule->condition))
10569 return -ENOBUFS;
10570
10571 nl_pats = nla_nest_start(msg,
10572 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
10573 if (!nl_pats)
10574 return -ENOBUFS;
10575
10576 for (j = 0; j < rule->n_patterns; j++) {
10577 nl_pat = nla_nest_start(msg, j + 1);
10578 if (!nl_pat)
10579 return -ENOBUFS;
10580 pat_len = rule->patterns[j].pattern_len;
10581 if (nla_put(msg, NL80211_PKTPAT_MASK,
10582 DIV_ROUND_UP(pat_len, 8),
10583 rule->patterns[j].mask) ||
10584 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10585 rule->patterns[j].pattern) ||
10586 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10587 rule->patterns[j].pkt_offset))
10588 return -ENOBUFS;
10589 nla_nest_end(msg, nl_pat);
10590 }
10591 nla_nest_end(msg, nl_pats);
10592 nla_nest_end(msg, nl_rule);
10593 }
10594 nla_nest_end(msg, nl_rules);
10595
10596 return 0;
10597}
10598
10599static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
10600{
10601 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10602 struct sk_buff *msg;
10603 void *hdr;
10604
10605 if (!rdev->wiphy.coalesce)
10606 return -EOPNOTSUPP;
10607
10608 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10609 if (!msg)
10610 return -ENOMEM;
10611
10612 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10613 NL80211_CMD_GET_COALESCE);
10614 if (!hdr)
10615 goto nla_put_failure;
10616
10617 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
10618 goto nla_put_failure;
10619
10620 genlmsg_end(msg, hdr);
10621 return genlmsg_reply(msg, info);
10622
10623nla_put_failure:
10624 nlmsg_free(msg);
10625 return -ENOBUFS;
10626}
10627
10628void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
10629{
10630 struct cfg80211_coalesce *coalesce = rdev->coalesce;
10631 int i, j;
10632 struct cfg80211_coalesce_rules *rule;
10633
10634 if (!coalesce)
10635 return;
10636
10637 for (i = 0; i < coalesce->n_rules; i++) {
10638 rule = &coalesce->rules[i];
10639 for (j = 0; j < rule->n_patterns; j++)
10640 kfree(rule->patterns[j].mask);
10641 kfree(rule->patterns);
10642 }
10643 kfree(coalesce->rules);
10644 kfree(coalesce);
10645 rdev->coalesce = NULL;
10646}
10647
10648static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
10649 struct nlattr *rule,
10650 struct cfg80211_coalesce_rules *new_rule)
10651{
10652 int err, i;
10653 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10654 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
10655 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
10656 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10657
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010658 err = nla_parse_nested(tb, NL80211_ATTR_COALESCE_RULE_MAX, rule,
10659 nl80211_coalesce_policy);
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010660 if (err)
10661 return err;
10662
10663 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
10664 new_rule->delay =
10665 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
10666 if (new_rule->delay > coalesce->max_delay)
10667 return -EINVAL;
10668
10669 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
10670 new_rule->condition =
10671 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
10672 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
10673 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
10674 return -EINVAL;
10675
10676 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
10677 return -EINVAL;
10678
10679 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10680 rem)
10681 n_patterns++;
10682 if (n_patterns > coalesce->n_patterns)
10683 return -EINVAL;
10684
10685 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
10686 GFP_KERNEL);
10687 if (!new_rule->patterns)
10688 return -ENOMEM;
10689
10690 new_rule->n_patterns = n_patterns;
10691 i = 0;
10692
10693 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10694 rem) {
Johannes Berg922bd802014-05-19 17:59:50 +020010695 u8 *mask_pat;
10696
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010697 nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat, NULL);
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010698 if (!pat_tb[NL80211_PKTPAT_MASK] ||
10699 !pat_tb[NL80211_PKTPAT_PATTERN])
10700 return -EINVAL;
10701 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10702 mask_len = DIV_ROUND_UP(pat_len, 8);
10703 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10704 return -EINVAL;
10705 if (pat_len > coalesce->pattern_max_len ||
10706 pat_len < coalesce->pattern_min_len)
10707 return -EINVAL;
10708
10709 if (!pat_tb[NL80211_PKTPAT_OFFSET])
10710 pkt_offset = 0;
10711 else
10712 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
10713 if (pkt_offset > coalesce->max_pkt_offset)
10714 return -EINVAL;
10715 new_rule->patterns[i].pkt_offset = pkt_offset;
10716
Johannes Berg922bd802014-05-19 17:59:50 +020010717 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10718 if (!mask_pat)
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010719 return -ENOMEM;
Johannes Berg922bd802014-05-19 17:59:50 +020010720
10721 new_rule->patterns[i].mask = mask_pat;
10722 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10723 mask_len);
10724
10725 mask_pat += mask_len;
10726 new_rule->patterns[i].pattern = mask_pat;
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010727 new_rule->patterns[i].pattern_len = pat_len;
Johannes Berg922bd802014-05-19 17:59:50 +020010728 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
10729 pat_len);
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010730 i++;
10731 }
10732
10733 return 0;
10734}
10735
10736static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
10737{
10738 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10739 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10740 struct cfg80211_coalesce new_coalesce = {};
10741 struct cfg80211_coalesce *n_coalesce;
10742 int err, rem_rule, n_rules = 0, i, j;
10743 struct nlattr *rule;
10744 struct cfg80211_coalesce_rules *tmp_rule;
10745
10746 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
10747 return -EOPNOTSUPP;
10748
10749 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
10750 cfg80211_rdev_free_coalesce(rdev);
Ilan Peera1056b1b2015-10-22 22:27:46 +030010751 rdev_set_coalesce(rdev, NULL);
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010752 return 0;
10753 }
10754
10755 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
10756 rem_rule)
10757 n_rules++;
10758 if (n_rules > coalesce->n_rules)
10759 return -EINVAL;
10760
10761 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
10762 GFP_KERNEL);
10763 if (!new_coalesce.rules)
10764 return -ENOMEM;
10765
10766 new_coalesce.n_rules = n_rules;
10767 i = 0;
10768
10769 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
10770 rem_rule) {
10771 err = nl80211_parse_coalesce_rule(rdev, rule,
10772 &new_coalesce.rules[i]);
10773 if (err)
10774 goto error;
10775
10776 i++;
10777 }
10778
Ilan Peera1056b1b2015-10-22 22:27:46 +030010779 err = rdev_set_coalesce(rdev, &new_coalesce);
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070010780 if (err)
10781 goto error;
10782
10783 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
10784 if (!n_coalesce) {
10785 err = -ENOMEM;
10786 goto error;
10787 }
10788 cfg80211_rdev_free_coalesce(rdev);
10789 rdev->coalesce = n_coalesce;
10790
10791 return 0;
10792error:
10793 for (i = 0; i < new_coalesce.n_rules; i++) {
10794 tmp_rule = &new_coalesce.rules[i];
10795 for (j = 0; j < tmp_rule->n_patterns; j++)
10796 kfree(tmp_rule->patterns[j].mask);
10797 kfree(tmp_rule->patterns);
10798 }
10799 kfree(new_coalesce.rules);
10800
10801 return err;
10802}
10803
Johannes Berge5497d72011-07-05 16:35:40 +020010804static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
10805{
10806 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10807 struct net_device *dev = info->user_ptr[1];
10808 struct wireless_dev *wdev = dev->ieee80211_ptr;
10809 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
10810 struct cfg80211_gtk_rekey_data rekey_data;
10811 int err;
10812
10813 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
10814 return -EINVAL;
10815
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020010816 err = nla_parse_nested(tb, MAX_NL80211_REKEY_DATA,
10817 info->attrs[NL80211_ATTR_REKEY_DATA],
10818 nl80211_rekey_policy);
Johannes Berge5497d72011-07-05 16:35:40 +020010819 if (err)
10820 return err;
10821
10822 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
10823 return -ERANGE;
10824 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
10825 return -ERANGE;
10826 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
10827 return -ERANGE;
10828
Johannes Berg78f686c2014-09-10 22:28:06 +030010829 rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
10830 rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
10831 rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
Johannes Berge5497d72011-07-05 16:35:40 +020010832
10833 wdev_lock(wdev);
10834 if (!wdev->current_bss) {
10835 err = -ENOTCONN;
10836 goto out;
10837 }
10838
10839 if (!rdev->ops->set_rekey_data) {
10840 err = -EOPNOTSUPP;
10841 goto out;
10842 }
10843
Hila Gonene35e4d22012-06-27 17:19:42 +030010844 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +020010845 out:
10846 wdev_unlock(wdev);
10847 return err;
10848}
10849
Johannes Berg28946da2011-11-04 11:18:12 +010010850static int nl80211_register_unexpected_frame(struct sk_buff *skb,
10851 struct genl_info *info)
10852{
10853 struct net_device *dev = info->user_ptr[1];
10854 struct wireless_dev *wdev = dev->ieee80211_ptr;
10855
10856 if (wdev->iftype != NL80211_IFTYPE_AP &&
10857 wdev->iftype != NL80211_IFTYPE_P2P_GO)
10858 return -EINVAL;
10859
Eric W. Biederman15e47302012-09-07 20:12:54 +000010860 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010861 return -EBUSY;
10862
Eric W. Biederman15e47302012-09-07 20:12:54 +000010863 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +010010864 return 0;
10865}
10866
Johannes Berg7f6cf312011-11-04 11:18:15 +010010867static int nl80211_probe_client(struct sk_buff *skb,
10868 struct genl_info *info)
10869{
10870 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10871 struct net_device *dev = info->user_ptr[1];
10872 struct wireless_dev *wdev = dev->ieee80211_ptr;
10873 struct sk_buff *msg;
10874 void *hdr;
10875 const u8 *addr;
10876 u64 cookie;
10877 int err;
10878
10879 if (wdev->iftype != NL80211_IFTYPE_AP &&
10880 wdev->iftype != NL80211_IFTYPE_P2P_GO)
10881 return -EOPNOTSUPP;
10882
10883 if (!info->attrs[NL80211_ATTR_MAC])
10884 return -EINVAL;
10885
10886 if (!rdev->ops->probe_client)
10887 return -EOPNOTSUPP;
10888
10889 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10890 if (!msg)
10891 return -ENOMEM;
10892
Eric W. Biederman15e47302012-09-07 20:12:54 +000010893 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +010010894 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +030010895 if (!hdr) {
10896 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010897 goto free_msg;
10898 }
10899
10900 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
10901
Hila Gonene35e4d22012-06-27 17:19:42 +030010902 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +010010903 if (err)
10904 goto free_msg;
10905
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020010906 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10907 NL80211_ATTR_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -040010908 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010909
10910 genlmsg_end(msg, hdr);
10911
10912 return genlmsg_reply(msg, info);
10913
10914 nla_put_failure:
10915 err = -ENOBUFS;
10916 free_msg:
10917 nlmsg_free(msg);
10918 return err;
10919}
10920
Johannes Berg5e760232011-11-04 11:18:17 +010010921static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
10922{
10923 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -070010924 struct cfg80211_beacon_registration *reg, *nreg;
10925 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +010010926
10927 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
10928 return -EOPNOTSUPP;
10929
Ben Greear37c73b52012-10-26 14:49:25 -070010930 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
10931 if (!nreg)
10932 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +010010933
Ben Greear37c73b52012-10-26 14:49:25 -070010934 /* First, check if already registered. */
10935 spin_lock_bh(&rdev->beacon_registrations_lock);
10936 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10937 if (reg->nlportid == info->snd_portid) {
10938 rv = -EALREADY;
10939 goto out_err;
10940 }
10941 }
10942 /* Add it to the list */
10943 nreg->nlportid = info->snd_portid;
10944 list_add(&nreg->list, &rdev->beacon_registrations);
10945
10946 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010947
10948 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -070010949out_err:
10950 spin_unlock_bh(&rdev->beacon_registrations_lock);
10951 kfree(nreg);
10952 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +010010953}
10954
Johannes Berg98104fde2012-06-16 00:19:54 +020010955static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
10956{
10957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10958 struct wireless_dev *wdev = info->user_ptr[1];
10959 int err;
10960
10961 if (!rdev->ops->start_p2p_device)
10962 return -EOPNOTSUPP;
10963
10964 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
10965 return -EOPNOTSUPP;
10966
Arend Van Spriel73c7da32016-10-20 20:08:22 +010010967 if (wdev_running(wdev))
Johannes Berg98104fde2012-06-16 00:19:54 +020010968 return 0;
10969
Luciano Coelhob6a55012014-02-27 11:07:21 +020010970 if (rfkill_blocked(rdev->rfkill))
10971 return -ERFKILL;
Johannes Berg98104fde2012-06-16 00:19:54 +020010972
Johannes Bergeeb126e2012-10-23 15:16:50 +020010973 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +020010974 if (err)
10975 return err;
10976
Arend Van Spriel73c7da32016-10-20 20:08:22 +010010977 wdev->is_running = true;
Johannes Berg98104fde2012-06-16 00:19:54 +020010978 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +020010979
10980 return 0;
10981}
10982
10983static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
10984{
10985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10986 struct wireless_dev *wdev = info->user_ptr[1];
10987
10988 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
10989 return -EOPNOTSUPP;
10990
10991 if (!rdev->ops->stop_p2p_device)
10992 return -EOPNOTSUPP;
10993
Johannes Bergf9f47522013-03-19 15:04:07 +010010994 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +020010995
10996 return 0;
10997}
10998
Ayala Bekercb3b7d82016-09-20 17:31:13 +030010999static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
11000{
11001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11002 struct wireless_dev *wdev = info->user_ptr[1];
11003 struct cfg80211_nan_conf conf = {};
11004 int err;
11005
11006 if (wdev->iftype != NL80211_IFTYPE_NAN)
11007 return -EOPNOTSUPP;
11008
Johannes Bergeeb04a92016-11-21 13:55:48 +010011009 if (wdev_running(wdev))
Ayala Bekercb3b7d82016-09-20 17:31:13 +030011010 return -EEXIST;
11011
11012 if (rfkill_blocked(rdev->rfkill))
11013 return -ERFKILL;
11014
11015 if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
11016 return -EINVAL;
11017
Ayala Bekercb3b7d82016-09-20 17:31:13 +030011018 conf.master_pref =
11019 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11020 if (!conf.master_pref)
11021 return -EINVAL;
11022
Luca Coelho85859892017-02-08 15:00:34 +020011023 if (info->attrs[NL80211_ATTR_BANDS]) {
11024 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11025
11026 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11027 return -EOPNOTSUPP;
11028
11029 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11030 return -EINVAL;
11031
11032 conf.bands = bands;
11033 }
Ayala Bekercb3b7d82016-09-20 17:31:13 +030011034
11035 err = rdev_start_nan(rdev, wdev, &conf);
11036 if (err)
11037 return err;
11038
Arend Van Spriel73c7da32016-10-20 20:08:22 +010011039 wdev->is_running = true;
Ayala Bekercb3b7d82016-09-20 17:31:13 +030011040 rdev->opencount++;
11041
11042 return 0;
11043}
11044
11045static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
11046{
11047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11048 struct wireless_dev *wdev = info->user_ptr[1];
11049
11050 if (wdev->iftype != NL80211_IFTYPE_NAN)
11051 return -EOPNOTSUPP;
11052
11053 cfg80211_stop_nan(rdev, wdev);
11054
11055 return 0;
11056}
11057
Ayala Bekera442b762016-09-20 17:31:15 +030011058static int validate_nan_filter(struct nlattr *filter_attr)
11059{
11060 struct nlattr *attr;
11061 int len = 0, n_entries = 0, rem;
11062
11063 nla_for_each_nested(attr, filter_attr, rem) {
11064 len += nla_len(attr);
11065 n_entries++;
11066 }
11067
11068 if (len >= U8_MAX)
11069 return -EINVAL;
11070
11071 return n_entries;
11072}
11073
11074static int handle_nan_filter(struct nlattr *attr_filter,
11075 struct cfg80211_nan_func *func,
11076 bool tx)
11077{
11078 struct nlattr *attr;
11079 int n_entries, rem, i;
11080 struct cfg80211_nan_func_filter *filter;
11081
11082 n_entries = validate_nan_filter(attr_filter);
11083 if (n_entries < 0)
11084 return n_entries;
11085
11086 BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
11087
11088 filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
11089 if (!filter)
11090 return -ENOMEM;
11091
11092 i = 0;
11093 nla_for_each_nested(attr, attr_filter, rem) {
Thomas Grafb15ca182016-10-26 10:53:16 +020011094 filter[i].filter = nla_memdup(attr, GFP_KERNEL);
Ayala Bekera442b762016-09-20 17:31:15 +030011095 filter[i].len = nla_len(attr);
11096 i++;
11097 }
11098 if (tx) {
11099 func->num_tx_filters = n_entries;
11100 func->tx_filters = filter;
11101 } else {
11102 func->num_rx_filters = n_entries;
11103 func->rx_filters = filter;
11104 }
11105
11106 return 0;
11107}
11108
11109static int nl80211_nan_add_func(struct sk_buff *skb,
11110 struct genl_info *info)
11111{
11112 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11113 struct wireless_dev *wdev = info->user_ptr[1];
11114 struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
11115 struct cfg80211_nan_func *func;
11116 struct sk_buff *msg = NULL;
11117 void *hdr = NULL;
11118 int err = 0;
11119
11120 if (wdev->iftype != NL80211_IFTYPE_NAN)
11121 return -EOPNOTSUPP;
11122
Arend Van Spriel73c7da32016-10-20 20:08:22 +010011123 if (!wdev_running(wdev))
Ayala Bekera442b762016-09-20 17:31:15 +030011124 return -ENOTCONN;
11125
11126 if (!info->attrs[NL80211_ATTR_NAN_FUNC])
11127 return -EINVAL;
11128
11129 if (wdev->owner_nlportid &&
11130 wdev->owner_nlportid != info->snd_portid)
11131 return -ENOTCONN;
11132
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020011133 err = nla_parse_nested(tb, NL80211_NAN_FUNC_ATTR_MAX,
11134 info->attrs[NL80211_ATTR_NAN_FUNC],
11135 nl80211_nan_func_policy);
Ayala Bekera442b762016-09-20 17:31:15 +030011136 if (err)
11137 return err;
11138
11139 func = kzalloc(sizeof(*func), GFP_KERNEL);
11140 if (!func)
11141 return -ENOMEM;
11142
11143 func->cookie = wdev->wiphy->cookie_counter++;
11144
11145 if (!tb[NL80211_NAN_FUNC_TYPE] ||
11146 nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
11147 err = -EINVAL;
11148 goto out;
11149 }
11150
11151
11152 func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
11153
11154 if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
11155 err = -EINVAL;
11156 goto out;
11157 }
11158
11159 memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
11160 sizeof(func->service_id));
11161
11162 func->close_range =
11163 nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
11164
11165 if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
11166 func->serv_spec_info_len =
11167 nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
11168 func->serv_spec_info =
11169 kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
11170 func->serv_spec_info_len,
11171 GFP_KERNEL);
11172 if (!func->serv_spec_info) {
11173 err = -ENOMEM;
11174 goto out;
11175 }
11176 }
11177
11178 if (tb[NL80211_NAN_FUNC_TTL])
11179 func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
11180
11181 switch (func->type) {
11182 case NL80211_NAN_FUNC_PUBLISH:
11183 if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
11184 err = -EINVAL;
11185 goto out;
11186 }
11187
11188 func->publish_type =
11189 nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
11190 func->publish_bcast =
11191 nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
11192
11193 if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
11194 func->publish_bcast) {
11195 err = -EINVAL;
11196 goto out;
11197 }
11198 break;
11199 case NL80211_NAN_FUNC_SUBSCRIBE:
11200 func->subscribe_active =
11201 nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
11202 break;
11203 case NL80211_NAN_FUNC_FOLLOW_UP:
11204 if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
11205 !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]) {
11206 err = -EINVAL;
11207 goto out;
11208 }
11209
11210 func->followup_id =
11211 nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
11212 func->followup_reqid =
11213 nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
11214 memcpy(func->followup_dest.addr,
11215 nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
11216 sizeof(func->followup_dest.addr));
11217 if (func->ttl) {
11218 err = -EINVAL;
11219 goto out;
11220 }
11221 break;
11222 default:
11223 err = -EINVAL;
11224 goto out;
11225 }
11226
11227 if (tb[NL80211_NAN_FUNC_SRF]) {
11228 struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
11229
Johannes Bergbfe2c7b2016-10-26 14:42:21 +020011230 err = nla_parse_nested(srf_tb, NL80211_NAN_SRF_ATTR_MAX,
11231 tb[NL80211_NAN_FUNC_SRF],
11232 nl80211_nan_srf_policy);
Ayala Bekera442b762016-09-20 17:31:15 +030011233 if (err)
11234 goto out;
11235
11236 func->srf_include =
11237 nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
11238
11239 if (srf_tb[NL80211_NAN_SRF_BF]) {
11240 if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
11241 !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
11242 err = -EINVAL;
11243 goto out;
11244 }
11245
11246 func->srf_bf_len =
11247 nla_len(srf_tb[NL80211_NAN_SRF_BF]);
11248 func->srf_bf =
11249 kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
11250 func->srf_bf_len, GFP_KERNEL);
11251 if (!func->srf_bf) {
11252 err = -ENOMEM;
11253 goto out;
11254 }
11255
11256 func->srf_bf_idx =
11257 nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
11258 } else {
11259 struct nlattr *attr, *mac_attr =
11260 srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
11261 int n_entries, rem, i = 0;
11262
11263 if (!mac_attr) {
11264 err = -EINVAL;
11265 goto out;
11266 }
11267
11268 n_entries = validate_acl_mac_addrs(mac_attr);
11269 if (n_entries <= 0) {
11270 err = -EINVAL;
11271 goto out;
11272 }
11273
11274 func->srf_num_macs = n_entries;
11275 func->srf_macs =
11276 kzalloc(sizeof(*func->srf_macs) * n_entries,
11277 GFP_KERNEL);
11278 if (!func->srf_macs) {
11279 err = -ENOMEM;
11280 goto out;
11281 }
11282
11283 nla_for_each_nested(attr, mac_attr, rem)
11284 memcpy(func->srf_macs[i++].addr, nla_data(attr),
11285 sizeof(*func->srf_macs));
11286 }
11287 }
11288
11289 if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
11290 err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
11291 func, true);
11292 if (err)
11293 goto out;
11294 }
11295
11296 if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
11297 err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
11298 func, false);
11299 if (err)
11300 goto out;
11301 }
11302
11303 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11304 if (!msg) {
11305 err = -ENOMEM;
11306 goto out;
11307 }
11308
11309 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11310 NL80211_CMD_ADD_NAN_FUNCTION);
11311 /* This can't really happen - we just allocated 4KB */
11312 if (WARN_ON(!hdr)) {
11313 err = -ENOMEM;
11314 goto out;
11315 }
11316
11317 err = rdev_add_nan_func(rdev, wdev, func);
11318out:
11319 if (err < 0) {
11320 cfg80211_free_nan_func(func);
11321 nlmsg_free(msg);
11322 return err;
11323 }
11324
11325 /* propagate the instance id and cookie to userspace */
11326 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
11327 NL80211_ATTR_PAD))
11328 goto nla_put_failure;
11329
11330 func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11331 if (!func_attr)
11332 goto nla_put_failure;
11333
11334 if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
11335 func->instance_id))
11336 goto nla_put_failure;
11337
11338 nla_nest_end(msg, func_attr);
11339
11340 genlmsg_end(msg, hdr);
11341 return genlmsg_reply(msg, info);
11342
11343nla_put_failure:
11344 nlmsg_free(msg);
11345 return -ENOBUFS;
11346}
11347
11348static int nl80211_nan_del_func(struct sk_buff *skb,
11349 struct genl_info *info)
11350{
11351 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11352 struct wireless_dev *wdev = info->user_ptr[1];
11353 u64 cookie;
11354
11355 if (wdev->iftype != NL80211_IFTYPE_NAN)
11356 return -EOPNOTSUPP;
11357
Arend Van Spriel73c7da32016-10-20 20:08:22 +010011358 if (!wdev_running(wdev))
Ayala Bekera442b762016-09-20 17:31:15 +030011359 return -ENOTCONN;
11360
11361 if (!info->attrs[NL80211_ATTR_COOKIE])
11362 return -EINVAL;
11363
11364 if (wdev->owner_nlportid &&
11365 wdev->owner_nlportid != info->snd_portid)
11366 return -ENOTCONN;
11367
11368 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11369
11370 rdev_del_nan_func(rdev, wdev, cookie);
11371
11372 return 0;
11373}
11374
Ayala Bekera5a9dcf2016-09-20 17:31:16 +030011375static int nl80211_nan_change_config(struct sk_buff *skb,
11376 struct genl_info *info)
11377{
11378 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11379 struct wireless_dev *wdev = info->user_ptr[1];
11380 struct cfg80211_nan_conf conf = {};
11381 u32 changed = 0;
11382
11383 if (wdev->iftype != NL80211_IFTYPE_NAN)
11384 return -EOPNOTSUPP;
11385
Arend Van Spriel73c7da32016-10-20 20:08:22 +010011386 if (!wdev_running(wdev))
Ayala Bekera5a9dcf2016-09-20 17:31:16 +030011387 return -ENOTCONN;
11388
11389 if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
11390 conf.master_pref =
11391 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11392 if (conf.master_pref <= 1 || conf.master_pref == 255)
11393 return -EINVAL;
11394
11395 changed |= CFG80211_NAN_CONF_CHANGED_PREF;
11396 }
11397
Luca Coelho85859892017-02-08 15:00:34 +020011398 if (info->attrs[NL80211_ATTR_BANDS]) {
11399 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11400
11401 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11402 return -EOPNOTSUPP;
11403
11404 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11405 return -EINVAL;
11406
11407 conf.bands = bands;
11408 changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
Ayala Bekera5a9dcf2016-09-20 17:31:16 +030011409 }
11410
11411 if (!changed)
11412 return -EINVAL;
11413
11414 return rdev_nan_change_conf(rdev, wdev, &conf, changed);
11415}
11416
Ayala Beker50bcd312016-09-20 17:31:17 +030011417void cfg80211_nan_match(struct wireless_dev *wdev,
11418 struct cfg80211_nan_match_params *match, gfp_t gfp)
11419{
11420 struct wiphy *wiphy = wdev->wiphy;
11421 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11422 struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
11423 struct sk_buff *msg;
11424 void *hdr;
11425
11426 if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
11427 return;
11428
11429 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11430 if (!msg)
11431 return;
11432
11433 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
11434 if (!hdr) {
11435 nlmsg_free(msg);
11436 return;
11437 }
11438
11439 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11440 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11441 wdev->netdev->ifindex)) ||
11442 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11443 NL80211_ATTR_PAD))
11444 goto nla_put_failure;
11445
11446 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
11447 NL80211_ATTR_PAD) ||
11448 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
11449 goto nla_put_failure;
11450
11451 match_attr = nla_nest_start(msg, NL80211_ATTR_NAN_MATCH);
11452 if (!match_attr)
11453 goto nla_put_failure;
11454
11455 local_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_LOCAL);
11456 if (!local_func_attr)
11457 goto nla_put_failure;
11458
11459 if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
11460 goto nla_put_failure;
11461
11462 nla_nest_end(msg, local_func_attr);
11463
11464 peer_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_PEER);
11465 if (!peer_func_attr)
11466 goto nla_put_failure;
11467
11468 if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
11469 nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
11470 goto nla_put_failure;
11471
11472 if (match->info && match->info_len &&
11473 nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
11474 match->info))
11475 goto nla_put_failure;
11476
11477 nla_nest_end(msg, peer_func_attr);
11478 nla_nest_end(msg, match_attr);
11479 genlmsg_end(msg, hdr);
11480
11481 if (!wdev->owner_nlportid)
11482 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11483 msg, 0, NL80211_MCGRP_NAN, gfp);
11484 else
11485 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11486 wdev->owner_nlportid);
11487
11488 return;
11489
11490nla_put_failure:
11491 nlmsg_free(msg);
11492}
11493EXPORT_SYMBOL(cfg80211_nan_match);
11494
Ayala Beker368e5a72016-09-20 17:31:18 +030011495void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
11496 u8 inst_id,
11497 enum nl80211_nan_func_term_reason reason,
11498 u64 cookie, gfp_t gfp)
11499{
11500 struct wiphy *wiphy = wdev->wiphy;
11501 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11502 struct sk_buff *msg;
11503 struct nlattr *func_attr;
11504 void *hdr;
11505
11506 if (WARN_ON(!inst_id))
11507 return;
11508
11509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11510 if (!msg)
11511 return;
11512
11513 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
11514 if (!hdr) {
11515 nlmsg_free(msg);
11516 return;
11517 }
11518
11519 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11520 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11521 wdev->netdev->ifindex)) ||
11522 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11523 NL80211_ATTR_PAD))
11524 goto nla_put_failure;
11525
11526 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11527 NL80211_ATTR_PAD))
11528 goto nla_put_failure;
11529
11530 func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11531 if (!func_attr)
11532 goto nla_put_failure;
11533
11534 if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
11535 nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
11536 goto nla_put_failure;
11537
11538 nla_nest_end(msg, func_attr);
11539 genlmsg_end(msg, hdr);
11540
11541 if (!wdev->owner_nlportid)
11542 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11543 msg, 0, NL80211_MCGRP_NAN, gfp);
11544 else
11545 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11546 wdev->owner_nlportid);
11547
11548 return;
11549
11550nla_put_failure:
11551 nlmsg_free(msg);
11552}
11553EXPORT_SYMBOL(cfg80211_nan_func_terminated);
11554
Johannes Berg3713b4e2013-02-14 16:19:38 +010011555static int nl80211_get_protocol_features(struct sk_buff *skb,
11556 struct genl_info *info)
11557{
11558 void *hdr;
11559 struct sk_buff *msg;
11560
11561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11562 if (!msg)
11563 return -ENOMEM;
11564
11565 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11566 NL80211_CMD_GET_PROTOCOL_FEATURES);
11567 if (!hdr)
11568 goto nla_put_failure;
11569
11570 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
11571 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
11572 goto nla_put_failure;
11573
11574 genlmsg_end(msg, hdr);
11575 return genlmsg_reply(msg, info);
11576
11577 nla_put_failure:
11578 kfree_skb(msg);
11579 return -ENOBUFS;
11580}
11581
Jouni Malinen355199e2013-02-27 17:14:27 +020011582static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
11583{
11584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11585 struct cfg80211_update_ft_ies_params ft_params;
11586 struct net_device *dev = info->user_ptr[1];
11587
11588 if (!rdev->ops->update_ft_ies)
11589 return -EOPNOTSUPP;
11590
11591 if (!info->attrs[NL80211_ATTR_MDID] ||
11592 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
11593 return -EINVAL;
11594
11595 memset(&ft_params, 0, sizeof(ft_params));
11596 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
11597 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11598 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11599
11600 return rdev_update_ft_ies(rdev, dev, &ft_params);
11601}
11602
Arend van Spriel5de17982013-04-18 15:49:00 +020011603static int nl80211_crit_protocol_start(struct sk_buff *skb,
11604 struct genl_info *info)
11605{
11606 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11607 struct wireless_dev *wdev = info->user_ptr[1];
11608 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
11609 u16 duration;
11610 int ret;
11611
11612 if (!rdev->ops->crit_proto_start)
11613 return -EOPNOTSUPP;
11614
11615 if (WARN_ON(!rdev->ops->crit_proto_stop))
11616 return -EINVAL;
11617
11618 if (rdev->crit_proto_nlportid)
11619 return -EBUSY;
11620
11621 /* determine protocol if provided */
11622 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
11623 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
11624
11625 if (proto >= NUM_NL80211_CRIT_PROTO)
11626 return -EINVAL;
11627
11628 /* timeout must be provided */
11629 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
11630 return -EINVAL;
11631
11632 duration =
11633 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
11634
11635 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
11636 return -ERANGE;
11637
11638 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
11639 if (!ret)
11640 rdev->crit_proto_nlportid = info->snd_portid;
11641
11642 return ret;
11643}
11644
11645static int nl80211_crit_protocol_stop(struct sk_buff *skb,
11646 struct genl_info *info)
11647{
11648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11649 struct wireless_dev *wdev = info->user_ptr[1];
11650
11651 if (!rdev->ops->crit_proto_stop)
11652 return -EOPNOTSUPP;
11653
11654 if (rdev->crit_proto_nlportid) {
11655 rdev->crit_proto_nlportid = 0;
11656 rdev_crit_proto_stop(rdev, wdev);
11657 }
11658 return 0;
11659}
11660
Johannes Bergad7e7182013-11-13 13:37:47 +010011661static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
11662{
11663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11664 struct wireless_dev *wdev =
11665 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
11666 int i, err;
11667 u32 vid, subcmd;
11668
11669 if (!rdev->wiphy.vendor_commands)
11670 return -EOPNOTSUPP;
11671
11672 if (IS_ERR(wdev)) {
11673 err = PTR_ERR(wdev);
11674 if (err != -EINVAL)
11675 return err;
11676 wdev = NULL;
11677 } else if (wdev->wiphy != &rdev->wiphy) {
11678 return -EINVAL;
11679 }
11680
11681 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
11682 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
11683 return -EINVAL;
11684
11685 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
11686 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
11687 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
11688 const struct wiphy_vendor_command *vcmd;
11689 void *data = NULL;
11690 int len = 0;
11691
11692 vcmd = &rdev->wiphy.vendor_commands[i];
11693
11694 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11695 continue;
11696
11697 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
11698 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
11699 if (!wdev)
11700 return -EINVAL;
11701 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
11702 !wdev->netdev)
11703 return -EINVAL;
11704
11705 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
Arend Van Spriel73c7da32016-10-20 20:08:22 +010011706 if (!wdev_running(wdev))
Johannes Bergad7e7182013-11-13 13:37:47 +010011707 return -ENETDOWN;
11708 }
Johannes Berg7bdbe402015-08-15 22:39:49 +030011709
11710 if (!vcmd->doit)
11711 return -EOPNOTSUPP;
Johannes Bergad7e7182013-11-13 13:37:47 +010011712 } else {
11713 wdev = NULL;
11714 }
11715
11716 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
11717 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11718 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11719 }
11720
11721 rdev->cur_cmd_info = info;
11722 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
11723 data, len);
11724 rdev->cur_cmd_info = NULL;
11725 return err;
11726 }
11727
11728 return -EOPNOTSUPP;
11729}
11730
Johannes Berg7bdbe402015-08-15 22:39:49 +030011731static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
11732 struct netlink_callback *cb,
11733 struct cfg80211_registered_device **rdev,
11734 struct wireless_dev **wdev)
11735{
Johannes Bergc90c39d2016-10-24 14:40:01 +020011736 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
Johannes Berg7bdbe402015-08-15 22:39:49 +030011737 u32 vid, subcmd;
11738 unsigned int i;
11739 int vcmd_idx = -1;
11740 int err;
11741 void *data = NULL;
11742 unsigned int data_len = 0;
11743
11744 rtnl_lock();
11745
11746 if (cb->args[0]) {
11747 /* subtract the 1 again here */
11748 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
11749 struct wireless_dev *tmp;
11750
11751 if (!wiphy) {
11752 err = -ENODEV;
11753 goto out_unlock;
11754 }
11755 *rdev = wiphy_to_rdev(wiphy);
11756 *wdev = NULL;
11757
11758 if (cb->args[1]) {
Johannes Berg53873f12016-05-03 16:52:04 +030011759 list_for_each_entry(tmp, &wiphy->wdev_list, list) {
Johannes Berg7bdbe402015-08-15 22:39:49 +030011760 if (tmp->identifier == cb->args[1] - 1) {
11761 *wdev = tmp;
11762 break;
11763 }
11764 }
11765 }
11766
11767 /* keep rtnl locked in successful case */
11768 return 0;
11769 }
11770
11771 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
Johannes Bergc90c39d2016-10-24 14:40:01 +020011772 attrbuf, nl80211_fam.maxattr, nl80211_policy);
Johannes Berg7bdbe402015-08-15 22:39:49 +030011773 if (err)
11774 goto out_unlock;
11775
Johannes Bergc90c39d2016-10-24 14:40:01 +020011776 if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
11777 !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
Johannes Berg7bdbe402015-08-15 22:39:49 +030011778 err = -EINVAL;
11779 goto out_unlock;
11780 }
11781
Johannes Bergc90c39d2016-10-24 14:40:01 +020011782 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
Johannes Berg7bdbe402015-08-15 22:39:49 +030011783 if (IS_ERR(*wdev))
11784 *wdev = NULL;
11785
Johannes Bergc90c39d2016-10-24 14:40:01 +020011786 *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
Johannes Berg7bdbe402015-08-15 22:39:49 +030011787 if (IS_ERR(*rdev)) {
11788 err = PTR_ERR(*rdev);
11789 goto out_unlock;
11790 }
11791
Johannes Bergc90c39d2016-10-24 14:40:01 +020011792 vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
11793 subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
Johannes Berg7bdbe402015-08-15 22:39:49 +030011794
11795 for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
11796 const struct wiphy_vendor_command *vcmd;
11797
11798 vcmd = &(*rdev)->wiphy.vendor_commands[i];
11799
11800 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11801 continue;
11802
11803 if (!vcmd->dumpit) {
11804 err = -EOPNOTSUPP;
11805 goto out_unlock;
11806 }
11807
11808 vcmd_idx = i;
11809 break;
11810 }
11811
11812 if (vcmd_idx < 0) {
11813 err = -EOPNOTSUPP;
11814 goto out_unlock;
11815 }
11816
Johannes Bergc90c39d2016-10-24 14:40:01 +020011817 if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
11818 data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
11819 data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
Johannes Berg7bdbe402015-08-15 22:39:49 +030011820 }
11821
11822 /* 0 is the first index - add 1 to parse only once */
11823 cb->args[0] = (*rdev)->wiphy_idx + 1;
11824 /* add 1 to know if it was NULL */
11825 cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
11826 cb->args[2] = vcmd_idx;
11827 cb->args[3] = (unsigned long)data;
11828 cb->args[4] = data_len;
11829
11830 /* keep rtnl locked in successful case */
11831 return 0;
11832 out_unlock:
11833 rtnl_unlock();
11834 return err;
11835}
11836
11837static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
11838 struct netlink_callback *cb)
11839{
11840 struct cfg80211_registered_device *rdev;
11841 struct wireless_dev *wdev;
11842 unsigned int vcmd_idx;
11843 const struct wiphy_vendor_command *vcmd;
11844 void *data;
11845 int data_len;
11846 int err;
11847 struct nlattr *vendor_data;
11848
11849 err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
11850 if (err)
11851 return err;
11852
11853 vcmd_idx = cb->args[2];
11854 data = (void *)cb->args[3];
11855 data_len = cb->args[4];
11856 vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
11857
11858 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
11859 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
11860 if (!wdev)
11861 return -EINVAL;
11862 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
11863 !wdev->netdev)
11864 return -EINVAL;
11865
11866 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
Arend Van Spriel73c7da32016-10-20 20:08:22 +010011867 if (!wdev_running(wdev))
Johannes Berg7bdbe402015-08-15 22:39:49 +030011868 return -ENETDOWN;
11869 }
11870 }
11871
11872 while (1) {
11873 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11874 cb->nlh->nlmsg_seq, NLM_F_MULTI,
11875 NL80211_CMD_VENDOR);
11876 if (!hdr)
11877 break;
11878
11879 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020011880 (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11881 wdev_id(wdev),
11882 NL80211_ATTR_PAD))) {
Johannes Berg7bdbe402015-08-15 22:39:49 +030011883 genlmsg_cancel(skb, hdr);
11884 break;
11885 }
11886
11887 vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
11888 if (!vendor_data) {
11889 genlmsg_cancel(skb, hdr);
11890 break;
11891 }
11892
11893 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
11894 (unsigned long *)&cb->args[5]);
11895 nla_nest_end(skb, vendor_data);
11896
11897 if (err == -ENOBUFS || err == -ENOENT) {
11898 genlmsg_cancel(skb, hdr);
11899 break;
11900 } else if (err) {
11901 genlmsg_cancel(skb, hdr);
11902 goto out;
11903 }
11904
11905 genlmsg_end(skb, hdr);
11906 }
11907
11908 err = skb->len;
11909 out:
11910 rtnl_unlock();
11911 return err;
11912}
11913
Johannes Bergad7e7182013-11-13 13:37:47 +010011914struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
11915 enum nl80211_commands cmd,
11916 enum nl80211_attrs attr,
11917 int approxlen)
11918{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011919 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Bergad7e7182013-11-13 13:37:47 +010011920
11921 if (WARN_ON(!rdev->cur_cmd_info))
11922 return NULL;
11923
Ahmad Kholaif6c09e792015-02-26 15:26:53 +020011924 return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
Johannes Bergad7e7182013-11-13 13:37:47 +010011925 rdev->cur_cmd_info->snd_portid,
11926 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +010011927 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +010011928}
11929EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
11930
11931int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
11932{
11933 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11934 void *hdr = ((void **)skb->cb)[1];
11935 struct nlattr *data = ((void **)skb->cb)[2];
11936
Johannes Bergbd8c78e2014-07-30 14:55:26 +020011937 /* clear CB data for netlink core to own from now on */
11938 memset(skb->cb, 0, sizeof(skb->cb));
11939
Johannes Bergad7e7182013-11-13 13:37:47 +010011940 if (WARN_ON(!rdev->cur_cmd_info)) {
11941 kfree_skb(skb);
11942 return -EINVAL;
11943 }
11944
11945 nla_nest_end(skb, data);
11946 genlmsg_end(skb, hdr);
11947 return genlmsg_reply(skb, rdev->cur_cmd_info);
11948}
11949EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
11950
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080011951static int nl80211_set_qos_map(struct sk_buff *skb,
11952 struct genl_info *info)
11953{
11954 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11955 struct cfg80211_qos_map *qos_map = NULL;
11956 struct net_device *dev = info->user_ptr[1];
11957 u8 *pos, len, num_des, des_len, des;
11958 int ret;
11959
11960 if (!rdev->ops->set_qos_map)
11961 return -EOPNOTSUPP;
11962
11963 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
11964 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
11965 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
11966
11967 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
11968 len > IEEE80211_QOS_MAP_LEN_MAX)
11969 return -EINVAL;
11970
11971 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
11972 if (!qos_map)
11973 return -ENOMEM;
11974
11975 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
11976 if (num_des) {
11977 des_len = num_des *
11978 sizeof(struct cfg80211_dscp_exception);
11979 memcpy(qos_map->dscp_exception, pos, des_len);
11980 qos_map->num_des = num_des;
11981 for (des = 0; des < num_des; des++) {
11982 if (qos_map->dscp_exception[des].up > 7) {
11983 kfree(qos_map);
11984 return -EINVAL;
11985 }
11986 }
11987 pos += des_len;
11988 }
11989 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
11990 }
11991
11992 wdev_lock(dev->ieee80211_ptr);
11993 ret = nl80211_key_allowed(dev->ieee80211_ptr);
11994 if (!ret)
11995 ret = rdev_set_qos_map(rdev, dev, qos_map);
11996 wdev_unlock(dev->ieee80211_ptr);
11997
11998 kfree(qos_map);
11999 return ret;
12000}
12001
Johannes Berg960d01a2014-09-09 22:55:35 +030012002static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
12003{
12004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12005 struct net_device *dev = info->user_ptr[1];
12006 struct wireless_dev *wdev = dev->ieee80211_ptr;
12007 const u8 *peer;
12008 u8 tsid, up;
12009 u16 admitted_time = 0;
12010 int err;
12011
Johannes Berg723e73a2014-10-22 09:25:06 +020012012 if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
Johannes Berg960d01a2014-09-09 22:55:35 +030012013 return -EOPNOTSUPP;
12014
12015 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
12016 !info->attrs[NL80211_ATTR_USER_PRIO])
12017 return -EINVAL;
12018
12019 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12020 if (tsid >= IEEE80211_NUM_TIDS)
12021 return -EINVAL;
12022
12023 up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
12024 if (up >= IEEE80211_NUM_UPS)
12025 return -EINVAL;
12026
12027 /* WMM uses TIDs 0-7 even for TSPEC */
Johannes Berg723e73a2014-10-22 09:25:06 +020012028 if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
Johannes Berg960d01a2014-09-09 22:55:35 +030012029 /* TODO: handle 802.11 TSPEC/admission control
Johannes Berg723e73a2014-10-22 09:25:06 +020012030 * need more attributes for that (e.g. BA session requirement);
12031 * change the WMM adminssion test above to allow both then
Johannes Berg960d01a2014-09-09 22:55:35 +030012032 */
12033 return -EINVAL;
12034 }
12035
12036 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12037
12038 if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
12039 admitted_time =
12040 nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
12041 if (!admitted_time)
12042 return -EINVAL;
12043 }
12044
12045 wdev_lock(wdev);
12046 switch (wdev->iftype) {
12047 case NL80211_IFTYPE_STATION:
12048 case NL80211_IFTYPE_P2P_CLIENT:
12049 if (wdev->current_bss)
12050 break;
12051 err = -ENOTCONN;
12052 goto out;
12053 default:
12054 err = -EOPNOTSUPP;
12055 goto out;
12056 }
12057
12058 err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
12059
12060 out:
12061 wdev_unlock(wdev);
12062 return err;
12063}
12064
12065static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
12066{
12067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12068 struct net_device *dev = info->user_ptr[1];
12069 struct wireless_dev *wdev = dev->ieee80211_ptr;
12070 const u8 *peer;
12071 u8 tsid;
12072 int err;
12073
12074 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
12075 return -EINVAL;
12076
12077 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12078 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12079
12080 wdev_lock(wdev);
12081 err = rdev_del_tx_ts(rdev, dev, tsid, peer);
12082 wdev_unlock(wdev);
12083
12084 return err;
12085}
12086
Arik Nemtsov1057d352014-11-19 12:54:26 +020012087static int nl80211_tdls_channel_switch(struct sk_buff *skb,
12088 struct genl_info *info)
12089{
12090 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12091 struct net_device *dev = info->user_ptr[1];
12092 struct wireless_dev *wdev = dev->ieee80211_ptr;
12093 struct cfg80211_chan_def chandef = {};
12094 const u8 *addr;
12095 u8 oper_class;
12096 int err;
12097
12098 if (!rdev->ops->tdls_channel_switch ||
12099 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12100 return -EOPNOTSUPP;
12101
12102 switch (dev->ieee80211_ptr->iftype) {
12103 case NL80211_IFTYPE_STATION:
12104 case NL80211_IFTYPE_P2P_CLIENT:
12105 break;
12106 default:
12107 return -EOPNOTSUPP;
12108 }
12109
12110 if (!info->attrs[NL80211_ATTR_MAC] ||
12111 !info->attrs[NL80211_ATTR_OPER_CLASS])
12112 return -EINVAL;
12113
12114 err = nl80211_parse_chandef(rdev, info, &chandef);
12115 if (err)
12116 return err;
12117
12118 /*
12119 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
12120 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
12121 * specification is not defined for them.
12122 */
Johannes Berg57fbcce2016-04-12 15:56:15 +020012123 if (chandef.chan->band == NL80211_BAND_2GHZ &&
Arik Nemtsov1057d352014-11-19 12:54:26 +020012124 chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
12125 chandef.width != NL80211_CHAN_WIDTH_20)
12126 return -EINVAL;
12127
12128 /* we will be active on the TDLS link */
Arik Nemtsov923b3522015-07-08 15:41:44 +030012129 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
12130 wdev->iftype))
Arik Nemtsov1057d352014-11-19 12:54:26 +020012131 return -EINVAL;
12132
12133 /* don't allow switching to DFS channels */
12134 if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
12135 return -EINVAL;
12136
12137 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12138 oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
12139
12140 wdev_lock(wdev);
12141 err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
12142 wdev_unlock(wdev);
12143
12144 return err;
12145}
12146
12147static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
12148 struct genl_info *info)
12149{
12150 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12151 struct net_device *dev = info->user_ptr[1];
12152 struct wireless_dev *wdev = dev->ieee80211_ptr;
12153 const u8 *addr;
12154
12155 if (!rdev->ops->tdls_channel_switch ||
12156 !rdev->ops->tdls_cancel_channel_switch ||
12157 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12158 return -EOPNOTSUPP;
12159
12160 switch (dev->ieee80211_ptr->iftype) {
12161 case NL80211_IFTYPE_STATION:
12162 case NL80211_IFTYPE_P2P_CLIENT:
12163 break;
12164 default:
12165 return -EOPNOTSUPP;
12166 }
12167
12168 if (!info->attrs[NL80211_ATTR_MAC])
12169 return -EINVAL;
12170
12171 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12172
12173 wdev_lock(wdev);
12174 rdev_tdls_cancel_channel_switch(rdev, dev, addr);
12175 wdev_unlock(wdev);
12176
12177 return 0;
12178}
12179
Michael Braunce0ce132016-10-10 19:12:22 +020012180static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
12181 struct genl_info *info)
12182{
12183 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12184 struct net_device *dev = info->user_ptr[1];
12185 struct wireless_dev *wdev = dev->ieee80211_ptr;
12186 const struct nlattr *nla;
12187 bool enabled;
12188
Michael Braunce0ce132016-10-10 19:12:22 +020012189 if (!rdev->ops->set_multicast_to_unicast)
12190 return -EOPNOTSUPP;
12191
12192 if (wdev->iftype != NL80211_IFTYPE_AP &&
12193 wdev->iftype != NL80211_IFTYPE_P2P_GO)
12194 return -EOPNOTSUPP;
12195
12196 nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
12197 enabled = nla_get_flag(nla);
12198
12199 return rdev_set_multicast_to_unicast(rdev, dev, enabled);
12200}
12201
Johannes Berg4c476992010-10-04 21:36:35 +020012202#define NL80211_FLAG_NEED_WIPHY 0x01
12203#define NL80211_FLAG_NEED_NETDEV 0x02
12204#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +020012205#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
12206#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
12207 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +020012208#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +020012209/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +020012210#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
12211 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg5393b912014-09-10 15:00:16 +030012212#define NL80211_FLAG_CLEAR_SKB 0x20
Johannes Berg4c476992010-10-04 21:36:35 +020012213
Johannes Bergf84f7712013-11-14 17:14:45 +010012214static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020012215 struct genl_info *info)
12216{
12217 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +020012218 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +020012219 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +020012220 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
12221
12222 if (rtnl)
12223 rtnl_lock();
12224
12225 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +020012226 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +020012227 if (IS_ERR(rdev)) {
12228 if (rtnl)
12229 rtnl_unlock();
12230 return PTR_ERR(rdev);
12231 }
12232 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +020012233 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
12234 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +020012235 ASSERT_RTNL();
12236
Johannes Berg89a54e42012-06-15 14:33:17 +020012237 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
12238 info->attrs);
12239 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +020012240 if (rtnl)
12241 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +020012242 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +020012243 }
Johannes Berg89a54e42012-06-15 14:33:17 +020012244
Johannes Berg89a54e42012-06-15 14:33:17 +020012245 dev = wdev->netdev;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012246 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg89a54e42012-06-15 14:33:17 +020012247
Johannes Berg1bf614e2012-06-15 15:23:36 +020012248 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
12249 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +020012250 if (rtnl)
12251 rtnl_unlock();
12252 return -EINVAL;
12253 }
12254
12255 info->user_ptr[1] = dev;
12256 } else {
12257 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +020012258 }
Johannes Berg89a54e42012-06-15 14:33:17 +020012259
Arend Van Spriel73c7da32016-10-20 20:08:22 +010012260 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
12261 !wdev_running(wdev)) {
12262 if (rtnl)
12263 rtnl_unlock();
12264 return -ENETDOWN;
Johannes Berg1bf614e2012-06-15 15:23:36 +020012265 }
12266
Arend Van Spriel73c7da32016-10-20 20:08:22 +010012267 if (dev)
12268 dev_hold(dev);
12269
Johannes Berg4c476992010-10-04 21:36:35 +020012270 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +020012271 }
12272
12273 return 0;
12274}
12275
Johannes Bergf84f7712013-11-14 17:14:45 +010012276static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020012277 struct genl_info *info)
12278{
Johannes Berg1bf614e2012-06-15 15:23:36 +020012279 if (info->user_ptr[1]) {
12280 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12281 struct wireless_dev *wdev = info->user_ptr[1];
12282
12283 if (wdev->netdev)
12284 dev_put(wdev->netdev);
12285 } else {
12286 dev_put(info->user_ptr[1]);
12287 }
12288 }
Johannes Berg5393b912014-09-10 15:00:16 +030012289
Johannes Berg4c476992010-10-04 21:36:35 +020012290 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
12291 rtnl_unlock();
Johannes Berg5393b912014-09-10 15:00:16 +030012292
12293 /* If needed, clear the netlink message payload from the SKB
12294 * as it might contain key data that shouldn't stick around on
12295 * the heap after the SKB is freed. The netlink message header
12296 * is still needed for further processing, so leave it intact.
12297 */
12298 if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
12299 struct nlmsghdr *nlh = nlmsg_hdr(skb);
12300
12301 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
12302 }
Johannes Berg4c476992010-10-04 21:36:35 +020012303}
12304
Johannes Berg4534de82013-11-14 17:14:46 +010012305static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -040012306 {
12307 .cmd = NL80211_CMD_GET_WIPHY,
12308 .doit = nl80211_get_wiphy,
12309 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +020012310 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -040012311 .policy = nl80211_policy,
12312 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +020012313 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12314 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040012315 },
12316 {
12317 .cmd = NL80211_CMD_SET_WIPHY,
12318 .doit = nl80211_set_wiphy,
12319 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012320 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012321 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040012322 },
12323 {
12324 .cmd = NL80211_CMD_GET_INTERFACE,
12325 .doit = nl80211_get_interface,
12326 .dumpit = nl80211_dump_interface,
12327 .policy = nl80211_policy,
12328 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +020012329 .internal_flags = NL80211_FLAG_NEED_WDEV |
12330 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040012331 },
12332 {
12333 .cmd = NL80211_CMD_SET_INTERFACE,
12334 .doit = nl80211_set_interface,
12335 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012336 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012337 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12338 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040012339 },
12340 {
12341 .cmd = NL80211_CMD_NEW_INTERFACE,
12342 .doit = nl80211_new_interface,
12343 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012344 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012345 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12346 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040012347 },
12348 {
12349 .cmd = NL80211_CMD_DEL_INTERFACE,
12350 .doit = nl80211_del_interface,
12351 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012352 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +020012353 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +020012354 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040012355 },
Johannes Berg41ade002007-12-19 02:03:29 +010012356 {
12357 .cmd = NL80211_CMD_GET_KEY,
12358 .doit = nl80211_get_key,
12359 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012360 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012361 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012362 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +010012363 },
12364 {
12365 .cmd = NL80211_CMD_SET_KEY,
12366 .doit = nl80211_set_key,
12367 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012368 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012369 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030012370 NL80211_FLAG_NEED_RTNL |
12371 NL80211_FLAG_CLEAR_SKB,
Johannes Berg41ade002007-12-19 02:03:29 +010012372 },
12373 {
12374 .cmd = NL80211_CMD_NEW_KEY,
12375 .doit = nl80211_new_key,
12376 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012377 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012378 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030012379 NL80211_FLAG_NEED_RTNL |
12380 NL80211_FLAG_CLEAR_SKB,
Johannes Berg41ade002007-12-19 02:03:29 +010012381 },
12382 {
12383 .cmd = NL80211_CMD_DEL_KEY,
12384 .doit = nl80211_del_key,
12385 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012386 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012387 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012388 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +010012389 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010012390 {
12391 .cmd = NL80211_CMD_SET_BEACON,
12392 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012393 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010012394 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012395 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012396 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010012397 },
12398 {
Johannes Berg88600202012-02-13 15:17:18 +010012399 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +010012400 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012401 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010012402 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012403 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012404 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010012405 },
12406 {
Johannes Berg88600202012-02-13 15:17:18 +010012407 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +010012408 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012409 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010012410 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012411 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012412 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010012413 },
Johannes Berg5727ef12007-12-19 02:03:34 +010012414 {
12415 .cmd = NL80211_CMD_GET_STATION,
12416 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012417 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +010012418 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +020012419 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12420 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010012421 },
12422 {
12423 .cmd = NL80211_CMD_SET_STATION,
12424 .doit = nl80211_set_station,
12425 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012426 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012427 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012428 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010012429 },
12430 {
12431 .cmd = NL80211_CMD_NEW_STATION,
12432 .doit = nl80211_new_station,
12433 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012434 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012435 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012436 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010012437 },
12438 {
12439 .cmd = NL80211_CMD_DEL_STATION,
12440 .doit = nl80211_del_station,
12441 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012442 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012443 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012444 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010012445 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012446 {
12447 .cmd = NL80211_CMD_GET_MPATH,
12448 .doit = nl80211_get_mpath,
12449 .dumpit = nl80211_dump_mpath,
12450 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012451 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012452 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012453 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012454 },
12455 {
Henning Rogge66be7d22014-09-12 08:58:49 +020012456 .cmd = NL80211_CMD_GET_MPP,
12457 .doit = nl80211_get_mpp,
12458 .dumpit = nl80211_dump_mpp,
12459 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012460 .flags = GENL_UNS_ADMIN_PERM,
Henning Rogge66be7d22014-09-12 08:58:49 +020012461 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12462 NL80211_FLAG_NEED_RTNL,
12463 },
12464 {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012465 .cmd = NL80211_CMD_SET_MPATH,
12466 .doit = nl80211_set_mpath,
12467 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012468 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012470 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012471 },
12472 {
12473 .cmd = NL80211_CMD_NEW_MPATH,
12474 .doit = nl80211_new_mpath,
12475 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012476 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012478 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012479 },
12480 {
12481 .cmd = NL80211_CMD_DEL_MPATH,
12482 .doit = nl80211_del_mpath,
12483 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012484 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012486 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010012487 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030012488 {
12489 .cmd = NL80211_CMD_SET_BSS,
12490 .doit = nl80211_set_bss,
12491 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012492 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012493 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012494 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +030012495 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012496 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080012497 .cmd = NL80211_CMD_GET_REG,
Arik Nemtsovad30ca22014-12-15 19:25:59 +020012498 .doit = nl80211_get_reg_do,
12499 .dumpit = nl80211_get_reg_dump,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080012500 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +020012501 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080012502 /* can be retrieved by unprivileged users */
12503 },
Johannes Bergb6863032015-10-15 09:25:18 +020012504#ifdef CONFIG_CFG80211_CRDA_SUPPORT
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080012505 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012506 .cmd = NL80211_CMD_SET_REG,
12507 .doit = nl80211_set_reg,
12508 .policy = nl80211_policy,
12509 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +020012510 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012511 },
Johannes Bergb6863032015-10-15 09:25:18 +020012512#endif
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070012513 {
12514 .cmd = NL80211_CMD_REQ_SET_REG,
12515 .doit = nl80211_req_set_reg,
12516 .policy = nl80211_policy,
12517 .flags = GENL_ADMIN_PERM,
12518 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070012519 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -080012520 .cmd = NL80211_CMD_GET_MESH_CONFIG,
12521 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070012522 .policy = nl80211_policy,
12523 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012524 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012525 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070012526 },
12527 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -080012528 .cmd = NL80211_CMD_SET_MESH_CONFIG,
12529 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070012530 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012531 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +010012532 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012533 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070012534 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +020012535 {
Johannes Berg2a519312009-02-10 21:25:55 +010012536 .cmd = NL80211_CMD_TRIGGER_SCAN,
12537 .doit = nl80211_trigger_scan,
12538 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012539 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +020012540 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012541 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +010012542 },
12543 {
Vidyullatha Kanchanapally91d3ab42015-10-30 19:14:49 +053012544 .cmd = NL80211_CMD_ABORT_SCAN,
12545 .doit = nl80211_abort_scan,
12546 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012547 .flags = GENL_UNS_ADMIN_PERM,
Vidyullatha Kanchanapally91d3ab42015-10-30 19:14:49 +053012548 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12549 NL80211_FLAG_NEED_RTNL,
12550 },
12551 {
Johannes Berg2a519312009-02-10 21:25:55 +010012552 .cmd = NL80211_CMD_GET_SCAN,
12553 .policy = nl80211_policy,
12554 .dumpit = nl80211_dump_scan,
12555 },
Jouni Malinen636a5d32009-03-19 13:39:22 +020012556 {
Luciano Coelho807f8a82011-05-11 17:09:35 +030012557 .cmd = NL80211_CMD_START_SCHED_SCAN,
12558 .doit = nl80211_start_sched_scan,
12559 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012560 .flags = GENL_UNS_ADMIN_PERM,
Luciano Coelho807f8a82011-05-11 17:09:35 +030012561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12562 NL80211_FLAG_NEED_RTNL,
12563 },
12564 {
12565 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
12566 .doit = nl80211_stop_sched_scan,
12567 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012568 .flags = GENL_UNS_ADMIN_PERM,
Luciano Coelho807f8a82011-05-11 17:09:35 +030012569 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12570 NL80211_FLAG_NEED_RTNL,
12571 },
12572 {
Jouni Malinen636a5d32009-03-19 13:39:22 +020012573 .cmd = NL80211_CMD_AUTHENTICATE,
12574 .doit = nl80211_authenticate,
12575 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012576 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012577 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030012578 NL80211_FLAG_NEED_RTNL |
12579 NL80211_FLAG_CLEAR_SKB,
Jouni Malinen636a5d32009-03-19 13:39:22 +020012580 },
12581 {
12582 .cmd = NL80211_CMD_ASSOCIATE,
12583 .doit = nl80211_associate,
12584 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012585 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012586 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012587 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020012588 },
12589 {
12590 .cmd = NL80211_CMD_DEAUTHENTICATE,
12591 .doit = nl80211_deauthenticate,
12592 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012593 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012594 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012595 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020012596 },
12597 {
12598 .cmd = NL80211_CMD_DISASSOCIATE,
12599 .doit = nl80211_disassociate,
12600 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012601 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012603 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020012604 },
Johannes Berg04a773a2009-04-19 21:24:32 +020012605 {
12606 .cmd = NL80211_CMD_JOIN_IBSS,
12607 .doit = nl80211_join_ibss,
12608 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012609 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012610 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012611 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +020012612 },
12613 {
12614 .cmd = NL80211_CMD_LEAVE_IBSS,
12615 .doit = nl80211_leave_ibss,
12616 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012617 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012618 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012619 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +020012620 },
Johannes Bergaff89a92009-07-01 21:26:51 +020012621#ifdef CONFIG_NL80211_TESTMODE
12622 {
12623 .cmd = NL80211_CMD_TESTMODE,
12624 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -070012625 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +020012626 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012627 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012628 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12629 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +020012630 },
12631#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +020012632 {
12633 .cmd = NL80211_CMD_CONNECT,
12634 .doit = nl80211_connect,
12635 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012636 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012638 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +020012639 },
12640 {
vamsi krishna088e8df2016-10-27 16:51:11 +030012641 .cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
12642 .doit = nl80211_update_connect_params,
12643 .policy = nl80211_policy,
12644 .flags = GENL_ADMIN_PERM,
12645 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12646 NL80211_FLAG_NEED_RTNL,
12647 },
12648 {
Samuel Ortizb23aa672009-07-01 21:26:54 +020012649 .cmd = NL80211_CMD_DISCONNECT,
12650 .doit = nl80211_disconnect,
12651 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012652 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020012653 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012654 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +020012655 },
Johannes Berg463d0182009-07-14 00:33:35 +020012656 {
12657 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
12658 .doit = nl80211_wiphy_netns,
12659 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012660 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012661 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12662 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +020012663 },
Holger Schurig61fa7132009-11-11 12:25:40 +010012664 {
12665 .cmd = NL80211_CMD_GET_SURVEY,
12666 .policy = nl80211_policy,
12667 .dumpit = nl80211_dump_survey,
12668 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +010012669 {
12670 .cmd = NL80211_CMD_SET_PMKSA,
12671 .doit = nl80211_setdel_pmksa,
12672 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012673 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012674 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012675 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010012676 },
12677 {
12678 .cmd = NL80211_CMD_DEL_PMKSA,
12679 .doit = nl80211_setdel_pmksa,
12680 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012681 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012682 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012683 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010012684 },
12685 {
12686 .cmd = NL80211_CMD_FLUSH_PMKSA,
12687 .doit = nl80211_flush_pmksa,
12688 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012689 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012690 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012691 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010012692 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +010012693 {
12694 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
12695 .doit = nl80211_remain_on_channel,
12696 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012697 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020012698 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012699 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010012700 },
12701 {
12702 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
12703 .doit = nl80211_cancel_remain_on_channel,
12704 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012705 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020012706 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012707 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010012708 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +020012709 {
12710 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
12711 .doit = nl80211_set_tx_bitrate_mask,
12712 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012713 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012714 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12715 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +020012716 },
Jouni Malinen026331c2010-02-15 12:53:10 +020012717 {
Johannes Berg2e161f72010-08-12 15:38:38 +020012718 .cmd = NL80211_CMD_REGISTER_FRAME,
12719 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +020012720 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012721 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020012722 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +020012723 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +020012724 },
12725 {
Johannes Berg2e161f72010-08-12 15:38:38 +020012726 .cmd = NL80211_CMD_FRAME,
12727 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +020012728 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012729 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020012730 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020012731 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +020012732 },
Kalle Valoffb9eb32010-02-17 17:58:10 +020012733 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +010012734 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
12735 .doit = nl80211_tx_mgmt_cancel_wait,
12736 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012737 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020012738 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +010012739 NL80211_FLAG_NEED_RTNL,
12740 },
12741 {
Kalle Valoffb9eb32010-02-17 17:58:10 +020012742 .cmd = NL80211_CMD_SET_POWER_SAVE,
12743 .doit = nl80211_set_power_save,
12744 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012745 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012746 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12747 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +020012748 },
12749 {
12750 .cmd = NL80211_CMD_GET_POWER_SAVE,
12751 .doit = nl80211_get_power_save,
12752 .policy = nl80211_policy,
12753 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +020012754 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12755 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +020012756 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020012757 {
12758 .cmd = NL80211_CMD_SET_CQM,
12759 .doit = nl80211_set_cqm,
12760 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012761 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012762 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12763 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020012764 },
Johannes Bergf444de02010-05-05 15:25:02 +020012765 {
12766 .cmd = NL80211_CMD_SET_CHANNEL,
12767 .doit = nl80211_set_channel,
12768 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012769 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020012770 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12771 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +020012772 },
Bill Jordane8347eb2010-10-01 13:54:28 -040012773 {
12774 .cmd = NL80211_CMD_SET_WDS_PEER,
12775 .doit = nl80211_set_wds_peer,
12776 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012777 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +020012778 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12779 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -040012780 },
Johannes Berg29cbe682010-12-03 09:20:44 +010012781 {
12782 .cmd = NL80211_CMD_JOIN_MESH,
12783 .doit = nl80211_join_mesh,
12784 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012785 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +010012786 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12787 NL80211_FLAG_NEED_RTNL,
12788 },
12789 {
12790 .cmd = NL80211_CMD_LEAVE_MESH,
12791 .doit = nl80211_leave_mesh,
12792 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012793 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +010012794 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12795 NL80211_FLAG_NEED_RTNL,
12796 },
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +010012797 {
12798 .cmd = NL80211_CMD_JOIN_OCB,
12799 .doit = nl80211_join_ocb,
12800 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012801 .flags = GENL_UNS_ADMIN_PERM,
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +010012802 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12803 NL80211_FLAG_NEED_RTNL,
12804 },
12805 {
12806 .cmd = NL80211_CMD_LEAVE_OCB,
12807 .doit = nl80211_leave_ocb,
12808 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012809 .flags = GENL_UNS_ADMIN_PERM,
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +010012810 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12811 NL80211_FLAG_NEED_RTNL,
12812 },
Johannes Bergdfb89c52012-06-27 09:23:48 +020012813#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +020012814 {
12815 .cmd = NL80211_CMD_GET_WOWLAN,
12816 .doit = nl80211_get_wowlan,
12817 .policy = nl80211_policy,
12818 /* can be retrieved by unprivileged users */
12819 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12820 NL80211_FLAG_NEED_RTNL,
12821 },
12822 {
12823 .cmd = NL80211_CMD_SET_WOWLAN,
12824 .doit = nl80211_set_wowlan,
12825 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012826 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergff1b6e62011-05-04 15:37:28 +020012827 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12828 NL80211_FLAG_NEED_RTNL,
12829 },
Johannes Bergdfb89c52012-06-27 09:23:48 +020012830#endif
Johannes Berge5497d72011-07-05 16:35:40 +020012831 {
12832 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
12833 .doit = nl80211_set_rekey_data,
12834 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012835 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berge5497d72011-07-05 16:35:40 +020012836 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030012837 NL80211_FLAG_NEED_RTNL |
12838 NL80211_FLAG_CLEAR_SKB,
Johannes Berge5497d72011-07-05 16:35:40 +020012839 },
Arik Nemtsov109086c2011-09-28 14:12:50 +030012840 {
12841 .cmd = NL80211_CMD_TDLS_MGMT,
12842 .doit = nl80211_tdls_mgmt,
12843 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012844 .flags = GENL_UNS_ADMIN_PERM,
Arik Nemtsov109086c2011-09-28 14:12:50 +030012845 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12846 NL80211_FLAG_NEED_RTNL,
12847 },
12848 {
12849 .cmd = NL80211_CMD_TDLS_OPER,
12850 .doit = nl80211_tdls_oper,
12851 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012852 .flags = GENL_UNS_ADMIN_PERM,
Arik Nemtsov109086c2011-09-28 14:12:50 +030012853 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12854 NL80211_FLAG_NEED_RTNL,
12855 },
Johannes Berg28946da2011-11-04 11:18:12 +010012856 {
12857 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
12858 .doit = nl80211_register_unexpected_frame,
12859 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012860 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg28946da2011-11-04 11:18:12 +010012861 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12862 NL80211_FLAG_NEED_RTNL,
12863 },
Johannes Berg7f6cf312011-11-04 11:18:15 +010012864 {
12865 .cmd = NL80211_CMD_PROBE_CLIENT,
12866 .doit = nl80211_probe_client,
12867 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012868 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020012869 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +010012870 NL80211_FLAG_NEED_RTNL,
12871 },
Johannes Berg5e760232011-11-04 11:18:17 +010012872 {
12873 .cmd = NL80211_CMD_REGISTER_BEACONS,
12874 .doit = nl80211_register_beacons,
12875 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012876 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg5e760232011-11-04 11:18:17 +010012877 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12878 NL80211_FLAG_NEED_RTNL,
12879 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +010012880 {
12881 .cmd = NL80211_CMD_SET_NOACK_MAP,
12882 .doit = nl80211_set_noack_map,
12883 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012884 .flags = GENL_UNS_ADMIN_PERM,
Simon Wunderlich1d9d9212011-11-18 14:20:43 +010012885 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12886 NL80211_FLAG_NEED_RTNL,
12887 },
Johannes Berg98104fde2012-06-16 00:19:54 +020012888 {
12889 .cmd = NL80211_CMD_START_P2P_DEVICE,
12890 .doit = nl80211_start_p2p_device,
12891 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012892 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg98104fde2012-06-16 00:19:54 +020012893 .internal_flags = NL80211_FLAG_NEED_WDEV |
12894 NL80211_FLAG_NEED_RTNL,
12895 },
12896 {
12897 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
12898 .doit = nl80211_stop_p2p_device,
12899 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012900 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg98104fde2012-06-16 00:19:54 +020012901 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12902 NL80211_FLAG_NEED_RTNL,
12903 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +010012904 {
Ayala Bekercb3b7d82016-09-20 17:31:13 +030012905 .cmd = NL80211_CMD_START_NAN,
12906 .doit = nl80211_start_nan,
12907 .policy = nl80211_policy,
12908 .flags = GENL_ADMIN_PERM,
12909 .internal_flags = NL80211_FLAG_NEED_WDEV |
12910 NL80211_FLAG_NEED_RTNL,
12911 },
12912 {
12913 .cmd = NL80211_CMD_STOP_NAN,
12914 .doit = nl80211_stop_nan,
12915 .policy = nl80211_policy,
12916 .flags = GENL_ADMIN_PERM,
12917 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12918 NL80211_FLAG_NEED_RTNL,
12919 },
12920 {
Ayala Bekera442b762016-09-20 17:31:15 +030012921 .cmd = NL80211_CMD_ADD_NAN_FUNCTION,
12922 .doit = nl80211_nan_add_func,
12923 .policy = nl80211_policy,
12924 .flags = GENL_ADMIN_PERM,
12925 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12926 NL80211_FLAG_NEED_RTNL,
12927 },
12928 {
12929 .cmd = NL80211_CMD_DEL_NAN_FUNCTION,
12930 .doit = nl80211_nan_del_func,
12931 .policy = nl80211_policy,
12932 .flags = GENL_ADMIN_PERM,
12933 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12934 NL80211_FLAG_NEED_RTNL,
12935 },
12936 {
Ayala Bekera5a9dcf2016-09-20 17:31:16 +030012937 .cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
12938 .doit = nl80211_nan_change_config,
12939 .policy = nl80211_policy,
12940 .flags = GENL_ADMIN_PERM,
12941 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12942 NL80211_FLAG_NEED_RTNL,
12943 },
12944 {
Antonio Quartullif4e583c2012-11-02 13:27:48 +010012945 .cmd = NL80211_CMD_SET_MCAST_RATE,
12946 .doit = nl80211_set_mcast_rate,
12947 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012948 .flags = GENL_UNS_ADMIN_PERM,
Antonio Quartullif4e583c2012-11-02 13:27:48 +010012949 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12950 NL80211_FLAG_NEED_RTNL,
12951 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +053012952 {
12953 .cmd = NL80211_CMD_SET_MAC_ACL,
12954 .doit = nl80211_set_mac_acl,
12955 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012956 .flags = GENL_UNS_ADMIN_PERM,
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +053012957 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12958 NL80211_FLAG_NEED_RTNL,
12959 },
Simon Wunderlich04f39042013-02-08 18:16:19 +010012960 {
12961 .cmd = NL80211_CMD_RADAR_DETECT,
12962 .doit = nl80211_start_radar_detection,
12963 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012964 .flags = GENL_UNS_ADMIN_PERM,
Simon Wunderlich04f39042013-02-08 18:16:19 +010012965 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12966 NL80211_FLAG_NEED_RTNL,
12967 },
Johannes Berg3713b4e2013-02-14 16:19:38 +010012968 {
12969 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
12970 .doit = nl80211_get_protocol_features,
12971 .policy = nl80211_policy,
12972 },
Jouni Malinen355199e2013-02-27 17:14:27 +020012973 {
12974 .cmd = NL80211_CMD_UPDATE_FT_IES,
12975 .doit = nl80211_update_ft_ies,
12976 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012977 .flags = GENL_UNS_ADMIN_PERM,
Jouni Malinen355199e2013-02-27 17:14:27 +020012978 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12979 NL80211_FLAG_NEED_RTNL,
12980 },
Arend van Spriel5de17982013-04-18 15:49:00 +020012981 {
12982 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
12983 .doit = nl80211_crit_protocol_start,
12984 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012985 .flags = GENL_UNS_ADMIN_PERM,
Arend van Spriel5de17982013-04-18 15:49:00 +020012986 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12987 NL80211_FLAG_NEED_RTNL,
12988 },
12989 {
12990 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
12991 .doit = nl80211_crit_protocol_stop,
12992 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020012993 .flags = GENL_UNS_ADMIN_PERM,
Arend van Spriel5de17982013-04-18 15:49:00 +020012994 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12995 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070012996 },
12997 {
12998 .cmd = NL80211_CMD_GET_COALESCE,
12999 .doit = nl80211_get_coalesce,
13000 .policy = nl80211_policy,
13001 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13002 NL80211_FLAG_NEED_RTNL,
13003 },
13004 {
13005 .cmd = NL80211_CMD_SET_COALESCE,
13006 .doit = nl80211_set_coalesce,
13007 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013008 .flags = GENL_UNS_ADMIN_PERM,
Amitkumar Karwarbe29b99a2013-06-28 11:51:26 -070013009 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13010 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020013011 },
13012 {
13013 .cmd = NL80211_CMD_CHANNEL_SWITCH,
13014 .doit = nl80211_channel_switch,
13015 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013016 .flags = GENL_UNS_ADMIN_PERM,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020013017 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13018 NL80211_FLAG_NEED_RTNL,
13019 },
Johannes Bergad7e7182013-11-13 13:37:47 +010013020 {
13021 .cmd = NL80211_CMD_VENDOR,
13022 .doit = nl80211_vendor_cmd,
Johannes Berg7bdbe402015-08-15 22:39:49 +030013023 .dumpit = nl80211_vendor_cmd_dump,
Johannes Bergad7e7182013-11-13 13:37:47 +010013024 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013025 .flags = GENL_UNS_ADMIN_PERM,
Johannes Bergad7e7182013-11-13 13:37:47 +010013026 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13027 NL80211_FLAG_NEED_RTNL,
13028 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080013029 {
13030 .cmd = NL80211_CMD_SET_QOS_MAP,
13031 .doit = nl80211_set_qos_map,
13032 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013033 .flags = GENL_UNS_ADMIN_PERM,
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080013034 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13035 NL80211_FLAG_NEED_RTNL,
13036 },
Johannes Berg960d01a2014-09-09 22:55:35 +030013037 {
13038 .cmd = NL80211_CMD_ADD_TX_TS,
13039 .doit = nl80211_add_tx_ts,
13040 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013041 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg960d01a2014-09-09 22:55:35 +030013042 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13043 NL80211_FLAG_NEED_RTNL,
13044 },
13045 {
13046 .cmd = NL80211_CMD_DEL_TX_TS,
13047 .doit = nl80211_del_tx_ts,
13048 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013049 .flags = GENL_UNS_ADMIN_PERM,
Johannes Berg960d01a2014-09-09 22:55:35 +030013050 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13051 NL80211_FLAG_NEED_RTNL,
13052 },
Arik Nemtsov1057d352014-11-19 12:54:26 +020013053 {
13054 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
13055 .doit = nl80211_tdls_channel_switch,
13056 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013057 .flags = GENL_UNS_ADMIN_PERM,
Arik Nemtsov1057d352014-11-19 12:54:26 +020013058 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13059 NL80211_FLAG_NEED_RTNL,
13060 },
13061 {
13062 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
13063 .doit = nl80211_tdls_cancel_channel_switch,
13064 .policy = nl80211_policy,
Martin Willi5617c6c2016-05-09 18:33:58 +020013065 .flags = GENL_UNS_ADMIN_PERM,
Arik Nemtsov1057d352014-11-19 12:54:26 +020013066 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13067 NL80211_FLAG_NEED_RTNL,
13068 },
Michael Braunce0ce132016-10-10 19:12:22 +020013069 {
13070 .cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
13071 .doit = nl80211_set_multicast_to_unicast,
13072 .policy = nl80211_policy,
13073 .flags = GENL_UNS_ADMIN_PERM,
13074 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13075 NL80211_FLAG_NEED_RTNL,
13076 },
Johannes Berg55682962007-09-20 13:09:35 -040013077};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013078
Johannes Berg56989f62016-10-24 14:40:05 +020013079static struct genl_family nl80211_fam __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +020013080 .name = NL80211_GENL_NAME, /* have users key off the name instead */
13081 .hdrsize = 0, /* no private header */
13082 .version = 1, /* no particular meaning now */
13083 .maxattr = NL80211_ATTR_MAX,
13084 .netnsok = true,
13085 .pre_doit = nl80211_pre_doit,
13086 .post_doit = nl80211_post_doit,
13087 .module = THIS_MODULE,
13088 .ops = nl80211_ops,
13089 .n_ops = ARRAY_SIZE(nl80211_ops),
13090 .mcgrps = nl80211_mcgrps,
13091 .n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
13092};
13093
Johannes Berg55682962007-09-20 13:09:35 -040013094/* notification functions */
13095
Johannes Berg3bb20552014-05-26 13:52:25 +020013096void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
13097 enum nl80211_commands cmd)
Johannes Berg55682962007-09-20 13:09:35 -040013098{
13099 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020013100 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040013101
Johannes Berg3bb20552014-05-26 13:52:25 +020013102 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
13103 cmd != NL80211_CMD_DEL_WIPHY);
13104
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070013105 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040013106 if (!msg)
13107 return;
13108
Johannes Berg3bb20552014-05-26 13:52:25 +020013109 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040013110 nlmsg_free(msg);
13111 return;
13112 }
13113
Johannes Berg68eb5502013-11-19 15:19:38 +010013114 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013115 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040013116}
13117
Denis Kenzior896ff062016-08-03 16:58:33 -050013118void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
13119 struct wireless_dev *wdev,
13120 enum nl80211_commands cmd)
13121{
13122 struct sk_buff *msg;
13123
13124 WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
13125 cmd != NL80211_CMD_DEL_INTERFACE);
13126
13127 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13128 if (!msg)
13129 return;
13130
13131 if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev,
13132 cmd == NL80211_CMD_DEL_INTERFACE) < 0) {
13133 nlmsg_free(msg);
13134 return;
13135 }
13136
13137 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13138 NL80211_MCGRP_CONFIG, GFP_KERNEL);
13139}
13140
Johannes Berg362a4152009-05-24 16:43:15 +020013141static int nl80211_add_scan_req(struct sk_buff *msg,
13142 struct cfg80211_registered_device *rdev)
13143{
13144 struct cfg80211_scan_request *req = rdev->scan_req;
13145 struct nlattr *nest;
13146 int i;
13147
13148 if (WARN_ON(!req))
13149 return 0;
13150
13151 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
13152 if (!nest)
13153 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040013154 for (i = 0; i < req->n_ssids; i++) {
13155 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
13156 goto nla_put_failure;
13157 }
Johannes Berg362a4152009-05-24 16:43:15 +020013158 nla_nest_end(msg, nest);
13159
13160 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13161 if (!nest)
13162 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040013163 for (i = 0; i < req->n_channels; i++) {
13164 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13165 goto nla_put_failure;
13166 }
Johannes Berg362a4152009-05-24 16:43:15 +020013167 nla_nest_end(msg, nest);
13168
David S. Miller9360ffd2012-03-29 04:41:26 -040013169 if (req->ie &&
13170 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
13171 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020013172
Johannes Bergae917c92013-10-25 11:05:22 +020013173 if (req->flags &&
13174 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
13175 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070013176
Avraham Stern1d762502016-07-05 17:10:13 +030013177 if (req->info.scan_start_tsf &&
13178 (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
13179 req->info.scan_start_tsf, NL80211_BSS_PAD) ||
13180 nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
13181 req->info.tsf_bssid)))
13182 goto nla_put_failure;
13183
Johannes Berg362a4152009-05-24 16:43:15 +020013184 return 0;
13185 nla_put_failure:
13186 return -ENOBUFS;
13187}
13188
Arend Van Spriel505a2e82016-12-16 11:21:54 +000013189static int nl80211_prep_scan_msg(struct sk_buff *msg,
Johannes Berga538e2d2009-06-16 19:56:42 +020013190 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020013191 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000013192 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020013193 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010013194{
13195 void *hdr;
13196
Eric W. Biederman15e47302012-09-07 20:12:54 +000013197 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010013198 if (!hdr)
13199 return -1;
13200
David S. Miller9360ffd2012-03-29 04:41:26 -040013201 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020013202 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13203 wdev->netdev->ifindex)) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020013204 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13205 NL80211_ATTR_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -040013206 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010013207
Johannes Berg362a4152009-05-24 16:43:15 +020013208 /* ignore errors and send incomplete event anyway */
13209 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010013210
Johannes Berg053c0952015-01-16 22:09:00 +010013211 genlmsg_end(msg, hdr);
13212 return 0;
Johannes Berg2a519312009-02-10 21:25:55 +010013213
13214 nla_put_failure:
13215 genlmsg_cancel(msg, hdr);
13216 return -EMSGSIZE;
13217}
13218
Luciano Coelho807f8a82011-05-11 17:09:35 +030013219static int
Arend Van Spriel505a2e82016-12-16 11:21:54 +000013220nl80211_prep_sched_scan_msg(struct sk_buff *msg,
Arend Van Spriel96b08fd2017-04-13 13:06:27 +010013221 struct cfg80211_sched_scan_request *req, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030013222{
13223 void *hdr;
13224
Arend Van Spriel96b08fd2017-04-13 13:06:27 +010013225 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030013226 if (!hdr)
13227 return -1;
13228
Arend Van Spriel96b08fd2017-04-13 13:06:27 +010013229 if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
13230 wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
13231 nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
13232 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
13233 NL80211_ATTR_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -040013234 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030013235
Johannes Berg053c0952015-01-16 22:09:00 +010013236 genlmsg_end(msg, hdr);
13237 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +030013238
13239 nla_put_failure:
13240 genlmsg_cancel(msg, hdr);
13241 return -EMSGSIZE;
13242}
13243
Johannes Berga538e2d2009-06-16 19:56:42 +020013244void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020013245 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020013246{
13247 struct sk_buff *msg;
13248
Thomas Graf58050fc2012-06-28 03:57:45 +000013249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020013250 if (!msg)
13251 return;
13252
Arend Van Spriel505a2e82016-12-16 11:21:54 +000013253 if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020013254 NL80211_CMD_TRIGGER_SCAN) < 0) {
13255 nlmsg_free(msg);
13256 return;
13257 }
13258
Johannes Berg68eb5502013-11-19 15:19:38 +010013259 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013260 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020013261}
13262
Johannes Bergf9d15d12014-01-22 11:14:19 +020013263struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
13264 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010013265{
13266 struct sk_buff *msg;
13267
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070013268 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010013269 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020013270 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010013271
Arend Van Spriel505a2e82016-12-16 11:21:54 +000013272 if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020013273 aborted ? NL80211_CMD_SCAN_ABORTED :
13274 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010013275 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020013276 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010013277 }
13278
Johannes Bergf9d15d12014-01-22 11:14:19 +020013279 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010013280}
13281
Arend Van Spriel505a2e82016-12-16 11:21:54 +000013282/* send message created by nl80211_build_scan_msg() */
13283void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
13284 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010013285{
Johannes Berg2a519312009-02-10 21:25:55 +010013286 if (!msg)
13287 return;
13288
Johannes Berg68eb5502013-11-19 15:19:38 +010013289 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013290 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010013291}
13292
Arend Van Spriel96b08fd2017-04-13 13:06:27 +010013293void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030013294{
13295 struct sk_buff *msg;
13296
Thomas Graf58050fc2012-06-28 03:57:45 +000013297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030013298 if (!msg)
13299 return;
13300
Arend Van Spriel96b08fd2017-04-13 13:06:27 +010013301 if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
Luciano Coelho807f8a82011-05-11 17:09:35 +030013302 nlmsg_free(msg);
13303 return;
13304 }
13305
Arend Van Spriel96b08fd2017-04-13 13:06:27 +010013306 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013307 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030013308}
13309
Jonathan Doronb0d7aa52014-12-15 19:26:00 +020013310static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
13311 struct regulatory_request *request)
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013312{
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013313 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040013314 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
13315 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013316
David S. Miller9360ffd2012-03-29 04:41:26 -040013317 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
13318 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13319 NL80211_REGDOM_TYPE_WORLD))
13320 goto nla_put_failure;
13321 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
13322 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13323 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
13324 goto nla_put_failure;
13325 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
13326 request->intersect) {
13327 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13328 NL80211_REGDOM_TYPE_INTERSECTION))
13329 goto nla_put_failure;
13330 } else {
13331 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13332 NL80211_REGDOM_TYPE_COUNTRY) ||
13333 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
13334 request->alpha2))
13335 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013336 }
13337
Arik Nemtsovad30ca22014-12-15 19:25:59 +020013338 if (request->wiphy_idx != WIPHY_IDX_INVALID) {
13339 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
13340
13341 if (wiphy &&
13342 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
13343 goto nla_put_failure;
Arik Nemtsov1bdd7162014-12-15 19:26:01 +020013344
13345 if (wiphy &&
13346 wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
13347 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
13348 goto nla_put_failure;
Arik Nemtsovad30ca22014-12-15 19:25:59 +020013349 }
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013350
Jonathan Doronb0d7aa52014-12-15 19:26:00 +020013351 return true;
13352
13353nla_put_failure:
13354 return false;
13355}
13356
13357/*
13358 * This can happen on global regulatory changes or device specific settings
13359 * based on custom regulatory domains.
13360 */
13361void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
13362 struct regulatory_request *request)
13363{
13364 struct sk_buff *msg;
13365 void *hdr;
13366
13367 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13368 if (!msg)
13369 return;
13370
13371 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
13372 if (!hdr) {
13373 nlmsg_free(msg);
13374 return;
13375 }
13376
13377 if (nl80211_reg_change_event_fill(msg, request) == false)
13378 goto nla_put_failure;
13379
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013380 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013381
Johannes Bergbc43b282009-07-25 10:54:13 +020013382 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010013383 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013384 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020013385 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040013386
13387 return;
13388
13389nla_put_failure:
13390 genlmsg_cancel(msg, hdr);
13391 nlmsg_free(msg);
13392}
13393
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013394static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
13395 struct net_device *netdev,
13396 const u8 *buf, size_t len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013397 enum nl80211_commands cmd, gfp_t gfp,
13398 int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013399{
13400 struct sk_buff *msg;
13401 void *hdr;
13402
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010013403 msg = nlmsg_new(100 + len, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013404 if (!msg)
13405 return;
13406
13407 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13408 if (!hdr) {
13409 nlmsg_free(msg);
13410 return;
13411 }
13412
David S. Miller9360ffd2012-03-29 04:41:26 -040013413 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13414 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13415 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
13416 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013417
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013418 if (uapsd_queues >= 0) {
13419 struct nlattr *nla_wmm =
13420 nla_nest_start(msg, NL80211_ATTR_STA_WME);
13421 if (!nla_wmm)
13422 goto nla_put_failure;
13423
13424 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
13425 uapsd_queues))
13426 goto nla_put_failure;
13427
13428 nla_nest_end(msg, nla_wmm);
13429 }
13430
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013431 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013432
Johannes Berg68eb5502013-11-19 15:19:38 +010013433 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013434 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013435 return;
13436
13437 nla_put_failure:
13438 genlmsg_cancel(msg, hdr);
13439 nlmsg_free(msg);
13440}
13441
13442void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020013443 struct net_device *netdev, const u8 *buf,
13444 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013445{
13446 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013447 NL80211_CMD_AUTHENTICATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013448}
13449
13450void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
13451 struct net_device *netdev, const u8 *buf,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013452 size_t len, gfp_t gfp, int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013453{
Johannes Berge6d6e342009-07-01 21:26:47 +020013454 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013455 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013456}
13457
Jouni Malinen53b46b82009-03-27 20:53:56 +020013458void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020013459 struct net_device *netdev, const u8 *buf,
13460 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013461{
13462 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013463 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013464}
13465
Jouni Malinen53b46b82009-03-27 20:53:56 +020013466void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
13467 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020013468 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013469{
13470 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013471 NL80211_CMD_DISASSOCIATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020013472}
13473
Johannes Berg6ff57cf2013-05-16 00:55:00 +020013474void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
13475 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020013476{
Johannes Berg947add32013-02-22 22:05:20 +010013477 struct wireless_dev *wdev = dev->ieee80211_ptr;
13478 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013479 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020013480 const struct ieee80211_mgmt *mgmt = (void *)buf;
13481 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020013482
Johannes Berg6ff57cf2013-05-16 00:55:00 +020013483 if (WARN_ON(len < 2))
13484 return;
13485
13486 if (ieee80211_is_deauth(mgmt->frame_control))
13487 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
13488 else
13489 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
13490
13491 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030013492 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
Jouni Malinencf4e5942010-12-16 00:52:40 +020013493}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020013494EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020013495
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040013496static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
13497 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020013498 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030013499{
13500 struct sk_buff *msg;
13501 void *hdr;
13502
Johannes Berge6d6e342009-07-01 21:26:47 +020013503 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030013504 if (!msg)
13505 return;
13506
13507 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13508 if (!hdr) {
13509 nlmsg_free(msg);
13510 return;
13511 }
13512
David S. Miller9360ffd2012-03-29 04:41:26 -040013513 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13514 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13515 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
13516 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
13517 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030013518
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013519 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030013520
Johannes Berg68eb5502013-11-19 15:19:38 +010013521 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013522 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030013523 return;
13524
13525 nla_put_failure:
13526 genlmsg_cancel(msg, hdr);
13527 nlmsg_free(msg);
13528}
13529
13530void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020013531 struct net_device *netdev, const u8 *addr,
13532 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030013533{
13534 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020013535 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030013536}
13537
13538void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020013539 struct net_device *netdev, const u8 *addr,
13540 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030013541{
Johannes Berge6d6e342009-07-01 21:26:47 +020013542 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
13543 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030013544}
13545
Samuel Ortizb23aa672009-07-01 21:26:54 +020013546void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +030013547 struct net_device *netdev,
13548 struct cfg80211_connect_resp_params *cr,
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +020013549 gfp_t gfp)
Samuel Ortizb23aa672009-07-01 21:26:54 +020013550{
13551 struct sk_buff *msg;
13552 void *hdr;
13553
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +030013554 msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
13555 cr->fils_kek_len + cr->pmk_len +
13556 (cr->pmkid ? WLAN_PMKID_LEN : 0), gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013557 if (!msg)
13558 return;
13559
13560 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
13561 if (!hdr) {
13562 nlmsg_free(msg);
13563 return;
13564 }
13565
David S. Miller9360ffd2012-03-29 04:41:26 -040013566 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13567 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +030013568 (cr->bssid &&
13569 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
Jouni Malinenbf1ecd22016-05-31 00:16:50 +030013570 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +030013571 cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
13572 cr->status) ||
13573 (cr->status < 0 &&
Purushottam Kushwaha3093ebbeab2017-01-13 01:12:21 +020013574 (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
Vidyullatha Kanchanapally5349a0f2017-03-31 00:22:33 +030013575 nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
13576 cr->timeout_reason))) ||
13577 (cr->req_ie &&
13578 nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
13579 (cr->resp_ie &&
13580 nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
Vidyullatha Kanchanapallya3caf742017-03-31 00:22:34 +030013581 cr->resp_ie)) ||
13582 (cr->update_erp_next_seq_num &&
13583 nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
13584 cr->fils_erp_next_seq_num)) ||
13585 (cr->status == WLAN_STATUS_SUCCESS &&
13586 ((cr->fils_kek &&
13587 nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils_kek_len,
13588 cr->fils_kek)) ||
13589 (cr->pmk &&
13590 nla_put(msg, NL80211_ATTR_PMK, cr->pmk_len, cr->pmk)) ||
13591 (cr->pmkid &&
13592 nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->pmkid)))))
David S. Miller9360ffd2012-03-29 04:41:26 -040013593 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020013594
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013595 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013596
Johannes Berg68eb5502013-11-19 15:19:38 +010013597 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013598 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013599 return;
13600
13601 nla_put_failure:
13602 genlmsg_cancel(msg, hdr);
13603 nlmsg_free(msg);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013604}
13605
13606void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
13607 struct net_device *netdev, const u8 *bssid,
13608 const u8 *req_ie, size_t req_ie_len,
13609 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
13610{
13611 struct sk_buff *msg;
13612 void *hdr;
13613
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010013614 msg = nlmsg_new(100 + req_ie_len + resp_ie_len, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013615 if (!msg)
13616 return;
13617
13618 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
13619 if (!hdr) {
13620 nlmsg_free(msg);
13621 return;
13622 }
13623
David S. Miller9360ffd2012-03-29 04:41:26 -040013624 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13625 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13626 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
13627 (req_ie &&
13628 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
13629 (resp_ie &&
13630 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
13631 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020013632
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013633 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013634
Johannes Berg68eb5502013-11-19 15:19:38 +010013635 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013636 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013637 return;
13638
13639 nla_put_failure:
13640 genlmsg_cancel(msg, hdr);
13641 nlmsg_free(msg);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013642}
13643
13644void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
13645 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +020013646 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020013647{
13648 struct sk_buff *msg;
13649 void *hdr;
13650
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010013651 msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013652 if (!msg)
13653 return;
13654
13655 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
13656 if (!hdr) {
13657 nlmsg_free(msg);
13658 return;
13659 }
13660
David S. Miller9360ffd2012-03-29 04:41:26 -040013661 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13662 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13663 (from_ap && reason &&
13664 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
13665 (from_ap &&
13666 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
13667 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
13668 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020013669
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013670 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013671
Johannes Berg68eb5502013-11-19 15:19:38 +010013672 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013673 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013674 return;
13675
13676 nla_put_failure:
13677 genlmsg_cancel(msg, hdr);
13678 nlmsg_free(msg);
Samuel Ortizb23aa672009-07-01 21:26:54 +020013679}
13680
Johannes Berg04a773a2009-04-19 21:24:32 +020013681void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
13682 struct net_device *netdev, const u8 *bssid,
13683 gfp_t gfp)
13684{
13685 struct sk_buff *msg;
13686 void *hdr;
13687
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070013688 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020013689 if (!msg)
13690 return;
13691
13692 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
13693 if (!hdr) {
13694 nlmsg_free(msg);
13695 return;
13696 }
13697
David S. Miller9360ffd2012-03-29 04:41:26 -040013698 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13699 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13700 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
13701 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020013702
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013703 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020013704
Johannes Berg68eb5502013-11-19 15:19:38 +010013705 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013706 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020013707 return;
13708
13709 nla_put_failure:
13710 genlmsg_cancel(msg, hdr);
13711 nlmsg_free(msg);
13712}
13713
Johannes Berg947add32013-02-22 22:05:20 +010013714void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
13715 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070013716{
Johannes Berg947add32013-02-22 22:05:20 +010013717 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013718 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070013719 struct sk_buff *msg;
13720 void *hdr;
13721
Johannes Berg947add32013-02-22 22:05:20 +010013722 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
13723 return;
13724
13725 trace_cfg80211_notify_new_peer_candidate(dev, addr);
13726
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010013727 msg = nlmsg_new(100 + ie_len, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070013728 if (!msg)
13729 return;
13730
13731 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
13732 if (!hdr) {
13733 nlmsg_free(msg);
13734 return;
13735 }
13736
David S. Miller9360ffd2012-03-29 04:41:26 -040013737 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010013738 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
13739 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040013740 (ie_len && ie &&
13741 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
13742 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070013743
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013744 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070013745
Johannes Berg68eb5502013-11-19 15:19:38 +010013746 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013747 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070013748 return;
13749
13750 nla_put_failure:
13751 genlmsg_cancel(msg, hdr);
13752 nlmsg_free(msg);
13753}
Johannes Berg947add32013-02-22 22:05:20 +010013754EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070013755
Jouni Malinena3b8b052009-03-27 21:59:49 +020013756void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
13757 struct net_device *netdev, const u8 *addr,
13758 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020013759 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020013760{
13761 struct sk_buff *msg;
13762 void *hdr;
13763
Johannes Berge6d6e342009-07-01 21:26:47 +020013764 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020013765 if (!msg)
13766 return;
13767
13768 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
13769 if (!hdr) {
13770 nlmsg_free(msg);
13771 return;
13772 }
13773
David S. Miller9360ffd2012-03-29 04:41:26 -040013774 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13775 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13776 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
13777 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
13778 (key_id != -1 &&
13779 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
13780 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
13781 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020013782
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013783 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020013784
Johannes Berg68eb5502013-11-19 15:19:38 +010013785 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013786 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020013787 return;
13788
13789 nla_put_failure:
13790 genlmsg_cancel(msg, hdr);
13791 nlmsg_free(msg);
13792}
13793
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013794void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
13795 struct ieee80211_channel *channel_before,
13796 struct ieee80211_channel *channel_after)
13797{
13798 struct sk_buff *msg;
13799 void *hdr;
13800 struct nlattr *nl_freq;
13801
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070013802 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013803 if (!msg)
13804 return;
13805
13806 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
13807 if (!hdr) {
13808 nlmsg_free(msg);
13809 return;
13810 }
13811
13812 /*
13813 * Since we are applying the beacon hint to a wiphy we know its
13814 * wiphy_idx is valid
13815 */
David S. Miller9360ffd2012-03-29 04:41:26 -040013816 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
13817 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013818
13819 /* Before */
13820 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
13821 if (!nl_freq)
13822 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010013823 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013824 goto nla_put_failure;
13825 nla_nest_end(msg, nl_freq);
13826
13827 /* After */
13828 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
13829 if (!nl_freq)
13830 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010013831 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013832 goto nla_put_failure;
13833 nla_nest_end(msg, nl_freq);
13834
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013835 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013836
Johannes Berg463d0182009-07-14 00:33:35 +020013837 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010013838 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013839 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020013840 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040013841
13842 return;
13843
13844nla_put_failure:
13845 genlmsg_cancel(msg, hdr);
13846 nlmsg_free(msg);
13847}
13848
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013849static void nl80211_send_remain_on_chan_event(
13850 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020013851 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013852 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013853 unsigned int duration, gfp_t gfp)
13854{
13855 struct sk_buff *msg;
13856 void *hdr;
13857
13858 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13859 if (!msg)
13860 return;
13861
13862 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13863 if (!hdr) {
13864 nlmsg_free(msg);
13865 return;
13866 }
13867
David S. Miller9360ffd2012-03-29 04:41:26 -040013868 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020013869 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13870 wdev->netdev->ifindex)) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020013871 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13872 NL80211_ATTR_PAD) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040013873 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010013874 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
13875 NL80211_CHAN_NO_HT) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020013876 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13877 NL80211_ATTR_PAD))
David S. Miller9360ffd2012-03-29 04:41:26 -040013878 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013879
David S. Miller9360ffd2012-03-29 04:41:26 -040013880 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
13881 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
13882 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013883
Johannes Berg3b7b72e2011-10-22 19:05:51 +020013884 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013885
Johannes Berg68eb5502013-11-19 15:19:38 +010013886 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013887 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013888 return;
13889
13890 nla_put_failure:
13891 genlmsg_cancel(msg, hdr);
13892 nlmsg_free(msg);
13893}
13894
Johannes Berg947add32013-02-22 22:05:20 +010013895void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
13896 struct ieee80211_channel *chan,
13897 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013898{
Johannes Berg947add32013-02-22 22:05:20 +010013899 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013900 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010013901
13902 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013903 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020013904 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010013905 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013906}
Johannes Berg947add32013-02-22 22:05:20 +010013907EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013908
Johannes Berg947add32013-02-22 22:05:20 +010013909void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
13910 struct ieee80211_channel *chan,
13911 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013912{
Johannes Berg947add32013-02-22 22:05:20 +010013913 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013914 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010013915
13916 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013917 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010013918 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013919}
Johannes Berg947add32013-02-22 22:05:20 +010013920EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010013921
Johannes Berg947add32013-02-22 22:05:20 +010013922void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
13923 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010013924{
Johannes Berg947add32013-02-22 22:05:20 +010013925 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013926 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010013927 struct sk_buff *msg;
13928
Johannes Berg947add32013-02-22 22:05:20 +010013929 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
13930
Thomas Graf58050fc2012-06-28 03:57:45 +000013931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010013932 if (!msg)
13933 return;
13934
Johannes Bergcf5ead82014-11-14 17:14:00 +010013935 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
John W. Linville66266b32012-03-15 13:25:41 -040013936 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010013937 nlmsg_free(msg);
13938 return;
13939 }
13940
Johannes Berg68eb5502013-11-19 15:19:38 +010013941 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013942 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010013943}
Johannes Berg947add32013-02-22 22:05:20 +010013944EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010013945
Johannes Bergcf5ead82014-11-14 17:14:00 +010013946void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
13947 struct station_info *sinfo, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020013948{
Johannes Berg947add32013-02-22 22:05:20 +010013949 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013950 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020013951 struct sk_buff *msg;
Johannes Bergcf5ead82014-11-14 17:14:00 +010013952 struct station_info empty_sinfo = {};
13953
13954 if (!sinfo)
13955 sinfo = &empty_sinfo;
Jouni Malinenec15e682011-03-23 15:29:52 +020013956
Johannes Berg947add32013-02-22 22:05:20 +010013957 trace_cfg80211_del_sta(dev, mac_addr);
13958
Thomas Graf58050fc2012-06-28 03:57:45 +000013959 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020013960 if (!msg)
13961 return;
13962
Johannes Bergcf5ead82014-11-14 17:14:00 +010013963 if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
Johannes Berg57007122015-01-16 21:05:02 +010013964 rdev, dev, mac_addr, sinfo) < 0) {
Jouni Malinenec15e682011-03-23 15:29:52 +020013965 nlmsg_free(msg);
13966 return;
13967 }
13968
Johannes Berg68eb5502013-11-19 15:19:38 +010013969 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010013970 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020013971}
Johannes Bergcf5ead82014-11-14 17:14:00 +010013972EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
Jouni Malinenec15e682011-03-23 15:29:52 +020013973
Johannes Berg947add32013-02-22 22:05:20 +010013974void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
13975 enum nl80211_connect_failed_reason reason,
13976 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053013977{
Johannes Berg947add32013-02-22 22:05:20 +010013978 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080013979 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053013980 struct sk_buff *msg;
13981 void *hdr;
13982
13983 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
13984 if (!msg)
13985 return;
13986
13987 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
13988 if (!hdr) {
13989 nlmsg_free(msg);
13990 return;
13991 }
13992
13993 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
13994 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
13995 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
13996 goto nla_put_failure;
13997
13998 genlmsg_end(msg, hdr);
13999
Johannes Berg68eb5502013-11-19 15:19:38 +010014000 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014001 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053014002 return;
14003
14004 nla_put_failure:
14005 genlmsg_cancel(msg, hdr);
14006 nlmsg_free(msg);
14007}
Johannes Berg947add32013-02-22 22:05:20 +010014008EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053014009
Johannes Bergb92ab5d2011-11-04 11:18:19 +010014010static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
14011 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010014012{
14013 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014014 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg28946da2011-11-04 11:18:12 +010014015 struct sk_buff *msg;
14016 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000014017 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010014018
Eric W. Biederman15e47302012-09-07 20:12:54 +000014019 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010014020 return false;
14021
14022 msg = nlmsg_new(100, gfp);
14023 if (!msg)
14024 return true;
14025
Johannes Bergb92ab5d2011-11-04 11:18:19 +010014026 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010014027 if (!hdr) {
14028 nlmsg_free(msg);
14029 return true;
14030 }
14031
David S. Miller9360ffd2012-03-29 04:41:26 -040014032 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14033 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14034 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
14035 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010014036
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014037 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000014038 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010014039 return true;
14040
14041 nla_put_failure:
14042 genlmsg_cancel(msg, hdr);
14043 nlmsg_free(msg);
14044 return true;
14045}
14046
Johannes Berg947add32013-02-22 22:05:20 +010014047bool cfg80211_rx_spurious_frame(struct net_device *dev,
14048 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010014049{
Johannes Berg947add32013-02-22 22:05:20 +010014050 struct wireless_dev *wdev = dev->ieee80211_ptr;
14051 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010014052
Johannes Berg947add32013-02-22 22:05:20 +010014053 trace_cfg80211_rx_spurious_frame(dev, addr);
14054
14055 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14056 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
14057 trace_cfg80211_return_bool(false);
14058 return false;
14059 }
14060 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
14061 addr, gfp);
14062 trace_cfg80211_return_bool(ret);
14063 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010014064}
Johannes Berg947add32013-02-22 22:05:20 +010014065EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
14066
14067bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
14068 const u8 *addr, gfp_t gfp)
14069{
14070 struct wireless_dev *wdev = dev->ieee80211_ptr;
14071 bool ret;
14072
14073 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
14074
14075 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14076 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
14077 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
14078 trace_cfg80211_return_bool(false);
14079 return false;
14080 }
14081 ret = __nl80211_unexpected_frame(dev,
14082 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
14083 addr, gfp);
14084 trace_cfg80211_return_bool(ret);
14085 return ret;
14086}
14087EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010014088
Johannes Berg2e161f72010-08-12 15:38:38 +020014089int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000014090 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010014091 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030014092 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020014093{
Johannes Berg71bbc992012-06-15 15:30:18 +020014094 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020014095 struct sk_buff *msg;
14096 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020014097
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010014098 msg = nlmsg_new(100 + len, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020014099 if (!msg)
14100 return -ENOMEM;
14101
Johannes Berg2e161f72010-08-12 15:38:38 +020014102 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020014103 if (!hdr) {
14104 nlmsg_free(msg);
14105 return -ENOMEM;
14106 }
14107
David S. Miller9360ffd2012-03-29 04:41:26 -040014108 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020014109 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14110 netdev->ifindex)) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020014111 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14112 NL80211_ATTR_PAD) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040014113 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
14114 (sig_dbm &&
14115 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030014116 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14117 (flags &&
14118 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040014119 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020014120
Johannes Berg3b7b72e2011-10-22 19:05:51 +020014121 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020014122
Eric W. Biederman15e47302012-09-07 20:12:54 +000014123 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020014124
14125 nla_put_failure:
14126 genlmsg_cancel(msg, hdr);
14127 nlmsg_free(msg);
14128 return -ENOBUFS;
14129}
14130
Johannes Berg947add32013-02-22 22:05:20 +010014131void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
14132 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020014133{
Johannes Berg947add32013-02-22 22:05:20 +010014134 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014135 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020014136 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020014137 struct sk_buff *msg;
14138 void *hdr;
14139
Johannes Berg947add32013-02-22 22:05:20 +010014140 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
14141
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010014142 msg = nlmsg_new(100 + len, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020014143 if (!msg)
14144 return;
14145
Johannes Berg2e161f72010-08-12 15:38:38 +020014146 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020014147 if (!hdr) {
14148 nlmsg_free(msg);
14149 return;
14150 }
14151
David S. Miller9360ffd2012-03-29 04:41:26 -040014152 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020014153 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14154 netdev->ifindex)) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020014155 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14156 NL80211_ATTR_PAD) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040014157 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020014158 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14159 NL80211_ATTR_PAD) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040014160 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
14161 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020014162
Johannes Berg3b7b72e2011-10-22 19:05:51 +020014163 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020014164
Johannes Berg68eb5502013-11-19 15:19:38 +010014165 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014166 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020014167 return;
14168
14169 nla_put_failure:
14170 genlmsg_cancel(msg, hdr);
14171 nlmsg_free(msg);
14172}
Johannes Berg947add32013-02-22 22:05:20 +010014173EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020014174
Johannes Berg5b97f492014-11-26 12:37:43 +010014175static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
14176 const char *mac, gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014177{
Johannes Berg947add32013-02-22 22:05:20 +010014178 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg5b97f492014-11-26 12:37:43 +010014179 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14180 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14181 void **cb;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014182
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014183 if (!msg)
Johannes Berg5b97f492014-11-26 12:37:43 +010014184 return NULL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014185
Johannes Berg5b97f492014-11-26 12:37:43 +010014186 cb = (void **)msg->cb;
14187
14188 cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
14189 if (!cb[0]) {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014190 nlmsg_free(msg);
Johannes Berg5b97f492014-11-26 12:37:43 +010014191 return NULL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014192 }
14193
David S. Miller9360ffd2012-03-29 04:41:26 -040014194 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010014195 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040014196 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014197
Johannes Berg5b97f492014-11-26 12:37:43 +010014198 if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014199 goto nla_put_failure;
14200
Johannes Berg5b97f492014-11-26 12:37:43 +010014201 cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
14202 if (!cb[1])
14203 goto nla_put_failure;
14204
14205 cb[2] = rdev;
14206
14207 return msg;
14208 nla_put_failure:
14209 nlmsg_free(msg);
14210 return NULL;
14211}
14212
14213static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
14214{
14215 void **cb = (void **)msg->cb;
14216 struct cfg80211_registered_device *rdev = cb[2];
14217
14218 nla_nest_end(msg, cb[1]);
14219 genlmsg_end(msg, cb[0]);
14220
14221 memset(msg->cb, 0, sizeof(msg->cb));
14222
14223 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14224 NL80211_MCGRP_MLME, gfp);
14225}
14226
14227void cfg80211_cqm_rssi_notify(struct net_device *dev,
14228 enum nl80211_cqm_rssi_threshold_event rssi_event,
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +010014229 s32 rssi_level, gfp_t gfp)
Johannes Berg5b97f492014-11-26 12:37:43 +010014230{
14231 struct sk_buff *msg;
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +010014232 struct wireless_dev *wdev = dev->ieee80211_ptr;
14233 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg5b97f492014-11-26 12:37:43 +010014234
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +010014235 trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
Johannes Berg5b97f492014-11-26 12:37:43 +010014236
Johannes Berg98f03342014-11-26 12:42:02 +010014237 if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
14238 rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
14239 return;
14240
Andrew Zaborowski4a4b8162017-02-10 10:02:31 +010014241 if (wdev->cqm_config) {
14242 wdev->cqm_config->last_rssi_event_value = rssi_level;
14243
14244 cfg80211_cqm_rssi_update(rdev, dev);
14245
14246 if (rssi_level == 0)
14247 rssi_level = wdev->cqm_config->last_rssi_event_value;
14248 }
14249
Johannes Berg5b97f492014-11-26 12:37:43 +010014250 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14251 if (!msg)
14252 return;
14253
David S. Miller9360ffd2012-03-29 04:41:26 -040014254 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
14255 rssi_event))
14256 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014257
Andrzej Zaborowskibee427b2017-01-25 12:43:41 +010014258 if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
14259 rssi_level))
14260 goto nla_put_failure;
14261
Johannes Berg5b97f492014-11-26 12:37:43 +010014262 cfg80211_send_cqm(msg, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014263
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014264 return;
14265
14266 nla_put_failure:
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014267 nlmsg_free(msg);
14268}
Johannes Berg947add32013-02-22 22:05:20 +010014269EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020014270
Johannes Berg5b97f492014-11-26 12:37:43 +010014271void cfg80211_cqm_txe_notify(struct net_device *dev,
14272 const u8 *peer, u32 num_packets,
14273 u32 rate, u32 intvl, gfp_t gfp)
14274{
14275 struct sk_buff *msg;
14276
14277 msg = cfg80211_prepare_cqm(dev, peer, gfp);
14278 if (!msg)
14279 return;
14280
14281 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
14282 goto nla_put_failure;
14283
14284 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
14285 goto nla_put_failure;
14286
14287 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
14288 goto nla_put_failure;
14289
14290 cfg80211_send_cqm(msg, gfp);
14291 return;
14292
14293 nla_put_failure:
14294 nlmsg_free(msg);
14295}
14296EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
14297
14298void cfg80211_cqm_pktloss_notify(struct net_device *dev,
14299 const u8 *peer, u32 num_packets, gfp_t gfp)
14300{
14301 struct sk_buff *msg;
14302
14303 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
14304
14305 msg = cfg80211_prepare_cqm(dev, peer, gfp);
14306 if (!msg)
14307 return;
14308
14309 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
14310 goto nla_put_failure;
14311
14312 cfg80211_send_cqm(msg, gfp);
14313 return;
14314
14315 nla_put_failure:
14316 nlmsg_free(msg);
14317}
14318EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
14319
Johannes Berg98f03342014-11-26 12:42:02 +010014320void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
14321{
14322 struct sk_buff *msg;
14323
14324 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14325 if (!msg)
14326 return;
14327
14328 if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
14329 goto nla_put_failure;
14330
14331 cfg80211_send_cqm(msg, gfp);
14332 return;
14333
14334 nla_put_failure:
14335 nlmsg_free(msg);
14336}
14337EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
14338
Johannes Berg947add32013-02-22 22:05:20 +010014339static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
14340 struct net_device *netdev, const u8 *bssid,
14341 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020014342{
14343 struct sk_buff *msg;
14344 struct nlattr *rekey_attr;
14345 void *hdr;
14346
Thomas Graf58050fc2012-06-28 03:57:45 +000014347 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020014348 if (!msg)
14349 return;
14350
14351 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
14352 if (!hdr) {
14353 nlmsg_free(msg);
14354 return;
14355 }
14356
David S. Miller9360ffd2012-03-29 04:41:26 -040014357 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14358 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14359 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14360 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020014361
14362 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
14363 if (!rekey_attr)
14364 goto nla_put_failure;
14365
David S. Miller9360ffd2012-03-29 04:41:26 -040014366 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
14367 NL80211_REPLAY_CTR_LEN, replay_ctr))
14368 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020014369
14370 nla_nest_end(msg, rekey_attr);
14371
Johannes Berg3b7b72e2011-10-22 19:05:51 +020014372 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020014373
Johannes Berg68eb5502013-11-19 15:19:38 +010014374 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014375 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020014376 return;
14377
14378 nla_put_failure:
14379 genlmsg_cancel(msg, hdr);
14380 nlmsg_free(msg);
14381}
14382
Johannes Berg947add32013-02-22 22:05:20 +010014383void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
14384 const u8 *replay_ctr, gfp_t gfp)
14385{
14386 struct wireless_dev *wdev = dev->ieee80211_ptr;
14387 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014388 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010014389
14390 trace_cfg80211_gtk_rekey_notify(dev, bssid);
14391 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
14392}
14393EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
14394
14395static void
14396nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
14397 struct net_device *netdev, int index,
14398 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030014399{
14400 struct sk_buff *msg;
14401 struct nlattr *attr;
14402 void *hdr;
14403
Thomas Graf58050fc2012-06-28 03:57:45 +000014404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030014405 if (!msg)
14406 return;
14407
14408 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
14409 if (!hdr) {
14410 nlmsg_free(msg);
14411 return;
14412 }
14413
David S. Miller9360ffd2012-03-29 04:41:26 -040014414 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14415 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14416 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030014417
14418 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
14419 if (!attr)
14420 goto nla_put_failure;
14421
David S. Miller9360ffd2012-03-29 04:41:26 -040014422 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
14423 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
14424 (preauth &&
14425 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
14426 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030014427
14428 nla_nest_end(msg, attr);
14429
Johannes Berg3b7b72e2011-10-22 19:05:51 +020014430 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030014431
Johannes Berg68eb5502013-11-19 15:19:38 +010014432 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014433 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030014434 return;
14435
14436 nla_put_failure:
14437 genlmsg_cancel(msg, hdr);
14438 nlmsg_free(msg);
14439}
14440
Johannes Berg947add32013-02-22 22:05:20 +010014441void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
14442 const u8 *bssid, bool preauth, gfp_t gfp)
14443{
14444 struct wireless_dev *wdev = dev->ieee80211_ptr;
14445 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014446 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010014447
14448 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
14449 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
14450}
14451EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
14452
14453static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
14454 struct net_device *netdev,
14455 struct cfg80211_chan_def *chandef,
Luciano Coelhof8d75522014-11-07 14:31:35 +020014456 gfp_t gfp,
14457 enum nl80211_commands notif,
14458 u8 count)
Thomas Pedersen53145262012-04-06 13:35:47 -070014459{
14460 struct sk_buff *msg;
14461 void *hdr;
14462
Thomas Graf58050fc2012-06-28 03:57:45 +000014463 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070014464 if (!msg)
14465 return;
14466
Luciano Coelhof8d75522014-11-07 14:31:35 +020014467 hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
Thomas Pedersen53145262012-04-06 13:35:47 -070014468 if (!hdr) {
14469 nlmsg_free(msg);
14470 return;
14471 }
14472
Johannes Berg683b6d32012-11-08 21:25:48 +010014473 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14474 goto nla_put_failure;
14475
14476 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040014477 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070014478
Luciano Coelhof8d75522014-11-07 14:31:35 +020014479 if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
14480 (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
14481 goto nla_put_failure;
14482
Thomas Pedersen53145262012-04-06 13:35:47 -070014483 genlmsg_end(msg, hdr);
14484
Johannes Berg68eb5502013-11-19 15:19:38 +010014485 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014486 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070014487 return;
14488
14489 nla_put_failure:
14490 genlmsg_cancel(msg, hdr);
14491 nlmsg_free(msg);
14492}
14493
Johannes Berg947add32013-02-22 22:05:20 +010014494void cfg80211_ch_switch_notify(struct net_device *dev,
14495 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070014496{
Johannes Berg947add32013-02-22 22:05:20 +010014497 struct wireless_dev *wdev = dev->ieee80211_ptr;
14498 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014499 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010014500
Simon Wunderliche487eae2013-11-21 18:19:51 +010014501 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010014502
Simon Wunderliche487eae2013-11-21 18:19:51 +010014503 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010014504
Michal Kazior9e0e2962014-01-29 14:22:27 +010014505 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010014506 wdev->preset_chandef = *chandef;
Luciano Coelhof8d75522014-11-07 14:31:35 +020014507 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
14508 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
Johannes Berg947add32013-02-22 22:05:20 +010014509}
14510EXPORT_SYMBOL(cfg80211_ch_switch_notify);
14511
Luciano Coelhof8d75522014-11-07 14:31:35 +020014512void cfg80211_ch_switch_started_notify(struct net_device *dev,
14513 struct cfg80211_chan_def *chandef,
14514 u8 count)
14515{
14516 struct wireless_dev *wdev = dev->ieee80211_ptr;
14517 struct wiphy *wiphy = wdev->wiphy;
14518 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14519
14520 trace_cfg80211_ch_switch_started_notify(dev, chandef);
14521
14522 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
14523 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
14524}
14525EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
14526
Thomas Pedersen84f10702012-07-12 16:17:33 -070014527void
Simon Wunderlich04f39042013-02-08 18:16:19 +010014528nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010014529 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010014530 enum nl80211_radar_event event,
14531 struct net_device *netdev, gfp_t gfp)
14532{
14533 struct sk_buff *msg;
14534 void *hdr;
14535
14536 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14537 if (!msg)
14538 return;
14539
14540 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
14541 if (!hdr) {
14542 nlmsg_free(msg);
14543 return;
14544 }
14545
14546 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
14547 goto nla_put_failure;
14548
14549 /* NOP and radar events don't need a netdev parameter */
14550 if (netdev) {
14551 struct wireless_dev *wdev = netdev->ieee80211_ptr;
14552
14553 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020014554 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14555 NL80211_ATTR_PAD))
Simon Wunderlich04f39042013-02-08 18:16:19 +010014556 goto nla_put_failure;
14557 }
14558
14559 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
14560 goto nla_put_failure;
14561
14562 if (nl80211_send_chandef(msg, chandef))
14563 goto nla_put_failure;
14564
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014565 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010014566
Johannes Berg68eb5502013-11-19 15:19:38 +010014567 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014568 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010014569 return;
14570
14571 nla_put_failure:
14572 genlmsg_cancel(msg, hdr);
14573 nlmsg_free(msg);
14574}
14575
Johannes Berg7f6cf312011-11-04 11:18:15 +010014576void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
14577 u64 cookie, bool acked, gfp_t gfp)
14578{
14579 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014580 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg7f6cf312011-11-04 11:18:15 +010014581 struct sk_buff *msg;
14582 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010014583
Beni Lev4ee3e062012-08-27 12:49:39 +030014584 trace_cfg80211_probe_status(dev, addr, cookie, acked);
14585
Thomas Graf58050fc2012-06-28 03:57:45 +000014586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030014587
Johannes Berg7f6cf312011-11-04 11:18:15 +010014588 if (!msg)
14589 return;
14590
14591 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
14592 if (!hdr) {
14593 nlmsg_free(msg);
14594 return;
14595 }
14596
David S. Miller9360ffd2012-03-29 04:41:26 -040014597 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14598 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14599 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020014600 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14601 NL80211_ATTR_PAD) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040014602 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
14603 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010014604
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014605 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010014606
Johannes Berg68eb5502013-11-19 15:19:38 +010014607 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014608 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010014609 return;
14610
14611 nla_put_failure:
14612 genlmsg_cancel(msg, hdr);
14613 nlmsg_free(msg);
14614}
14615EXPORT_SYMBOL(cfg80211_probe_status);
14616
Johannes Berg5e760232011-11-04 11:18:17 +010014617void cfg80211_report_obss_beacon(struct wiphy *wiphy,
14618 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070014619 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010014620{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014621 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg5e760232011-11-04 11:18:17 +010014622 struct sk_buff *msg;
14623 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070014624 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010014625
Beni Lev4ee3e062012-08-27 12:49:39 +030014626 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
14627
Ben Greear37c73b52012-10-26 14:49:25 -070014628 spin_lock_bh(&rdev->beacon_registrations_lock);
14629 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14630 msg = nlmsg_new(len + 100, GFP_ATOMIC);
14631 if (!msg) {
14632 spin_unlock_bh(&rdev->beacon_registrations_lock);
14633 return;
14634 }
Johannes Berg5e760232011-11-04 11:18:17 +010014635
Ben Greear37c73b52012-10-26 14:49:25 -070014636 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
14637 if (!hdr)
14638 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010014639
Ben Greear37c73b52012-10-26 14:49:25 -070014640 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14641 (freq &&
14642 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
14643 (sig_dbm &&
14644 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
14645 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
14646 goto nla_put_failure;
14647
14648 genlmsg_end(msg, hdr);
14649
14650 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010014651 }
Ben Greear37c73b52012-10-26 14:49:25 -070014652 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010014653 return;
14654
14655 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070014656 spin_unlock_bh(&rdev->beacon_registrations_lock);
14657 if (hdr)
14658 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010014659 nlmsg_free(msg);
14660}
14661EXPORT_SYMBOL(cfg80211_report_obss_beacon);
14662
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014663#ifdef CONFIG_PM
Luciano Coelho8cd4d452014-09-17 11:55:28 +030014664static int cfg80211_net_detect_results(struct sk_buff *msg,
14665 struct cfg80211_wowlan_wakeup *wakeup)
14666{
14667 struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
14668 struct nlattr *nl_results, *nl_match, *nl_freqs;
14669 int i, j;
14670
14671 nl_results = nla_nest_start(
14672 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
14673 if (!nl_results)
14674 return -EMSGSIZE;
14675
14676 for (i = 0; i < nd->n_matches; i++) {
14677 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
14678
14679 nl_match = nla_nest_start(msg, i);
14680 if (!nl_match)
14681 break;
14682
14683 /* The SSID attribute is optional in nl80211, but for
14684 * simplicity reasons it's always present in the
14685 * cfg80211 structure. If a driver can't pass the
14686 * SSID, that needs to be changed. A zero length SSID
14687 * is still a valid SSID (wildcard), so it cannot be
14688 * used for this purpose.
14689 */
14690 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
14691 match->ssid.ssid)) {
14692 nla_nest_cancel(msg, nl_match);
14693 goto out;
14694 }
14695
14696 if (match->n_channels) {
14697 nl_freqs = nla_nest_start(
14698 msg, NL80211_ATTR_SCAN_FREQUENCIES);
14699 if (!nl_freqs) {
14700 nla_nest_cancel(msg, nl_match);
14701 goto out;
14702 }
14703
14704 for (j = 0; j < match->n_channels; j++) {
Samuel Tan5528fae82015-02-09 21:29:15 +020014705 if (nla_put_u32(msg, j, match->channels[j])) {
Luciano Coelho8cd4d452014-09-17 11:55:28 +030014706 nla_nest_cancel(msg, nl_freqs);
14707 nla_nest_cancel(msg, nl_match);
14708 goto out;
14709 }
14710 }
14711
14712 nla_nest_end(msg, nl_freqs);
14713 }
14714
14715 nla_nest_end(msg, nl_match);
14716 }
14717
14718out:
14719 nla_nest_end(msg, nl_results);
14720 return 0;
14721}
14722
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014723void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
14724 struct cfg80211_wowlan_wakeup *wakeup,
14725 gfp_t gfp)
14726{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014727 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014728 struct sk_buff *msg;
14729 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014730 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014731
14732 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
14733
14734 if (wakeup)
14735 size += wakeup->packet_present_len;
14736
14737 msg = nlmsg_new(size, gfp);
14738 if (!msg)
14739 return;
14740
14741 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
14742 if (!hdr)
14743 goto free_msg;
14744
14745 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020014746 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14747 NL80211_ATTR_PAD))
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014748 goto free_msg;
14749
14750 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14751 wdev->netdev->ifindex))
14752 goto free_msg;
14753
14754 if (wakeup) {
14755 struct nlattr *reasons;
14756
14757 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020014758 if (!reasons)
14759 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014760
14761 if (wakeup->disconnect &&
14762 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
14763 goto free_msg;
14764 if (wakeup->magic_pkt &&
14765 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
14766 goto free_msg;
14767 if (wakeup->gtk_rekey_failure &&
14768 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
14769 goto free_msg;
14770 if (wakeup->eap_identity_req &&
14771 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
14772 goto free_msg;
14773 if (wakeup->four_way_handshake &&
14774 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
14775 goto free_msg;
14776 if (wakeup->rfkill_release &&
14777 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
14778 goto free_msg;
14779
14780 if (wakeup->pattern_idx >= 0 &&
14781 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
14782 wakeup->pattern_idx))
14783 goto free_msg;
14784
Johannes Bergae917c92013-10-25 11:05:22 +020014785 if (wakeup->tcp_match &&
14786 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
14787 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010014788
Johannes Bergae917c92013-10-25 11:05:22 +020014789 if (wakeup->tcp_connlost &&
14790 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
14791 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010014792
Johannes Bergae917c92013-10-25 11:05:22 +020014793 if (wakeup->tcp_nomoretokens &&
14794 nla_put_flag(msg,
14795 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
14796 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010014797
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014798 if (wakeup->packet) {
14799 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
14800 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
14801
14802 if (!wakeup->packet_80211) {
14803 pkt_attr =
14804 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
14805 len_attr =
14806 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
14807 }
14808
14809 if (wakeup->packet_len &&
14810 nla_put_u32(msg, len_attr, wakeup->packet_len))
14811 goto free_msg;
14812
14813 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
14814 wakeup->packet))
14815 goto free_msg;
14816 }
14817
Luciano Coelho8cd4d452014-09-17 11:55:28 +030014818 if (wakeup->net_detect &&
14819 cfg80211_net_detect_results(msg, wakeup))
14820 goto free_msg;
14821
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014822 nla_nest_end(msg, reasons);
14823 }
14824
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014825 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014826
Johannes Berg68eb5502013-11-19 15:19:38 +010014827 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014828 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010014829 return;
14830
14831 free_msg:
14832 nlmsg_free(msg);
14833}
14834EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
14835#endif
14836
Jouni Malinen3475b092012-11-16 22:49:57 +020014837void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
14838 enum nl80211_tdls_operation oper,
14839 u16 reason_code, gfp_t gfp)
14840{
14841 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014842 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinen3475b092012-11-16 22:49:57 +020014843 struct sk_buff *msg;
14844 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020014845
14846 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
14847 reason_code);
14848
14849 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14850 if (!msg)
14851 return;
14852
14853 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
14854 if (!hdr) {
14855 nlmsg_free(msg);
14856 return;
14857 }
14858
14859 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14860 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14861 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
14862 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
14863 (reason_code > 0 &&
14864 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
14865 goto nla_put_failure;
14866
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014867 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020014868
Johannes Berg68eb5502013-11-19 15:19:38 +010014869 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014870 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020014871 return;
14872
14873 nla_put_failure:
14874 genlmsg_cancel(msg, hdr);
14875 nlmsg_free(msg);
14876}
14877EXPORT_SYMBOL(cfg80211_tdls_oper_request);
14878
Jouni Malinen026331c2010-02-15 12:53:10 +020014879static int nl80211_netlink_notify(struct notifier_block * nb,
14880 unsigned long state,
14881 void *_notify)
14882{
14883 struct netlink_notify *notify = _notify;
14884 struct cfg80211_registered_device *rdev;
14885 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070014886 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020014887
Dmitry Ivanov8f815cd2016-04-06 17:23:18 +030014888 if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
Jouni Malinen026331c2010-02-15 12:53:10 +020014889 return NOTIFY_DONE;
14890
14891 rcu_read_lock();
14892
Johannes Berg5e760232011-11-04 11:18:17 +010014893 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010014894 bool schedule_destroy_work = false;
Jukka Rissanen93a1e862014-12-15 13:25:39 +020014895 struct cfg80211_sched_scan_request *sched_scan_req =
14896 rcu_dereference(rdev->sched_scan_req);
14897
14898 if (sched_scan_req && notify->portid &&
Johannes Berg753aacf2017-01-05 10:57:14 +010014899 sched_scan_req->owner_nlportid == notify->portid) {
14900 sched_scan_req->owner_nlportid = 0;
14901
14902 if (rdev->ops->sched_scan_stop &&
14903 rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
14904 schedule_work(&rdev->sched_scan_stop_wk);
14905 }
Johannes Berg78f22b62014-03-24 17:57:27 +010014906
Johannes Berg53873f12016-05-03 16:52:04 +030014907 list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000014908 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070014909
Johannes Berg78f22b62014-03-24 17:57:27 +010014910 if (wdev->owner_nlportid == notify->portid)
14911 schedule_destroy_work = true;
Andrzej Zaborowskibd2522b2017-01-06 16:33:43 -050014912 else if (wdev->conn_owner_nlportid == notify->portid)
14913 schedule_work(&wdev->disconnect_wk);
Johannes Berg78f22b62014-03-24 17:57:27 +010014914 }
14915
Ben Greear37c73b52012-10-26 14:49:25 -070014916 spin_lock_bh(&rdev->beacon_registrations_lock);
14917 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
14918 list) {
14919 if (reg->nlportid == notify->portid) {
14920 list_del(&reg->list);
14921 kfree(reg);
14922 break;
14923 }
14924 }
14925 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010014926
14927 if (schedule_destroy_work) {
14928 struct cfg80211_iface_destroy *destroy;
14929
14930 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
14931 if (destroy) {
14932 destroy->nlportid = notify->portid;
14933 spin_lock(&rdev->destroy_list_lock);
14934 list_add(&destroy->list, &rdev->destroy_list);
14935 spin_unlock(&rdev->destroy_list_lock);
14936 schedule_work(&rdev->destroy_work);
14937 }
14938 }
Johannes Berg5e760232011-11-04 11:18:17 +010014939 }
Jouni Malinen026331c2010-02-15 12:53:10 +020014940
14941 rcu_read_unlock();
14942
Ilan peer05050752015-03-04 00:32:06 -050014943 /*
14944 * It is possible that the user space process that is controlling the
14945 * indoor setting disappeared, so notify the regulatory core.
14946 */
14947 regulatory_netlink_notify(notify->portid);
Zhao, Gang6784c7d2014-04-21 12:53:04 +080014948 return NOTIFY_OK;
Jouni Malinen026331c2010-02-15 12:53:10 +020014949}
14950
14951static struct notifier_block nl80211_netlink_notifier = {
14952 .notifier_call = nl80211_netlink_notify,
14953};
14954
Jouni Malinen355199e2013-02-27 17:14:27 +020014955void cfg80211_ft_event(struct net_device *netdev,
14956 struct cfg80211_ft_event_params *ft_event)
14957{
14958 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080014959 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Jouni Malinen355199e2013-02-27 17:14:27 +020014960 struct sk_buff *msg;
14961 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020014962
14963 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
14964
14965 if (!ft_event->target_ap)
14966 return;
14967
Johannes Berg4ef8c1c2017-01-09 11:10:42 +010014968 msg = nlmsg_new(100 + ft_event->ric_ies_len, GFP_KERNEL);
Jouni Malinen355199e2013-02-27 17:14:27 +020014969 if (!msg)
14970 return;
14971
14972 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020014973 if (!hdr)
14974 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020014975
Johannes Bergae917c92013-10-25 11:05:22 +020014976 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14977 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14978 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
14979 goto out;
14980
14981 if (ft_event->ies &&
14982 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
14983 goto out;
14984 if (ft_event->ric_ies &&
14985 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
14986 ft_event->ric_ies))
14987 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020014988
Johannes Berg9c90a9f2013-06-04 12:46:03 +020014989 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020014990
Johannes Berg68eb5502013-11-19 15:19:38 +010014991 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010014992 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020014993 return;
14994 out:
14995 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020014996}
14997EXPORT_SYMBOL(cfg80211_ft_event);
14998
Arend van Spriel5de17982013-04-18 15:49:00 +020014999void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
15000{
15001 struct cfg80211_registered_device *rdev;
15002 struct sk_buff *msg;
15003 void *hdr;
15004 u32 nlportid;
15005
Zhao, Gangf26cbf42014-04-21 12:53:03 +080015006 rdev = wiphy_to_rdev(wdev->wiphy);
Arend van Spriel5de17982013-04-18 15:49:00 +020015007 if (!rdev->crit_proto_nlportid)
15008 return;
15009
15010 nlportid = rdev->crit_proto_nlportid;
15011 rdev->crit_proto_nlportid = 0;
15012
15013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15014 if (!msg)
15015 return;
15016
15017 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
15018 if (!hdr)
15019 goto nla_put_failure;
15020
15021 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020015022 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15023 NL80211_ATTR_PAD))
Arend van Spriel5de17982013-04-18 15:49:00 +020015024 goto nla_put_failure;
15025
15026 genlmsg_end(msg, hdr);
15027
15028 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15029 return;
15030
15031 nla_put_failure:
15032 if (hdr)
15033 genlmsg_cancel(msg, hdr);
15034 nlmsg_free(msg);
Arend van Spriel5de17982013-04-18 15:49:00 +020015035}
15036EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
15037
Johannes Berg348baf02014-01-24 14:06:29 +010015038void nl80211_send_ap_stopped(struct wireless_dev *wdev)
15039{
15040 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080015041 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg348baf02014-01-24 14:06:29 +010015042 struct sk_buff *msg;
15043 void *hdr;
15044
15045 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15046 if (!msg)
15047 return;
15048
15049 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
15050 if (!hdr)
15051 goto out;
15052
15053 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15054 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
Nicolas Dichtel2dad6242016-04-25 10:25:22 +020015055 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15056 NL80211_ATTR_PAD))
Johannes Berg348baf02014-01-24 14:06:29 +010015057 goto out;
15058
15059 genlmsg_end(msg, hdr);
15060
15061 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
15062 NL80211_MCGRP_MLME, GFP_KERNEL);
15063 return;
15064 out:
15065 nlmsg_free(msg);
15066}
15067
Johannes Berg55682962007-09-20 13:09:35 -040015068/* initialisation/exit functions */
15069
Johannes Berg56989f62016-10-24 14:40:05 +020015070int __init nl80211_init(void)
Johannes Berg55682962007-09-20 13:09:35 -040015071{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000015072 int err;
Johannes Berg55682962007-09-20 13:09:35 -040015073
Johannes Berg489111e2016-10-24 14:40:03 +020015074 err = genl_register_family(&nl80211_fam);
Johannes Berg55682962007-09-20 13:09:35 -040015075 if (err)
15076 return err;
15077
Jouni Malinen026331c2010-02-15 12:53:10 +020015078 err = netlink_register_notifier(&nl80211_netlink_notifier);
15079 if (err)
15080 goto err_out;
15081
Johannes Berg55682962007-09-20 13:09:35 -040015082 return 0;
15083 err_out:
15084 genl_unregister_family(&nl80211_fam);
15085 return err;
15086}
15087
15088void nl80211_exit(void)
15089{
Jouni Malinen026331c2010-02-15 12:53:10 +020015090 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040015091 genl_unregister_family(&nl80211_fam);
15092}