blob: a17d6bc6b22ca7ccda9716af9a3367b66bc47632 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg2740f0c2014-09-03 15:24:58 +03005 * Copyright 2013-2014 Intel Mobile Communications GmbH
Johannes Berg55682962007-09-20 13:09:35 -04006 */
7
8#include <linux/if.h>
9#include <linux/module.h>
10#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040012#include <linux/list.h>
13#include <linux/if_ether.h>
14#include <linux/ieee80211.h>
15#include <linux/nl80211.h>
16#include <linux/rtnetlink.h>
17#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010018#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020019#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040020#include <net/genetlink.h>
21#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020022#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010023#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040024#include "core.h"
25#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070026#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030027#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040028
Jouni Malinen5fb628e2011-08-10 23:54:35 +030029static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
30 struct genl_info *info,
31 struct cfg80211_crypto_settings *settings,
32 int cipher_limit);
33
Johannes Bergf84f7712013-11-14 17:14:45 +010034static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020035 struct genl_info *info);
Johannes Bergf84f7712013-11-14 17:14:45 +010036static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020037 struct genl_info *info);
38
Johannes Berg55682962007-09-20 13:09:35 -040039/* the netlink family */
40static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070041 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
42 .name = NL80211_GENL_NAME, /* have users key off the name instead */
43 .hdrsize = 0, /* no private header */
44 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040045 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020046 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020047 .pre_doit = nl80211_pre_doit,
48 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040049};
50
Johannes Berg2a94fe42013-11-19 15:19:39 +010051/* multicast groups */
52enum nl80211_multicast_groups {
53 NL80211_MCGRP_CONFIG,
54 NL80211_MCGRP_SCAN,
55 NL80211_MCGRP_REGULATORY,
56 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010057 NL80211_MCGRP_VENDOR,
Johannes Berg2a94fe42013-11-19 15:19:39 +010058 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
59};
60
61static const struct genl_multicast_group nl80211_mcgrps[] = {
62 [NL80211_MCGRP_CONFIG] = { .name = "config", },
63 [NL80211_MCGRP_SCAN] = { .name = "scan", },
64 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
65 [NL80211_MCGRP_MLME] = { .name = "mlme", },
Johannes Berg567ffc32013-12-18 14:43:31 +010066 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
Johannes Berg2a94fe42013-11-19 15:19:39 +010067#ifdef CONFIG_NL80211_TESTMODE
68 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
69#endif
70};
71
Johannes Berg89a54e42012-06-15 14:33:17 +020072/* returns ERR_PTR values */
73static struct wireless_dev *
74__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040075{
Johannes Berg89a54e42012-06-15 14:33:17 +020076 struct cfg80211_registered_device *rdev;
77 struct wireless_dev *result = NULL;
78 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
79 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
80 u64 wdev_id;
81 int wiphy_idx = -1;
82 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040083
Johannes Berg5fe231e2013-05-08 21:45:15 +020084 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040085
Johannes Berg89a54e42012-06-15 14:33:17 +020086 if (!have_ifidx && !have_wdev_id)
87 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040088
Johannes Berg89a54e42012-06-15 14:33:17 +020089 if (have_ifidx)
90 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
91 if (have_wdev_id) {
92 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
93 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040094 }
95
Johannes Berg89a54e42012-06-15 14:33:17 +020096 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
97 struct wireless_dev *wdev;
98
99 if (wiphy_net(&rdev->wiphy) != netns)
100 continue;
101
102 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
103 continue;
104
Johannes Berg89a54e42012-06-15 14:33:17 +0200105 list_for_each_entry(wdev, &rdev->wdev_list, list) {
106 if (have_ifidx && wdev->netdev &&
107 wdev->netdev->ifindex == ifidx) {
108 result = wdev;
109 break;
110 }
111 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
112 result = wdev;
113 break;
114 }
115 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200116
117 if (result)
118 break;
119 }
120
121 if (result)
122 return result;
123 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400124}
125
Johannes Berga9455402012-06-15 13:32:49 +0200126static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200127__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200128{
Johannes Berg7fee4772012-06-15 14:09:58 +0200129 struct cfg80211_registered_device *rdev = NULL, *tmp;
130 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200131
Johannes Berg5fe231e2013-05-08 21:45:15 +0200132 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200133
Johannes Berg878d9ec2012-06-15 14:18:32 +0200134 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200135 !attrs[NL80211_ATTR_IFINDEX] &&
136 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200137 return ERR_PTR(-EINVAL);
138
Johannes Berg878d9ec2012-06-15 14:18:32 +0200139 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200140 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200141 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200142
Johannes Berg89a54e42012-06-15 14:33:17 +0200143 if (attrs[NL80211_ATTR_WDEV]) {
144 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
145 struct wireless_dev *wdev;
146 bool found = false;
147
148 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
149 if (tmp) {
150 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200151 list_for_each_entry(wdev, &tmp->wdev_list, list) {
152 if (wdev->identifier != (u32)wdev_id)
153 continue;
154 found = true;
155 break;
156 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200157
158 if (!found)
159 tmp = NULL;
160
161 if (rdev && tmp != rdev)
162 return ERR_PTR(-EINVAL);
163 rdev = tmp;
164 }
165 }
166
Johannes Berg878d9ec2012-06-15 14:18:32 +0200167 if (attrs[NL80211_ATTR_IFINDEX]) {
168 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Ying Xue7f2b8562014-01-15 10:23:45 +0800169 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200170 if (netdev) {
171 if (netdev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800172 tmp = wiphy_to_rdev(
173 netdev->ieee80211_ptr->wiphy);
Johannes Berg7fee4772012-06-15 14:09:58 +0200174 else
175 tmp = NULL;
176
Johannes Berg7fee4772012-06-15 14:09:58 +0200177 /* not wireless device -- return error */
178 if (!tmp)
179 return ERR_PTR(-EINVAL);
180
181 /* mismatch -- return error */
182 if (rdev && tmp != rdev)
183 return ERR_PTR(-EINVAL);
184
185 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200186 }
Johannes Berga9455402012-06-15 13:32:49 +0200187 }
188
Johannes Berg4f7eff12012-06-15 14:14:22 +0200189 if (!rdev)
190 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200191
Johannes Berg4f7eff12012-06-15 14:14:22 +0200192 if (netns != wiphy_net(&rdev->wiphy))
193 return ERR_PTR(-ENODEV);
194
195 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200196}
197
198/*
199 * This function returns a pointer to the driver
200 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200201 *
202 * The result of this can be a PTR_ERR and hence must
203 * be checked with IS_ERR() for errors.
204 */
205static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200206cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200207{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200208 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209}
210
Johannes Berg55682962007-09-20 13:09:35 -0400211/* policy for the attributes */
Luciano Coelho8cd4d452014-09-17 11:55:28 +0300212static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
Johannes Berg55682962007-09-20 13:09:35 -0400213 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
214 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700215 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200216 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100217
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200218 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530219 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100220 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
222 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
223
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200224 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
226 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
227 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100228 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +0200229 [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400230
231 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
232 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
233 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100234
Eliad Pellere007b852011-11-24 18:13:56 +0200235 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
236 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Johannes Bergb9454e82009-07-08 13:29:08 +0200238 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100239 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
240 .len = WLAN_MAX_KEY_LEN },
241 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
242 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
243 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200244 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200245 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100246
247 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
248 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
249 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
251 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100253 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
255 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
256 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
257 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100258 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100259 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200260 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800262 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100263 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300264
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700265 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
266 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
267
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300268 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
269 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
270 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200271 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
272 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100273 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300274
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800275 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700276 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700277
Johannes Berg6c739412011-11-03 09:27:01 +0100278 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200279
280 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
281 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
282 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100283 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
284 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200285
286 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
287 .len = IEEE80211_MAX_SSID_LEN },
288 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
289 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200290 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300291 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300292 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300293 [NL80211_ATTR_STA_FLAGS2] = {
294 .len = sizeof(struct nl80211_sta_flag_update),
295 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300296 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300297 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
298 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200299 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
300 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
301 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200302 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100303 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100304 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
305 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100306 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
307 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200308 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200309 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
310 .len = IEEE80211_MAX_DATA_LEN },
311 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200312 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200313 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300314 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200315 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300316 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
317 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200318 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900319 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100321 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100322 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100323 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200324 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700325 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300326 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200327 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200328 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300329 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300330 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
332 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
333 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530334 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300335 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530336 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300337 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
339 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
340 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
341 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Arik Nemtsov31fa97c2014-06-11 17:18:21 +0300342 [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100343 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200344 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
345 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700346 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800347 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
348 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
349 .len = NL80211_HT_CAPABILITY_LEN
350 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100351 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530352 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530353 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200354 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700355 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300356 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000357 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700358 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100359 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
360 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530361 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
362 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200363 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
364 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100365 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100366 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
367 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
368 .len = NL80211_VHT_CAPABILITY_LEN,
369 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200370 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
371 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
372 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300373 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200374 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
375 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
376 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +0300377 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
378 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530379 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
380 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200381 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100382 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100383 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
384 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
385 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800386 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
387 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200388 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
389 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530390 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Jukka Rissanen18e5ca62014-11-13 17:25:14 +0200391 [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
Andrei Otcheretianski34d22ce2014-05-09 14:11:44 +0300392 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
Assaf Kraussbab5ab72014-09-03 15:25:01 +0300393 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
Johannes Berg960d01a2014-09-09 22:55:35 +0300394 [NL80211_ATTR_TSID] = { .type = NLA_U8 },
395 [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
396 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
Eliad Peller18998c32014-09-10 14:07:34 +0300397 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
Johannes Bergad2b26a2014-06-12 21:39:05 +0200398 [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
Johannes Berg55682962007-09-20 13:09:35 -0400399};
400
Johannes Berge31b8212010-10-05 19:39:30 +0200401/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000402static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200403 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200404 [NL80211_KEY_IDX] = { .type = NLA_U8 },
405 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200406 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200407 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
408 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200409 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100410 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
411};
412
413/* policy for the key default flags */
414static const struct nla_policy
415nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
416 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
417 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200418};
419
Johannes Bergff1b6e62011-05-04 15:37:28 +0200420/* policy for WoWLAN attributes */
421static const struct nla_policy
422nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
423 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
424 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
425 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
426 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200427 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
428 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
429 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
430 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100431 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
Luciano Coelho8cd4d452014-09-17 11:55:28 +0300432 [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100433};
434
435static const struct nla_policy
436nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
437 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
438 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
439 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
440 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
441 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
442 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
443 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
444 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
445 },
446 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
447 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
448 },
449 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
450 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
451 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200452};
453
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700454/* policy for coalesce rule attributes */
455static const struct nla_policy
456nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
457 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
458 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
459 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
460};
461
Johannes Berge5497d72011-07-05 16:35:40 +0200462/* policy for GTK rekey offload attributes */
463static const struct nla_policy
464nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
465 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
466 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
467 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
468};
469
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300470static const struct nla_policy
471nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200472 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300473 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700474 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300475};
476
Johannes Berg97990a02013-04-19 01:02:55 +0200477static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
478 struct netlink_callback *cb,
479 struct cfg80211_registered_device **rdev,
480 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100481{
Johannes Berg67748892010-10-04 21:14:06 +0200482 int err;
483
Johannes Berg67748892010-10-04 21:14:06 +0200484 rtnl_lock();
485
Johannes Berg97990a02013-04-19 01:02:55 +0200486 if (!cb->args[0]) {
487 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
488 nl80211_fam.attrbuf, nl80211_fam.maxattr,
489 nl80211_policy);
490 if (err)
491 goto out_unlock;
492
493 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
494 nl80211_fam.attrbuf);
495 if (IS_ERR(*wdev)) {
496 err = PTR_ERR(*wdev);
497 goto out_unlock;
498 }
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800499 *rdev = wiphy_to_rdev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200500 /* 0 is the first index - add 1 to parse only once */
501 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200502 cb->args[1] = (*wdev)->identifier;
503 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200504 /* subtract the 1 again here */
505 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200506 struct wireless_dev *tmp;
507
508 if (!wiphy) {
509 err = -ENODEV;
510 goto out_unlock;
511 }
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800512 *rdev = wiphy_to_rdev(wiphy);
Johannes Berg97990a02013-04-19 01:02:55 +0200513 *wdev = NULL;
514
Johannes Berg97990a02013-04-19 01:02:55 +0200515 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
516 if (tmp->identifier == cb->args[1]) {
517 *wdev = tmp;
518 break;
519 }
520 }
Johannes Berg97990a02013-04-19 01:02:55 +0200521
522 if (!*wdev) {
523 err = -ENODEV;
524 goto out_unlock;
525 }
Johannes Berg67748892010-10-04 21:14:06 +0200526 }
527
Johannes Berg67748892010-10-04 21:14:06 +0200528 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200529 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200530 rtnl_unlock();
531 return err;
532}
533
Johannes Berg97990a02013-04-19 01:02:55 +0200534static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200535{
Johannes Berg67748892010-10-04 21:14:06 +0200536 rtnl_unlock();
537}
538
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100539/* IE validation */
540static bool is_valid_ie_attr(const struct nlattr *attr)
541{
542 const u8 *pos;
543 int len;
544
545 if (!attr)
546 return true;
547
548 pos = nla_data(attr);
549 len = nla_len(attr);
550
551 while (len) {
552 u8 elemlen;
553
554 if (len < 2)
555 return false;
556 len -= 2;
557
558 elemlen = pos[1];
559 if (elemlen > len)
560 return false;
561
562 len -= elemlen;
563 pos += 2 + elemlen;
564 }
565
566 return true;
567}
568
Johannes Berg55682962007-09-20 13:09:35 -0400569/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000570static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400571 int flags, u8 cmd)
572{
573 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000574 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400575}
576
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400577static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100578 struct ieee80211_channel *chan,
579 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400580{
Rostislav Lisovyea077c12014-04-15 14:37:55 +0200581 /* Some channels must be completely excluded from the
582 * list to protect old user-space tools from breaking
583 */
584 if (!large && chan->flags &
585 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
586 return 0;
587
David S. Miller9360ffd2012-03-29 04:41:26 -0400588 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
589 chan->center_freq))
590 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400591
David S. Miller9360ffd2012-03-29 04:41:26 -0400592 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
593 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
594 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200595 if (chan->flags & IEEE80211_CHAN_NO_IR) {
596 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
597 goto nla_put_failure;
598 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
599 goto nla_put_failure;
600 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100601 if (chan->flags & IEEE80211_CHAN_RADAR) {
602 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
603 goto nla_put_failure;
604 if (large) {
605 u32 time;
606
607 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
608
609 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
610 chan->dfs_state))
611 goto nla_put_failure;
612 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
613 time))
614 goto nla_put_failure;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100615 if (nla_put_u32(msg,
616 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
617 chan->dfs_cac_ms))
618 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100619 }
620 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400621
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100622 if (large) {
623 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
624 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
625 goto nla_put_failure;
626 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
627 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
628 goto nla_put_failure;
629 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
630 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
631 goto nla_put_failure;
632 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
633 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
634 goto nla_put_failure;
David Spinadel570dbde2014-02-23 09:12:59 +0200635 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
636 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
637 goto nla_put_failure;
638 if ((chan->flags & IEEE80211_CHAN_GO_CONCURRENT) &&
639 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_GO_CONCURRENT))
640 goto nla_put_failure;
Rostislav Lisovyea077c12014-04-15 14:37:55 +0200641 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
642 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
643 goto nla_put_failure;
644 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
645 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
646 goto nla_put_failure;
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100647 }
648
David S. Miller9360ffd2012-03-29 04:41:26 -0400649 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
650 DBM_TO_MBM(chan->max_power)))
651 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400652
653 return 0;
654
655 nla_put_failure:
656 return -ENOBUFS;
657}
658
Johannes Berg55682962007-09-20 13:09:35 -0400659/* netlink command implementations */
660
Johannes Bergb9454e82009-07-08 13:29:08 +0200661struct key_parse {
662 struct key_params p;
663 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200664 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200665 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100666 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200667};
668
669static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
670{
671 struct nlattr *tb[NL80211_KEY_MAX + 1];
672 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
673 nl80211_key_policy);
674 if (err)
675 return err;
676
677 k->def = !!tb[NL80211_KEY_DEFAULT];
678 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
679
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100680 if (k->def) {
681 k->def_uni = true;
682 k->def_multi = true;
683 }
684 if (k->defmgmt)
685 k->def_multi = true;
686
Johannes Bergb9454e82009-07-08 13:29:08 +0200687 if (tb[NL80211_KEY_IDX])
688 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
689
690 if (tb[NL80211_KEY_DATA]) {
691 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
692 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
693 }
694
695 if (tb[NL80211_KEY_SEQ]) {
696 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
697 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
698 }
699
700 if (tb[NL80211_KEY_CIPHER])
701 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
702
Johannes Berge31b8212010-10-05 19:39:30 +0200703 if (tb[NL80211_KEY_TYPE]) {
704 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
705 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
706 return -EINVAL;
707 }
708
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100709 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
710 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100711 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
712 tb[NL80211_KEY_DEFAULT_TYPES],
713 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (err)
715 return err;
716
717 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
718 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
719 }
720
Johannes Bergb9454e82009-07-08 13:29:08 +0200721 return 0;
722}
723
724static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
725{
726 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
727 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
728 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
729 }
730
731 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
732 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
733 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
734 }
735
736 if (info->attrs[NL80211_ATTR_KEY_IDX])
737 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
738
739 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
740 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
741
742 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
743 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
744
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100745 if (k->def) {
746 k->def_uni = true;
747 k->def_multi = true;
748 }
749 if (k->defmgmt)
750 k->def_multi = true;
751
Johannes Berge31b8212010-10-05 19:39:30 +0200752 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
753 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
754 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
755 return -EINVAL;
756 }
757
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100758 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
759 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
760 int err = nla_parse_nested(
761 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
762 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
763 nl80211_key_default_policy);
764 if (err)
765 return err;
766
767 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
768 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
769 }
770
Johannes Bergb9454e82009-07-08 13:29:08 +0200771 return 0;
772}
773
774static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
775{
776 int err;
777
778 memset(k, 0, sizeof(*k));
779 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200780 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200781
782 if (info->attrs[NL80211_ATTR_KEY])
783 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
784 else
785 err = nl80211_parse_key_old(info, k);
786
787 if (err)
788 return err;
789
790 if (k->def && k->defmgmt)
791 return -EINVAL;
792
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100793 if (k->defmgmt) {
794 if (k->def_uni || !k->def_multi)
795 return -EINVAL;
796 }
797
Johannes Bergb9454e82009-07-08 13:29:08 +0200798 if (k->idx != -1) {
799 if (k->defmgmt) {
800 if (k->idx < 4 || k->idx > 5)
801 return -EINVAL;
802 } else if (k->def) {
803 if (k->idx < 0 || k->idx > 3)
804 return -EINVAL;
805 } else {
806 if (k->idx < 0 || k->idx > 5)
807 return -EINVAL;
808 }
809 }
810
811 return 0;
812}
813
Johannes Bergfffd0932009-07-08 14:22:54 +0200814static struct cfg80211_cached_keys *
815nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530816 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200817{
818 struct key_parse parse;
819 struct nlattr *key;
820 struct cfg80211_cached_keys *result;
821 int rem, err, def = 0;
822
823 result = kzalloc(sizeof(*result), GFP_KERNEL);
824 if (!result)
825 return ERR_PTR(-ENOMEM);
826
827 result->def = -1;
828 result->defmgmt = -1;
829
830 nla_for_each_nested(key, keys, rem) {
831 memset(&parse, 0, sizeof(parse));
832 parse.idx = -1;
833
834 err = nl80211_parse_key_new(key, &parse);
835 if (err)
836 goto error;
837 err = -EINVAL;
838 if (!parse.p.key)
839 goto error;
840 if (parse.idx < 0 || parse.idx > 4)
841 goto error;
842 if (parse.def) {
843 if (def)
844 goto error;
845 def = 1;
846 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100847 if (!parse.def_uni || !parse.def_multi)
848 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 } else if (parse.defmgmt)
850 goto error;
851 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200852 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 if (err)
854 goto error;
855 result->params[parse.idx].cipher = parse.p.cipher;
856 result->params[parse.idx].key_len = parse.p.key_len;
857 result->params[parse.idx].key = result->data[parse.idx];
858 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530859
860 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
861 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
862 if (no_ht)
863 *no_ht = true;
864 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200865 }
866
867 return result;
868 error:
869 kfree(result);
870 return ERR_PTR(err);
871}
872
873static int nl80211_key_allowed(struct wireless_dev *wdev)
874{
875 ASSERT_WDEV_LOCK(wdev);
876
Johannes Bergfffd0932009-07-08 14:22:54 +0200877 switch (wdev->iftype) {
878 case NL80211_IFTYPE_AP:
879 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200880 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700881 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200882 break;
883 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200884 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200885 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200886 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200887 return -ENOLINK;
888 break;
Johannes Bergde4fcba2014-10-31 14:16:12 +0100889 case NL80211_IFTYPE_UNSPECIFIED:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100890 case NL80211_IFTYPE_OCB:
Johannes Bergde4fcba2014-10-31 14:16:12 +0100891 case NL80211_IFTYPE_MONITOR:
892 case NL80211_IFTYPE_P2P_DEVICE:
893 case NL80211_IFTYPE_WDS:
894 case NUM_NL80211_IFTYPES:
Johannes Bergfffd0932009-07-08 14:22:54 +0200895 return -EINVAL;
896 }
897
898 return 0;
899}
900
Jouni Malinen664834d2014-01-15 00:01:44 +0200901static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
902 struct nlattr *tb)
903{
904 struct ieee80211_channel *chan;
905
906 if (tb == NULL)
907 return NULL;
908 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
909 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
910 return NULL;
911 return chan;
912}
913
Johannes Berg7527a782011-05-13 10:58:57 +0200914static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
915{
916 struct nlattr *nl_modes = nla_nest_start(msg, attr);
917 int i;
918
919 if (!nl_modes)
920 goto nla_put_failure;
921
922 i = 0;
923 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400924 if ((ifmodes & 1) && nla_put_flag(msg, i))
925 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200926 ifmodes >>= 1;
927 i++;
928 }
929
930 nla_nest_end(msg, nl_modes);
931 return 0;
932
933nla_put_failure:
934 return -ENOBUFS;
935}
936
937static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100938 struct sk_buff *msg,
939 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200940{
941 struct nlattr *nl_combis;
942 int i, j;
943
944 nl_combis = nla_nest_start(msg,
945 NL80211_ATTR_INTERFACE_COMBINATIONS);
946 if (!nl_combis)
947 goto nla_put_failure;
948
949 for (i = 0; i < wiphy->n_iface_combinations; i++) {
950 const struct ieee80211_iface_combination *c;
951 struct nlattr *nl_combi, *nl_limits;
952
953 c = &wiphy->iface_combinations[i];
954
955 nl_combi = nla_nest_start(msg, i + 1);
956 if (!nl_combi)
957 goto nla_put_failure;
958
959 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
960 if (!nl_limits)
961 goto nla_put_failure;
962
963 for (j = 0; j < c->n_limits; j++) {
964 struct nlattr *nl_limit;
965
966 nl_limit = nla_nest_start(msg, j + 1);
967 if (!nl_limit)
968 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400969 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
970 c->limits[j].max))
971 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200972 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
973 c->limits[j].types))
974 goto nla_put_failure;
975 nla_nest_end(msg, nl_limit);
976 }
977
978 nla_nest_end(msg, nl_limits);
979
David S. Miller9360ffd2012-03-29 04:41:26 -0400980 if (c->beacon_int_infra_match &&
981 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
982 goto nla_put_failure;
983 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
984 c->num_different_channels) ||
985 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
986 c->max_interfaces))
987 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100988 if (large &&
Felix Fietkau8c48b502014-05-05 11:48:40 +0200989 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
990 c->radar_detect_widths) ||
991 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
992 c->radar_detect_regions)))
Johannes Bergcdc89b92013-02-18 23:54:36 +0100993 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200994
995 nla_nest_end(msg, nl_combi);
996 }
997
998 nla_nest_end(msg, nl_combis);
999
1000 return 0;
1001nla_put_failure:
1002 return -ENOBUFS;
1003}
1004
Johannes Berg3713b4e2013-02-14 16:19:38 +01001005#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001006static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1007 struct sk_buff *msg)
1008{
Johannes Berg964dc9e2013-06-03 17:25:34 +02001009 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +01001010 struct nlattr *nl_tcp;
1011
1012 if (!tcp)
1013 return 0;
1014
1015 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1016 if (!nl_tcp)
1017 return -ENOBUFS;
1018
1019 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1020 tcp->data_payload_max))
1021 return -ENOBUFS;
1022
1023 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1024 tcp->data_payload_max))
1025 return -ENOBUFS;
1026
1027 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1028 return -ENOBUFS;
1029
1030 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1031 sizeof(*tcp->tok), tcp->tok))
1032 return -ENOBUFS;
1033
1034 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1035 tcp->data_interval_max))
1036 return -ENOBUFS;
1037
1038 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1039 tcp->wake_payload_max))
1040 return -ENOBUFS;
1041
1042 nla_nest_end(msg, nl_tcp);
1043 return 0;
1044}
1045
Johannes Berg3713b4e2013-02-14 16:19:38 +01001046static int nl80211_send_wowlan(struct sk_buff *msg,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001047 struct cfg80211_registered_device *rdev,
Johannes Bergb56cf722013-02-20 01:02:38 +01001048 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001049{
1050 struct nlattr *nl_wowlan;
1051
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001052 if (!rdev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001053 return 0;
1054
1055 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1056 if (!nl_wowlan)
1057 return -ENOBUFS;
1058
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001059 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001060 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001061 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001062 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001063 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001064 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001065 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001066 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001067 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001068 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001069 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001070 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001071 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001072 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001073 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001074 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1075 return -ENOBUFS;
1076
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001077 if (rdev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001078 struct nl80211_pattern_support pat = {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001079 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1080 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1081 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1082 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001083 };
1084
1085 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1086 sizeof(pat), &pat))
1087 return -ENOBUFS;
1088 }
1089
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001090 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
Johannes Bergb56cf722013-02-20 01:02:38 +01001091 return -ENOBUFS;
1092
Luciano Coelho8cd4d452014-09-17 11:55:28 +03001093 /* TODO: send wowlan net detect */
1094
Johannes Berg3713b4e2013-02-14 16:19:38 +01001095 nla_nest_end(msg, nl_wowlan);
1096
1097 return 0;
1098}
1099#endif
1100
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001101static int nl80211_send_coalesce(struct sk_buff *msg,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001102 struct cfg80211_registered_device *rdev)
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001103{
1104 struct nl80211_coalesce_rule_support rule;
1105
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001106 if (!rdev->wiphy.coalesce)
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001107 return 0;
1108
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001109 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1110 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1111 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1112 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1113 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1114 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001115
1116 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1117 return -ENOBUFS;
1118
1119 return 0;
1120}
1121
Johannes Berg3713b4e2013-02-14 16:19:38 +01001122static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1123 struct ieee80211_supported_band *sband)
1124{
1125 struct nlattr *nl_rates, *nl_rate;
1126 struct ieee80211_rate *rate;
1127 int i;
1128
1129 /* add HT info */
1130 if (sband->ht_cap.ht_supported &&
1131 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1132 sizeof(sband->ht_cap.mcs),
1133 &sband->ht_cap.mcs) ||
1134 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1135 sband->ht_cap.cap) ||
1136 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1137 sband->ht_cap.ampdu_factor) ||
1138 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1139 sband->ht_cap.ampdu_density)))
1140 return -ENOBUFS;
1141
1142 /* add VHT info */
1143 if (sband->vht_cap.vht_supported &&
1144 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1145 sizeof(sband->vht_cap.vht_mcs),
1146 &sband->vht_cap.vht_mcs) ||
1147 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1148 sband->vht_cap.cap)))
1149 return -ENOBUFS;
1150
1151 /* add bitrates */
1152 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1153 if (!nl_rates)
1154 return -ENOBUFS;
1155
1156 for (i = 0; i < sband->n_bitrates; i++) {
1157 nl_rate = nla_nest_start(msg, i);
1158 if (!nl_rate)
1159 return -ENOBUFS;
1160
1161 rate = &sband->bitrates[i];
1162 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1163 rate->bitrate))
1164 return -ENOBUFS;
1165 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1166 nla_put_flag(msg,
1167 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1168 return -ENOBUFS;
1169
1170 nla_nest_end(msg, nl_rate);
1171 }
1172
1173 nla_nest_end(msg, nl_rates);
1174
1175 return 0;
1176}
1177
1178static int
1179nl80211_send_mgmt_stypes(struct sk_buff *msg,
1180 const struct ieee80211_txrx_stypes *mgmt_stypes)
1181{
1182 u16 stypes;
1183 struct nlattr *nl_ftypes, *nl_ifs;
1184 enum nl80211_iftype ift;
1185 int i;
1186
1187 if (!mgmt_stypes)
1188 return 0;
1189
1190 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1191 if (!nl_ifs)
1192 return -ENOBUFS;
1193
1194 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1195 nl_ftypes = nla_nest_start(msg, ift);
1196 if (!nl_ftypes)
1197 return -ENOBUFS;
1198 i = 0;
1199 stypes = mgmt_stypes[ift].tx;
1200 while (stypes) {
1201 if ((stypes & 1) &&
1202 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1203 (i << 4) | IEEE80211_FTYPE_MGMT))
1204 return -ENOBUFS;
1205 stypes >>= 1;
1206 i++;
1207 }
1208 nla_nest_end(msg, nl_ftypes);
1209 }
1210
1211 nla_nest_end(msg, nl_ifs);
1212
1213 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1214 if (!nl_ifs)
1215 return -ENOBUFS;
1216
1217 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1218 nl_ftypes = nla_nest_start(msg, ift);
1219 if (!nl_ftypes)
1220 return -ENOBUFS;
1221 i = 0;
1222 stypes = mgmt_stypes[ift].rx;
1223 while (stypes) {
1224 if ((stypes & 1) &&
1225 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1226 (i << 4) | IEEE80211_FTYPE_MGMT))
1227 return -ENOBUFS;
1228 stypes >>= 1;
1229 i++;
1230 }
1231 nla_nest_end(msg, nl_ftypes);
1232 }
1233 nla_nest_end(msg, nl_ifs);
1234
1235 return 0;
1236}
1237
Johannes Berg86e8cf92013-06-19 10:57:22 +02001238struct nl80211_dump_wiphy_state {
1239 s64 filter_wiphy;
1240 long start;
1241 long split_start, band_start, chan_start;
1242 bool split;
1243};
1244
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001245static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
Johannes Berg3bb20552014-05-26 13:52:25 +02001246 enum nl80211_commands cmd,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001247 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001248 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001249{
1250 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001251 struct nlattr *nl_bands, *nl_band;
1252 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001253 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001254 enum ieee80211_band band;
1255 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001256 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001257 const struct ieee80211_txrx_stypes *mgmt_stypes =
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001258 rdev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001259 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001260
Johannes Berg3bb20552014-05-26 13:52:25 +02001261 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -04001262 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001263 return -ENOBUFS;
1264
Johannes Berg86e8cf92013-06-19 10:57:22 +02001265 if (WARN_ON(!state))
1266 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001267
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001268 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001269 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001270 wiphy_name(&rdev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001271 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001272 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001273 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001274
Johannes Berg3bb20552014-05-26 13:52:25 +02001275 if (cmd != NL80211_CMD_NEW_WIPHY)
1276 goto finish;
1277
Johannes Berg86e8cf92013-06-19 10:57:22 +02001278 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001279 case 0:
1280 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001281 rdev->wiphy.retry_short) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001282 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001283 rdev->wiphy.retry_long) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001284 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001285 rdev->wiphy.frag_threshold) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001286 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001287 rdev->wiphy.rts_threshold) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001288 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001289 rdev->wiphy.coverage_class) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001290 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001291 rdev->wiphy.max_scan_ssids) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001293 rdev->wiphy.max_sched_scan_ssids) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001294 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001295 rdev->wiphy.max_scan_ie_len) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001296 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001297 rdev->wiphy.max_sched_scan_ie_len) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001298 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001299 rdev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001300 goto nla_put_failure;
1301
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001302 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001303 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1304 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001305 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001306 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1307 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001308 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001309 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1310 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001311 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001312 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1313 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001314 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001315 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1316 goto nla_put_failure;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001317 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001318 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001319 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001320 state->split_start++;
1321 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001322 break;
1323 case 1:
1324 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001325 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1326 rdev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001327 goto nla_put_failure;
1328
Johannes Berg3713b4e2013-02-14 16:19:38 +01001329 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001330 rdev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001331 goto nla_put_failure;
1332
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001333 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001334 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1335 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001336
Johannes Berg3713b4e2013-02-14 16:19:38 +01001337 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001338 rdev->wiphy.available_antennas_tx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001339 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001340 rdev->wiphy.available_antennas_rx))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001341 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001342
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001343 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001344 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001345 rdev->wiphy.probe_resp_offload))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001346 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001347
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001348 if ((rdev->wiphy.available_antennas_tx ||
1349 rdev->wiphy.available_antennas_rx) &&
1350 rdev->ops->get_antenna) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 u32 tx_ant = 0, rx_ant = 0;
1352 int res;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001353 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001354 if (!res) {
1355 if (nla_put_u32(msg,
1356 NL80211_ATTR_WIPHY_ANTENNA_TX,
1357 tx_ant) ||
1358 nla_put_u32(msg,
1359 NL80211_ATTR_WIPHY_ANTENNA_RX,
1360 rx_ant))
1361 goto nla_put_failure;
1362 }
Johannes Bergee688b002008-01-24 19:38:39 +01001363 }
1364
Johannes Berg86e8cf92013-06-19 10:57:22 +02001365 state->split_start++;
1366 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001367 break;
1368 case 2:
1369 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001370 rdev->wiphy.interface_modes))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001372 state->split_start++;
1373 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001374 break;
1375 case 3:
1376 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1377 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001378 goto nla_put_failure;
1379
Johannes Berg86e8cf92013-06-19 10:57:22 +02001380 for (band = state->band_start;
1381 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001382 struct ieee80211_supported_band *sband;
1383
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001384 sband = rdev->wiphy.bands[band];
Johannes Berg3713b4e2013-02-14 16:19:38 +01001385
1386 if (!sband)
1387 continue;
1388
1389 nl_band = nla_nest_start(msg, band);
1390 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001391 goto nla_put_failure;
1392
Johannes Berg86e8cf92013-06-19 10:57:22 +02001393 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001394 case 0:
1395 if (nl80211_send_band_rateinfo(msg, sband))
1396 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001397 state->chan_start++;
1398 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001399 break;
1400 default:
1401 /* add frequencies */
1402 nl_freqs = nla_nest_start(
1403 msg, NL80211_BAND_ATTR_FREQS);
1404 if (!nl_freqs)
1405 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001406
Johannes Berg86e8cf92013-06-19 10:57:22 +02001407 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001408 i < sband->n_channels;
1409 i++) {
1410 nl_freq = nla_nest_start(msg, i);
1411 if (!nl_freq)
1412 goto nla_put_failure;
1413
1414 chan = &sband->channels[i];
1415
Johannes Berg86e8cf92013-06-19 10:57:22 +02001416 if (nl80211_msg_put_channel(
1417 msg, chan,
1418 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001419 goto nla_put_failure;
1420
1421 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001422 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001423 break;
1424 }
1425 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001426 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001427 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001428 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001429 nla_nest_end(msg, nl_freqs);
1430 }
1431
1432 nla_nest_end(msg, nl_band);
1433
Johannes Berg86e8cf92013-06-19 10:57:22 +02001434 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001435 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001436 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001437 band--;
1438 break;
1439 }
Johannes Bergee688b002008-01-24 19:38:39 +01001440 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001441 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001442
Johannes Berg3713b4e2013-02-14 16:19:38 +01001443 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001444 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001445 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001446 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001447
Johannes Berg3713b4e2013-02-14 16:19:38 +01001448 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001449 if (state->band_start == 0 && state->chan_start == 0)
1450 state->split_start++;
1451 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001452 break;
1453 case 4:
1454 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1455 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001456 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001457
1458 i = 0;
1459#define CMD(op, n) \
1460 do { \
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001461 if (rdev->ops->op) { \
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 i++; \
1463 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1464 goto nla_put_failure; \
1465 } \
1466 } while (0)
1467
1468 CMD(add_virtual_intf, NEW_INTERFACE);
1469 CMD(change_virtual_intf, SET_INTERFACE);
1470 CMD(add_key, NEW_KEY);
1471 CMD(start_ap, START_AP);
1472 CMD(add_station, NEW_STATION);
1473 CMD(add_mpath, NEW_MPATH);
1474 CMD(update_mesh_config, SET_MESH_CONFIG);
1475 CMD(change_bss, SET_BSS);
1476 CMD(auth, AUTHENTICATE);
1477 CMD(assoc, ASSOCIATE);
1478 CMD(deauth, DEAUTHENTICATE);
1479 CMD(disassoc, DISASSOCIATE);
1480 CMD(join_ibss, JOIN_IBSS);
1481 CMD(join_mesh, JOIN_MESH);
1482 CMD(set_pmksa, SET_PMKSA);
1483 CMD(del_pmksa, DEL_PMKSA);
1484 CMD(flush_pmksa, FLUSH_PMKSA);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001485 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001486 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1487 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1488 CMD(mgmt_tx, FRAME);
1489 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001490 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001491 i++;
1492 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1493 goto nla_put_failure;
1494 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001495 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1496 rdev->ops->join_mesh) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001497 i++;
1498 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1499 goto nla_put_failure;
1500 }
1501 CMD(set_wds_peer, SET_WDS_PEER);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001502 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001503 CMD(tdls_mgmt, TDLS_MGMT);
1504 CMD(tdls_oper, TDLS_OPER);
1505 }
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001506 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 CMD(sched_scan_start, START_SCHED_SCAN);
1508 CMD(probe_client, PROBE_CLIENT);
1509 CMD(set_noack_map, SET_NOACK_MAP);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001510 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001511 i++;
1512 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1513 goto nla_put_failure;
1514 }
1515 CMD(start_p2p_device, START_P2P_DEVICE);
1516 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg02df00e2014-06-10 14:06:25 +02001517#ifdef CONFIG_NL80211_TESTMODE
1518 CMD(testmode_cmd, TESTMODE);
1519#endif
Johannes Berg86e8cf92013-06-19 10:57:22 +02001520 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001521 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1522 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001523 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001524 CMD(channel_switch, CHANNEL_SWITCH);
Johannes Berg02df00e2014-06-10 14:06:25 +02001525 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg723e73a2014-10-22 09:25:06 +02001526 if (rdev->wiphy.features &
1527 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
Johannes Berg960d01a2014-09-09 22:55:35 +03001528 CMD(add_tx_ts, ADD_TX_TS);
Arend van Spriel5de17982013-04-18 15:49:00 +02001529 }
Johannes Berg02df00e2014-06-10 14:06:25 +02001530 /* add into the if now */
Johannes Berg8fdc6212009-03-14 09:34:01 +01001531#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001532
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001533 if (rdev->ops->connect || rdev->ops->auth) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001534 i++;
1535 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001536 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001537 }
1538
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001539 if (rdev->ops->disconnect || rdev->ops->deauth) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001540 i++;
1541 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1542 goto nla_put_failure;
1543 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001544
Johannes Berg3713b4e2013-02-14 16:19:38 +01001545 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001546 state->split_start++;
1547 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001548 break;
1549 case 5:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001550 if (rdev->ops->remain_on_channel &&
1551 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001552 nla_put_u32(msg,
1553 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001554 rdev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001555 goto nla_put_failure;
1556
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001557 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001558 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1559 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001560
Johannes Berg3713b4e2013-02-14 16:19:38 +01001561 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1562 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001563 state->split_start++;
1564 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001565 break;
1566 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001567#ifdef CONFIG_PM
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001568 if (nl80211_send_wowlan(msg, rdev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001569 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001570 state->split_start++;
1571 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001572 break;
1573#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001574 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001575#endif
1576 case 7:
1577 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001578 rdev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001579 goto nla_put_failure;
1580
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001581 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001582 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001583 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001584
Johannes Berg86e8cf92013-06-19 10:57:22 +02001585 state->split_start++;
1586 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001587 break;
1588 case 8:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001589 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001590 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001591 rdev->wiphy.ap_sme_capa))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001592 goto nla_put_failure;
1593
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001594 features = rdev->wiphy.features;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001595 /*
1596 * We can only add the per-channel limit information if the
1597 * dump is split, otherwise it makes it too big. Therefore
1598 * only advertise it in that case.
1599 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001600 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001601 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1602 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001603 goto nla_put_failure;
1604
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001605 if (rdev->wiphy.ht_capa_mod_mask &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001606 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001607 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1608 rdev->wiphy.ht_capa_mod_mask))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001609 goto nla_put_failure;
1610
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001611 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1612 rdev->wiphy.max_acl_mac_addrs &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001613 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001614 rdev->wiphy.max_acl_mac_addrs))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001615 goto nla_put_failure;
1616
1617 /*
1618 * Any information below this point is only available to
1619 * applications that can deal with it being split. This
1620 * helps ensure that newly added capabilities don't break
1621 * older tools by overrunning their buffers.
1622 *
1623 * We still increment split_start so that in the split
1624 * case we'll continue with more data in the next round,
1625 * but break unconditionally so unsplit data stops here.
1626 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001627 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001628 break;
1629 case 9:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001630 if (rdev->wiphy.extended_capabilities &&
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001631 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001632 rdev->wiphy.extended_capabilities_len,
1633 rdev->wiphy.extended_capabilities) ||
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001634 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001635 rdev->wiphy.extended_capabilities_len,
1636 rdev->wiphy.extended_capabilities_mask)))
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001637 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001638
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001639 if (rdev->wiphy.vht_capa_mod_mask &&
Johannes Bergee2aca32013-02-21 17:36:01 +01001640 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001641 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1642 rdev->wiphy.vht_capa_mod_mask))
Johannes Bergee2aca32013-02-21 17:36:01 +01001643 goto nla_put_failure;
1644
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001645 state->split_start++;
1646 break;
1647 case 10:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001648 if (nl80211_send_coalesce(msg, rdev))
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001649 goto nla_put_failure;
1650
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001651 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001652 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1653 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1654 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001655
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001656 if (rdev->wiphy.max_ap_assoc_sta &&
Jouni Malinenb43504c2014-01-15 00:01:08 +02001657 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001658 rdev->wiphy.max_ap_assoc_sta))
Jouni Malinenb43504c2014-01-15 00:01:08 +02001659 goto nla_put_failure;
1660
Johannes Bergad7e7182013-11-13 13:37:47 +01001661 state->split_start++;
1662 break;
1663 case 11:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001664 if (rdev->wiphy.n_vendor_commands) {
Johannes Berg567ffc32013-12-18 14:43:31 +01001665 const struct nl80211_vendor_cmd_info *info;
1666 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001667
Johannes Berg567ffc32013-12-18 14:43:31 +01001668 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1669 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001670 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001671
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001672 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1673 info = &rdev->wiphy.vendor_commands[i].info;
Johannes Berg567ffc32013-12-18 14:43:31 +01001674 if (nla_put(msg, i + 1, sizeof(*info), info))
1675 goto nla_put_failure;
1676 }
1677 nla_nest_end(msg, nested);
1678 }
1679
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001680 if (rdev->wiphy.n_vendor_events) {
Johannes Berg567ffc32013-12-18 14:43:31 +01001681 const struct nl80211_vendor_cmd_info *info;
1682 struct nlattr *nested;
1683
1684 nested = nla_nest_start(msg,
1685 NL80211_ATTR_VENDOR_EVENTS);
1686 if (!nested)
1687 goto nla_put_failure;
1688
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001689 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1690 info = &rdev->wiphy.vendor_events[i];
Johannes Berg567ffc32013-12-18 14:43:31 +01001691 if (nla_put(msg, i + 1, sizeof(*info), info))
1692 goto nla_put_failure;
1693 }
1694 nla_nest_end(msg, nested);
1695 }
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03001696 state->split_start++;
1697 break;
1698 case 12:
1699 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1700 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1701 rdev->wiphy.max_num_csa_counters))
1702 goto nla_put_failure;
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001703
Johannes Berg3713b4e2013-02-14 16:19:38 +01001704 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001705 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001706 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001707 }
Johannes Berg3bb20552014-05-26 13:52:25 +02001708 finish:
Johannes Berg55682962007-09-20 13:09:35 -04001709 return genlmsg_end(msg, hdr);
1710
1711 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001712 genlmsg_cancel(msg, hdr);
1713 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001714}
1715
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1717 struct netlink_callback *cb,
1718 struct nl80211_dump_wiphy_state *state)
1719{
1720 struct nlattr **tb = nl80211_fam.attrbuf;
1721 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1722 tb, nl80211_fam.maxattr, nl80211_policy);
1723 /* ignore parse errors for backward compatibility */
1724 if (ret)
1725 return 0;
1726
1727 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1728 if (tb[NL80211_ATTR_WIPHY])
1729 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1730 if (tb[NL80211_ATTR_WDEV])
1731 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1732 if (tb[NL80211_ATTR_IFINDEX]) {
1733 struct net_device *netdev;
1734 struct cfg80211_registered_device *rdev;
1735 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1736
Ying Xue7f2b8562014-01-15 10:23:45 +08001737 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001738 if (!netdev)
1739 return -ENODEV;
1740 if (netdev->ieee80211_ptr) {
Zhao, Gangf26cbf42014-04-21 12:53:03 +08001741 rdev = wiphy_to_rdev(
Johannes Berg86e8cf92013-06-19 10:57:22 +02001742 netdev->ieee80211_ptr->wiphy);
1743 state->filter_wiphy = rdev->wiphy_idx;
1744 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001745 }
1746
1747 return 0;
1748}
1749
Johannes Berg55682962007-09-20 13:09:35 -04001750static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1751{
Johannes Berg645e77d2013-03-01 14:03:49 +01001752 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001753 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001754 struct cfg80211_registered_device *rdev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001755
Johannes Berg5fe231e2013-05-08 21:45:15 +02001756 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001757 if (!state) {
1758 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001759 if (!state) {
1760 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001761 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001762 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001763 state->filter_wiphy = -1;
1764 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1765 if (ret) {
1766 kfree(state);
1767 rtnl_unlock();
1768 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001769 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001770 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001771 }
1772
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001773 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1774 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001775 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001776 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001777 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001778 if (state->filter_wiphy != -1 &&
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001779 state->filter_wiphy != rdev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001780 continue;
1781 /* attempt to fit multiple wiphy data chunks into the skb */
1782 do {
Johannes Berg3bb20552014-05-26 13:52:25 +02001783 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
1784 skb,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001785 NETLINK_CB(cb->skb).portid,
1786 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001787 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001788 if (ret < 0) {
1789 /*
1790 * If sending the wiphy data didn't fit (ENOBUFS
1791 * or EMSGSIZE returned), this SKB is still
1792 * empty (so it's not too big because another
1793 * wiphy dataset is already in the skb) and
1794 * we've not tried to adjust the dump allocation
1795 * yet ... then adjust the alloc size to be
1796 * bigger, and return 1 but with the empty skb.
1797 * This results in an empty message being RX'ed
1798 * in userspace, but that is ignored.
1799 *
1800 * We can then retry with the larger buffer.
1801 */
1802 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001803 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001804 cb->min_dump_alloc < 4096) {
1805 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001806 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001807 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001808 return 1;
1809 }
1810 idx--;
1811 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001812 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001813 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001814 break;
Johannes Berg55682962007-09-20 13:09:35 -04001815 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001816 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001817
Johannes Berg86e8cf92013-06-19 10:57:22 +02001818 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001819
1820 return skb->len;
1821}
1822
Johannes Berg86e8cf92013-06-19 10:57:22 +02001823static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1824{
1825 kfree((void *)cb->args[0]);
1826 return 0;
1827}
1828
Johannes Berg55682962007-09-20 13:09:35 -04001829static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1830{
1831 struct sk_buff *msg;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08001832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001833 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001834
Johannes Berg645e77d2013-03-01 14:03:49 +01001835 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001836 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001837 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001838
Johannes Berg3bb20552014-05-26 13:52:25 +02001839 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
1840 info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001841 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001842 nlmsg_free(msg);
1843 return -ENOBUFS;
1844 }
Johannes Berg55682962007-09-20 13:09:35 -04001845
Johannes Berg134e6372009-07-10 09:51:34 +00001846 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001847}
1848
Jouni Malinen31888482008-10-30 16:59:24 +02001849static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1850 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1851 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1852 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1853 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1854 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1855};
1856
1857static int parse_txq_params(struct nlattr *tb[],
1858 struct ieee80211_txq_params *txq_params)
1859{
Johannes Berga3304b02012-03-28 11:04:24 +02001860 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001861 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1862 !tb[NL80211_TXQ_ATTR_AIFS])
1863 return -EINVAL;
1864
Johannes Berga3304b02012-03-28 11:04:24 +02001865 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001866 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1867 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1868 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1869 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1870
Johannes Berga3304b02012-03-28 11:04:24 +02001871 if (txq_params->ac >= NL80211_NUM_ACS)
1872 return -EINVAL;
1873
Jouni Malinen31888482008-10-30 16:59:24 +02001874 return 0;
1875}
1876
Johannes Bergf444de02010-05-05 15:25:02 +02001877static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1878{
1879 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001880 * You can only set the channel explicitly for WDS interfaces,
1881 * all others have their channel managed via their respective
1882 * "establish a connection" command (connect, join, ...)
1883 *
1884 * For AP/GO and mesh mode, the channel can be set with the
1885 * channel userspace API, but is only stored and passed to the
1886 * low-level driver when the AP starts or the mesh is joined.
1887 * This is for backward compatibility, userspace can also give
1888 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001889 *
1890 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001891 * whatever else is going on, so they have their own special
1892 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001893 */
1894 return !wdev ||
1895 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001896 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001897 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1898 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001899}
1900
Johannes Berg683b6d32012-11-08 21:25:48 +01001901static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1902 struct genl_info *info,
1903 struct cfg80211_chan_def *chandef)
1904{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301905 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001906
1907 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1908 return -EINVAL;
1909
1910 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1911
1912 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001913 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1914 chandef->center_freq1 = control_freq;
1915 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001916
1917 /* Primary channel not allowed */
1918 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1919 return -EINVAL;
1920
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001921 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1922 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001923
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001924 chantype = nla_get_u32(
1925 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1926
1927 switch (chantype) {
1928 case NL80211_CHAN_NO_HT:
1929 case NL80211_CHAN_HT20:
1930 case NL80211_CHAN_HT40PLUS:
1931 case NL80211_CHAN_HT40MINUS:
1932 cfg80211_chandef_create(chandef, chandef->chan,
1933 chantype);
1934 break;
1935 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001936 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001937 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001938 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1939 chandef->width =
1940 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1941 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1942 chandef->center_freq1 =
1943 nla_get_u32(
1944 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1945 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1946 chandef->center_freq2 =
1947 nla_get_u32(
1948 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1949 }
1950
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001951 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001952 return -EINVAL;
1953
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001954 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1955 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001956 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001957
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001958 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1959 chandef->width == NL80211_CHAN_WIDTH_10) &&
1960 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1961 return -EINVAL;
1962
Johannes Berg683b6d32012-11-08 21:25:48 +01001963 return 0;
1964}
1965
Johannes Bergf444de02010-05-05 15:25:02 +02001966static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
Jouni Malinene16821b2014-04-28 11:22:08 +03001967 struct net_device *dev,
Johannes Bergf444de02010-05-05 15:25:02 +02001968 struct genl_info *info)
1969{
Johannes Berg683b6d32012-11-08 21:25:48 +01001970 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001971 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001972 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
Jouni Malinene16821b2014-04-28 11:22:08 +03001973 struct wireless_dev *wdev = NULL;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001974
Jouni Malinene16821b2014-04-28 11:22:08 +03001975 if (dev)
1976 wdev = dev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001977 if (!nl80211_can_set_dev_channel(wdev))
1978 return -EOPNOTSUPP;
Jouni Malinene16821b2014-04-28 11:22:08 +03001979 if (wdev)
1980 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001981
Johannes Berg683b6d32012-11-08 21:25:48 +01001982 result = nl80211_parse_chandef(rdev, info, &chandef);
1983 if (result)
1984 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001985
Johannes Berge8c9bd52012-06-06 08:18:22 +02001986 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001987 case NL80211_IFTYPE_AP:
1988 case NL80211_IFTYPE_P2P_GO:
Ilan Peer174e0cd2014-02-23 09:13:01 +02001989 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, iftype)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001990 result = -EINVAL;
1991 break;
1992 }
Jouni Malinene16821b2014-04-28 11:22:08 +03001993 if (wdev->beacon_interval) {
1994 if (!dev || !rdev->ops->set_ap_chanwidth ||
1995 !(rdev->wiphy.features &
1996 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
1997 result = -EBUSY;
1998 break;
1999 }
2000
2001 /* Only allow dynamic channel width changes */
2002 if (chandef.chan != wdev->preset_chandef.chan) {
2003 result = -EBUSY;
2004 break;
2005 }
2006 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2007 if (result)
2008 break;
2009 }
Johannes Berg683b6d32012-11-08 21:25:48 +01002010 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02002011 result = 0;
2012 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02002013 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01002014 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02002015 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02002016 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01002017 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02002018 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02002019 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02002020 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02002021 }
Johannes Bergf444de02010-05-05 15:25:02 +02002022
2023 return result;
2024}
2025
2026static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2027{
Johannes Berg4c476992010-10-04 21:36:35 +02002028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2029 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02002030
Jouni Malinene16821b2014-04-28 11:22:08 +03002031 return __nl80211_set_channel(rdev, netdev, info);
Johannes Bergf444de02010-05-05 15:25:02 +02002032}
2033
Bill Jordane8347eb2010-10-01 13:54:28 -04002034static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2035{
Johannes Berg43b19952010-10-07 13:10:30 +02002036 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2037 struct net_device *dev = info->user_ptr[1];
2038 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02002039 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04002040
2041 if (!info->attrs[NL80211_ATTR_MAC])
2042 return -EINVAL;
2043
Johannes Berg43b19952010-10-07 13:10:30 +02002044 if (netif_running(dev))
2045 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04002046
Johannes Berg43b19952010-10-07 13:10:30 +02002047 if (!rdev->ops->set_wds_peer)
2048 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04002049
Johannes Berg43b19952010-10-07 13:10:30 +02002050 if (wdev->iftype != NL80211_IFTYPE_WDS)
2051 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04002052
2053 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03002054 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04002055}
2056
2057
Johannes Berg55682962007-09-20 13:09:35 -04002058static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2059{
2060 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02002061 struct net_device *netdev = NULL;
2062 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04002063 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02002064 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002065 u32 changed;
2066 u8 retry_short = 0, retry_long = 0;
2067 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01002068 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04002069
Johannes Berg5fe231e2013-05-08 21:45:15 +02002070 ASSERT_RTNL();
2071
Johannes Bergf444de02010-05-05 15:25:02 +02002072 /*
2073 * Try to find the wiphy and netdev. Normally this
2074 * function shouldn't need the netdev, but this is
2075 * done for backward compatibility -- previously
2076 * setting the channel was done per wiphy, but now
2077 * it is per netdev. Previous userland like hostapd
2078 * also passed a netdev to set_wiphy, so that it is
2079 * possible to let that go to the right netdev!
2080 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002081
Johannes Bergf444de02010-05-05 15:25:02 +02002082 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2083 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2084
Ying Xue7f2b8562014-01-15 10:23:45 +08002085 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002086 if (netdev && netdev->ieee80211_ptr)
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002087 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002088 else
Johannes Bergf444de02010-05-05 15:25:02 +02002089 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002090 }
2091
Johannes Bergf444de02010-05-05 15:25:02 +02002092 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002093 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2094 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002095 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002096 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002097 wdev = NULL;
2098 netdev = NULL;
2099 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002100 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002101 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002102
2103 /*
2104 * end workaround code, by now the rdev is available
2105 * and locked, and wdev may or may not be NULL.
2106 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002107
2108 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002109 result = cfg80211_dev_rename(
2110 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002111
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002112 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002113 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002114
Jouni Malinen31888482008-10-30 16:59:24 +02002115 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2116 struct ieee80211_txq_params txq_params;
2117 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2118
Ying Xue7f2b8562014-01-15 10:23:45 +08002119 if (!rdev->ops->set_txq_params)
2120 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002121
Ying Xue7f2b8562014-01-15 10:23:45 +08002122 if (!netdev)
2123 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002124
Johannes Berg133a3ff2011-11-03 14:50:13 +01002125 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002126 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2127 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002128
Ying Xue7f2b8562014-01-15 10:23:45 +08002129 if (!netif_running(netdev))
2130 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002131
Jouni Malinen31888482008-10-30 16:59:24 +02002132 nla_for_each_nested(nl_txq_params,
2133 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2134 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002135 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2136 nla_data(nl_txq_params),
2137 nla_len(nl_txq_params),
2138 txq_params_policy);
2139 if (result)
2140 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002141 result = parse_txq_params(tb, &txq_params);
2142 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002143 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002144
Hila Gonene35e4d22012-06-27 17:19:42 +03002145 result = rdev_set_txq_params(rdev, netdev,
2146 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002147 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002148 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002149 }
2150 }
2151
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002152 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinene16821b2014-04-28 11:22:08 +03002153 result = __nl80211_set_channel(
2154 rdev,
2155 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2156 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002157 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002158 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002159 }
2160
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002161 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002162 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002163 enum nl80211_tx_power_setting type;
2164 int idx, mbm = 0;
2165
Johannes Bergc8442112012-10-24 10:17:18 +02002166 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2167 txp_wdev = NULL;
2168
Ying Xue7f2b8562014-01-15 10:23:45 +08002169 if (!rdev->ops->set_tx_power)
2170 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002171
2172 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2173 type = nla_get_u32(info->attrs[idx]);
2174
2175 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002176 (type != NL80211_TX_POWER_AUTOMATIC))
2177 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002178
2179 if (type != NL80211_TX_POWER_AUTOMATIC) {
2180 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2181 mbm = nla_get_u32(info->attrs[idx]);
2182 }
2183
Johannes Bergc8442112012-10-24 10:17:18 +02002184 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002185 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002186 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002187 }
2188
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002189 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2190 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2191 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002192 if ((!rdev->wiphy.available_antennas_tx &&
2193 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002194 !rdev->ops->set_antenna)
2195 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002196
2197 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2198 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2199
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002200 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002201 * available antenna masks, except for the "all" mask */
2202 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002203 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2204 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002205
Bruno Randolf7f531e02010-12-16 11:30:22 +09002206 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2207 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002208
Hila Gonene35e4d22012-06-27 17:19:42 +03002209 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002210 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002211 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002212 }
2213
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002214 changed = 0;
2215
2216 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2217 retry_short = nla_get_u8(
2218 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002219 if (retry_short == 0)
2220 return -EINVAL;
2221
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002222 changed |= WIPHY_PARAM_RETRY_SHORT;
2223 }
2224
2225 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2226 retry_long = nla_get_u8(
2227 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002228 if (retry_long == 0)
2229 return -EINVAL;
2230
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002231 changed |= WIPHY_PARAM_RETRY_LONG;
2232 }
2233
2234 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2235 frag_threshold = nla_get_u32(
2236 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002237 if (frag_threshold < 256)
2238 return -EINVAL;
2239
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002240 if (frag_threshold != (u32) -1) {
2241 /*
2242 * Fragments (apart from the last one) are required to
2243 * have even length. Make the fragmentation code
2244 * simpler by stripping LSB should someone try to use
2245 * odd threshold value.
2246 */
2247 frag_threshold &= ~0x1;
2248 }
2249 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2250 }
2251
2252 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2253 rts_threshold = nla_get_u32(
2254 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2255 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2256 }
2257
Lukáš Turek81077e82009-12-21 22:50:47 +01002258 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +02002259 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2260 return -EINVAL;
2261
Lukáš Turek81077e82009-12-21 22:50:47 +01002262 coverage_class = nla_get_u8(
2263 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2264 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2265 }
2266
Lorenzo Bianconi3057dbf2014-09-04 23:57:40 +02002267 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2268 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2269 return -EOPNOTSUPP;
2270
2271 changed |= WIPHY_PARAM_DYN_ACK;
2272 }
2273
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002274 if (changed) {
2275 u8 old_retry_short, old_retry_long;
2276 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002277 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002278
Ying Xue7f2b8562014-01-15 10:23:45 +08002279 if (!rdev->ops->set_wiphy_params)
2280 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002281
2282 old_retry_short = rdev->wiphy.retry_short;
2283 old_retry_long = rdev->wiphy.retry_long;
2284 old_frag_threshold = rdev->wiphy.frag_threshold;
2285 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002286 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002287
2288 if (changed & WIPHY_PARAM_RETRY_SHORT)
2289 rdev->wiphy.retry_short = retry_short;
2290 if (changed & WIPHY_PARAM_RETRY_LONG)
2291 rdev->wiphy.retry_long = retry_long;
2292 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2293 rdev->wiphy.frag_threshold = frag_threshold;
2294 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2295 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002296 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2297 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002298
Hila Gonene35e4d22012-06-27 17:19:42 +03002299 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002300 if (result) {
2301 rdev->wiphy.retry_short = old_retry_short;
2302 rdev->wiphy.retry_long = old_retry_long;
2303 rdev->wiphy.frag_threshold = old_frag_threshold;
2304 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002305 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002306 }
2307 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002308 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002309}
2310
Johannes Berg71bbc992012-06-15 15:30:18 +02002311static inline u64 wdev_id(struct wireless_dev *wdev)
2312{
2313 return (u64)wdev->identifier |
Zhao, Gangf26cbf42014-04-21 12:53:03 +08002314 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
Johannes Berg71bbc992012-06-15 15:30:18 +02002315}
Johannes Berg55682962007-09-20 13:09:35 -04002316
Johannes Berg683b6d32012-11-08 21:25:48 +01002317static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002318 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002319{
Johannes Berg601555c2014-11-27 17:26:56 +01002320 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2321 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002322
Johannes Berg683b6d32012-11-08 21:25:48 +01002323 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2324 chandef->chan->center_freq))
2325 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002326 switch (chandef->width) {
2327 case NL80211_CHAN_WIDTH_20_NOHT:
2328 case NL80211_CHAN_WIDTH_20:
2329 case NL80211_CHAN_WIDTH_40:
2330 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2331 cfg80211_get_chandef_type(chandef)))
2332 return -ENOBUFS;
2333 break;
2334 default:
2335 break;
2336 }
2337 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2338 return -ENOBUFS;
2339 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2340 return -ENOBUFS;
2341 if (chandef->center_freq2 &&
2342 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002343 return -ENOBUFS;
2344 return 0;
2345}
2346
Eric W. Biederman15e47302012-09-07 20:12:54 +00002347static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002348 struct cfg80211_registered_device *rdev,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002349 struct wireless_dev *wdev, bool removal)
Johannes Berg55682962007-09-20 13:09:35 -04002350{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002351 struct net_device *dev = wdev->netdev;
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002352 u8 cmd = NL80211_CMD_NEW_INTERFACE;
Johannes Berg55682962007-09-20 13:09:35 -04002353 void *hdr;
2354
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002355 if (removal)
2356 cmd = NL80211_CMD_DEL_INTERFACE;
2357
2358 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -04002359 if (!hdr)
2360 return -1;
2361
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002362 if (dev &&
2363 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002364 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002365 goto nla_put_failure;
2366
2367 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2368 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002369 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002370 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002371 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2372 rdev->devlist_generation ^
2373 (cfg80211_rdev_list_generation << 2)))
2374 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002375
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002376 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002377 int ret;
2378 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002379
Johannes Berg683b6d32012-11-08 21:25:48 +01002380 ret = rdev_get_channel(rdev, wdev, &chandef);
2381 if (ret == 0) {
2382 if (nl80211_send_chandef(msg, &chandef))
2383 goto nla_put_failure;
2384 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002385 }
2386
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002387 if (wdev->ssid_len) {
2388 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2389 goto nla_put_failure;
2390 }
2391
Johannes Berg55682962007-09-20 13:09:35 -04002392 return genlmsg_end(msg, hdr);
2393
2394 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002395 genlmsg_cancel(msg, hdr);
2396 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002397}
2398
2399static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2400{
2401 int wp_idx = 0;
2402 int if_idx = 0;
2403 int wp_start = cb->args[0];
2404 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002405 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002406 struct wireless_dev *wdev;
2407
Johannes Berg5fe231e2013-05-08 21:45:15 +02002408 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002409 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2410 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002411 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002412 if (wp_idx < wp_start) {
2413 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002414 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002415 }
Johannes Berg55682962007-09-20 13:09:35 -04002416 if_idx = 0;
2417
Johannes Berg89a54e42012-06-15 14:33:17 +02002418 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002419 if (if_idx < if_start) {
2420 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002421 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002422 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002423 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002424 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002425 rdev, wdev, false) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002426 goto out;
2427 }
2428 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002429 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002430
2431 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002432 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002433 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002434 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002435
2436 cb->args[0] = wp_idx;
2437 cb->args[1] = if_idx;
2438
2439 return skb->len;
2440}
2441
2442static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2443{
2444 struct sk_buff *msg;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08002445 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002446 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002447
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002448 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002449 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002450 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002451
Eric W. Biederman15e47302012-09-07 20:12:54 +00002452 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002453 rdev, wdev, false) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002454 nlmsg_free(msg);
2455 return -ENOBUFS;
2456 }
Johannes Berg55682962007-09-20 13:09:35 -04002457
Johannes Berg134e6372009-07-10 09:51:34 +00002458 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002459}
2460
Michael Wu66f7ac52008-01-31 19:48:22 +01002461static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2462 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2463 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2464 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2465 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2466 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002467 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002468};
2469
2470static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2471{
2472 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2473 int flag;
2474
2475 *mntrflags = 0;
2476
2477 if (!nla)
2478 return -EINVAL;
2479
2480 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2481 nla, mntr_flags_policy))
2482 return -EINVAL;
2483
2484 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2485 if (flags[flag])
2486 *mntrflags |= (1<<flag);
2487
2488 return 0;
2489}
2490
Johannes Berg9bc383d2009-11-19 11:55:19 +01002491static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002492 struct net_device *netdev, u8 use_4addr,
2493 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002494{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002495 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002496 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002497 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002498 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002499 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002500
2501 switch (iftype) {
2502 case NL80211_IFTYPE_AP_VLAN:
2503 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2504 return 0;
2505 break;
2506 case NL80211_IFTYPE_STATION:
2507 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2508 return 0;
2509 break;
2510 default:
2511 break;
2512 }
2513
2514 return -EOPNOTSUPP;
2515}
2516
Johannes Berg55682962007-09-20 13:09:35 -04002517static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2518{
Johannes Berg4c476992010-10-04 21:36:35 +02002519 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002520 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002521 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002522 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002523 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002524 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002525 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002526
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002527 memset(&params, 0, sizeof(params));
2528
Johannes Berg04a773a2009-04-19 21:24:32 +02002529 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002530
Johannes Berg723b0382008-09-16 20:22:09 +02002531 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002532 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002533 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002534 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002535 if (ntype > NL80211_IFTYPE_MAX)
2536 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002537 }
2538
Johannes Berg92ffe052008-09-16 20:39:36 +02002539 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002540 struct wireless_dev *wdev = dev->ieee80211_ptr;
2541
Johannes Berg4c476992010-10-04 21:36:35 +02002542 if (ntype != NL80211_IFTYPE_MESH_POINT)
2543 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002544 if (netif_running(dev))
2545 return -EBUSY;
2546
2547 wdev_lock(wdev);
2548 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2549 IEEE80211_MAX_MESH_ID_LEN);
2550 wdev->mesh_id_up_len =
2551 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2552 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2553 wdev->mesh_id_up_len);
2554 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002555 }
2556
Felix Fietkau8b787642009-11-10 18:53:10 +01002557 if (info->attrs[NL80211_ATTR_4ADDR]) {
2558 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2559 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002560 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002561 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002562 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002563 } else {
2564 params.use_4addr = -1;
2565 }
2566
Johannes Berg92ffe052008-09-16 20:39:36 +02002567 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002568 if (ntype != NL80211_IFTYPE_MONITOR)
2569 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002570 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2571 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002572 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002573 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002574
2575 flags = &_flags;
2576 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002577 }
Johannes Berg3b858752009-03-12 09:55:09 +01002578
Luciano Coelho18003292013-08-29 13:26:57 +03002579 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002580 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2581 return -EOPNOTSUPP;
2582
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002583 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002584 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002585 else
2586 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002587
Johannes Berg9bc383d2009-11-19 11:55:19 +01002588 if (!err && params.use_4addr != -1)
2589 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2590
Johannes Berg55682962007-09-20 13:09:35 -04002591 return err;
2592}
2593
2594static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2595{
Johannes Berg4c476992010-10-04 21:36:35 +02002596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002597 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002598 struct wireless_dev *wdev;
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002599 struct sk_buff *msg, *event;
Johannes Berg55682962007-09-20 13:09:35 -04002600 int err;
2601 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002602 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002603
Johannes Berg78f22b62014-03-24 17:57:27 +01002604 /* to avoid failing a new interface creation due to pending removal */
2605 cfg80211_destroy_ifaces(rdev);
2606
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002607 memset(&params, 0, sizeof(params));
2608
Johannes Berg55682962007-09-20 13:09:35 -04002609 if (!info->attrs[NL80211_ATTR_IFNAME])
2610 return -EINVAL;
2611
2612 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2613 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2614 if (type > NL80211_IFTYPE_MAX)
2615 return -EINVAL;
2616 }
2617
Johannes Berg79c97e92009-07-07 03:56:12 +02002618 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002619 !(rdev->wiphy.interface_modes & (1 << type)))
2620 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002621
Ben Greeare8f479b2014-10-22 12:23:05 -07002622 if ((type == NL80211_IFTYPE_P2P_DEVICE ||
2623 rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2624 info->attrs[NL80211_ATTR_MAC]) {
Arend van Spriel1c18f142013-01-08 10:17:27 +01002625 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2626 ETH_ALEN);
2627 if (!is_valid_ether_addr(params.macaddr))
2628 return -EADDRNOTAVAIL;
2629 }
2630
Johannes Berg9bc383d2009-11-19 11:55:19 +01002631 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002632 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002633 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002634 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002635 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002636 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002637
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002638 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2639 if (!msg)
2640 return -ENOMEM;
2641
Michael Wu66f7ac52008-01-31 19:48:22 +01002642 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2643 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2644 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002645
Luciano Coelho18003292013-08-29 13:26:57 +03002646 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002647 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2648 return -EOPNOTSUPP;
2649
Hila Gonene35e4d22012-06-27 17:19:42 +03002650 wdev = rdev_add_virtual_intf(rdev,
2651 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2652 type, err ? NULL : &flags, &params);
Rafał Miłeckid687cbb2014-11-14 18:43:28 +01002653 if (WARN_ON(!wdev)) {
2654 nlmsg_free(msg);
2655 return -EPROTO;
2656 } else if (IS_ERR(wdev)) {
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002657 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002658 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002659 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002660
Jukka Rissanen18e5ca62014-11-13 17:25:14 +02002661 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
Johannes Berg78f22b62014-03-24 17:57:27 +01002662 wdev->owner_nlportid = info->snd_portid;
2663
Johannes Berg98104fde2012-06-16 00:19:54 +02002664 switch (type) {
2665 case NL80211_IFTYPE_MESH_POINT:
2666 if (!info->attrs[NL80211_ATTR_MESH_ID])
2667 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002668 wdev_lock(wdev);
2669 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2670 IEEE80211_MAX_MESH_ID_LEN);
2671 wdev->mesh_id_up_len =
2672 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2673 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2674 wdev->mesh_id_up_len);
2675 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002676 break;
2677 case NL80211_IFTYPE_P2P_DEVICE:
2678 /*
2679 * P2P Device doesn't have a netdev, so doesn't go
2680 * through the netdev notifier and must be added here
2681 */
2682 mutex_init(&wdev->mtx);
2683 INIT_LIST_HEAD(&wdev->event_list);
2684 spin_lock_init(&wdev->event_lock);
2685 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2686 spin_lock_init(&wdev->mgmt_registrations_lock);
2687
Johannes Berg98104fde2012-06-16 00:19:54 +02002688 wdev->identifier = ++rdev->wdev_id;
2689 list_add_rcu(&wdev->list, &rdev->wdev_list);
2690 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002691 break;
2692 default:
2693 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002694 }
2695
Eric W. Biederman15e47302012-09-07 20:12:54 +00002696 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002697 rdev, wdev, false) < 0) {
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002698 nlmsg_free(msg);
2699 return -ENOBUFS;
2700 }
2701
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002702 event = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2703 if (event) {
2704 if (nl80211_send_iface(event, 0, 0, 0,
2705 rdev, wdev, false) < 0) {
2706 nlmsg_free(event);
2707 goto out;
2708 }
2709
2710 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
2711 event, 0, NL80211_MCGRP_CONFIG,
2712 GFP_KERNEL);
2713 }
2714
2715out:
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002716 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002717}
2718
2719static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2720{
Johannes Berg4c476992010-10-04 21:36:35 +02002721 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002722 struct wireless_dev *wdev = info->user_ptr[1];
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002723 struct sk_buff *msg;
2724 int status;
Johannes Berg55682962007-09-20 13:09:35 -04002725
Johannes Berg4c476992010-10-04 21:36:35 +02002726 if (!rdev->ops->del_virtual_intf)
2727 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002728
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002729 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2730 if (msg && nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, true) < 0) {
2731 nlmsg_free(msg);
2732 msg = NULL;
2733 }
2734
Johannes Berg84efbb82012-06-16 00:00:26 +02002735 /*
2736 * If we remove a wireless device without a netdev then clear
2737 * user_ptr[1] so that nl80211_post_doit won't dereference it
2738 * to check if it needs to do dev_put(). Otherwise it crashes
2739 * since the wdev has been freed, unlike with a netdev where
2740 * we need the dev_put() for the netdev to really be freed.
2741 */
2742 if (!wdev->netdev)
2743 info->user_ptr[1] = NULL;
2744
Tomasz Bursztyka8f894be2014-11-12 16:26:45 +02002745 status = rdev_del_virtual_intf(rdev, wdev);
2746 if (status >= 0 && msg)
2747 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
2748 msg, 0, NL80211_MCGRP_CONFIG,
2749 GFP_KERNEL);
2750 else
2751 nlmsg_free(msg);
2752
2753 return status;
Johannes Berg55682962007-09-20 13:09:35 -04002754}
2755
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002756static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2757{
2758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2759 struct net_device *dev = info->user_ptr[1];
2760 u16 noack_map;
2761
2762 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2763 return -EINVAL;
2764
2765 if (!rdev->ops->set_noack_map)
2766 return -EOPNOTSUPP;
2767
2768 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2769
Hila Gonene35e4d22012-06-27 17:19:42 +03002770 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002771}
2772
Johannes Berg41ade002007-12-19 02:03:29 +01002773struct get_key_cookie {
2774 struct sk_buff *msg;
2775 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002776 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002777};
2778
2779static void get_key_callback(void *c, struct key_params *params)
2780{
Johannes Bergb9454e82009-07-08 13:29:08 +02002781 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002782 struct get_key_cookie *cookie = c;
2783
David S. Miller9360ffd2012-03-29 04:41:26 -04002784 if ((params->key &&
2785 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2786 params->key_len, params->key)) ||
2787 (params->seq &&
2788 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2789 params->seq_len, params->seq)) ||
2790 (params->cipher &&
2791 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2792 params->cipher)))
2793 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002794
Johannes Bergb9454e82009-07-08 13:29:08 +02002795 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2796 if (!key)
2797 goto nla_put_failure;
2798
David S. Miller9360ffd2012-03-29 04:41:26 -04002799 if ((params->key &&
2800 nla_put(cookie->msg, NL80211_KEY_DATA,
2801 params->key_len, params->key)) ||
2802 (params->seq &&
2803 nla_put(cookie->msg, NL80211_KEY_SEQ,
2804 params->seq_len, params->seq)) ||
2805 (params->cipher &&
2806 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2807 params->cipher)))
2808 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002809
David S. Miller9360ffd2012-03-29 04:41:26 -04002810 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2811 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002812
2813 nla_nest_end(cookie->msg, key);
2814
Johannes Berg41ade002007-12-19 02:03:29 +01002815 return;
2816 nla_put_failure:
2817 cookie->error = 1;
2818}
2819
2820static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2821{
Johannes Berg4c476992010-10-04 21:36:35 +02002822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002823 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002824 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002825 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002826 const u8 *mac_addr = NULL;
2827 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002828 struct get_key_cookie cookie = {
2829 .error = 0,
2830 };
2831 void *hdr;
2832 struct sk_buff *msg;
2833
2834 if (info->attrs[NL80211_ATTR_KEY_IDX])
2835 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2836
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002837 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002838 return -EINVAL;
2839
2840 if (info->attrs[NL80211_ATTR_MAC])
2841 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2842
Johannes Berge31b8212010-10-05 19:39:30 +02002843 pairwise = !!mac_addr;
2844 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2845 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2846 if (kt >= NUM_NL80211_KEYTYPES)
2847 return -EINVAL;
2848 if (kt != NL80211_KEYTYPE_GROUP &&
2849 kt != NL80211_KEYTYPE_PAIRWISE)
2850 return -EINVAL;
2851 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2852 }
2853
Johannes Berg4c476992010-10-04 21:36:35 +02002854 if (!rdev->ops->get_key)
2855 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002856
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002857 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002858 if (!msg)
2859 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002860
Eric W. Biederman15e47302012-09-07 20:12:54 +00002861 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002862 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002863 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002864 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002865
2866 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002867 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002868
David S. Miller9360ffd2012-03-29 04:41:26 -04002869 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2870 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2871 goto nla_put_failure;
2872 if (mac_addr &&
2873 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2874 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002875
Johannes Berge31b8212010-10-05 19:39:30 +02002876 if (pairwise && mac_addr &&
2877 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2878 return -ENOENT;
2879
Hila Gonene35e4d22012-06-27 17:19:42 +03002880 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2881 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002882
2883 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002884 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002885
2886 if (cookie.error)
2887 goto nla_put_failure;
2888
2889 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002890 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002891
2892 nla_put_failure:
2893 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002894 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002895 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002896 return err;
2897}
2898
2899static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2900{
Johannes Berg4c476992010-10-04 21:36:35 +02002901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002902 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002903 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002904 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002905
Johannes Bergb9454e82009-07-08 13:29:08 +02002906 err = nl80211_parse_key(info, &key);
2907 if (err)
2908 return err;
2909
2910 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002911 return -EINVAL;
2912
Johannes Bergb9454e82009-07-08 13:29:08 +02002913 /* only support setting default key */
2914 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002915 return -EINVAL;
2916
Johannes Bergfffd0932009-07-08 14:22:54 +02002917 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002918
2919 if (key.def) {
2920 if (!rdev->ops->set_default_key) {
2921 err = -EOPNOTSUPP;
2922 goto out;
2923 }
2924
2925 err = nl80211_key_allowed(dev->ieee80211_ptr);
2926 if (err)
2927 goto out;
2928
Hila Gonene35e4d22012-06-27 17:19:42 +03002929 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002930 key.def_uni, key.def_multi);
2931
2932 if (err)
2933 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002934
Johannes Berg3d23e342009-09-29 23:27:28 +02002935#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002936 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002937#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002938 } else {
2939 if (key.def_uni || !key.def_multi) {
2940 err = -EINVAL;
2941 goto out;
2942 }
2943
2944 if (!rdev->ops->set_default_mgmt_key) {
2945 err = -EOPNOTSUPP;
2946 goto out;
2947 }
2948
2949 err = nl80211_key_allowed(dev->ieee80211_ptr);
2950 if (err)
2951 goto out;
2952
Hila Gonene35e4d22012-06-27 17:19:42 +03002953 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002954 if (err)
2955 goto out;
2956
2957#ifdef CONFIG_CFG80211_WEXT
2958 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2959#endif
2960 }
2961
2962 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002963 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002964
Johannes Berg41ade002007-12-19 02:03:29 +01002965 return err;
2966}
2967
2968static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2969{
Johannes Berg4c476992010-10-04 21:36:35 +02002970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002971 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002972 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002973 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002974 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002975
Johannes Bergb9454e82009-07-08 13:29:08 +02002976 err = nl80211_parse_key(info, &key);
2977 if (err)
2978 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002979
Johannes Bergb9454e82009-07-08 13:29:08 +02002980 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002981 return -EINVAL;
2982
Johannes Berg41ade002007-12-19 02:03:29 +01002983 if (info->attrs[NL80211_ATTR_MAC])
2984 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2985
Johannes Berge31b8212010-10-05 19:39:30 +02002986 if (key.type == -1) {
2987 if (mac_addr)
2988 key.type = NL80211_KEYTYPE_PAIRWISE;
2989 else
2990 key.type = NL80211_KEYTYPE_GROUP;
2991 }
2992
2993 /* for now */
2994 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2995 key.type != NL80211_KEYTYPE_GROUP)
2996 return -EINVAL;
2997
Johannes Berg4c476992010-10-04 21:36:35 +02002998 if (!rdev->ops->add_key)
2999 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003000
Johannes Berge31b8212010-10-05 19:39:30 +02003001 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3002 key.type == NL80211_KEYTYPE_PAIRWISE,
3003 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02003004 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003005
3006 wdev_lock(dev->ieee80211_ptr);
3007 err = nl80211_key_allowed(dev->ieee80211_ptr);
3008 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03003009 err = rdev_add_key(rdev, dev, key.idx,
3010 key.type == NL80211_KEYTYPE_PAIRWISE,
3011 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02003012 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01003013
Johannes Berg41ade002007-12-19 02:03:29 +01003014 return err;
3015}
3016
3017static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3018{
Johannes Berg4c476992010-10-04 21:36:35 +02003019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01003020 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003021 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01003022 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02003023 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01003024
Johannes Bergb9454e82009-07-08 13:29:08 +02003025 err = nl80211_parse_key(info, &key);
3026 if (err)
3027 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01003028
3029 if (info->attrs[NL80211_ATTR_MAC])
3030 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3031
Johannes Berge31b8212010-10-05 19:39:30 +02003032 if (key.type == -1) {
3033 if (mac_addr)
3034 key.type = NL80211_KEYTYPE_PAIRWISE;
3035 else
3036 key.type = NL80211_KEYTYPE_GROUP;
3037 }
3038
3039 /* for now */
3040 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3041 key.type != NL80211_KEYTYPE_GROUP)
3042 return -EINVAL;
3043
Johannes Berg4c476992010-10-04 21:36:35 +02003044 if (!rdev->ops->del_key)
3045 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01003046
Johannes Bergfffd0932009-07-08 14:22:54 +02003047 wdev_lock(dev->ieee80211_ptr);
3048 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02003049
3050 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
3051 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3052 err = -ENOENT;
3053
Johannes Bergfffd0932009-07-08 14:22:54 +02003054 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03003055 err = rdev_del_key(rdev, dev, key.idx,
3056 key.type == NL80211_KEYTYPE_PAIRWISE,
3057 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01003058
Johannes Berg3d23e342009-09-29 23:27:28 +02003059#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02003060 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02003061 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02003062 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02003063 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02003064 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3065 }
3066#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02003067 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02003068
Johannes Berg41ade002007-12-19 02:03:29 +01003069 return err;
3070}
3071
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303072/* This function returns an error or the number of nested attributes */
3073static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3074{
3075 struct nlattr *attr;
3076 int n_entries = 0, tmp;
3077
3078 nla_for_each_nested(attr, nl_attr, tmp) {
3079 if (nla_len(attr) != ETH_ALEN)
3080 return -EINVAL;
3081
3082 n_entries++;
3083 }
3084
3085 return n_entries;
3086}
3087
3088/*
3089 * This function parses ACL information and allocates memory for ACL data.
3090 * On successful return, the calling function is responsible to free the
3091 * ACL buffer returned by this function.
3092 */
3093static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3094 struct genl_info *info)
3095{
3096 enum nl80211_acl_policy acl_policy;
3097 struct nlattr *attr;
3098 struct cfg80211_acl_data *acl;
3099 int i = 0, n_entries, tmp;
3100
3101 if (!wiphy->max_acl_mac_addrs)
3102 return ERR_PTR(-EOPNOTSUPP);
3103
3104 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3105 return ERR_PTR(-EINVAL);
3106
3107 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3108 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3109 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3110 return ERR_PTR(-EINVAL);
3111
3112 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3113 return ERR_PTR(-EINVAL);
3114
3115 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3116 if (n_entries < 0)
3117 return ERR_PTR(n_entries);
3118
3119 if (n_entries > wiphy->max_acl_mac_addrs)
3120 return ERR_PTR(-ENOTSUPP);
3121
3122 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3123 GFP_KERNEL);
3124 if (!acl)
3125 return ERR_PTR(-ENOMEM);
3126
3127 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3128 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3129 i++;
3130 }
3131
3132 acl->n_acl_entries = n_entries;
3133 acl->acl_policy = acl_policy;
3134
3135 return acl;
3136}
3137
3138static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3139{
3140 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3141 struct net_device *dev = info->user_ptr[1];
3142 struct cfg80211_acl_data *acl;
3143 int err;
3144
3145 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3146 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3147 return -EOPNOTSUPP;
3148
3149 if (!dev->ieee80211_ptr->beacon_interval)
3150 return -EINVAL;
3151
3152 acl = parse_acl_data(&rdev->wiphy, info);
3153 if (IS_ERR(acl))
3154 return PTR_ERR(acl);
3155
3156 err = rdev_set_mac_acl(rdev, dev, acl);
3157
3158 kfree(acl);
3159
3160 return err;
3161}
3162
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003163static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003164 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003165{
Johannes Berg88600202012-02-13 15:17:18 +01003166 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003167
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003168 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3169 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3170 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3171 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003172 return -EINVAL;
3173
Johannes Berg88600202012-02-13 15:17:18 +01003174 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003175
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003176 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3177 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3178 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003179 if (!bcn->head_len)
3180 return -EINVAL;
3181 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003182 }
3183
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003184 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3185 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3186 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003187 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003188 }
3189
Johannes Berg4c476992010-10-04 21:36:35 +02003190 if (!haveinfo)
3191 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003192
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003193 if (attrs[NL80211_ATTR_IE]) {
3194 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3195 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003196 }
3197
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003198 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003199 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003200 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003201 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003202 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003203 }
3204
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003205 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003206 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003207 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003208 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003209 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003210 }
3211
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003212 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3213 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3214 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003215 }
3216
Johannes Berg88600202012-02-13 15:17:18 +01003217 return 0;
3218}
3219
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003220static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3221 struct cfg80211_ap_settings *params)
3222{
3223 struct wireless_dev *wdev;
3224 bool ret = false;
3225
Johannes Berg89a54e42012-06-15 14:33:17 +02003226 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003227 if (wdev->iftype != NL80211_IFTYPE_AP &&
3228 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3229 continue;
3230
Johannes Berg683b6d32012-11-08 21:25:48 +01003231 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003232 continue;
3233
Johannes Berg683b6d32012-11-08 21:25:48 +01003234 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003235 ret = true;
3236 break;
3237 }
3238
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003239 return ret;
3240}
3241
Jouni Malinene39e5b52012-09-30 19:29:39 +03003242static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3243 enum nl80211_auth_type auth_type,
3244 enum nl80211_commands cmd)
3245{
3246 if (auth_type > NL80211_AUTHTYPE_MAX)
3247 return false;
3248
3249 switch (cmd) {
3250 case NL80211_CMD_AUTHENTICATE:
3251 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3252 auth_type == NL80211_AUTHTYPE_SAE)
3253 return false;
3254 return true;
3255 case NL80211_CMD_CONNECT:
3256 case NL80211_CMD_START_AP:
3257 /* SAE not supported yet */
3258 if (auth_type == NL80211_AUTHTYPE_SAE)
3259 return false;
3260 return true;
3261 default:
3262 return false;
3263 }
3264}
3265
Johannes Berg88600202012-02-13 15:17:18 +01003266static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3267{
3268 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3269 struct net_device *dev = info->user_ptr[1];
3270 struct wireless_dev *wdev = dev->ieee80211_ptr;
3271 struct cfg80211_ap_settings params;
3272 int err;
3273
3274 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3275 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3276 return -EOPNOTSUPP;
3277
3278 if (!rdev->ops->start_ap)
3279 return -EOPNOTSUPP;
3280
3281 if (wdev->beacon_interval)
3282 return -EALREADY;
3283
3284 memset(&params, 0, sizeof(params));
3285
3286 /* these are required for START_AP */
3287 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3288 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3289 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3290 return -EINVAL;
3291
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003292 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003293 if (err)
3294 return err;
3295
3296 params.beacon_interval =
3297 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3298 params.dtim_period =
3299 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3300
3301 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3302 if (err)
3303 return err;
3304
3305 /*
3306 * In theory, some of these attributes should be required here
3307 * but since they were not used when the command was originally
3308 * added, keep them optional for old user space programs to let
3309 * them continue to work with drivers that do not need the
3310 * additional information -- drivers must check!
3311 */
3312 if (info->attrs[NL80211_ATTR_SSID]) {
3313 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3314 params.ssid_len =
3315 nla_len(info->attrs[NL80211_ATTR_SSID]);
3316 if (params.ssid_len == 0 ||
3317 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3318 return -EINVAL;
3319 }
3320
3321 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3322 params.hidden_ssid = nla_get_u32(
3323 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3324 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3325 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3326 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3327 return -EINVAL;
3328 }
3329
3330 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3331
3332 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3333 params.auth_type = nla_get_u32(
3334 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003335 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3336 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003337 return -EINVAL;
3338 } else
3339 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3340
3341 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3342 NL80211_MAX_NR_CIPHER_SUITES);
3343 if (err)
3344 return err;
3345
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303346 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3347 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3348 return -EOPNOTSUPP;
3349 params.inactivity_timeout = nla_get_u16(
3350 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3351 }
3352
Johannes Berg53cabad2012-11-14 15:17:28 +01003353 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3354 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3355 return -EINVAL;
3356 params.p2p_ctwindow =
3357 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3358 if (params.p2p_ctwindow > 127)
3359 return -EINVAL;
3360 if (params.p2p_ctwindow != 0 &&
3361 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3362 return -EINVAL;
3363 }
3364
3365 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3366 u8 tmp;
3367
3368 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3369 return -EINVAL;
3370 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3371 if (tmp > 1)
3372 return -EINVAL;
3373 params.p2p_opp_ps = tmp;
3374 if (params.p2p_opp_ps != 0 &&
3375 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3376 return -EINVAL;
3377 }
3378
Johannes Bergaa430da2012-05-16 23:50:18 +02003379 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003380 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3381 if (err)
3382 return err;
3383 } else if (wdev->preset_chandef.chan) {
3384 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003385 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003386 return -EINVAL;
3387
Ilan Peer174e0cd2014-02-23 09:13:01 +02003388 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
3389 wdev->iftype))
Johannes Bergaa430da2012-05-16 23:50:18 +02003390 return -EINVAL;
3391
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303392 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3393 params.acl = parse_acl_data(&rdev->wiphy, info);
3394 if (IS_ERR(params.acl))
3395 return PTR_ERR(params.acl);
3396 }
3397
Eliad Peller18998c32014-09-10 14:07:34 +03003398 if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
3399 params.smps_mode =
3400 nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
3401 switch (params.smps_mode) {
3402 case NL80211_SMPS_OFF:
3403 break;
3404 case NL80211_SMPS_STATIC:
3405 if (!(rdev->wiphy.features &
3406 NL80211_FEATURE_STATIC_SMPS))
3407 return -EINVAL;
3408 break;
3409 case NL80211_SMPS_DYNAMIC:
3410 if (!(rdev->wiphy.features &
3411 NL80211_FEATURE_DYNAMIC_SMPS))
3412 return -EINVAL;
3413 break;
3414 default:
3415 return -EINVAL;
3416 }
3417 } else {
3418 params.smps_mode = NL80211_SMPS_OFF;
3419 }
3420
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003421 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003422 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003423 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003424 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003425 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003426 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003427 wdev->ssid_len = params.ssid_len;
3428 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003429 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003430 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303431
3432 kfree(params.acl);
3433
Johannes Berg56d18932011-05-09 18:41:15 +02003434 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003435}
3436
Johannes Berg88600202012-02-13 15:17:18 +01003437static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3438{
3439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3440 struct net_device *dev = info->user_ptr[1];
3441 struct wireless_dev *wdev = dev->ieee80211_ptr;
3442 struct cfg80211_beacon_data params;
3443 int err;
3444
3445 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3446 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3447 return -EOPNOTSUPP;
3448
3449 if (!rdev->ops->change_beacon)
3450 return -EOPNOTSUPP;
3451
3452 if (!wdev->beacon_interval)
3453 return -EINVAL;
3454
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003455 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003456 if (err)
3457 return err;
3458
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003459 wdev_lock(wdev);
3460 err = rdev_change_beacon(rdev, dev, &params);
3461 wdev_unlock(wdev);
3462
3463 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003464}
3465
3466static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003467{
Johannes Berg4c476992010-10-04 21:36:35 +02003468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3469 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003470
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003471 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003472}
3473
Johannes Berg5727ef12007-12-19 02:03:34 +01003474static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3475 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3476 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3477 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003478 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003479 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003480 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003481};
3482
Johannes Bergeccb8e82009-05-11 21:57:56 +03003483static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003484 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003485 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003486{
3487 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003488 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003489 int flag;
3490
Johannes Bergeccb8e82009-05-11 21:57:56 +03003491 /*
3492 * Try parsing the new attribute first so userspace
3493 * can specify both for older kernels.
3494 */
3495 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3496 if (nla) {
3497 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003498
Johannes Bergeccb8e82009-05-11 21:57:56 +03003499 sta_flags = nla_data(nla);
3500 params->sta_flags_mask = sta_flags->mask;
3501 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003502 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003503 if ((params->sta_flags_mask |
3504 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3505 return -EINVAL;
3506 return 0;
3507 }
3508
3509 /* if present, parse the old attribute */
3510
3511 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003512 if (!nla)
3513 return 0;
3514
3515 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3516 nla, sta_flags_policy))
3517 return -EINVAL;
3518
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003519 /*
3520 * Only allow certain flags for interface types so that
3521 * other attributes are silently ignored. Remember that
3522 * this is backward compatibility code with old userspace
3523 * and shouldn't be hit in other cases anyway.
3524 */
3525 switch (iftype) {
3526 case NL80211_IFTYPE_AP:
3527 case NL80211_IFTYPE_AP_VLAN:
3528 case NL80211_IFTYPE_P2P_GO:
3529 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3530 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3531 BIT(NL80211_STA_FLAG_WME) |
3532 BIT(NL80211_STA_FLAG_MFP);
3533 break;
3534 case NL80211_IFTYPE_P2P_CLIENT:
3535 case NL80211_IFTYPE_STATION:
3536 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3537 BIT(NL80211_STA_FLAG_TDLS_PEER);
3538 break;
3539 case NL80211_IFTYPE_MESH_POINT:
3540 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3541 BIT(NL80211_STA_FLAG_MFP) |
3542 BIT(NL80211_STA_FLAG_AUTHORIZED);
3543 default:
3544 return -EINVAL;
3545 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003546
Johannes Berg3383b5a2012-05-10 20:14:43 +02003547 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3548 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003549 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003550
Johannes Berg3383b5a2012-05-10 20:14:43 +02003551 /* no longer support new API additions in old API */
3552 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3553 return -EINVAL;
3554 }
3555 }
3556
Johannes Berg5727ef12007-12-19 02:03:34 +01003557 return 0;
3558}
3559
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003560static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3561 int attr)
3562{
3563 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003564 u32 bitrate;
3565 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003566
3567 rate = nla_nest_start(msg, attr);
3568 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003569 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003570
3571 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3572 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003573 /* report 16-bit bitrate only if we can */
3574 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003575 if (bitrate > 0 &&
3576 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3577 return false;
3578 if (bitrate_compat > 0 &&
3579 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3580 return false;
3581
3582 if (info->flags & RATE_INFO_FLAGS_MCS) {
3583 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3584 return false;
3585 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3586 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3587 return false;
3588 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3589 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3590 return false;
3591 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3592 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3593 return false;
3594 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3595 return false;
3596 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3597 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3598 return false;
3599 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3600 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3601 return false;
3602 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3603 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3604 return false;
3605 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3606 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3607 return false;
3608 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3609 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3610 return false;
3611 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003612
3613 nla_nest_end(msg, rate);
3614 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003615}
3616
Felix Fietkau119363c2013-04-22 16:29:30 +02003617static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3618 int id)
3619{
3620 void *attr;
3621 int i = 0;
3622
3623 if (!mask)
3624 return true;
3625
3626 attr = nla_nest_start(msg, id);
3627 if (!attr)
3628 return false;
3629
3630 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3631 if (!(mask & BIT(i)))
3632 continue;
3633
3634 if (nla_put_u8(msg, i, signal[i]))
3635 return false;
3636 }
3637
3638 nla_nest_end(msg, attr);
3639
3640 return true;
3641}
3642
Eric W. Biederman15e47302012-09-07 20:12:54 +00003643static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003644 int flags,
3645 struct cfg80211_registered_device *rdev,
3646 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003647 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003648{
3649 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003650 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003651
Eric W. Biederman15e47302012-09-07 20:12:54 +00003652 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003653 if (!hdr)
3654 return -1;
3655
David S. Miller9360ffd2012-03-29 04:41:26 -04003656 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3657 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3658 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3659 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003660
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003661 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3662 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003663 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003664 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3665 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3666 sinfo->connected_time))
3667 goto nla_put_failure;
3668 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3669 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3670 sinfo->inactive_time))
3671 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003672 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3673 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003674 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003675 (u32)sinfo->rx_bytes))
3676 goto nla_put_failure;
3677 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003678 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003679 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3680 (u32)sinfo->tx_bytes))
3681 goto nla_put_failure;
3682 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3683 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003684 sinfo->rx_bytes))
3685 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003686 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3687 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003688 sinfo->tx_bytes))
3689 goto nla_put_failure;
3690 if ((sinfo->filled & STATION_INFO_LLID) &&
3691 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3692 goto nla_put_failure;
3693 if ((sinfo->filled & STATION_INFO_PLID) &&
3694 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3695 goto nla_put_failure;
3696 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3697 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3698 sinfo->plink_state))
3699 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003700 switch (rdev->wiphy.signal_type) {
3701 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003702 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3703 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3704 sinfo->signal))
3705 goto nla_put_failure;
3706 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3707 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3708 sinfo->signal_avg))
3709 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003710 break;
3711 default:
3712 break;
3713 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003714 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3715 if (!nl80211_put_signal(msg, sinfo->chains,
3716 sinfo->chain_signal,
3717 NL80211_STA_INFO_CHAIN_SIGNAL))
3718 goto nla_put_failure;
3719 }
3720 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3721 if (!nl80211_put_signal(msg, sinfo->chains,
3722 sinfo->chain_signal_avg,
3723 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3724 goto nla_put_failure;
3725 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003726 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003727 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3728 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003729 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003730 }
3731 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3732 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3733 NL80211_STA_INFO_RX_BITRATE))
3734 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003735 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003736 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3737 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3738 sinfo->rx_packets))
3739 goto nla_put_failure;
3740 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3741 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3742 sinfo->tx_packets))
3743 goto nla_put_failure;
3744 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3745 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3746 sinfo->tx_retries))
3747 goto nla_put_failure;
3748 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3749 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3750 sinfo->tx_failed))
3751 goto nla_put_failure;
Antonio Quartulli867d8492014-05-19 21:53:19 +02003752 if ((sinfo->filled & STATION_INFO_EXPECTED_THROUGHPUT) &&
3753 nla_put_u32(msg, NL80211_STA_INFO_EXPECTED_THROUGHPUT,
3754 sinfo->expected_throughput))
3755 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003756 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3757 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3758 sinfo->beacon_loss_count))
3759 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003760 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3761 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3762 sinfo->local_pm))
3763 goto nla_put_failure;
3764 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3765 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3766 sinfo->peer_pm))
3767 goto nla_put_failure;
3768 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3769 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3770 sinfo->nonpeer_pm))
3771 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003772 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3773 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3774 if (!bss_param)
3775 goto nla_put_failure;
3776
David S. Miller9360ffd2012-03-29 04:41:26 -04003777 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3778 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3779 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3780 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3781 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3782 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3783 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3784 sinfo->bss_param.dtim_period) ||
3785 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3786 sinfo->bss_param.beacon_interval))
3787 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003788
3789 nla_nest_end(msg, bss_param);
3790 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003791 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3792 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3793 sizeof(struct nl80211_sta_flag_update),
3794 &sinfo->sta_flags))
3795 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003796 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3797 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3798 sinfo->t_offset))
3799 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003800 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003801
David S. Miller9360ffd2012-03-29 04:41:26 -04003802 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3803 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3804 sinfo->assoc_req_ies))
3805 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003806
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003807 return genlmsg_end(msg, hdr);
3808
3809 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003810 genlmsg_cancel(msg, hdr);
3811 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003812}
3813
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003814static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003815 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003816{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003817 struct station_info sinfo;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003818 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02003819 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003820 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003821 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003822 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003823
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003824 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003825 if (err)
3826 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003827
Johannes Berg97990a02013-04-19 01:02:55 +02003828 if (!wdev->netdev) {
3829 err = -EINVAL;
3830 goto out_err;
3831 }
3832
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003833 if (!rdev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003834 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003835 goto out_err;
3836 }
3837
Johannes Bergbba95fe2008-07-29 13:22:51 +02003838 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003839 memset(&sinfo, 0, sizeof(sinfo));
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003840 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003841 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003842 if (err == -ENOENT)
3843 break;
3844 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003845 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003846
3847 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003848 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003849 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003850 rdev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003851 &sinfo) < 0)
3852 goto out;
3853
3854 sta_idx++;
3855 }
3856
3857
3858 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003859 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003860 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003861 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08003862 nl80211_finish_wdev_dump(rdev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003863
3864 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003865}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003866
Johannes Berg5727ef12007-12-19 02:03:34 +01003867static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3868{
Johannes Berg4c476992010-10-04 21:36:35 +02003869 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3870 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003871 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003872 struct sk_buff *msg;
3873 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003874 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003875
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003876 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003877
3878 if (!info->attrs[NL80211_ATTR_MAC])
3879 return -EINVAL;
3880
3881 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3882
Johannes Berg4c476992010-10-04 21:36:35 +02003883 if (!rdev->ops->get_station)
3884 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003885
Hila Gonene35e4d22012-06-27 17:19:42 +03003886 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003887 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003888 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003889
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003890 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003891 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003892 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003893
Eric W. Biederman15e47302012-09-07 20:12:54 +00003894 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003895 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003896 nlmsg_free(msg);
3897 return -ENOBUFS;
3898 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003899
Johannes Berg4c476992010-10-04 21:36:35 +02003900 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003901}
3902
Johannes Berg77ee7c82013-02-15 00:48:33 +01003903int cfg80211_check_station_change(struct wiphy *wiphy,
3904 struct station_parameters *params,
3905 enum cfg80211_station_type statype)
3906{
3907 if (params->listen_interval != -1)
3908 return -EINVAL;
Arik Nemtsovc72e1142014-07-17 17:14:29 +03003909 if (params->aid &&
3910 !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
Johannes Berg77ee7c82013-02-15 00:48:33 +01003911 return -EINVAL;
3912
3913 /* When you run into this, adjust the code below for the new flag */
3914 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3915
3916 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003917 case CFG80211_STA_MESH_PEER_KERNEL:
3918 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003919 /*
3920 * No ignoring the TDLS flag here -- the userspace mesh
3921 * code doesn't have the bug of including TDLS in the
3922 * mask everywhere.
3923 */
3924 if (params->sta_flags_mask &
3925 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3926 BIT(NL80211_STA_FLAG_MFP) |
3927 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3928 return -EINVAL;
3929 break;
3930 case CFG80211_STA_TDLS_PEER_SETUP:
3931 case CFG80211_STA_TDLS_PEER_ACTIVE:
3932 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3933 return -EINVAL;
3934 /* ignore since it can't change */
3935 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3936 break;
3937 default:
3938 /* disallow mesh-specific things */
3939 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3940 return -EINVAL;
3941 if (params->local_pm)
3942 return -EINVAL;
3943 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3944 return -EINVAL;
3945 }
3946
3947 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3948 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3949 /* TDLS can't be set, ... */
3950 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3951 return -EINVAL;
3952 /*
3953 * ... but don't bother the driver with it. This works around
3954 * a hostapd/wpa_supplicant issue -- it always includes the
3955 * TLDS_PEER flag in the mask even for AP mode.
3956 */
3957 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3958 }
3959
3960 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3961 /* reject other things that can't change */
3962 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3963 return -EINVAL;
3964 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3965 return -EINVAL;
3966 if (params->supported_rates)
3967 return -EINVAL;
3968 if (params->ext_capab || params->ht_capa || params->vht_capa)
3969 return -EINVAL;
3970 }
3971
3972 if (statype != CFG80211_STA_AP_CLIENT) {
3973 if (params->vlan)
3974 return -EINVAL;
3975 }
3976
3977 switch (statype) {
3978 case CFG80211_STA_AP_MLME_CLIENT:
3979 /* Use this only for authorizing/unauthorizing a station */
3980 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3981 return -EOPNOTSUPP;
3982 break;
3983 case CFG80211_STA_AP_CLIENT:
3984 /* accept only the listed bits */
3985 if (params->sta_flags_mask &
3986 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3987 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3988 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3989 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3990 BIT(NL80211_STA_FLAG_WME) |
3991 BIT(NL80211_STA_FLAG_MFP)))
3992 return -EINVAL;
3993
3994 /* but authenticated/associated only if driver handles it */
3995 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3996 params->sta_flags_mask &
3997 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3998 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3999 return -EINVAL;
4000 break;
4001 case CFG80211_STA_IBSS:
4002 case CFG80211_STA_AP_STA:
4003 /* reject any changes other than AUTHORIZED */
4004 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4005 return -EINVAL;
4006 break;
4007 case CFG80211_STA_TDLS_PEER_SETUP:
4008 /* reject any changes other than AUTHORIZED or WME */
4009 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4010 BIT(NL80211_STA_FLAG_WME)))
4011 return -EINVAL;
4012 /* force (at least) rates when authorizing */
4013 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4014 !params->supported_rates)
4015 return -EINVAL;
4016 break;
4017 case CFG80211_STA_TDLS_PEER_ACTIVE:
4018 /* reject any changes */
4019 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08004020 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004021 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4022 return -EINVAL;
4023 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08004024 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004025 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4026 return -EINVAL;
4027 break;
4028 }
4029
4030 return 0;
4031}
4032EXPORT_SYMBOL(cfg80211_check_station_change);
4033
Johannes Berg5727ef12007-12-19 02:03:34 +01004034/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01004035 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01004036 */
Johannes Berg80b99892011-11-18 16:23:01 +01004037static struct net_device *get_vlan(struct genl_info *info,
4038 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01004039{
Johannes Berg463d0182009-07-14 00:33:35 +02004040 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01004041 struct net_device *v;
4042 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01004043
Johannes Berg80b99892011-11-18 16:23:01 +01004044 if (!vlanattr)
4045 return NULL;
4046
4047 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4048 if (!v)
4049 return ERR_PTR(-ENODEV);
4050
4051 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4052 ret = -EINVAL;
4053 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01004054 }
Johannes Berg80b99892011-11-18 16:23:01 +01004055
Johannes Berg77ee7c82013-02-15 00:48:33 +01004056 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4057 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4058 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4059 ret = -EINVAL;
4060 goto error;
4061 }
4062
Johannes Berg80b99892011-11-18 16:23:01 +01004063 if (!netif_running(v)) {
4064 ret = -ENETDOWN;
4065 goto error;
4066 }
4067
4068 return v;
4069 error:
4070 dev_put(v);
4071 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01004072}
4073
Johannes Berg94e860f2014-01-20 23:58:15 +01004074static const struct nla_policy
4075nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02004076 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4077 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4078};
4079
Johannes Bergff276692013-02-15 00:09:01 +01004080static int nl80211_parse_sta_wme(struct genl_info *info,
4081 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02004082{
Jouni Malinendf881292013-02-14 21:10:54 +02004083 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4084 struct nlattr *nla;
4085 int err;
4086
Jouni Malinendf881292013-02-14 21:10:54 +02004087 /* parse WME attributes if present */
4088 if (!info->attrs[NL80211_ATTR_STA_WME])
4089 return 0;
4090
4091 nla = info->attrs[NL80211_ATTR_STA_WME];
4092 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4093 nl80211_sta_wme_policy);
4094 if (err)
4095 return err;
4096
4097 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4098 params->uapsd_queues = nla_get_u8(
4099 tb[NL80211_STA_WME_UAPSD_QUEUES]);
4100 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4101 return -EINVAL;
4102
4103 if (tb[NL80211_STA_WME_MAX_SP])
4104 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4105
4106 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4107 return -EINVAL;
4108
4109 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4110
4111 return 0;
4112}
4113
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304114static int nl80211_parse_sta_channel_info(struct genl_info *info,
4115 struct station_parameters *params)
4116{
4117 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4118 params->supported_channels =
4119 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4120 params->supported_channels_len =
4121 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4122 /*
4123 * Need to include at least one (first channel, number of
4124 * channels) tuple for each subband, and must have proper
4125 * tuples for the rest of the data as well.
4126 */
4127 if (params->supported_channels_len < 2)
4128 return -EINVAL;
4129 if (params->supported_channels_len % 2)
4130 return -EINVAL;
4131 }
4132
4133 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4134 params->supported_oper_classes =
4135 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4136 params->supported_oper_classes_len =
4137 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4138 /*
4139 * The value of the Length field of the Supported Operating
4140 * Classes element is between 2 and 253.
4141 */
4142 if (params->supported_oper_classes_len < 2 ||
4143 params->supported_oper_classes_len > 253)
4144 return -EINVAL;
4145 }
4146 return 0;
4147}
4148
Johannes Bergff276692013-02-15 00:09:01 +01004149static int nl80211_set_station_tdls(struct genl_info *info,
4150 struct station_parameters *params)
4151{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304152 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004153 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004154 if (info->attrs[NL80211_ATTR_PEER_AID])
4155 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004156 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4157 params->ht_capa =
4158 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4159 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4160 params->vht_capa =
4161 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4162
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304163 err = nl80211_parse_sta_channel_info(info, params);
4164 if (err)
4165 return err;
4166
Johannes Bergff276692013-02-15 00:09:01 +01004167 return nl80211_parse_sta_wme(info, params);
4168}
4169
Johannes Berg5727ef12007-12-19 02:03:34 +01004170static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4171{
Johannes Berg4c476992010-10-04 21:36:35 +02004172 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004173 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004174 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004175 u8 *mac_addr;
4176 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004177
4178 memset(&params, 0, sizeof(params));
4179
4180 params.listen_interval = -1;
4181
Johannes Berg77ee7c82013-02-15 00:48:33 +01004182 if (!rdev->ops->change_station)
4183 return -EOPNOTSUPP;
4184
Johannes Berg5727ef12007-12-19 02:03:34 +01004185 if (info->attrs[NL80211_ATTR_STA_AID])
4186 return -EINVAL;
4187
4188 if (!info->attrs[NL80211_ATTR_MAC])
4189 return -EINVAL;
4190
4191 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4192
4193 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4194 params.supported_rates =
4195 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4196 params.supported_rates_len =
4197 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4198 }
4199
Jouni Malinen9d62a982013-02-14 21:10:13 +02004200 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4201 params.capability =
4202 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4203 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4204 }
4205
4206 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4207 params.ext_capab =
4208 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4209 params.ext_capab_len =
4210 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4211 }
4212
Jouni Malinendf881292013-02-14 21:10:54 +02004213 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004214 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03004215
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004216 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004217 return -EINVAL;
4218
Johannes Bergf8bacc22013-02-14 23:27:01 +01004219 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004220 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004221 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4222 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4223 return -EINVAL;
4224 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004225
Johannes Bergf8bacc22013-02-14 23:27:01 +01004226 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004227 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004228 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4229 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4230 return -EINVAL;
4231 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4232 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004233
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004234 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4235 enum nl80211_mesh_power_mode pm = nla_get_u32(
4236 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4237
4238 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4239 pm > NL80211_MESH_POWER_MAX)
4240 return -EINVAL;
4241
4242 params.local_pm = pm;
4243 }
4244
Johannes Berg77ee7c82013-02-15 00:48:33 +01004245 /* Include parameters for TDLS peer (will check later) */
4246 err = nl80211_set_station_tdls(info, &params);
4247 if (err)
4248 return err;
4249
4250 params.vlan = get_vlan(info, rdev);
4251 if (IS_ERR(params.vlan))
4252 return PTR_ERR(params.vlan);
4253
Johannes Berga97f4422009-06-18 17:23:43 +02004254 switch (dev->ieee80211_ptr->iftype) {
4255 case NL80211_IFTYPE_AP:
4256 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004257 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004258 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004259 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004260 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004261 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004262 break;
4263 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004264 err = -EOPNOTSUPP;
4265 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004266 }
4267
Johannes Berg77ee7c82013-02-15 00:48:33 +01004268 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004269 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004270
Johannes Berg77ee7c82013-02-15 00:48:33 +01004271 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004272 if (params.vlan)
4273 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004274
Johannes Berg5727ef12007-12-19 02:03:34 +01004275 return err;
4276}
4277
4278static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4279{
Johannes Berg4c476992010-10-04 21:36:35 +02004280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004281 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004282 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004283 struct station_parameters params;
4284 u8 *mac_addr = NULL;
4285
4286 memset(&params, 0, sizeof(params));
4287
Johannes Berg984c3112013-02-14 23:43:25 +01004288 if (!rdev->ops->add_station)
4289 return -EOPNOTSUPP;
4290
Johannes Berg5727ef12007-12-19 02:03:34 +01004291 if (!info->attrs[NL80211_ATTR_MAC])
4292 return -EINVAL;
4293
Johannes Berg5727ef12007-12-19 02:03:34 +01004294 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4295 return -EINVAL;
4296
4297 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4298 return -EINVAL;
4299
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004300 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4301 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004302 return -EINVAL;
4303
Johannes Berg5727ef12007-12-19 02:03:34 +01004304 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4305 params.supported_rates =
4306 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4307 params.supported_rates_len =
4308 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4309 params.listen_interval =
4310 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004311
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004312 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004313 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004314 else
4315 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004316 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4317 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004318
Jouni Malinen9d62a982013-02-14 21:10:13 +02004319 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4320 params.capability =
4321 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4322 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4323 }
4324
4325 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4326 params.ext_capab =
4327 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4328 params.ext_capab_len =
4329 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4330 }
4331
Jouni Malinen36aedc92008-08-25 11:58:58 +03004332 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4333 params.ht_capa =
4334 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004335
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004336 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4337 params.vht_capa =
4338 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4339
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004340 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4341 params.opmode_notif_used = true;
4342 params.opmode_notif =
4343 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4344 }
4345
Johannes Bergf8bacc22013-02-14 23:27:01 +01004346 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004347 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004348 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4349 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4350 return -EINVAL;
4351 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004352
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304353 err = nl80211_parse_sta_channel_info(info, &params);
4354 if (err)
4355 return err;
4356
Johannes Bergff276692013-02-15 00:09:01 +01004357 err = nl80211_parse_sta_wme(info, &params);
4358 if (err)
4359 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004360
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004361 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004362 return -EINVAL;
4363
Johannes Berg77ee7c82013-02-15 00:48:33 +01004364 /* When you run into this, adjust the code below for the new flag */
4365 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4366
Johannes Bergbdd90d52011-12-14 12:20:27 +01004367 switch (dev->ieee80211_ptr->iftype) {
4368 case NL80211_IFTYPE_AP:
4369 case NL80211_IFTYPE_AP_VLAN:
4370 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004371 /* ignore WME attributes if iface/sta is not capable */
4372 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4373 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4374 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004375
Johannes Bergbdd90d52011-12-14 12:20:27 +01004376 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004377 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4378 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004379 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004380 /* but don't bother the driver with it */
4381 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004382
Johannes Bergd582cff2012-10-26 17:53:44 +02004383 /* allow authenticated/associated only if driver handles it */
4384 if (!(rdev->wiphy.features &
4385 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4386 params.sta_flags_mask &
4387 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4388 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4389 return -EINVAL;
4390
Johannes Bergbdd90d52011-12-14 12:20:27 +01004391 /* must be last in here for error handling */
4392 params.vlan = get_vlan(info, rdev);
4393 if (IS_ERR(params.vlan))
4394 return PTR_ERR(params.vlan);
4395 break;
4396 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004397 /* ignore uAPSD data */
4398 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4399
Johannes Bergd582cff2012-10-26 17:53:44 +02004400 /* associated is disallowed */
4401 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4402 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004403 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004404 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4405 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004406 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004407 break;
4408 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004409 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004410 /* ignore uAPSD data */
4411 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4412
Johannes Berg77ee7c82013-02-15 00:48:33 +01004413 /* these are disallowed */
4414 if (params.sta_flags_mask &
4415 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4416 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004417 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004418 /* Only TDLS peers can be added */
4419 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4420 return -EINVAL;
4421 /* Can only add if TDLS ... */
4422 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4423 return -EOPNOTSUPP;
4424 /* ... with external setup is supported */
4425 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4426 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004427 /*
4428 * Older wpa_supplicant versions always mark the TDLS peer
4429 * as authorized, but it shouldn't yet be.
4430 */
4431 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004432 break;
4433 default:
4434 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004435 }
4436
Johannes Bergbdd90d52011-12-14 12:20:27 +01004437 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004438
Hila Gonene35e4d22012-06-27 17:19:42 +03004439 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004440
Johannes Berg5727ef12007-12-19 02:03:34 +01004441 if (params.vlan)
4442 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004443 return err;
4444}
4445
4446static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4447{
Johannes Berg4c476992010-10-04 21:36:35 +02004448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4449 struct net_device *dev = info->user_ptr[1];
Jouni Malinen89c771e2014-10-10 20:52:40 +03004450 struct station_del_parameters params;
4451
4452 memset(&params, 0, sizeof(params));
Johannes Berg5727ef12007-12-19 02:03:34 +01004453
4454 if (info->attrs[NL80211_ATTR_MAC])
Jouni Malinen89c771e2014-10-10 20:52:40 +03004455 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004456
Johannes Berge80cf852009-05-11 14:43:13 +02004457 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004458 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004459 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004460 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4461 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 if (!rdev->ops->del_station)
4464 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004465
Jouni Malinen98856862014-10-20 13:20:45 +03004466 if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
4467 params.subtype =
4468 nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
4469 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
4470 params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
4471 return -EINVAL;
4472 } else {
4473 /* Default to Deauthentication frame */
4474 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
4475 }
4476
4477 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
4478 params.reason_code =
4479 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4480 if (params.reason_code == 0)
4481 return -EINVAL; /* 0 is reserved */
4482 } else {
4483 /* Default to reason code 2 */
4484 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
4485 }
4486
Jouni Malinen89c771e2014-10-10 20:52:40 +03004487 return rdev_del_station(rdev, dev, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004488}
4489
Eric W. Biederman15e47302012-09-07 20:12:54 +00004490static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491 int flags, struct net_device *dev,
4492 u8 *dst, u8 *next_hop,
4493 struct mpath_info *pinfo)
4494{
4495 void *hdr;
4496 struct nlattr *pinfoattr;
4497
Henning Rogge1ef4c852014-11-04 16:14:58 +01004498 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004499 if (!hdr)
4500 return -1;
4501
David S. Miller9360ffd2012-03-29 04:41:26 -04004502 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4503 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4504 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4505 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4506 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004507
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004508 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4509 if (!pinfoattr)
4510 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004511 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4512 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4513 pinfo->frame_qlen))
4514 goto nla_put_failure;
4515 if (((pinfo->filled & MPATH_INFO_SN) &&
4516 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4517 ((pinfo->filled & MPATH_INFO_METRIC) &&
4518 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4519 pinfo->metric)) ||
4520 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4521 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4522 pinfo->exptime)) ||
4523 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4524 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4525 pinfo->flags)) ||
4526 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4527 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4528 pinfo->discovery_timeout)) ||
4529 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4530 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4531 pinfo->discovery_retries)))
4532 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004533
4534 nla_nest_end(msg, pinfoattr);
4535
4536 return genlmsg_end(msg, hdr);
4537
4538 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004539 genlmsg_cancel(msg, hdr);
4540 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004541}
4542
4543static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004544 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004545{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004546 struct mpath_info pinfo;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004547 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02004548 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004549 u8 dst[ETH_ALEN];
4550 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004551 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004552 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004553
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004554 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004555 if (err)
4556 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004557
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004558 if (!rdev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004559 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004560 goto out_err;
4561 }
4562
Johannes Berg97990a02013-04-19 01:02:55 +02004563 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004564 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004565 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004566 }
4567
Johannes Bergbba95fe2008-07-29 13:22:51 +02004568 while (1) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004569 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
Johannes Berg97990a02013-04-19 01:02:55 +02004570 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004571 if (err == -ENOENT)
4572 break;
4573 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004574 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004575
Eric W. Biederman15e47302012-09-07 20:12:54 +00004576 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004577 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004578 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004579 &pinfo) < 0)
4580 goto out;
4581
4582 path_idx++;
4583 }
4584
4585
4586 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004587 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004588 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004589 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08004590 nl80211_finish_wdev_dump(rdev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004591 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004592}
4593
4594static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4595{
Johannes Berg4c476992010-10-04 21:36:35 +02004596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004597 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004598 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004599 struct mpath_info pinfo;
4600 struct sk_buff *msg;
4601 u8 *dst = NULL;
4602 u8 next_hop[ETH_ALEN];
4603
4604 memset(&pinfo, 0, sizeof(pinfo));
4605
4606 if (!info->attrs[NL80211_ATTR_MAC])
4607 return -EINVAL;
4608
4609 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4610
Johannes Berg4c476992010-10-04 21:36:35 +02004611 if (!rdev->ops->get_mpath)
4612 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004613
Johannes Berg4c476992010-10-04 21:36:35 +02004614 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4615 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004616
Hila Gonene35e4d22012-06-27 17:19:42 +03004617 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004618 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004619 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004620
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004622 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004623 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004624
Eric W. Biederman15e47302012-09-07 20:12:54 +00004625 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004626 dev, dst, next_hop, &pinfo) < 0) {
4627 nlmsg_free(msg);
4628 return -ENOBUFS;
4629 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004630
Johannes Berg4c476992010-10-04 21:36:35 +02004631 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004632}
4633
4634static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4635{
Johannes Berg4c476992010-10-04 21:36:35 +02004636 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4637 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004638 u8 *dst = NULL;
4639 u8 *next_hop = NULL;
4640
4641 if (!info->attrs[NL80211_ATTR_MAC])
4642 return -EINVAL;
4643
4644 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4645 return -EINVAL;
4646
4647 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4648 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4649
Johannes Berg4c476992010-10-04 21:36:35 +02004650 if (!rdev->ops->change_mpath)
4651 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004652
Johannes Berg4c476992010-10-04 21:36:35 +02004653 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4654 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004655
Hila Gonene35e4d22012-06-27 17:19:42 +03004656 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004657}
Johannes Berg4c476992010-10-04 21:36:35 +02004658
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004659static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4660{
Johannes Berg4c476992010-10-04 21:36:35 +02004661 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4662 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004663 u8 *dst = NULL;
4664 u8 *next_hop = NULL;
4665
4666 if (!info->attrs[NL80211_ATTR_MAC])
4667 return -EINVAL;
4668
4669 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4670 return -EINVAL;
4671
4672 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4673 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4674
Johannes Berg4c476992010-10-04 21:36:35 +02004675 if (!rdev->ops->add_mpath)
4676 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004677
Johannes Berg4c476992010-10-04 21:36:35 +02004678 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4679 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004680
Hila Gonene35e4d22012-06-27 17:19:42 +03004681 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004682}
4683
4684static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4685{
Johannes Berg4c476992010-10-04 21:36:35 +02004686 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4687 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004688 u8 *dst = NULL;
4689
4690 if (info->attrs[NL80211_ATTR_MAC])
4691 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4692
Johannes Berg4c476992010-10-04 21:36:35 +02004693 if (!rdev->ops->del_mpath)
4694 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004695
Hila Gonene35e4d22012-06-27 17:19:42 +03004696 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004697}
4698
Henning Rogge66be7d22014-09-12 08:58:49 +02004699static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
4700{
4701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4702 int err;
4703 struct net_device *dev = info->user_ptr[1];
4704 struct mpath_info pinfo;
4705 struct sk_buff *msg;
4706 u8 *dst = NULL;
4707 u8 mpp[ETH_ALEN];
4708
4709 memset(&pinfo, 0, sizeof(pinfo));
4710
4711 if (!info->attrs[NL80211_ATTR_MAC])
4712 return -EINVAL;
4713
4714 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4715
4716 if (!rdev->ops->get_mpp)
4717 return -EOPNOTSUPP;
4718
4719 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4720 return -EOPNOTSUPP;
4721
4722 err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
4723 if (err)
4724 return err;
4725
4726 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4727 if (!msg)
4728 return -ENOMEM;
4729
4730 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4731 dev, dst, mpp, &pinfo) < 0) {
4732 nlmsg_free(msg);
4733 return -ENOBUFS;
4734 }
4735
4736 return genlmsg_reply(msg, info);
4737}
4738
4739static int nl80211_dump_mpp(struct sk_buff *skb,
4740 struct netlink_callback *cb)
4741{
4742 struct mpath_info pinfo;
4743 struct cfg80211_registered_device *rdev;
4744 struct wireless_dev *wdev;
4745 u8 dst[ETH_ALEN];
4746 u8 mpp[ETH_ALEN];
4747 int path_idx = cb->args[2];
4748 int err;
4749
4750 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4751 if (err)
4752 return err;
4753
4754 if (!rdev->ops->dump_mpp) {
4755 err = -EOPNOTSUPP;
4756 goto out_err;
4757 }
4758
4759 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4760 err = -EOPNOTSUPP;
4761 goto out_err;
4762 }
4763
4764 while (1) {
4765 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
4766 mpp, &pinfo);
4767 if (err == -ENOENT)
4768 break;
4769 if (err)
4770 goto out_err;
4771
4772 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4773 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4774 wdev->netdev, dst, mpp,
4775 &pinfo) < 0)
4776 goto out;
4777
4778 path_idx++;
4779 }
4780
4781 out:
4782 cb->args[2] = path_idx;
4783 err = skb->len;
4784 out_err:
4785 nl80211_finish_wdev_dump(rdev);
4786 return err;
4787}
4788
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004789static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4790{
Johannes Berg4c476992010-10-04 21:36:35 +02004791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4792 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004793 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004794 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004795 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004796
4797 memset(&params, 0, sizeof(params));
4798 /* default to not changing parameters */
4799 params.use_cts_prot = -1;
4800 params.use_short_preamble = -1;
4801 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004802 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004803 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004804 params.p2p_ctwindow = -1;
4805 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004806
4807 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4808 params.use_cts_prot =
4809 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4810 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4811 params.use_short_preamble =
4812 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4813 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4814 params.use_short_slot_time =
4815 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004816 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4817 params.basic_rates =
4818 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4819 params.basic_rates_len =
4820 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4821 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004822 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4823 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004824 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4825 params.ht_opmode =
4826 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004827
Johannes Berg53cabad2012-11-14 15:17:28 +01004828 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4829 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4830 return -EINVAL;
4831 params.p2p_ctwindow =
4832 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4833 if (params.p2p_ctwindow < 0)
4834 return -EINVAL;
4835 if (params.p2p_ctwindow != 0 &&
4836 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4837 return -EINVAL;
4838 }
4839
4840 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4841 u8 tmp;
4842
4843 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4844 return -EINVAL;
4845 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4846 if (tmp > 1)
4847 return -EINVAL;
4848 params.p2p_opp_ps = tmp;
4849 if (params.p2p_opp_ps &&
4850 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4851 return -EINVAL;
4852 }
4853
Johannes Berg4c476992010-10-04 21:36:35 +02004854 if (!rdev->ops->change_bss)
4855 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004856
Johannes Berg074ac8d2010-09-16 14:58:22 +02004857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004858 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4859 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004860
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004861 wdev_lock(wdev);
4862 err = rdev_change_bss(rdev, dev, &params);
4863 wdev_unlock(wdev);
4864
4865 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004866}
4867
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004868static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004869 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4870 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4871 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4872 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4873 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4874 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004875 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004876};
4877
4878static int parse_reg_rule(struct nlattr *tb[],
4879 struct ieee80211_reg_rule *reg_rule)
4880{
4881 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4882 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4883
4884 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4885 return -EINVAL;
4886 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4887 return -EINVAL;
4888 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4889 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004890 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4891 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004892 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4893 return -EINVAL;
4894
4895 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4896
4897 freq_range->start_freq_khz =
4898 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4899 freq_range->end_freq_khz =
4900 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004901 freq_range->max_bandwidth_khz =
4902 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004903
4904 power_rule->max_eirp =
4905 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4906
4907 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4908 power_rule->max_antenna_gain =
4909 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4910
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004911 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4912 reg_rule->dfs_cac_ms =
4913 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4914
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004915 return 0;
4916}
4917
4918static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4919{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004920 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004921 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004922
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004923 /*
4924 * You should only get this when cfg80211 hasn't yet initialized
4925 * completely when built-in to the kernel right between the time
4926 * window between nl80211_init() and regulatory_init(), if that is
4927 * even possible.
4928 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004929 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004930 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004931
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004932 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4933 user_reg_hint_type =
4934 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4935 else
4936 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4937
4938 switch (user_reg_hint_type) {
4939 case NL80211_USER_REG_HINT_USER:
4940 case NL80211_USER_REG_HINT_CELL_BASE:
Ilan Peer52616f22014-02-25 16:26:00 +02004941 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4942 return -EINVAL;
4943
4944 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4945 return regulatory_hint_user(data, user_reg_hint_type);
4946 case NL80211_USER_REG_HINT_INDOOR:
4947 return regulatory_hint_indoor_user();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004948 default:
4949 return -EINVAL;
4950 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004951}
4952
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004953static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004954 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004955{
Johannes Berg4c476992010-10-04 21:36:35 +02004956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004957 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004958 struct wireless_dev *wdev = dev->ieee80211_ptr;
4959 struct mesh_config cur_params;
4960 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004961 void *hdr;
4962 struct nlattr *pinfoattr;
4963 struct sk_buff *msg;
4964
Johannes Berg29cbe682010-12-03 09:20:44 +01004965 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4966 return -EOPNOTSUPP;
4967
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004968 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004969 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004970
Johannes Berg29cbe682010-12-03 09:20:44 +01004971 wdev_lock(wdev);
4972 /* If not connected, get default parameters */
4973 if (!wdev->mesh_id_len)
4974 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4975 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004976 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004977 wdev_unlock(wdev);
4978
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004979 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004980 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004981
4982 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004984 if (!msg)
4985 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004986 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004987 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004988 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004989 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004990 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004991 if (!pinfoattr)
4992 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004993 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4994 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4995 cur_params.dot11MeshRetryTimeout) ||
4996 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4997 cur_params.dot11MeshConfirmTimeout) ||
4998 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4999 cur_params.dot11MeshHoldingTimeout) ||
5000 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5001 cur_params.dot11MeshMaxPeerLinks) ||
5002 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5003 cur_params.dot11MeshMaxRetries) ||
5004 nla_put_u8(msg, NL80211_MESHCONF_TTL,
5005 cur_params.dot11MeshTTL) ||
5006 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5007 cur_params.element_ttl) ||
5008 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5009 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04005010 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5011 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005012 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5013 cur_params.dot11MeshHWMPmaxPREQretries) ||
5014 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5015 cur_params.path_refresh_time) ||
5016 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5017 cur_params.min_discovery_timeout) ||
5018 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5019 cur_params.dot11MeshHWMPactivePathTimeout) ||
5020 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5021 cur_params.dot11MeshHWMPpreqMinInterval) ||
5022 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5023 cur_params.dot11MeshHWMPperrMinInterval) ||
5024 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5025 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5026 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5027 cur_params.dot11MeshHWMPRootMode) ||
5028 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5029 cur_params.dot11MeshHWMPRannInterval) ||
5030 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5031 cur_params.dot11MeshGateAnnouncementProtocol) ||
5032 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5033 cur_params.dot11MeshForwarding) ||
5034 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07005035 cur_params.rssi_threshold) ||
5036 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005037 cur_params.ht_opmode) ||
5038 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5039 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5040 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005041 cur_params.dot11MeshHWMProotInterval) ||
5042 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005043 cur_params.dot11MeshHWMPconfirmationInterval) ||
5044 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5045 cur_params.power_mode) ||
5046 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005047 cur_params.dot11MeshAwakeWindowDuration) ||
5048 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5049 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04005050 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005051 nla_nest_end(msg, pinfoattr);
5052 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005053 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005054
Johannes Berg3b858752009-03-12 09:55:09 +01005055 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005056 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005057 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04005058 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02005059 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005060}
5061
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005062static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005063 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5064 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5065 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5066 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5067 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5068 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01005069 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005070 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07005071 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005072 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5073 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5074 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5075 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5076 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08005077 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005078 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07005079 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07005080 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07005081 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08005082 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005083 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5084 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005085 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5086 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005087 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005088 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5089 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005090 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005091};
5092
Javier Cardonac80d5452010-12-16 17:37:49 -08005093static const struct nla_policy
5094 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07005095 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08005096 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5097 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07005098 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07005099 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005100 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07005101 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005102 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07005103 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08005104};
5105
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005106static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005107 struct mesh_config *cfg,
5108 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005109{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005110 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005111 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005112
Marco Porschea54fba2013-01-07 16:04:48 +01005113#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5114do { \
5115 if (tb[attr]) { \
5116 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
5117 return -EINVAL; \
5118 cfg->param = fn(tb[attr]); \
5119 mask |= (1 << (attr - 1)); \
5120 } \
5121} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005122
5123
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005124 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005125 return -EINVAL;
5126 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005127 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005128 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005129 return -EINVAL;
5130
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005131 /* This makes sure that there aren't more than 32 mesh config
5132 * parameters (otherwise our bitfield scheme would not work.) */
5133 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5134
5135 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01005136 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005137 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5138 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005139 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005140 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5141 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005142 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005143 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
5144 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005145 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005146 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
5147 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005148 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005149 mask, NL80211_MESHCONF_MAX_RETRIES,
5150 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005151 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005152 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005153 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005154 mask, NL80211_MESHCONF_ELEMENT_TTL,
5155 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005156 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005157 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5158 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005159 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
5160 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005161 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5162 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005163 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005164 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5165 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005166 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005167 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
5168 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005169 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005170 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5171 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005172 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
5173 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005174 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5175 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005176 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01005177 1, 65535, mask,
5178 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005179 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08005180 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01005181 1, 65535, mask,
5182 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005183 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005184 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005185 dot11MeshHWMPnetDiameterTraversalTime,
5186 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005187 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5188 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01005189 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
5190 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
5191 nla_get_u8);
5192 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
5193 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005194 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00005195 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005196 dot11MeshGateAnnouncementProtocol, 0, 1,
5197 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005198 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01005199 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005200 mask, NL80211_MESHCONF_FORWARDING,
5201 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08005202 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005203 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08005204 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01005205 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08005206 mask, NL80211_MESHCONF_HT_OPMODE,
5207 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005208 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01005209 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005210 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5211 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01005212 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08005213 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5214 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005215 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01005216 dot11MeshHWMPconfirmationInterval,
5217 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08005218 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5219 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01005220 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
5221 NL80211_MESH_POWER_ACTIVE,
5222 NL80211_MESH_POWER_MAX,
5223 mask, NL80211_MESHCONF_POWER_MODE,
5224 nla_get_u32);
5225 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
5226 0, 65535, mask,
5227 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07005228 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
5229 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
5230 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005231 if (mask_out)
5232 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08005233
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005234 return 0;
5235
5236#undef FILL_IN_MESH_PARAM_IF_SET
5237}
5238
Javier Cardonac80d5452010-12-16 17:37:49 -08005239static int nl80211_parse_mesh_setup(struct genl_info *info,
5240 struct mesh_setup *setup)
5241{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08005243 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5244
5245 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5246 return -EINVAL;
5247 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5248 info->attrs[NL80211_ATTR_MESH_SETUP],
5249 nl80211_mesh_setup_params_policy))
5250 return -EINVAL;
5251
Javier Cardonad299a1f2012-03-31 11:31:33 -07005252 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5253 setup->sync_method =
5254 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5255 IEEE80211_SYNC_METHOD_VENDOR :
5256 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5257
Javier Cardonac80d5452010-12-16 17:37:49 -08005258 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5259 setup->path_sel_proto =
5260 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5261 IEEE80211_PATH_PROTOCOL_VENDOR :
5262 IEEE80211_PATH_PROTOCOL_HWMP;
5263
5264 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5265 setup->path_metric =
5266 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5267 IEEE80211_PATH_METRIC_VENDOR :
5268 IEEE80211_PATH_METRIC_AIRTIME;
5269
Javier Cardona581a8b02011-04-07 15:08:27 -07005270
5271 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005272 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005273 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005274 if (!is_valid_ie_attr(ieattr))
5275 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005276 setup->ie = nla_data(ieattr);
5277 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005278 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005279 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5280 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5281 return -EINVAL;
5282 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005283 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5284 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005285 if (setup->is_secure)
5286 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005287
Colleen Twitty6e16d902013-05-08 11:45:59 -07005288 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5289 if (!setup->user_mpm)
5290 return -EINVAL;
5291 setup->auth_id =
5292 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5293 }
5294
Javier Cardonac80d5452010-12-16 17:37:49 -08005295 return 0;
5296}
5297
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005298static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005299 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005300{
5301 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5302 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005303 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005304 struct mesh_config cfg;
5305 u32 mask;
5306 int err;
5307
Johannes Berg29cbe682010-12-03 09:20:44 +01005308 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5309 return -EOPNOTSUPP;
5310
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005311 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005312 return -EOPNOTSUPP;
5313
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005314 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005315 if (err)
5316 return err;
5317
Johannes Berg29cbe682010-12-03 09:20:44 +01005318 wdev_lock(wdev);
5319 if (!wdev->mesh_id_len)
5320 err = -ENOLINK;
5321
5322 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005323 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005324
5325 wdev_unlock(wdev);
5326
5327 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005328}
5329
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005330static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5331{
Johannes Berg458f4f92012-12-06 15:47:38 +01005332 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005333 struct sk_buff *msg;
5334 void *hdr = NULL;
5335 struct nlattr *nl_reg_rules;
5336 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005337
5338 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005339 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005340
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005341 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005342 if (!msg)
5343 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005344
Eric W. Biederman15e47302012-09-07 20:12:54 +00005345 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005346 NL80211_CMD_GET_REG);
5347 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005348 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005349
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005350 if (reg_last_request_cell_base() &&
5351 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5352 NL80211_USER_REG_HINT_CELL_BASE))
5353 goto nla_put_failure;
5354
Johannes Berg458f4f92012-12-06 15:47:38 +01005355 rcu_read_lock();
5356 regdom = rcu_dereference(cfg80211_regdomain);
5357
5358 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5359 (regdom->dfs_region &&
5360 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5361 goto nla_put_failure_rcu;
5362
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005363 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5364 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005365 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005366
Johannes Berg458f4f92012-12-06 15:47:38 +01005367 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005368 struct nlattr *nl_reg_rule;
5369 const struct ieee80211_reg_rule *reg_rule;
5370 const struct ieee80211_freq_range *freq_range;
5371 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005372 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005373
Johannes Berg458f4f92012-12-06 15:47:38 +01005374 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005375 freq_range = &reg_rule->freq_range;
5376 power_rule = &reg_rule->power_rule;
5377
5378 nl_reg_rule = nla_nest_start(msg, i);
5379 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005380 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005381
Janusz Dziedzic97524822014-01-30 09:52:20 +01005382 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5383 if (!max_bandwidth_khz)
5384 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5385 reg_rule);
5386
David S. Miller9360ffd2012-03-29 04:41:26 -04005387 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5388 reg_rule->flags) ||
5389 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5390 freq_range->start_freq_khz) ||
5391 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5392 freq_range->end_freq_khz) ||
5393 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005394 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005395 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5396 power_rule->max_antenna_gain) ||
5397 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005398 power_rule->max_eirp) ||
5399 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5400 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005401 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005402
5403 nla_nest_end(msg, nl_reg_rule);
5404 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005405 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005406
5407 nla_nest_end(msg, nl_reg_rules);
5408
5409 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005410 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005411
Johannes Berg458f4f92012-12-06 15:47:38 +01005412nla_put_failure_rcu:
5413 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005414nla_put_failure:
5415 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005416put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005417 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005418 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005419}
5420
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005421static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5422{
5423 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5424 struct nlattr *nl_reg_rule;
Johannes Bergea372c52014-11-28 14:54:31 +01005425 char *alpha2;
5426 int rem_reg_rules, r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005427 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005428 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Johannes Bergea372c52014-11-28 14:54:31 +01005429 struct ieee80211_regdomain *rd;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005430
5431 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5432 return -EINVAL;
5433
5434 if (!info->attrs[NL80211_ATTR_REG_RULES])
5435 return -EINVAL;
5436
5437 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5438
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005439 if (info->attrs[NL80211_ATTR_DFS_REGION])
5440 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5441
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005442 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005443 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005444 num_rules++;
5445 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005446 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005447 }
5448
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005449 if (!reg_is_valid_request(alpha2))
5450 return -EINVAL;
5451
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005452 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005453 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005454
5455 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005456 if (!rd)
5457 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005458
5459 rd->n_reg_rules = num_rules;
5460 rd->alpha2[0] = alpha2[0];
5461 rd->alpha2[1] = alpha2[1];
5462
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005463 /*
5464 * Disable DFS master mode if the DFS region was
5465 * not supported or known on this kernel.
5466 */
5467 if (reg_supported_dfs_region(dfs_region))
5468 rd->dfs_region = dfs_region;
5469
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005470 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005471 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005472 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5473 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5474 reg_rule_policy);
5475 if (r)
5476 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005477 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5478 if (r)
5479 goto bad_reg;
5480
5481 rule_idx++;
5482
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005483 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5484 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005485 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005486 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005487 }
5488
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005489 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005490 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005491 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005492
Johannes Bergd2372b32008-10-24 20:32:20 +02005493 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005494 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005495 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005496}
5497
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005498static int validate_scan_freqs(struct nlattr *freqs)
5499{
5500 struct nlattr *attr1, *attr2;
5501 int n_channels = 0, tmp1, tmp2;
5502
5503 nla_for_each_nested(attr1, freqs, tmp1) {
5504 n_channels++;
5505 /*
5506 * Some hardware has a limited channel list for
5507 * scanning, and it is pretty much nonsensical
5508 * to scan for a channel twice, so disallow that
5509 * and don't require drivers to check that the
5510 * channel list they get isn't longer than what
5511 * they can scan, as long as they can scan all
5512 * the channels they registered at once.
5513 */
5514 nla_for_each_nested(attr2, freqs, tmp2)
5515 if (attr1 != attr2 &&
5516 nla_get_u32(attr1) == nla_get_u32(attr2))
5517 return 0;
5518 }
5519
5520 return n_channels;
5521}
5522
Johannes Bergad2b26a2014-06-12 21:39:05 +02005523static int nl80211_parse_random_mac(struct nlattr **attrs,
5524 u8 *mac_addr, u8 *mac_addr_mask)
5525{
5526 int i;
5527
5528 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
5529 memset(mac_addr, 0, ETH_ALEN);
5530 memset(mac_addr_mask, 0, ETH_ALEN);
5531 mac_addr[0] = 0x2;
5532 mac_addr_mask[0] = 0x3;
5533
5534 return 0;
5535 }
5536
5537 /* need both or none */
5538 if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
5539 return -EINVAL;
5540
5541 memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
5542 memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
5543
5544 /* don't allow or configure an mcast address */
5545 if (!is_multicast_ether_addr(mac_addr_mask) ||
5546 is_multicast_ether_addr(mac_addr))
5547 return -EINVAL;
5548
5549 /*
5550 * allow users to pass a MAC address that has bits set outside
5551 * of the mask, but don't bother drivers with having to deal
5552 * with such bits
5553 */
5554 for (i = 0; i < ETH_ALEN; i++)
5555 mac_addr[i] &= mac_addr_mask[i];
5556
5557 return 0;
5558}
5559
Johannes Berg2a519312009-02-10 21:25:55 +01005560static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5561{
Johannes Berg4c476992010-10-04 21:36:35 +02005562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005563 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005564 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005565 struct nlattr *attr;
5566 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005567 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005568 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005569
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005570 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5571 return -EINVAL;
5572
Johannes Berg79c97e92009-07-07 03:56:12 +02005573 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005574
Johannes Berg4c476992010-10-04 21:36:35 +02005575 if (!rdev->ops->scan)
5576 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005577
Johannes Bergf9d15d12014-01-22 11:14:19 +02005578 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005579 err = -EBUSY;
5580 goto unlock;
5581 }
Johannes Berg2a519312009-02-10 21:25:55 +01005582
5583 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005584 n_channels = validate_scan_freqs(
5585 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005586 if (!n_channels) {
5587 err = -EINVAL;
5588 goto unlock;
5589 }
Johannes Berg2a519312009-02-10 21:25:55 +01005590 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005591 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005592 }
5593
5594 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5595 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5596 n_ssids++;
5597
Johannes Bergf9f47522013-03-19 15:04:07 +01005598 if (n_ssids > wiphy->max_scan_ssids) {
5599 err = -EINVAL;
5600 goto unlock;
5601 }
Johannes Berg2a519312009-02-10 21:25:55 +01005602
Jouni Malinen70692ad2009-02-16 19:39:13 +02005603 if (info->attrs[NL80211_ATTR_IE])
5604 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5605 else
5606 ie_len = 0;
5607
Johannes Bergf9f47522013-03-19 15:04:07 +01005608 if (ie_len > wiphy->max_scan_ie_len) {
5609 err = -EINVAL;
5610 goto unlock;
5611 }
Johannes Berg18a83652009-03-31 12:12:05 +02005612
Johannes Berg2a519312009-02-10 21:25:55 +01005613 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005614 + sizeof(*request->ssids) * n_ssids
5615 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005616 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005617 if (!request) {
5618 err = -ENOMEM;
5619 goto unlock;
5620 }
Johannes Berg2a519312009-02-10 21:25:55 +01005621
Johannes Berg2a519312009-02-10 21:25:55 +01005622 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005623 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005624 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005625 if (ie_len) {
5626 if (request->ssids)
5627 request->ie = (void *)(request->ssids + n_ssids);
5628 else
5629 request->ie = (void *)(request->channels + n_channels);
5630 }
Johannes Berg2a519312009-02-10 21:25:55 +01005631
Johannes Berg584991d2009-11-02 13:32:03 +01005632 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005633 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5634 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005635 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005636 struct ieee80211_channel *chan;
5637
5638 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5639
5640 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005641 err = -EINVAL;
5642 goto out_free;
5643 }
Johannes Berg584991d2009-11-02 13:32:03 +01005644
5645 /* ignore disabled channels */
5646 if (chan->flags & IEEE80211_CHAN_DISABLED)
5647 continue;
5648
5649 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005650 i++;
5651 }
5652 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005653 enum ieee80211_band band;
5654
Johannes Berg2a519312009-02-10 21:25:55 +01005655 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005656 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5657 int j;
5658 if (!wiphy->bands[band])
5659 continue;
5660 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005661 struct ieee80211_channel *chan;
5662
5663 chan = &wiphy->bands[band]->channels[j];
5664
5665 if (chan->flags & IEEE80211_CHAN_DISABLED)
5666 continue;
5667
5668 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005669 i++;
5670 }
5671 }
5672 }
5673
Johannes Berg584991d2009-11-02 13:32:03 +01005674 if (!i) {
5675 err = -EINVAL;
5676 goto out_free;
5677 }
5678
5679 request->n_channels = i;
5680
Johannes Berg2a519312009-02-10 21:25:55 +01005681 i = 0;
5682 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5683 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005684 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005685 err = -EINVAL;
5686 goto out_free;
5687 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005688 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005689 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005690 i++;
5691 }
5692 }
5693
Jouni Malinen70692ad2009-02-16 19:39:13 +02005694 if (info->attrs[NL80211_ATTR_IE]) {
5695 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005696 memcpy((void *)request->ie,
5697 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005698 request->ie_len);
5699 }
5700
Johannes Berg34850ab2011-07-18 18:08:35 +02005701 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005702 if (wiphy->bands[i])
5703 request->rates[i] =
5704 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005705
5706 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5707 nla_for_each_nested(attr,
5708 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5709 tmp) {
5710 enum ieee80211_band band = nla_type(attr);
5711
Dan Carpenter84404622011-07-29 11:52:18 +03005712 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005713 err = -EINVAL;
5714 goto out_free;
5715 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005716
5717 if (!wiphy->bands[band])
5718 continue;
5719
Johannes Berg34850ab2011-07-18 18:08:35 +02005720 err = ieee80211_get_ratemask(wiphy->bands[band],
5721 nla_data(attr),
5722 nla_len(attr),
5723 &request->rates[band]);
5724 if (err)
5725 goto out_free;
5726 }
5727 }
5728
Sam Leffler46856bb2012-10-11 21:03:32 -07005729 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005730 request->flags = nla_get_u32(
5731 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005732 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5733 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005734 err = -EOPNOTSUPP;
5735 goto out_free;
5736 }
Johannes Bergad2b26a2014-06-12 21:39:05 +02005737
5738 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
5739 if (!(wiphy->features &
5740 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)) {
5741 err = -EOPNOTSUPP;
5742 goto out_free;
5743 }
5744
5745 if (wdev->current_bss) {
5746 err = -EOPNOTSUPP;
5747 goto out_free;
5748 }
5749
5750 err = nl80211_parse_random_mac(info->attrs,
5751 request->mac_addr,
5752 request->mac_addr_mask);
5753 if (err)
5754 goto out_free;
5755 }
Sam Leffler46856bb2012-10-11 21:03:32 -07005756 }
Sam Lefflered4737712012-10-11 21:03:31 -07005757
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305758 request->no_cck =
5759 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5760
Johannes Bergfd014282012-06-18 19:17:03 +02005761 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005762 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005763 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005764
Johannes Berg79c97e92009-07-07 03:56:12 +02005765 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005766 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005767
Johannes Berg463d0182009-07-14 00:33:35 +02005768 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005769 nl80211_send_scan_start(rdev, wdev);
5770 if (wdev->netdev)
5771 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005772 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005773 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005774 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005775 kfree(request);
5776 }
Johannes Berg3b858752009-03-12 09:55:09 +01005777
Johannes Bergf9f47522013-03-19 15:04:07 +01005778 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005779 return err;
5780}
5781
Luciano Coelho256da022014-11-10 16:13:46 +02005782static struct cfg80211_sched_scan_request *
Johannes Bergad2b26a2014-06-12 21:39:05 +02005783nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
Luciano Coelho256da022014-11-10 16:13:46 +02005784 struct nlattr **attrs)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005785{
5786 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005787 struct nlattr *attr;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005788 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005789 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005790 enum ieee80211_band band;
5791 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005792 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005793 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005794
Luciano Coelho256da022014-11-10 16:13:46 +02005795 if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
5796 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005797
Luciano Coelho256da022014-11-10 16:13:46 +02005798 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5799 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005800
Luciano Coelho256da022014-11-10 16:13:46 +02005801 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005802 if (interval == 0)
Luciano Coelho256da022014-11-10 16:13:46 +02005803 return ERR_PTR(-EINVAL);
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005804
Luciano Coelho256da022014-11-10 16:13:46 +02005805 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005806 n_channels = validate_scan_freqs(
Luciano Coelho256da022014-11-10 16:13:46 +02005807 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005808 if (!n_channels)
Luciano Coelho256da022014-11-10 16:13:46 +02005809 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005810 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005811 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005812 }
5813
Luciano Coelho256da022014-11-10 16:13:46 +02005814 if (attrs[NL80211_ATTR_SCAN_SSIDS])
5815 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
Luciano Coelho807f8a82011-05-11 17:09:35 +03005816 tmp)
5817 n_ssids++;
5818
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005819 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho256da022014-11-10 16:13:46 +02005820 return ERR_PTR(-EINVAL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005821
Johannes Bergea73cbc2014-01-24 10:53:53 +01005822 /*
5823 * First, count the number of 'real' matchsets. Due to an issue with
5824 * the old implementation, matchsets containing only the RSSI attribute
5825 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5826 * RSSI for all matchsets, rather than their own matchset for reporting
5827 * all APs with a strong RSSI. This is needed to be compatible with
5828 * older userspace that treated a matchset with only the RSSI as the
5829 * global RSSI for all other matchsets - if there are other matchsets.
5830 */
Luciano Coelho256da022014-11-10 16:13:46 +02005831 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005832 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02005833 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005834 tmp) {
5835 struct nlattr *rssi;
5836
5837 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5838 nla_data(attr), nla_len(attr),
5839 nl80211_match_policy);
5840 if (err)
Luciano Coelho256da022014-11-10 16:13:46 +02005841 return ERR_PTR(err);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005842 /* add other standalone attributes here */
5843 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5844 n_match_sets++;
5845 continue;
5846 }
5847 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5848 if (rssi)
5849 default_match_rssi = nla_get_s32(rssi);
5850 }
5851 }
5852
5853 /* However, if there's no other matchset, add the RSSI one */
5854 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5855 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005856
5857 if (n_match_sets > wiphy->max_match_sets)
Luciano Coelho256da022014-11-10 16:13:46 +02005858 return ERR_PTR(-EINVAL);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005859
Luciano Coelho256da022014-11-10 16:13:46 +02005860 if (attrs[NL80211_ATTR_IE])
5861 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005862 else
5863 ie_len = 0;
5864
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005865 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho256da022014-11-10 16:13:46 +02005866 return ERR_PTR(-EINVAL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005867
Luciano Coelho807f8a82011-05-11 17:09:35 +03005868 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005869 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005870 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005871 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005872 + ie_len, GFP_KERNEL);
Luciano Coelho256da022014-11-10 16:13:46 +02005873 if (!request)
5874 return ERR_PTR(-ENOMEM);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005875
5876 if (n_ssids)
5877 request->ssids = (void *)&request->channels[n_channels];
5878 request->n_ssids = n_ssids;
5879 if (ie_len) {
5880 if (request->ssids)
5881 request->ie = (void *)(request->ssids + n_ssids);
5882 else
5883 request->ie = (void *)(request->channels + n_channels);
5884 }
5885
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005886 if (n_match_sets) {
5887 if (request->ie)
5888 request->match_sets = (void *)(request->ie + ie_len);
5889 else if (request->ssids)
5890 request->match_sets =
5891 (void *)(request->ssids + n_ssids);
5892 else
5893 request->match_sets =
5894 (void *)(request->channels + n_channels);
5895 }
5896 request->n_match_sets = n_match_sets;
5897
Luciano Coelho807f8a82011-05-11 17:09:35 +03005898 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02005899 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005900 /* user specified, bail out if channel not found */
5901 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02005902 attrs[NL80211_ATTR_SCAN_FREQUENCIES],
Luciano Coelho807f8a82011-05-11 17:09:35 +03005903 tmp) {
5904 struct ieee80211_channel *chan;
5905
5906 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5907
5908 if (!chan) {
5909 err = -EINVAL;
5910 goto out_free;
5911 }
5912
5913 /* ignore disabled channels */
5914 if (chan->flags & IEEE80211_CHAN_DISABLED)
5915 continue;
5916
5917 request->channels[i] = chan;
5918 i++;
5919 }
5920 } else {
5921 /* all channels */
5922 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5923 int j;
5924 if (!wiphy->bands[band])
5925 continue;
5926 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5927 struct ieee80211_channel *chan;
5928
5929 chan = &wiphy->bands[band]->channels[j];
5930
5931 if (chan->flags & IEEE80211_CHAN_DISABLED)
5932 continue;
5933
5934 request->channels[i] = chan;
5935 i++;
5936 }
5937 }
5938 }
5939
5940 if (!i) {
5941 err = -EINVAL;
5942 goto out_free;
5943 }
5944
5945 request->n_channels = i;
5946
5947 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02005948 if (attrs[NL80211_ATTR_SCAN_SSIDS]) {
5949 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
Luciano Coelho807f8a82011-05-11 17:09:35 +03005950 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005951 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005952 err = -EINVAL;
5953 goto out_free;
5954 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005955 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005956 memcpy(request->ssids[i].ssid, nla_data(attr),
5957 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005958 i++;
5959 }
5960 }
5961
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005962 i = 0;
Luciano Coelho256da022014-11-10 16:13:46 +02005963 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005964 nla_for_each_nested(attr,
Luciano Coelho256da022014-11-10 16:13:46 +02005965 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005966 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005967 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005968
Johannes Bergae811e22014-01-24 10:17:47 +01005969 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5970 nla_data(attr), nla_len(attr),
5971 nl80211_match_policy);
5972 if (err)
5973 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005974 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005975 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005976 if (WARN_ON(i >= n_match_sets)) {
5977 /* this indicates a programming error,
5978 * the loop above should have verified
5979 * things properly
5980 */
5981 err = -EINVAL;
5982 goto out_free;
5983 }
5984
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005985 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5986 err = -EINVAL;
5987 goto out_free;
5988 }
5989 memcpy(request->match_sets[i].ssid.ssid,
5990 nla_data(ssid), nla_len(ssid));
5991 request->match_sets[i].ssid.ssid_len =
5992 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005993 /* special attribute - old implemenation w/a */
5994 request->match_sets[i].rssi_thold =
5995 default_match_rssi;
5996 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5997 if (rssi)
5998 request->match_sets[i].rssi_thold =
5999 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03006000 }
6001 i++;
6002 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01006003
6004 /* there was no other matchset, so the RSSI one is alone */
6005 if (i == 0)
6006 request->match_sets[0].rssi_thold = default_match_rssi;
6007
6008 request->min_rssi_thold = INT_MAX;
6009 for (i = 0; i < n_match_sets; i++)
6010 request->min_rssi_thold =
6011 min(request->match_sets[i].rssi_thold,
6012 request->min_rssi_thold);
6013 } else {
6014 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03006015 }
6016
Johannes Berg9900e482014-02-04 21:01:25 +01006017 if (ie_len) {
6018 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006019 memcpy((void *)request->ie,
Luciano Coelho256da022014-11-10 16:13:46 +02006020 nla_data(attrs[NL80211_ATTR_IE]),
Luciano Coelho807f8a82011-05-11 17:09:35 +03006021 request->ie_len);
6022 }
6023
Luciano Coelho256da022014-11-10 16:13:46 +02006024 if (attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07006025 request->flags = nla_get_u32(
Luciano Coelho256da022014-11-10 16:13:46 +02006026 attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02006027 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6028 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07006029 err = -EOPNOTSUPP;
6030 goto out_free;
6031 }
Johannes Bergad2b26a2014-06-12 21:39:05 +02006032
6033 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6034 u32 flg = NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
6035
6036 if (!wdev) /* must be net-detect */
6037 flg = NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
6038
6039 if (!(wiphy->features & flg)) {
6040 err = -EOPNOTSUPP;
6041 goto out_free;
6042 }
6043
6044 if (wdev && wdev->current_bss) {
6045 err = -EOPNOTSUPP;
6046 goto out_free;
6047 }
6048
6049 err = nl80211_parse_random_mac(attrs, request->mac_addr,
6050 request->mac_addr_mask);
6051 if (err)
6052 goto out_free;
6053 }
Sam Leffler46856bb2012-10-11 21:03:32 -07006054 }
Sam Lefflered4737712012-10-11 21:03:31 -07006055
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03006056 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07006057 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006058
Luciano Coelho256da022014-11-10 16:13:46 +02006059 return request;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006060
6061out_free:
6062 kfree(request);
Luciano Coelho256da022014-11-10 16:13:46 +02006063 return ERR_PTR(err);
6064}
6065
6066static int nl80211_start_sched_scan(struct sk_buff *skb,
6067 struct genl_info *info)
6068{
6069 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6070 struct net_device *dev = info->user_ptr[1];
Johannes Bergad2b26a2014-06-12 21:39:05 +02006071 struct wireless_dev *wdev = dev->ieee80211_ptr;
Luciano Coelho256da022014-11-10 16:13:46 +02006072 int err;
6073
6074 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6075 !rdev->ops->sched_scan_start)
6076 return -EOPNOTSUPP;
6077
6078 if (rdev->sched_scan_req)
6079 return -EINPROGRESS;
6080
Johannes Bergad2b26a2014-06-12 21:39:05 +02006081 rdev->sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
Luciano Coelho256da022014-11-10 16:13:46 +02006082 info->attrs);
6083 err = PTR_ERR_OR_ZERO(rdev->sched_scan_req);
6084 if (err)
6085 goto out_err;
6086
6087 err = rdev_sched_scan_start(rdev, dev, rdev->sched_scan_req);
6088 if (err)
6089 goto out_free;
6090
6091 rdev->sched_scan_req->dev = dev;
6092 rdev->sched_scan_req->wiphy = &rdev->wiphy;
6093
6094 nl80211_send_sched_scan(rdev, dev,
6095 NL80211_CMD_START_SCHED_SCAN);
6096 return 0;
6097
6098out_free:
6099 kfree(rdev->sched_scan_req);
6100out_err:
6101 rdev->sched_scan_req = NULL;
Luciano Coelho807f8a82011-05-11 17:09:35 +03006102 return err;
6103}
6104
6105static int nl80211_stop_sched_scan(struct sk_buff *skb,
6106 struct genl_info *info)
6107{
6108 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6109
6110 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6111 !rdev->ops->sched_scan_stop)
6112 return -EOPNOTSUPP;
6113
Johannes Berg5fe231e2013-05-08 21:45:15 +02006114 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03006115}
6116
Simon Wunderlich04f39042013-02-08 18:16:19 +01006117static int nl80211_start_radar_detection(struct sk_buff *skb,
6118 struct genl_info *info)
6119{
6120 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6121 struct net_device *dev = info->user_ptr[1];
6122 struct wireless_dev *wdev = dev->ieee80211_ptr;
6123 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01006124 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01006125 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01006126 int err;
6127
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01006128 dfs_region = reg_get_dfs_region(wdev->wiphy);
6129 if (dfs_region == NL80211_DFS_UNSET)
6130 return -EINVAL;
6131
Simon Wunderlich04f39042013-02-08 18:16:19 +01006132 err = nl80211_parse_chandef(rdev, info, &chandef);
6133 if (err)
6134 return err;
6135
Simon Wunderlichff311bc2013-09-03 19:43:18 +02006136 if (netif_carrier_ok(dev))
6137 return -EBUSY;
6138
Simon Wunderlich04f39042013-02-08 18:16:19 +01006139 if (wdev->cac_started)
6140 return -EBUSY;
6141
Luciano Coelho2beb6da2014-02-18 11:40:36 +02006142 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
Luciano Coelho00ec75f2014-05-15 13:05:39 +03006143 wdev->iftype);
Simon Wunderlich04f39042013-02-08 18:16:19 +01006144 if (err < 0)
6145 return err;
6146
6147 if (err == 0)
6148 return -EINVAL;
6149
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01006150 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01006151 return -EINVAL;
6152
6153 if (!rdev->ops->start_radar_detection)
6154 return -EOPNOTSUPP;
6155
Janusz Dziedzic31559f32014-02-21 19:46:13 +01006156 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
6157 if (WARN_ON(!cac_time_ms))
6158 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
6159
6160 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
6161 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01006162 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01006163 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01006164 wdev->cac_started = true;
6165 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01006166 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01006167 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01006168 return err;
6169}
6170
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006171static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
6172{
6173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6174 struct net_device *dev = info->user_ptr[1];
6175 struct wireless_dev *wdev = dev->ieee80211_ptr;
6176 struct cfg80211_csa_settings params;
6177 /* csa_attrs is defined static to avoid waste of stack size - this
6178 * function is called under RTNL lock, so this should not be a problem.
6179 */
6180 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006181 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006182 bool need_new_beacon = false;
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006183 int len, i;
Luciano Coelho252e07c2014-10-08 09:48:34 +03006184 u32 cs_count;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006185
6186 if (!rdev->ops->channel_switch ||
6187 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
6188 return -EOPNOTSUPP;
6189
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006190 switch (dev->ieee80211_ptr->iftype) {
6191 case NL80211_IFTYPE_AP:
6192 case NL80211_IFTYPE_P2P_GO:
6193 need_new_beacon = true;
6194
6195 /* useless if AP is not running */
6196 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01006197 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006198 break;
6199 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01006200 if (!wdev->ssid_len)
6201 return -ENOTCONN;
6202 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07006203 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01006204 if (!wdev->mesh_id_len)
6205 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006206 break;
6207 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006208 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006209 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006210
6211 memset(&params, 0, sizeof(params));
6212
6213 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6214 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
6215 return -EINVAL;
6216
6217 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02006218 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006219 return -EINVAL;
6220
Luciano Coelho252e07c2014-10-08 09:48:34 +03006221 /* Even though the attribute is u32, the specification says
6222 * u8, so let's make sure we don't overflow.
6223 */
6224 cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
6225 if (cs_count > 255)
6226 return -EINVAL;
6227
6228 params.count = cs_count;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006229
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006230 if (!need_new_beacon)
6231 goto skip_beacons;
6232
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006233 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
6234 if (err)
6235 return err;
6236
6237 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
6238 info->attrs[NL80211_ATTR_CSA_IES],
6239 nl80211_policy);
6240 if (err)
6241 return err;
6242
6243 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
6244 if (err)
6245 return err;
6246
6247 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
6248 return -EINVAL;
6249
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006250 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6251 if (!len || (len % sizeof(u16)))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006252 return -EINVAL;
6253
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006254 params.n_counter_offsets_beacon = len / sizeof(u16);
6255 if (rdev->wiphy.max_num_csa_counters &&
6256 (params.n_counter_offsets_beacon >
6257 rdev->wiphy.max_num_csa_counters))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006258 return -EINVAL;
6259
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006260 params.counter_offsets_beacon =
6261 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6262
6263 /* sanity checks - counters should fit and be the same */
6264 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
6265 u16 offset = params.counter_offsets_beacon[i];
6266
6267 if (offset >= params.beacon_csa.tail_len)
6268 return -EINVAL;
6269
6270 if (params.beacon_csa.tail[offset] != params.count)
6271 return -EINVAL;
6272 }
6273
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006274 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006275 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6276 if (!len || (len % sizeof(u16)))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006277 return -EINVAL;
6278
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006279 params.n_counter_offsets_presp = len / sizeof(u16);
6280 if (rdev->wiphy.max_num_csa_counters &&
6281 (params.n_counter_offsets_beacon >
6282 rdev->wiphy.max_num_csa_counters))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006283 return -EINVAL;
Andrei Otcheretianski9a774c72014-05-09 14:11:46 +03006284
6285 params.counter_offsets_presp =
6286 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6287
6288 /* sanity checks - counters should fit and be the same */
6289 for (i = 0; i < params.n_counter_offsets_presp; i++) {
6290 u16 offset = params.counter_offsets_presp[i];
6291
6292 if (offset >= params.beacon_csa.probe_resp_len)
6293 return -EINVAL;
6294
6295 if (params.beacon_csa.probe_resp[offset] !=
6296 params.count)
6297 return -EINVAL;
6298 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006299 }
6300
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02006301skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006302 err = nl80211_parse_chandef(rdev, info, &params.chandef);
6303 if (err)
6304 return err;
6305
Ilan Peer174e0cd2014-02-23 09:13:01 +02006306 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
6307 wdev->iftype))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006308 return -EINVAL;
6309
Luciano Coelho2beb6da2014-02-18 11:40:36 +02006310 err = cfg80211_chandef_dfs_required(wdev->wiphy,
6311 &params.chandef,
6312 wdev->iftype);
6313 if (err < 0)
6314 return err;
6315
Fabian Frederickdcc6c2f2014-10-25 17:57:35 +02006316 if (err > 0)
Luciano Coelho2beb6da2014-02-18 11:40:36 +02006317 params.radar_required = true;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006318
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006319 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
6320 params.block_tx = true;
6321
Simon Wunderlichc56589e2013-11-21 18:19:49 +01006322 wdev_lock(wdev);
6323 err = rdev_channel_switch(rdev, dev, &params);
6324 wdev_unlock(wdev);
6325
6326 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02006327}
6328
Johannes Berg9720bb32011-06-21 09:45:33 +02006329static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
6330 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01006331 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02006332 struct wireless_dev *wdev,
6333 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01006334{
Johannes Berg48ab9052009-07-10 18:42:31 +02006335 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01006336 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01006337 void *hdr;
6338 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02006339
6340 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006341
Eric W. Biederman15e47302012-09-07 20:12:54 +00006342 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01006343 NL80211_CMD_NEW_SCAN_RESULTS);
6344 if (!hdr)
6345 return -1;
6346
Johannes Berg9720bb32011-06-21 09:45:33 +02006347 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6348
Johannes Berg97990a02013-04-19 01:02:55 +02006349 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6350 goto nla_put_failure;
6351 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04006352 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6353 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02006354 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6355 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006356
6357 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6358 if (!bss)
6359 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006360 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006361 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006362 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006363
6364 rcu_read_lock();
Johannes Berg0e227082014-08-12 20:34:30 +02006365 /* indicate whether we have probe response data or not */
6366 if (rcu_access_pointer(res->proberesp_ies) &&
6367 nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
6368 goto fail_unlock_rcu;
6369
6370 /* this pointer prefers to be pointed to probe response data
6371 * but is always valid
6372 */
Johannes Berg9caf0362012-11-29 01:25:20 +01006373 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006374 if (ies) {
6375 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6376 goto fail_unlock_rcu;
Johannes Berg8cef2c92013-02-05 16:54:31 +01006377 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6378 ies->len, ies->data))
6379 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006380 }
Johannes Berg0e227082014-08-12 20:34:30 +02006381
6382 /* and this pointer is always (unless driver didn't know) beacon data */
Johannes Berg9caf0362012-11-29 01:25:20 +01006383 ies = rcu_dereference(res->beacon_ies);
Johannes Berg0e227082014-08-12 20:34:30 +02006384 if (ies && ies->from_beacon) {
6385 if (nla_put_u64(msg, NL80211_BSS_BEACON_TSF, ies->tsf))
Johannes Berg8cef2c92013-02-05 16:54:31 +01006386 goto fail_unlock_rcu;
6387 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6388 ies->len, ies->data))
6389 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006390 }
6391 rcu_read_unlock();
6392
David S. Miller9360ffd2012-03-29 04:41:26 -04006393 if (res->beacon_interval &&
6394 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6395 goto nla_put_failure;
6396 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6397 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006398 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006399 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6400 jiffies_to_msecs(jiffies - intbss->ts)))
6401 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006402
Johannes Berg77965c92009-02-18 18:45:06 +01006403 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006404 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006405 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6406 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006407 break;
6408 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006409 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6410 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006411 break;
6412 default:
6413 break;
6414 }
6415
Johannes Berg48ab9052009-07-10 18:42:31 +02006416 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006417 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006418 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006419 if (intbss == wdev->current_bss &&
6420 nla_put_u32(msg, NL80211_BSS_STATUS,
6421 NL80211_BSS_STATUS_ASSOCIATED))
6422 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006423 break;
6424 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006425 if (intbss == wdev->current_bss &&
6426 nla_put_u32(msg, NL80211_BSS_STATUS,
6427 NL80211_BSS_STATUS_IBSS_JOINED))
6428 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006429 break;
6430 default:
6431 break;
6432 }
6433
Johannes Berg2a519312009-02-10 21:25:55 +01006434 nla_nest_end(msg, bss);
6435
6436 return genlmsg_end(msg, hdr);
6437
Johannes Berg8cef2c92013-02-05 16:54:31 +01006438 fail_unlock_rcu:
6439 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006440 nla_put_failure:
6441 genlmsg_cancel(msg, hdr);
6442 return -EMSGSIZE;
6443}
6444
Johannes Berg97990a02013-04-19 01:02:55 +02006445static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006446{
Johannes Berg48ab9052009-07-10 18:42:31 +02006447 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006448 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006449 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006450 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006451 int err;
6452
Johannes Berg97990a02013-04-19 01:02:55 +02006453 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006454 if (err)
6455 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006456
Johannes Berg48ab9052009-07-10 18:42:31 +02006457 wdev_lock(wdev);
6458 spin_lock_bh(&rdev->bss_lock);
6459 cfg80211_bss_expire(rdev);
6460
Johannes Berg9720bb32011-06-21 09:45:33 +02006461 cb->seq = rdev->bss_generation;
6462
Johannes Berg48ab9052009-07-10 18:42:31 +02006463 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006464 if (++idx <= start)
6465 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006466 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006467 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006468 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006469 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006470 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006471 }
6472 }
6473
Johannes Berg48ab9052009-07-10 18:42:31 +02006474 spin_unlock_bh(&rdev->bss_lock);
6475 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006476
Johannes Berg97990a02013-04-19 01:02:55 +02006477 cb->args[2] = idx;
6478 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006479
Johannes Berg67748892010-10-04 21:14:06 +02006480 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006481}
6482
Eric W. Biederman15e47302012-09-07 20:12:54 +00006483static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006484 int flags, struct net_device *dev,
6485 struct survey_info *survey)
6486{
6487 void *hdr;
6488 struct nlattr *infoattr;
6489
Eric W. Biederman15e47302012-09-07 20:12:54 +00006490 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006491 NL80211_CMD_NEW_SURVEY_RESULTS);
6492 if (!hdr)
6493 return -ENOMEM;
6494
David S. Miller9360ffd2012-03-29 04:41:26 -04006495 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6496 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006497
6498 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6499 if (!infoattr)
6500 goto nla_put_failure;
6501
David S. Miller9360ffd2012-03-29 04:41:26 -04006502 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6503 survey->channel->center_freq))
6504 goto nla_put_failure;
6505
6506 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6507 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6508 goto nla_put_failure;
6509 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6510 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6511 goto nla_put_failure;
6512 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6513 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6514 survey->channel_time))
6515 goto nla_put_failure;
6516 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6517 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6518 survey->channel_time_busy))
6519 goto nla_put_failure;
6520 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6521 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6522 survey->channel_time_ext_busy))
6523 goto nla_put_failure;
6524 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6525 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6526 survey->channel_time_rx))
6527 goto nla_put_failure;
6528 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6529 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6530 survey->channel_time_tx))
6531 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006532
6533 nla_nest_end(msg, infoattr);
6534
6535 return genlmsg_end(msg, hdr);
6536
6537 nla_put_failure:
6538 genlmsg_cancel(msg, hdr);
6539 return -EMSGSIZE;
6540}
6541
6542static int nl80211_dump_survey(struct sk_buff *skb,
6543 struct netlink_callback *cb)
6544{
6545 struct survey_info survey;
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006546 struct cfg80211_registered_device *rdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006547 struct wireless_dev *wdev;
6548 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006549 int res;
6550
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006551 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006552 if (res)
6553 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006554
Johannes Berg97990a02013-04-19 01:02:55 +02006555 if (!wdev->netdev) {
6556 res = -EINVAL;
6557 goto out_err;
6558 }
6559
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006560 if (!rdev->ops->dump_survey) {
Holger Schurig61fa7132009-11-11 12:25:40 +01006561 res = -EOPNOTSUPP;
6562 goto out_err;
6563 }
6564
6565 while (1) {
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006566 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006567 if (res == -ENOENT)
6568 break;
6569 if (res)
6570 goto out_err;
6571
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006572 /* Survey without a channel doesn't make sense */
6573 if (!survey.channel) {
6574 res = -EINVAL;
6575 goto out;
6576 }
6577
Johannes Bergea9eba62014-11-21 15:24:31 +01006578 if (survey.channel->flags & IEEE80211_CHAN_DISABLED) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006579 survey_idx++;
6580 continue;
6581 }
6582
Holger Schurig61fa7132009-11-11 12:25:40 +01006583 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006584 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006585 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006586 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006587 goto out;
6588 survey_idx++;
6589 }
6590
6591 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006592 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006593 res = skb->len;
6594 out_err:
Zhao, Gang1b8ec872014-04-21 12:53:02 +08006595 nl80211_finish_wdev_dump(rdev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006596 return res;
6597}
6598
Samuel Ortizb23aa672009-07-01 21:26:54 +02006599static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6600{
6601 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6602 NL80211_WPA_VERSION_2));
6603}
6604
Jouni Malinen636a5d32009-03-19 13:39:22 +02006605static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6606{
Johannes Berg4c476992010-10-04 21:36:35 +02006607 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6608 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006609 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006610 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6611 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006612 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006613 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006614 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006615
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006616 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6617 return -EINVAL;
6618
6619 if (!info->attrs[NL80211_ATTR_MAC])
6620 return -EINVAL;
6621
Jouni Malinen17780922009-03-27 20:52:47 +02006622 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6623 return -EINVAL;
6624
Johannes Berg19957bb2009-07-02 17:20:43 +02006625 if (!info->attrs[NL80211_ATTR_SSID])
6626 return -EINVAL;
6627
6628 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6629 return -EINVAL;
6630
Johannes Bergfffd0932009-07-08 14:22:54 +02006631 err = nl80211_parse_key(info, &key);
6632 if (err)
6633 return err;
6634
6635 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006636 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6637 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006638 if (!key.p.key || !key.p.key_len)
6639 return -EINVAL;
6640 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6641 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6642 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6643 key.p.key_len != WLAN_KEY_LEN_WEP104))
6644 return -EINVAL;
6645 if (key.idx > 4)
6646 return -EINVAL;
6647 } else {
6648 key.p.key_len = 0;
6649 key.p.key = NULL;
6650 }
6651
Johannes Bergafea0b72010-08-10 09:46:42 +02006652 if (key.idx >= 0) {
6653 int i;
6654 bool ok = false;
6655 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6656 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6657 ok = true;
6658 break;
6659 }
6660 }
Johannes Berg4c476992010-10-04 21:36:35 +02006661 if (!ok)
6662 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006663 }
6664
Johannes Berg4c476992010-10-04 21:36:35 +02006665 if (!rdev->ops->auth)
6666 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006667
Johannes Berg074ac8d2010-09-16 14:58:22 +02006668 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006669 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6670 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006671
Johannes Berg19957bb2009-07-02 17:20:43 +02006672 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006673 chan = nl80211_get_valid_chan(&rdev->wiphy,
6674 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6675 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006676 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006677
Johannes Berg19957bb2009-07-02 17:20:43 +02006678 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6679 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6680
6681 if (info->attrs[NL80211_ATTR_IE]) {
6682 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6683 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6684 }
6685
6686 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006687 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006688 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006689
Jouni Malinene39e5b52012-09-30 19:29:39 +03006690 if (auth_type == NL80211_AUTHTYPE_SAE &&
6691 !info->attrs[NL80211_ATTR_SAE_DATA])
6692 return -EINVAL;
6693
6694 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6695 if (auth_type != NL80211_AUTHTYPE_SAE)
6696 return -EINVAL;
6697 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6698 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6699 /* need to include at least Auth Transaction and Status Code */
6700 if (sae_data_len < 4)
6701 return -EINVAL;
6702 }
6703
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006704 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6705
Johannes Berg95de8172012-01-20 13:55:25 +01006706 /*
6707 * Since we no longer track auth state, ignore
6708 * requests to only change local state.
6709 */
6710 if (local_state_change)
6711 return 0;
6712
Johannes Berg91bf9b22013-05-15 17:44:01 +02006713 wdev_lock(dev->ieee80211_ptr);
6714 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6715 ssid, ssid_len, ie, ie_len,
6716 key.p.key, key.p.key_len, key.idx,
6717 sae_data, sae_data_len);
6718 wdev_unlock(dev->ieee80211_ptr);
6719 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006720}
6721
Johannes Bergc0692b82010-08-27 14:26:53 +03006722static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6723 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006724 struct cfg80211_crypto_settings *settings,
6725 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006726{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006727 memset(settings, 0, sizeof(*settings));
6728
Samuel Ortizb23aa672009-07-01 21:26:54 +02006729 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6730
Johannes Bergc0692b82010-08-27 14:26:53 +03006731 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6732 u16 proto;
6733 proto = nla_get_u16(
6734 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6735 settings->control_port_ethertype = cpu_to_be16(proto);
6736 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6737 proto != ETH_P_PAE)
6738 return -EINVAL;
6739 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6740 settings->control_port_no_encrypt = true;
6741 } else
6742 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6743
Samuel Ortizb23aa672009-07-01 21:26:54 +02006744 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6745 void *data;
6746 int len, i;
6747
6748 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6749 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6750 settings->n_ciphers_pairwise = len / sizeof(u32);
6751
6752 if (len % sizeof(u32))
6753 return -EINVAL;
6754
Johannes Berg3dc27d22009-07-02 21:36:37 +02006755 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006756 return -EINVAL;
6757
6758 memcpy(settings->ciphers_pairwise, data, len);
6759
6760 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006761 if (!cfg80211_supported_cipher_suite(
6762 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006763 settings->ciphers_pairwise[i]))
6764 return -EINVAL;
6765 }
6766
6767 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6768 settings->cipher_group =
6769 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006770 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6771 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006772 return -EINVAL;
6773 }
6774
6775 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6776 settings->wpa_versions =
6777 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6778 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6779 return -EINVAL;
6780 }
6781
6782 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6783 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006784 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006785
6786 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6787 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6788 settings->n_akm_suites = len / sizeof(u32);
6789
6790 if (len % sizeof(u32))
6791 return -EINVAL;
6792
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006793 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6794 return -EINVAL;
6795
Samuel Ortizb23aa672009-07-01 21:26:54 +02006796 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006797 }
6798
6799 return 0;
6800}
6801
Jouni Malinen636a5d32009-03-19 13:39:22 +02006802static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6803{
Johannes Berg4c476992010-10-04 21:36:35 +02006804 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6805 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006806 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006807 struct cfg80211_assoc_request req = {};
6808 const u8 *bssid, *ssid;
6809 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006810
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006811 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6812 return -EINVAL;
6813
6814 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006815 !info->attrs[NL80211_ATTR_SSID] ||
6816 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006817 return -EINVAL;
6818
Johannes Berg4c476992010-10-04 21:36:35 +02006819 if (!rdev->ops->assoc)
6820 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006821
Johannes Berg074ac8d2010-09-16 14:58:22 +02006822 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006823 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6824 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006825
Johannes Berg19957bb2009-07-02 17:20:43 +02006826 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006827
Jouni Malinen664834d2014-01-15 00:01:44 +02006828 chan = nl80211_get_valid_chan(&rdev->wiphy,
6829 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6830 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006831 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006832
Johannes Berg19957bb2009-07-02 17:20:43 +02006833 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6834 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006835
6836 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006837 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6838 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006839 }
6840
Jouni Malinendc6382c2009-05-06 22:09:37 +03006841 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006842 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006843 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006844 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006845 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006846 else if (mfp != NL80211_MFP_NO)
6847 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006848 }
6849
Johannes Berg3e5d7642009-07-07 14:37:26 +02006850 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006851 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006852
Ben Greear7e7c8922011-11-18 11:31:59 -08006853 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006854 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006855
6856 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006857 memcpy(&req.ht_capa_mask,
6858 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6859 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006860
6861 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006862 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006863 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006864 memcpy(&req.ht_capa,
6865 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6866 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006867 }
6868
Johannes Bergee2aca32013-02-21 17:36:01 +01006869 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006870 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006871
6872 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006873 memcpy(&req.vht_capa_mask,
6874 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6875 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006876
6877 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006878 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006879 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006880 memcpy(&req.vht_capa,
6881 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6882 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006883 }
6884
Assaf Kraussbab5ab72014-09-03 15:25:01 +03006885 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
6886 if (!(rdev->wiphy.features &
6887 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
6888 !(rdev->wiphy.features & NL80211_FEATURE_QUIET))
6889 return -EINVAL;
6890 req.flags |= ASSOC_REQ_USE_RRM;
6891 }
6892
Johannes Bergf62fab72013-02-21 20:09:09 +01006893 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006894 if (!err) {
6895 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006896 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6897 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006898 wdev_unlock(dev->ieee80211_ptr);
6899 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006900
Jouni Malinen636a5d32009-03-19 13:39:22 +02006901 return err;
6902}
6903
6904static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6905{
Johannes Berg4c476992010-10-04 21:36:35 +02006906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6907 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006908 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006909 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006910 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006911 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006912
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006913 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6914 return -EINVAL;
6915
6916 if (!info->attrs[NL80211_ATTR_MAC])
6917 return -EINVAL;
6918
6919 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6920 return -EINVAL;
6921
Johannes Berg4c476992010-10-04 21:36:35 +02006922 if (!rdev->ops->deauth)
6923 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006924
Johannes Berg074ac8d2010-09-16 14:58:22 +02006925 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006926 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6927 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006928
Johannes Berg19957bb2009-07-02 17:20:43 +02006929 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006930
Johannes Berg19957bb2009-07-02 17:20:43 +02006931 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6932 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006933 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006934 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006935 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006936
6937 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006938 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6939 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006940 }
6941
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006942 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6943
Johannes Berg91bf9b22013-05-15 17:44:01 +02006944 wdev_lock(dev->ieee80211_ptr);
6945 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6946 local_state_change);
6947 wdev_unlock(dev->ieee80211_ptr);
6948 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006949}
6950
6951static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6952{
Johannes Berg4c476992010-10-04 21:36:35 +02006953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6954 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006955 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006956 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006957 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006958 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006959
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006960 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6961 return -EINVAL;
6962
6963 if (!info->attrs[NL80211_ATTR_MAC])
6964 return -EINVAL;
6965
6966 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6967 return -EINVAL;
6968
Johannes Berg4c476992010-10-04 21:36:35 +02006969 if (!rdev->ops->disassoc)
6970 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006971
Johannes Berg074ac8d2010-09-16 14:58:22 +02006972 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006973 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6974 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006975
Johannes Berg19957bb2009-07-02 17:20:43 +02006976 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006977
Johannes Berg19957bb2009-07-02 17:20:43 +02006978 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6979 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006980 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006981 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006982 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006983
6984 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006985 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6986 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006987 }
6988
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006989 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6990
Johannes Berg91bf9b22013-05-15 17:44:01 +02006991 wdev_lock(dev->ieee80211_ptr);
6992 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6993 local_state_change);
6994 wdev_unlock(dev->ieee80211_ptr);
6995 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006996}
6997
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006998static bool
6999nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
7000 int mcast_rate[IEEE80211_NUM_BANDS],
7001 int rateval)
7002{
7003 struct wiphy *wiphy = &rdev->wiphy;
7004 bool found = false;
7005 int band, i;
7006
7007 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
7008 struct ieee80211_supported_band *sband;
7009
7010 sband = wiphy->bands[band];
7011 if (!sband)
7012 continue;
7013
7014 for (i = 0; i < sband->n_bitrates; i++) {
7015 if (sband->bitrates[i].bitrate == rateval) {
7016 mcast_rate[band] = i + 1;
7017 found = true;
7018 break;
7019 }
7020 }
7021 }
7022
7023 return found;
7024}
7025
Johannes Berg04a773a2009-04-19 21:24:32 +02007026static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
7027{
Johannes Berg4c476992010-10-04 21:36:35 +02007028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7029 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02007030 struct cfg80211_ibss_params ibss;
7031 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007032 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02007033 int err;
7034
Johannes Berg8e30bc52009-04-22 17:45:38 +02007035 memset(&ibss, 0, sizeof(ibss));
7036
Johannes Berg04a773a2009-04-19 21:24:32 +02007037 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7038 return -EINVAL;
7039
Johannes Berg683b6d32012-11-08 21:25:48 +01007040 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02007041 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7042 return -EINVAL;
7043
Johannes Berg8e30bc52009-04-22 17:45:38 +02007044 ibss.beacon_interval = 100;
7045
7046 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7047 ibss.beacon_interval =
7048 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7049 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
7050 return -EINVAL;
7051 }
7052
Johannes Berg4c476992010-10-04 21:36:35 +02007053 if (!rdev->ops->join_ibss)
7054 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007055
Johannes Berg4c476992010-10-04 21:36:35 +02007056 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7057 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007058
Johannes Berg79c97e92009-07-07 03:56:12 +02007059 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02007060
Johannes Berg39193492011-09-16 13:45:25 +02007061 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02007062 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02007063
7064 if (!is_valid_ether_addr(ibss.bssid))
7065 return -EINVAL;
7066 }
Johannes Berg04a773a2009-04-19 21:24:32 +02007067 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7068 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7069
7070 if (info->attrs[NL80211_ATTR_IE]) {
7071 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7072 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7073 }
7074
Johannes Berg683b6d32012-11-08 21:25:48 +01007075 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
7076 if (err)
7077 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01007078
Ilan Peer174e0cd2014-02-23 09:13:01 +02007079 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
7080 NL80211_IFTYPE_ADHOC))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01007081 return -EINVAL;
7082
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02007083 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02007084 case NL80211_CHAN_WIDTH_5:
7085 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02007086 case NL80211_CHAN_WIDTH_20_NOHT:
7087 break;
7088 case NL80211_CHAN_WIDTH_20:
7089 case NL80211_CHAN_WIDTH_40:
7090 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
7091 break;
7092 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01007093 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02007094 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01007095
Johannes Berg04a773a2009-04-19 21:24:32 +02007096 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02007097 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02007098
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007099 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7100 u8 *rates =
7101 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7102 int n_rates =
7103 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7104 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01007105 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007106
Johannes Berg34850ab2011-07-18 18:08:35 +02007107 err = ieee80211_get_ratemask(sband, rates, n_rates,
7108 &ibss.basic_rates);
7109 if (err)
7110 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007111 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01007112
Simon Wunderlich803768f2013-06-28 10:39:58 +02007113 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7114 memcpy(&ibss.ht_capa_mask,
7115 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7116 sizeof(ibss.ht_capa_mask));
7117
7118 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7119 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7120 return -EINVAL;
7121 memcpy(&ibss.ht_capa,
7122 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7123 sizeof(ibss.ht_capa));
7124 }
7125
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01007126 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7127 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
7128 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7129 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03007130
Johannes Berg4c476992010-10-04 21:36:35 +02007131 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05307132 bool no_ht = false;
7133
Johannes Berg4c476992010-10-04 21:36:35 +02007134 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307135 info->attrs[NL80211_ATTR_KEYS],
7136 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02007137 if (IS_ERR(connkeys))
7138 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05307139
Johannes Berg3d9d1d62012-11-08 23:14:50 +01007140 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
7141 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05307142 kfree(connkeys);
7143 return -EINVAL;
7144 }
Johannes Berg4c476992010-10-04 21:36:35 +02007145 }
Johannes Berg04a773a2009-04-19 21:24:32 +02007146
Antonio Quartulli267335d2012-01-31 20:25:47 +01007147 ibss.control_port =
7148 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
7149
Simon Wunderlich5336fa82013-10-07 18:41:05 +02007150 ibss.userspace_handles_dfs =
7151 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
7152
Johannes Berg4c476992010-10-04 21:36:35 +02007153 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007154 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +03007155 kzfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02007156 return err;
7157}
7158
7159static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
7160{
Johannes Berg4c476992010-10-04 21:36:35 +02007161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7162 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02007163
Johannes Berg4c476992010-10-04 21:36:35 +02007164 if (!rdev->ops->leave_ibss)
7165 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007166
Johannes Berg4c476992010-10-04 21:36:35 +02007167 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7168 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02007169
Johannes Berg4c476992010-10-04 21:36:35 +02007170 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02007171}
7172
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007173static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
7174{
7175 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7176 struct net_device *dev = info->user_ptr[1];
7177 int mcast_rate[IEEE80211_NUM_BANDS];
7178 u32 nla_rate;
7179 int err;
7180
7181 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
7182 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7183 return -EOPNOTSUPP;
7184
7185 if (!rdev->ops->set_mcast_rate)
7186 return -EOPNOTSUPP;
7187
7188 memset(mcast_rate, 0, sizeof(mcast_rate));
7189
7190 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
7191 return -EINVAL;
7192
7193 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
7194 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
7195 return -EINVAL;
7196
7197 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
7198
7199 return err;
7200}
7201
Johannes Bergad7e7182013-11-13 13:37:47 +01007202static struct sk_buff *
7203__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7204 int approxlen, u32 portid, u32 seq,
7205 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01007206 enum nl80211_attrs attr,
7207 const struct nl80211_vendor_cmd_info *info,
7208 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01007209{
7210 struct sk_buff *skb;
7211 void *hdr;
7212 struct nlattr *data;
7213
7214 skb = nlmsg_new(approxlen + 100, gfp);
7215 if (!skb)
7216 return NULL;
7217
7218 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
7219 if (!hdr) {
7220 kfree_skb(skb);
7221 return NULL;
7222 }
7223
7224 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
7225 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01007226
7227 if (info) {
7228 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
7229 info->vendor_id))
7230 goto nla_put_failure;
7231 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
7232 info->subcmd))
7233 goto nla_put_failure;
7234 }
7235
Johannes Bergad7e7182013-11-13 13:37:47 +01007236 data = nla_nest_start(skb, attr);
7237
7238 ((void **)skb->cb)[0] = rdev;
7239 ((void **)skb->cb)[1] = hdr;
7240 ((void **)skb->cb)[2] = data;
7241
7242 return skb;
7243
7244 nla_put_failure:
7245 kfree_skb(skb);
7246 return NULL;
7247}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007248
Johannes Berge03ad6e2014-01-01 17:22:30 +01007249struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
7250 enum nl80211_commands cmd,
7251 enum nl80211_attrs attr,
7252 int vendor_event_idx,
7253 int approxlen, gfp_t gfp)
7254{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08007255 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berge03ad6e2014-01-01 17:22:30 +01007256 const struct nl80211_vendor_cmd_info *info;
7257
7258 switch (cmd) {
7259 case NL80211_CMD_TESTMODE:
7260 if (WARN_ON(vendor_event_idx != -1))
7261 return NULL;
7262 info = NULL;
7263 break;
7264 case NL80211_CMD_VENDOR:
7265 if (WARN_ON(vendor_event_idx < 0 ||
7266 vendor_event_idx >= wiphy->n_vendor_events))
7267 return NULL;
7268 info = &wiphy->vendor_events[vendor_event_idx];
7269 break;
7270 default:
7271 WARN_ON(1);
7272 return NULL;
7273 }
7274
7275 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
7276 cmd, attr, info, gfp);
7277}
7278EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
7279
7280void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
7281{
7282 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
7283 void *hdr = ((void **)skb->cb)[1];
7284 struct nlattr *data = ((void **)skb->cb)[2];
7285 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
7286
Johannes Bergbd8c78e2014-07-30 14:55:26 +02007287 /* clear CB data for netlink core to own from now on */
7288 memset(skb->cb, 0, sizeof(skb->cb));
7289
Johannes Berge03ad6e2014-01-01 17:22:30 +01007290 nla_nest_end(skb, data);
7291 genlmsg_end(skb, hdr);
7292
7293 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
7294 mcgrp = NL80211_MCGRP_VENDOR;
7295
7296 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
7297 mcgrp, gfp);
7298}
7299EXPORT_SYMBOL(__cfg80211_send_event_skb);
7300
Johannes Bergaff89a92009-07-01 21:26:51 +02007301#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02007302static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
7303{
Johannes Berg4c476992010-10-04 21:36:35 +02007304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03007305 struct wireless_dev *wdev =
7306 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02007307 int err;
7308
David Spinadelfc73f112013-07-31 18:04:15 +03007309 if (!rdev->ops->testmode_cmd)
7310 return -EOPNOTSUPP;
7311
7312 if (IS_ERR(wdev)) {
7313 err = PTR_ERR(wdev);
7314 if (err != -EINVAL)
7315 return err;
7316 wdev = NULL;
7317 } else if (wdev->wiphy != &rdev->wiphy) {
7318 return -EINVAL;
7319 }
7320
Johannes Bergaff89a92009-07-01 21:26:51 +02007321 if (!info->attrs[NL80211_ATTR_TESTDATA])
7322 return -EINVAL;
7323
Johannes Bergad7e7182013-11-13 13:37:47 +01007324 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03007325 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02007326 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
7327 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01007328 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02007329
Johannes Bergaff89a92009-07-01 21:26:51 +02007330 return err;
7331}
7332
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007333static int nl80211_testmode_dump(struct sk_buff *skb,
7334 struct netlink_callback *cb)
7335{
Johannes Berg00918d32011-12-13 17:22:05 +01007336 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007337 int err;
7338 long phy_idx;
7339 void *data = NULL;
7340 int data_len = 0;
7341
Johannes Berg5fe231e2013-05-08 21:45:15 +02007342 rtnl_lock();
7343
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007344 if (cb->args[0]) {
7345 /*
7346 * 0 is a valid index, but not valid for args[0],
7347 * so we need to offset by 1.
7348 */
7349 phy_idx = cb->args[0] - 1;
7350 } else {
7351 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
7352 nl80211_fam.attrbuf, nl80211_fam.maxattr,
7353 nl80211_policy);
7354 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02007355 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01007356
Johannes Berg2bd7e352012-06-15 14:23:16 +02007357 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
7358 nl80211_fam.attrbuf);
7359 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007360 err = PTR_ERR(rdev);
7361 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01007362 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02007363 phy_idx = rdev->wiphy_idx;
7364 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02007365
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007366 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7367 cb->args[1] =
7368 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7369 }
7370
7371 if (cb->args[1]) {
7372 data = nla_data((void *)cb->args[1]);
7373 data_len = nla_len((void *)cb->args[1]);
7374 }
7375
Johannes Berg00918d32011-12-13 17:22:05 +01007376 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7377 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007378 err = -ENOENT;
7379 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007380 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007381
Johannes Berg00918d32011-12-13 17:22:05 +01007382 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007383 err = -EOPNOTSUPP;
7384 goto out_err;
7385 }
7386
7387 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007388 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007389 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7390 NL80211_CMD_TESTMODE);
7391 struct nlattr *tmdata;
7392
Dan Carpentercb35fba2013-08-14 14:50:01 +03007393 if (!hdr)
7394 break;
7395
David S. Miller9360ffd2012-03-29 04:41:26 -04007396 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007397 genlmsg_cancel(skb, hdr);
7398 break;
7399 }
7400
7401 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7402 if (!tmdata) {
7403 genlmsg_cancel(skb, hdr);
7404 break;
7405 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007406 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007407 nla_nest_end(skb, tmdata);
7408
7409 if (err == -ENOBUFS || err == -ENOENT) {
7410 genlmsg_cancel(skb, hdr);
7411 break;
7412 } else if (err) {
7413 genlmsg_cancel(skb, hdr);
7414 goto out_err;
7415 }
7416
7417 genlmsg_end(skb, hdr);
7418 }
7419
7420 err = skb->len;
7421 /* see above */
7422 cb->args[0] = phy_idx + 1;
7423 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007424 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007425 return err;
7426}
Johannes Bergaff89a92009-07-01 21:26:51 +02007427#endif
7428
Samuel Ortizb23aa672009-07-01 21:26:54 +02007429static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7430{
Johannes Berg4c476992010-10-04 21:36:35 +02007431 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7432 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007433 struct cfg80211_connect_params connect;
7434 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007435 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007436 int err;
7437
7438 memset(&connect, 0, sizeof(connect));
7439
7440 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7441 return -EINVAL;
7442
7443 if (!info->attrs[NL80211_ATTR_SSID] ||
7444 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7445 return -EINVAL;
7446
7447 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7448 connect.auth_type =
7449 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007450 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7451 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007452 return -EINVAL;
7453 } else
7454 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7455
7456 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7457
Johannes Bergc0692b82010-08-27 14:26:53 +03007458 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007459 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007460 if (err)
7461 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007462
Johannes Berg074ac8d2010-09-16 14:58:22 +02007463 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007464 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7465 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007466
Johannes Berg79c97e92009-07-07 03:56:12 +02007467 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007468
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307469 connect.bg_scan_period = -1;
7470 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7471 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7472 connect.bg_scan_period =
7473 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7474 }
7475
Samuel Ortizb23aa672009-07-01 21:26:54 +02007476 if (info->attrs[NL80211_ATTR_MAC])
7477 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007478 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7479 connect.bssid_hint =
7480 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007481 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7482 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7483
7484 if (info->attrs[NL80211_ATTR_IE]) {
7485 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7486 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7487 }
7488
Jouni Malinencee00a92013-01-15 17:15:57 +02007489 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7490 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7491 if (connect.mfp != NL80211_MFP_REQUIRED &&
7492 connect.mfp != NL80211_MFP_NO)
7493 return -EINVAL;
7494 } else {
7495 connect.mfp = NL80211_MFP_NO;
7496 }
7497
Samuel Ortizb23aa672009-07-01 21:26:54 +02007498 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007499 connect.channel = nl80211_get_valid_chan(
7500 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7501 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007502 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007503 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007504 connect.channel_hint = nl80211_get_valid_chan(
7505 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7506 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007507 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007508 }
7509
Johannes Bergfffd0932009-07-08 14:22:54 +02007510 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7511 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307512 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007513 if (IS_ERR(connkeys))
7514 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007515 }
7516
Ben Greear7e7c8922011-11-18 11:31:59 -08007517 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7518 connect.flags |= ASSOC_REQ_DISABLE_HT;
7519
7520 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7521 memcpy(&connect.ht_capa_mask,
7522 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7523 sizeof(connect.ht_capa_mask));
7524
7525 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007526 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
Johannes Bergb47f6102014-09-10 13:39:54 +03007527 kzfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007528 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007529 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007530 memcpy(&connect.ht_capa,
7531 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7532 sizeof(connect.ht_capa));
7533 }
7534
Johannes Bergee2aca32013-02-21 17:36:01 +01007535 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7536 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7537
7538 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7539 memcpy(&connect.vht_capa_mask,
7540 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7541 sizeof(connect.vht_capa_mask));
7542
7543 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7544 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
Johannes Bergb47f6102014-09-10 13:39:54 +03007545 kzfree(connkeys);
Johannes Bergee2aca32013-02-21 17:36:01 +01007546 return -EINVAL;
7547 }
7548 memcpy(&connect.vht_capa,
7549 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7550 sizeof(connect.vht_capa));
7551 }
7552
Assaf Kraussbab5ab72014-09-03 15:25:01 +03007553 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
7554 if (!(rdev->wiphy.features &
7555 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
7556 !(rdev->wiphy.features & NL80211_FEATURE_QUIET))
7557 return -EINVAL;
7558 connect.flags |= ASSOC_REQ_USE_RRM;
7559 }
7560
Johannes Berg83739b02013-05-15 17:44:01 +02007561 wdev_lock(dev->ieee80211_ptr);
7562 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7563 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007564 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +03007565 kzfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007566 return err;
7567}
7568
7569static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7570{
Johannes Berg4c476992010-10-04 21:36:35 +02007571 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7572 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007573 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007574 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007575
7576 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7577 reason = WLAN_REASON_DEAUTH_LEAVING;
7578 else
7579 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7580
7581 if (reason == 0)
7582 return -EINVAL;
7583
Johannes Berg074ac8d2010-09-16 14:58:22 +02007584 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007585 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7586 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007587
Johannes Berg83739b02013-05-15 17:44:01 +02007588 wdev_lock(dev->ieee80211_ptr);
7589 ret = cfg80211_disconnect(rdev, dev, reason, true);
7590 wdev_unlock(dev->ieee80211_ptr);
7591 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007592}
7593
Johannes Berg463d0182009-07-14 00:33:35 +02007594static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7595{
Johannes Berg4c476992010-10-04 21:36:35 +02007596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007597 struct net *net;
7598 int err;
7599 u32 pid;
7600
7601 if (!info->attrs[NL80211_ATTR_PID])
7602 return -EINVAL;
7603
7604 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7605
Johannes Berg463d0182009-07-14 00:33:35 +02007606 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007607 if (IS_ERR(net))
7608 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007609
7610 err = 0;
7611
7612 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007613 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7614 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007615
Johannes Berg463d0182009-07-14 00:33:35 +02007616 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007617 return err;
7618}
7619
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007620static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7621{
Johannes Berg4c476992010-10-04 21:36:35 +02007622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007623 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7624 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007625 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007626 struct cfg80211_pmksa pmksa;
7627
7628 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7629
7630 if (!info->attrs[NL80211_ATTR_MAC])
7631 return -EINVAL;
7632
7633 if (!info->attrs[NL80211_ATTR_PMKID])
7634 return -EINVAL;
7635
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007636 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7637 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7638
Johannes Berg074ac8d2010-09-16 14:58:22 +02007639 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007640 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7641 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007642
7643 switch (info->genlhdr->cmd) {
7644 case NL80211_CMD_SET_PMKSA:
7645 rdev_ops = rdev->ops->set_pmksa;
7646 break;
7647 case NL80211_CMD_DEL_PMKSA:
7648 rdev_ops = rdev->ops->del_pmksa;
7649 break;
7650 default:
7651 WARN_ON(1);
7652 break;
7653 }
7654
Johannes Berg4c476992010-10-04 21:36:35 +02007655 if (!rdev_ops)
7656 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007657
Johannes Berg4c476992010-10-04 21:36:35 +02007658 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007659}
7660
7661static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7662{
Johannes Berg4c476992010-10-04 21:36:35 +02007663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7664 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007665
Johannes Berg074ac8d2010-09-16 14:58:22 +02007666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7668 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007669
Johannes Berg4c476992010-10-04 21:36:35 +02007670 if (!rdev->ops->flush_pmksa)
7671 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007672
Hila Gonene35e4d22012-06-27 17:19:42 +03007673 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007674}
7675
Arik Nemtsov109086c2011-09-28 14:12:50 +03007676static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7677{
7678 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7679 struct net_device *dev = info->user_ptr[1];
7680 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307681 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007682 u16 status_code;
7683 u8 *peer;
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03007684 bool initiator;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007685
7686 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7687 !rdev->ops->tdls_mgmt)
7688 return -EOPNOTSUPP;
7689
7690 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7691 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7692 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7693 !info->attrs[NL80211_ATTR_IE] ||
7694 !info->attrs[NL80211_ATTR_MAC])
7695 return -EINVAL;
7696
7697 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7698 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7699 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7700 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03007701 initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307702 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7703 peer_capability =
7704 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007705
Hila Gonene35e4d22012-06-27 17:19:42 +03007706 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307707 dialog_token, status_code, peer_capability,
Arik Nemtsov31fa97c2014-06-11 17:18:21 +03007708 initiator,
Hila Gonene35e4d22012-06-27 17:19:42 +03007709 nla_data(info->attrs[NL80211_ATTR_IE]),
7710 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007711}
7712
7713static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7714{
7715 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7716 struct net_device *dev = info->user_ptr[1];
7717 enum nl80211_tdls_operation operation;
7718 u8 *peer;
7719
7720 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7721 !rdev->ops->tdls_oper)
7722 return -EOPNOTSUPP;
7723
7724 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7725 !info->attrs[NL80211_ATTR_MAC])
7726 return -EINVAL;
7727
7728 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7729 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7730
Hila Gonene35e4d22012-06-27 17:19:42 +03007731 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007732}
7733
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007734static int nl80211_remain_on_channel(struct sk_buff *skb,
7735 struct genl_info *info)
7736{
Johannes Berg4c476992010-10-04 21:36:35 +02007737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007738 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007739 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007740 struct sk_buff *msg;
7741 void *hdr;
7742 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007743 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007744 int err;
7745
7746 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7747 !info->attrs[NL80211_ATTR_DURATION])
7748 return -EINVAL;
7749
7750 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7751
Johannes Berg7c4ef712011-11-18 15:33:48 +01007752 if (!rdev->ops->remain_on_channel ||
7753 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007754 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007755
Johannes Bergebf348f2012-06-01 12:50:54 +02007756 /*
7757 * We should be on that channel for at least a minimum amount of
7758 * time (10ms) but no longer than the driver supports.
7759 */
7760 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7761 duration > rdev->wiphy.max_remain_on_channel_duration)
7762 return -EINVAL;
7763
Johannes Berg683b6d32012-11-08 21:25:48 +01007764 err = nl80211_parse_chandef(rdev, info, &chandef);
7765 if (err)
7766 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007767
7768 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007769 if (!msg)
7770 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007771
Eric W. Biederman15e47302012-09-07 20:12:54 +00007772 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007773 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007774 if (!hdr) {
7775 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007776 goto free_msg;
7777 }
7778
Johannes Berg683b6d32012-11-08 21:25:48 +01007779 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7780 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007781
7782 if (err)
7783 goto free_msg;
7784
David S. Miller9360ffd2012-03-29 04:41:26 -04007785 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7786 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007787
7788 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007789
7790 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007791
7792 nla_put_failure:
7793 err = -ENOBUFS;
7794 free_msg:
7795 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007796 return err;
7797}
7798
7799static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7800 struct genl_info *info)
7801{
Johannes Berg4c476992010-10-04 21:36:35 +02007802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007803 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007804 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007805
7806 if (!info->attrs[NL80211_ATTR_COOKIE])
7807 return -EINVAL;
7808
Johannes Berg4c476992010-10-04 21:36:35 +02007809 if (!rdev->ops->cancel_remain_on_channel)
7810 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007811
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007812 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7813
Hila Gonene35e4d22012-06-27 17:19:42 +03007814 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007815}
7816
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007817static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7818 u8 *rates, u8 rates_len)
7819{
7820 u8 i;
7821 u32 mask = 0;
7822
7823 for (i = 0; i < rates_len; i++) {
7824 int rate = (rates[i] & 0x7f) * 5;
7825 int ridx;
7826 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7827 struct ieee80211_rate *srate =
7828 &sband->bitrates[ridx];
7829 if (rate == srate->bitrate) {
7830 mask |= 1 << ridx;
7831 break;
7832 }
7833 }
7834 if (ridx == sband->n_bitrates)
7835 return 0; /* rate not found */
7836 }
7837
7838 return mask;
7839}
7840
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007841static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7842 u8 *rates, u8 rates_len,
7843 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7844{
7845 u8 i;
7846
7847 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7848
7849 for (i = 0; i < rates_len; i++) {
7850 int ridx, rbit;
7851
7852 ridx = rates[i] / 8;
7853 rbit = BIT(rates[i] % 8);
7854
7855 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007856 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007857 return false;
7858
7859 /* check availability */
7860 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7861 mcs[ridx] |= rbit;
7862 else
7863 return false;
7864 }
7865
7866 return true;
7867}
7868
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007869static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7870{
7871 u16 mcs_mask = 0;
7872
7873 switch (vht_mcs_map) {
7874 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7875 break;
7876 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7877 mcs_mask = 0x00FF;
7878 break;
7879 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7880 mcs_mask = 0x01FF;
7881 break;
7882 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7883 mcs_mask = 0x03FF;
7884 break;
7885 default:
7886 break;
7887 }
7888
7889 return mcs_mask;
7890}
7891
7892static void vht_build_mcs_mask(u16 vht_mcs_map,
7893 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7894{
7895 u8 nss;
7896
7897 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7898 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7899 vht_mcs_map >>= 2;
7900 }
7901}
7902
7903static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7904 struct nl80211_txrate_vht *txrate,
7905 u16 mcs[NL80211_VHT_NSS_MAX])
7906{
7907 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7908 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7909 u8 i;
7910
7911 if (!sband->vht_cap.vht_supported)
7912 return false;
7913
7914 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7915
7916 /* Build vht_mcs_mask from VHT capabilities */
7917 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7918
7919 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7920 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7921 mcs[i] = txrate->mcs[i];
7922 else
7923 return false;
7924 }
7925
7926 return true;
7927}
7928
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007929static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007930 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7931 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007932 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7933 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007934 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007935 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007936};
7937
7938static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7939 struct genl_info *info)
7940{
7941 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007942 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007943 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007944 int rem, i;
7945 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007946 struct nlattr *tx_rates;
7947 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007948 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007949
Johannes Berg4c476992010-10-04 21:36:35 +02007950 if (!rdev->ops->set_bitrate_mask)
7951 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007952
7953 memset(&mask, 0, sizeof(mask));
7954 /* Default to all rates enabled */
7955 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7956 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007957
7958 if (!sband)
7959 continue;
7960
7961 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007962 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007963 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007964 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007965
7966 if (!sband->vht_cap.vht_supported)
7967 continue;
7968
7969 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7970 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007971 }
7972
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007973 /* if no rates are given set it back to the defaults */
7974 if (!info->attrs[NL80211_ATTR_TX_RATES])
7975 goto out;
7976
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007977 /*
7978 * The nested attribute uses enum nl80211_band as the index. This maps
7979 * directly to the enum ieee80211_band values used in cfg80211.
7980 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007981 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007982 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007983 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007984 int err;
7985
Johannes Berg4c476992010-10-04 21:36:35 +02007986 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7987 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007988 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007989 if (sband == NULL)
7990 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007991 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7992 nla_len(tx_rates), nl80211_txattr_policy);
7993 if (err)
7994 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007995 if (tb[NL80211_TXRATE_LEGACY]) {
7996 mask.control[band].legacy = rateset_to_mask(
7997 sband,
7998 nla_data(tb[NL80211_TXRATE_LEGACY]),
7999 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05308000 if ((mask.control[band].legacy == 0) &&
8001 nla_len(tb[NL80211_TXRATE_LEGACY]))
8002 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008003 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01008004 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008005 if (!ht_rateset_to_mask(
8006 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01008007 nla_data(tb[NL80211_TXRATE_HT]),
8008 nla_len(tb[NL80211_TXRATE_HT]),
8009 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008010 return -EINVAL;
8011 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01008012 if (tb[NL80211_TXRATE_VHT]) {
8013 if (!vht_set_mcs_mask(
8014 sband,
8015 nla_data(tb[NL80211_TXRATE_VHT]),
8016 mask.control[band].vht_mcs))
8017 return -EINVAL;
8018 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01008019 if (tb[NL80211_TXRATE_GI]) {
8020 mask.control[band].gi =
8021 nla_get_u8(tb[NL80211_TXRATE_GI]);
8022 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
8023 return -EINVAL;
8024 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008025
8026 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01008027 /* don't allow empty legacy rates if HT or VHT
8028 * are not even supported.
8029 */
8030 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
8031 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008032 return -EINVAL;
8033
8034 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01008035 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01008036 goto out;
8037
8038 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
8039 if (mask.control[band].vht_mcs[i])
8040 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01008041
8042 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01008043 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008044 }
8045 }
8046
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01008047out:
Hila Gonene35e4d22012-06-27 17:19:42 +03008048 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008049}
8050
Johannes Berg2e161f72010-08-12 15:38:38 +02008051static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02008052{
Johannes Berg4c476992010-10-04 21:36:35 +02008053 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02008054 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02008055 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02008056
8057 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
8058 return -EINVAL;
8059
Johannes Berg2e161f72010-08-12 15:38:38 +02008060 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
8061 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02008062
Johannes Berg71bbc992012-06-15 15:30:18 +02008063 switch (wdev->iftype) {
8064 case NL80211_IFTYPE_STATION:
8065 case NL80211_IFTYPE_ADHOC:
8066 case NL80211_IFTYPE_P2P_CLIENT:
8067 case NL80211_IFTYPE_AP:
8068 case NL80211_IFTYPE_AP_VLAN:
8069 case NL80211_IFTYPE_MESH_POINT:
8070 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02008071 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02008072 break;
8073 default:
Johannes Berg4c476992010-10-04 21:36:35 +02008074 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02008075 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008076
8077 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02008078 if (!rdev->ops->mgmt_tx)
8079 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02008080
Eric W. Biederman15e47302012-09-07 20:12:54 +00008081 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02008082 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
8083 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02008084}
8085
Johannes Berg2e161f72010-08-12 15:38:38 +02008086static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02008087{
Johannes Berg4c476992010-10-04 21:36:35 +02008088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02008089 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01008090 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02008091 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01008092 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02008093 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01008094 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008095 struct cfg80211_mgmt_tx_params params = {
8096 .dont_wait_for_ack =
8097 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
8098 };
Jouni Malinen026331c2010-02-15 12:53:10 +02008099
Johannes Berg683b6d32012-11-08 21:25:48 +01008100 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02008101 return -EINVAL;
8102
Johannes Berg4c476992010-10-04 21:36:35 +02008103 if (!rdev->ops->mgmt_tx)
8104 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02008105
Johannes Berg71bbc992012-06-15 15:30:18 +02008106 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02008107 case NL80211_IFTYPE_P2P_DEVICE:
8108 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8109 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02008110 case NL80211_IFTYPE_STATION:
8111 case NL80211_IFTYPE_ADHOC:
8112 case NL80211_IFTYPE_P2P_CLIENT:
8113 case NL80211_IFTYPE_AP:
8114 case NL80211_IFTYPE_AP_VLAN:
8115 case NL80211_IFTYPE_MESH_POINT:
8116 case NL80211_IFTYPE_P2P_GO:
8117 break;
8118 default:
Johannes Berg4c476992010-10-04 21:36:35 +02008119 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02008120 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008121
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008122 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01008123 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008124 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008125 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02008126
8127 /*
8128 * We should wait on the channel for at least a minimum amount
8129 * of time (10ms) but no longer than the driver supports.
8130 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008131 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
8132 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02008133 return -EINVAL;
8134
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008135 }
8136
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008137 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008138
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008139 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01008140 return -EINVAL;
8141
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008142 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05308143
Antonio Quartulliea141b752013-06-11 14:20:03 +02008144 /* get the channel if any has been specified, otherwise pass NULL to
8145 * the driver. The latter will use the current one
8146 */
8147 chandef.chan = NULL;
8148 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8149 err = nl80211_parse_chandef(rdev, info, &chandef);
8150 if (err)
8151 return err;
8152 }
8153
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008154 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02008155 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02008156
Andrei Otcheretianski34d22ce2014-05-09 14:11:44 +03008157 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
8158 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
8159
8160 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
8161 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8162 int i;
8163
8164 if (len % sizeof(u16))
8165 return -EINVAL;
8166
8167 params.n_csa_offsets = len / sizeof(u16);
8168 params.csa_offsets =
8169 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8170
8171 /* check that all the offsets fit the frame */
8172 for (i = 0; i < params.n_csa_offsets; i++) {
8173 if (params.csa_offsets[i] >= params.len)
8174 return -EINVAL;
8175 }
8176 }
8177
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008178 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01008179 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8180 if (!msg)
8181 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02008182
Eric W. Biederman15e47302012-09-07 20:12:54 +00008183 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01008184 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008185 if (!hdr) {
8186 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01008187 goto free_msg;
8188 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008189 }
Johannes Berge247bd902011-11-04 11:18:21 +01008190
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02008191 params.chan = chandef.chan;
8192 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02008193 if (err)
8194 goto free_msg;
8195
Johannes Berge247bd902011-11-04 11:18:21 +01008196 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04008197 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8198 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008199
Johannes Berge247bd902011-11-04 11:18:21 +01008200 genlmsg_end(msg, hdr);
8201 return genlmsg_reply(msg, info);
8202 }
8203
8204 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02008205
8206 nla_put_failure:
8207 err = -ENOBUFS;
8208 free_msg:
8209 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02008210 return err;
8211}
8212
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008213static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
8214{
8215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02008216 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008217 u64 cookie;
8218
8219 if (!info->attrs[NL80211_ATTR_COOKIE])
8220 return -EINVAL;
8221
8222 if (!rdev->ops->mgmt_tx_cancel_wait)
8223 return -EOPNOTSUPP;
8224
Johannes Berg71bbc992012-06-15 15:30:18 +02008225 switch (wdev->iftype) {
8226 case NL80211_IFTYPE_STATION:
8227 case NL80211_IFTYPE_ADHOC:
8228 case NL80211_IFTYPE_P2P_CLIENT:
8229 case NL80211_IFTYPE_AP:
8230 case NL80211_IFTYPE_AP_VLAN:
8231 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02008232 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02008233 break;
8234 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008235 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02008236 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008237
8238 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8239
Hila Gonene35e4d22012-06-27 17:19:42 +03008240 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008241}
8242
Kalle Valoffb9eb32010-02-17 17:58:10 +02008243static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
8244{
Johannes Berg4c476992010-10-04 21:36:35 +02008245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008246 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008247 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008248 u8 ps_state;
8249 bool state;
8250 int err;
8251
Johannes Berg4c476992010-10-04 21:36:35 +02008252 if (!info->attrs[NL80211_ATTR_PS_STATE])
8253 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008254
8255 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
8256
Johannes Berg4c476992010-10-04 21:36:35 +02008257 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
8258 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008259
8260 wdev = dev->ieee80211_ptr;
8261
Johannes Berg4c476992010-10-04 21:36:35 +02008262 if (!rdev->ops->set_power_mgmt)
8263 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008264
8265 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
8266
8267 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02008268 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008269
Hila Gonene35e4d22012-06-27 17:19:42 +03008270 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02008271 if (!err)
8272 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008273 return err;
8274}
8275
8276static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
8277{
Johannes Berg4c476992010-10-04 21:36:35 +02008278 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008279 enum nl80211_ps_state ps_state;
8280 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008281 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02008282 struct sk_buff *msg;
8283 void *hdr;
8284 int err;
8285
Kalle Valoffb9eb32010-02-17 17:58:10 +02008286 wdev = dev->ieee80211_ptr;
8287
Johannes Berg4c476992010-10-04 21:36:35 +02008288 if (!rdev->ops->set_power_mgmt)
8289 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008290
8291 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02008292 if (!msg)
8293 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008294
Eric W. Biederman15e47302012-09-07 20:12:54 +00008295 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008296 NL80211_CMD_GET_POWER_SAVE);
8297 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02008298 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008299 goto free_msg;
8300 }
8301
8302 if (wdev->ps)
8303 ps_state = NL80211_PS_ENABLED;
8304 else
8305 ps_state = NL80211_PS_DISABLED;
8306
David S. Miller9360ffd2012-03-29 04:41:26 -04008307 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
8308 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02008309
8310 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02008311 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02008312
Johannes Berg4c476992010-10-04 21:36:35 +02008313 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02008314 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02008315 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02008316 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02008317 return err;
8318}
8319
Johannes Berg94e860f2014-01-20 23:58:15 +01008320static const struct nla_policy
8321nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008322 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
8323 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
8324 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07008325 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
8326 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
8327 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008328};
8329
Thomas Pedersen84f10702012-07-12 16:17:33 -07008330static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01008331 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07008332{
8333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07008334 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008335 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07008336
Johannes Bergd9d8b012012-11-26 12:51:52 +01008337 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07008338 return -EINVAL;
8339
Thomas Pedersen84f10702012-07-12 16:17:33 -07008340 if (!rdev->ops->set_cqm_txe_config)
8341 return -EOPNOTSUPP;
8342
8343 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8344 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8345 return -EOPNOTSUPP;
8346
Hila Gonene35e4d22012-06-27 17:19:42 +03008347 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07008348}
8349
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008350static int nl80211_set_cqm_rssi(struct genl_info *info,
8351 s32 threshold, u32 hysteresis)
8352{
Johannes Berg4c476992010-10-04 21:36:35 +02008353 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02008354 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008355 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008356
8357 if (threshold > 0)
8358 return -EINVAL;
8359
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008360 /* disabling - hysteresis should also be zero then */
8361 if (threshold == 0)
8362 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008363
Johannes Berg4c476992010-10-04 21:36:35 +02008364 if (!rdev->ops->set_cqm_rssi_config)
8365 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008366
Johannes Berg074ac8d2010-09-16 14:58:22 +02008367 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02008368 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8369 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008370
Hila Gonene35e4d22012-06-27 17:19:42 +03008371 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008372}
8373
8374static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
8375{
8376 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
8377 struct nlattr *cqm;
8378 int err;
8379
8380 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008381 if (!cqm)
8382 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008383
8384 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
8385 nl80211_attr_cqm_policy);
8386 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008387 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008388
8389 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
8390 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008391 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
8392 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008393
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008394 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8395 }
8396
8397 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8398 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8399 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8400 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8401 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8402 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8403
8404 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8405 }
8406
8407 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008408}
8409
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +01008410static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
8411{
8412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8413 struct net_device *dev = info->user_ptr[1];
8414 struct ocb_setup setup = {};
8415 int err;
8416
8417 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8418 if (err)
8419 return err;
8420
8421 return cfg80211_join_ocb(rdev, dev, &setup);
8422}
8423
8424static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
8425{
8426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8427 struct net_device *dev = info->user_ptr[1];
8428
8429 return cfg80211_leave_ocb(rdev, dev);
8430}
8431
Johannes Berg29cbe682010-12-03 09:20:44 +01008432static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8433{
8434 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8435 struct net_device *dev = info->user_ptr[1];
8436 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008437 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008438 int err;
8439
8440 /* start with default */
8441 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008442 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008443
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008444 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008445 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008446 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008447 if (err)
8448 return err;
8449 }
8450
8451 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8452 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8453 return -EINVAL;
8454
Javier Cardonac80d5452010-12-16 17:37:49 -08008455 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8456 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8457
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008458 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8459 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8460 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8461 return -EINVAL;
8462
Marco Porsch9bdbf042013-01-07 16:04:51 +01008463 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8464 setup.beacon_interval =
8465 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8466 if (setup.beacon_interval < 10 ||
8467 setup.beacon_interval > 10000)
8468 return -EINVAL;
8469 }
8470
8471 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8472 setup.dtim_period =
8473 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8474 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8475 return -EINVAL;
8476 }
8477
Javier Cardonac80d5452010-12-16 17:37:49 -08008478 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8479 /* parse additional setup parameters if given */
8480 err = nl80211_parse_mesh_setup(info, &setup);
8481 if (err)
8482 return err;
8483 }
8484
Thomas Pedersend37bb182013-03-04 13:06:13 -08008485 if (setup.user_mpm)
8486 cfg.auto_open_plinks = false;
8487
Johannes Bergcc1d2802012-05-16 23:50:20 +02008488 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008489 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8490 if (err)
8491 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008492 } else {
8493 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008494 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008495 }
8496
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008497 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8498 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8499 int n_rates =
8500 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8501 struct ieee80211_supported_band *sband;
8502
8503 if (!setup.chandef.chan)
8504 return -EINVAL;
8505
8506 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8507
8508 err = ieee80211_get_ratemask(sband, rates, n_rates,
8509 &setup.basic_rates);
8510 if (err)
8511 return err;
8512 }
8513
Javier Cardonac80d5452010-12-16 17:37:49 -08008514 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008515}
8516
8517static int nl80211_leave_mesh(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];
8521
8522 return cfg80211_leave_mesh(rdev, dev);
8523}
8524
Johannes Bergdfb89c52012-06-27 09:23:48 +02008525#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008526static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8527 struct cfg80211_registered_device *rdev)
8528{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008529 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008530 struct nlattr *nl_pats, *nl_pat;
8531 int i, pat_len;
8532
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008533 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008534 return 0;
8535
8536 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8537 if (!nl_pats)
8538 return -ENOBUFS;
8539
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008540 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008541 nl_pat = nla_nest_start(msg, i + 1);
8542 if (!nl_pat)
8543 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008544 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008545 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008546 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008547 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8548 wowlan->patterns[i].pattern) ||
8549 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008550 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008551 return -ENOBUFS;
8552 nla_nest_end(msg, nl_pat);
8553 }
8554 nla_nest_end(msg, nl_pats);
8555
8556 return 0;
8557}
8558
Johannes Berg2a0e0472013-01-23 22:57:40 +01008559static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8560 struct cfg80211_wowlan_tcp *tcp)
8561{
8562 struct nlattr *nl_tcp;
8563
8564 if (!tcp)
8565 return 0;
8566
8567 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8568 if (!nl_tcp)
8569 return -ENOBUFS;
8570
8571 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8572 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8573 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8574 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8575 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8576 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8577 tcp->payload_len, tcp->payload) ||
8578 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8579 tcp->data_interval) ||
8580 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8581 tcp->wake_len, tcp->wake_data) ||
8582 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8583 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8584 return -ENOBUFS;
8585
8586 if (tcp->payload_seq.len &&
8587 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8588 sizeof(tcp->payload_seq), &tcp->payload_seq))
8589 return -ENOBUFS;
8590
8591 if (tcp->payload_tok.len &&
8592 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8593 sizeof(tcp->payload_tok) + tcp->tokens_size,
8594 &tcp->payload_tok))
8595 return -ENOBUFS;
8596
Johannes Berge248ad32013-05-16 10:24:28 +02008597 nla_nest_end(msg, nl_tcp);
8598
Johannes Berg2a0e0472013-01-23 22:57:40 +01008599 return 0;
8600}
8601
Johannes Bergff1b6e62011-05-04 15:37:28 +02008602static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8603{
8604 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8605 struct sk_buff *msg;
8606 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008607 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008608
Johannes Berg964dc9e2013-06-03 17:25:34 +02008609 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008610 return -EOPNOTSUPP;
8611
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008612 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008613 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008614 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8615 rdev->wiphy.wowlan_config->tcp->payload_len +
8616 rdev->wiphy.wowlan_config->tcp->wake_len +
8617 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008618 }
8619
8620 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008621 if (!msg)
8622 return -ENOMEM;
8623
Eric W. Biederman15e47302012-09-07 20:12:54 +00008624 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008625 NL80211_CMD_GET_WOWLAN);
8626 if (!hdr)
8627 goto nla_put_failure;
8628
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008629 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008630 struct nlattr *nl_wowlan;
8631
8632 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8633 if (!nl_wowlan)
8634 goto nla_put_failure;
8635
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008636 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008637 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008638 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008639 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008640 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008641 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008642 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008643 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008644 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008645 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008646 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008647 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008648 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008649 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8650 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008651
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008652 if (nl80211_send_wowlan_patterns(msg, rdev))
8653 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008654
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008655 if (nl80211_send_wowlan_tcp(msg,
8656 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008657 goto nla_put_failure;
8658
Johannes Bergff1b6e62011-05-04 15:37:28 +02008659 nla_nest_end(msg, nl_wowlan);
8660 }
8661
8662 genlmsg_end(msg, hdr);
8663 return genlmsg_reply(msg, info);
8664
8665nla_put_failure:
8666 nlmsg_free(msg);
8667 return -ENOBUFS;
8668}
8669
Johannes Berg2a0e0472013-01-23 22:57:40 +01008670static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8671 struct nlattr *attr,
8672 struct cfg80211_wowlan *trig)
8673{
8674 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8675 struct cfg80211_wowlan_tcp *cfg;
8676 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8677 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8678 u32 size;
8679 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8680 int err, port;
8681
Johannes Berg964dc9e2013-06-03 17:25:34 +02008682 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008683 return -EINVAL;
8684
8685 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8686 nla_data(attr), nla_len(attr),
8687 nl80211_wowlan_tcp_policy);
8688 if (err)
8689 return err;
8690
8691 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8692 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8693 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8694 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8695 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8696 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8697 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8698 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8699 return -EINVAL;
8700
8701 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008702 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008703 return -EINVAL;
8704
8705 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008706 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008707 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008708 return -EINVAL;
8709
8710 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008711 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008712 return -EINVAL;
8713
8714 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8715 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8716 return -EINVAL;
8717
8718 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8719 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8720
8721 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8722 tokens_size = tokln - sizeof(*tok);
8723
8724 if (!tok->len || tokens_size % tok->len)
8725 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008726 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008727 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008728 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008729 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008730 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008731 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008732 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008733 return -EINVAL;
8734 if (tok->offset + tok->len > data_size)
8735 return -EINVAL;
8736 }
8737
8738 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8739 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008740 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008741 return -EINVAL;
8742 if (seq->len == 0 || seq->len > 4)
8743 return -EINVAL;
8744 if (seq->len + seq->offset > data_size)
8745 return -EINVAL;
8746 }
8747
8748 size = sizeof(*cfg);
8749 size += data_size;
8750 size += wake_size + wake_mask_size;
8751 size += tokens_size;
8752
8753 cfg = kzalloc(size, GFP_KERNEL);
8754 if (!cfg)
8755 return -ENOMEM;
8756 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8757 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8758 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8759 ETH_ALEN);
8760 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8761 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8762 else
8763 port = 0;
8764#ifdef CONFIG_INET
8765 /* allocate a socket and port for it and use it */
8766 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8767 IPPROTO_TCP, &cfg->sock, 1);
8768 if (err) {
8769 kfree(cfg);
8770 return err;
8771 }
8772 if (inet_csk_get_port(cfg->sock->sk, port)) {
8773 sock_release(cfg->sock);
8774 kfree(cfg);
8775 return -EADDRINUSE;
8776 }
8777 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8778#else
8779 if (!port) {
8780 kfree(cfg);
8781 return -EINVAL;
8782 }
8783 cfg->src_port = port;
8784#endif
8785
8786 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8787 cfg->payload_len = data_size;
8788 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8789 memcpy((void *)cfg->payload,
8790 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8791 data_size);
8792 if (seq)
8793 cfg->payload_seq = *seq;
8794 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8795 cfg->wake_len = wake_size;
8796 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8797 memcpy((void *)cfg->wake_data,
8798 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8799 wake_size);
8800 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8801 data_size + wake_size;
8802 memcpy((void *)cfg->wake_mask,
8803 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8804 wake_mask_size);
8805 if (tok) {
8806 cfg->tokens_size = tokens_size;
8807 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8808 }
8809
8810 trig->tcp = cfg;
8811
8812 return 0;
8813}
8814
Luciano Coelho8cd4d452014-09-17 11:55:28 +03008815static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
8816 const struct wiphy_wowlan_support *wowlan,
8817 struct nlattr *attr,
8818 struct cfg80211_wowlan *trig)
8819{
8820 struct nlattr **tb;
8821 int err;
8822
8823 tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
8824 if (!tb)
8825 return -ENOMEM;
8826
8827 if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
8828 err = -EOPNOTSUPP;
8829 goto out;
8830 }
8831
8832 err = nla_parse(tb, NL80211_ATTR_MAX,
8833 nla_data(attr), nla_len(attr),
8834 nl80211_policy);
8835 if (err)
8836 goto out;
8837
Johannes Bergad2b26a2014-06-12 21:39:05 +02008838 trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb);
Luciano Coelho8cd4d452014-09-17 11:55:28 +03008839 err = PTR_ERR_OR_ZERO(trig->nd_config);
8840 if (err)
8841 trig->nd_config = NULL;
8842
8843out:
8844 kfree(tb);
8845 return err;
8846}
8847
Johannes Bergff1b6e62011-05-04 15:37:28 +02008848static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8849{
8850 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8851 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008852 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008853 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008854 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008855 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008856 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008857
Johannes Berg964dc9e2013-06-03 17:25:34 +02008858 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008859 return -EOPNOTSUPP;
8860
Johannes Bergae33bd82012-07-12 16:25:02 +02008861 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8862 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008863 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008864 goto set_wakeup;
8865 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008866
8867 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8868 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8869 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8870 nl80211_wowlan_policy);
8871 if (err)
8872 return err;
8873
8874 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8875 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8876 return -EINVAL;
8877 new_triggers.any = true;
8878 }
8879
8880 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8881 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8882 return -EINVAL;
8883 new_triggers.disconnect = true;
8884 }
8885
8886 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8887 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8888 return -EINVAL;
8889 new_triggers.magic_pkt = true;
8890 }
8891
Johannes Berg77dbbb12011-07-13 10:48:55 +02008892 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8893 return -EINVAL;
8894
8895 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8896 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8897 return -EINVAL;
8898 new_triggers.gtk_rekey_failure = true;
8899 }
8900
8901 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8902 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8903 return -EINVAL;
8904 new_triggers.eap_identity_req = true;
8905 }
8906
8907 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8908 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8909 return -EINVAL;
8910 new_triggers.four_way_handshake = true;
8911 }
8912
8913 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8914 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8915 return -EINVAL;
8916 new_triggers.rfkill_release = true;
8917 }
8918
Johannes Bergff1b6e62011-05-04 15:37:28 +02008919 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8920 struct nlattr *pat;
8921 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008922 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008923 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008924
8925 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8926 rem)
8927 n_patterns++;
8928 if (n_patterns > wowlan->n_patterns)
8929 return -EINVAL;
8930
8931 new_triggers.patterns = kcalloc(n_patterns,
8932 sizeof(new_triggers.patterns[0]),
8933 GFP_KERNEL);
8934 if (!new_triggers.patterns)
8935 return -ENOMEM;
8936
8937 new_triggers.n_patterns = n_patterns;
8938 i = 0;
8939
8940 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8941 rem) {
Johannes Berg922bd802014-05-19 17:59:50 +02008942 u8 *mask_pat;
8943
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008944 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8945 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008946 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008947 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8948 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008949 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008950 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008951 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008952 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008953 goto error;
8954 if (pat_len > wowlan->pattern_max_len ||
8955 pat_len < wowlan->pattern_min_len)
8956 goto error;
8957
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008958 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008959 pkt_offset = 0;
8960 else
8961 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008962 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008963 if (pkt_offset > wowlan->max_pkt_offset)
8964 goto error;
8965 new_triggers.patterns[i].pkt_offset = pkt_offset;
8966
Johannes Berg922bd802014-05-19 17:59:50 +02008967 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
8968 if (!mask_pat) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008969 err = -ENOMEM;
8970 goto error;
8971 }
Johannes Berg922bd802014-05-19 17:59:50 +02008972 new_triggers.patterns[i].mask = mask_pat;
8973 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008974 mask_len);
Johannes Berg922bd802014-05-19 17:59:50 +02008975 mask_pat += mask_len;
8976 new_triggers.patterns[i].pattern = mask_pat;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008977 new_triggers.patterns[i].pattern_len = pat_len;
Johannes Berg922bd802014-05-19 17:59:50 +02008978 memcpy(mask_pat,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008979 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008980 pat_len);
8981 i++;
8982 }
8983 }
8984
Johannes Berg2a0e0472013-01-23 22:57:40 +01008985 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8986 err = nl80211_parse_wowlan_tcp(
8987 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8988 &new_triggers);
8989 if (err)
8990 goto error;
8991 }
8992
Luciano Coelho8cd4d452014-09-17 11:55:28 +03008993 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
8994 err = nl80211_parse_wowlan_nd(
8995 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
8996 &new_triggers);
8997 if (err)
8998 goto error;
8999 }
9000
Johannes Bergae33bd82012-07-12 16:25:02 +02009001 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
9002 if (!ntrig) {
9003 err = -ENOMEM;
9004 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02009005 }
Johannes Bergae33bd82012-07-12 16:25:02 +02009006 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009007 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02009008
Johannes Bergae33bd82012-07-12 16:25:02 +02009009 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02009010 if (rdev->ops->set_wakeup &&
9011 prev_enabled != !!rdev->wiphy.wowlan_config)
9012 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02009013
Johannes Bergff1b6e62011-05-04 15:37:28 +02009014 return 0;
9015 error:
9016 for (i = 0; i < new_triggers.n_patterns; i++)
9017 kfree(new_triggers.patterns[i].mask);
9018 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01009019 if (new_triggers.tcp && new_triggers.tcp->sock)
9020 sock_release(new_triggers.tcp->sock);
9021 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02009022 return err;
9023}
Johannes Bergdfb89c52012-06-27 09:23:48 +02009024#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02009025
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009026static int nl80211_send_coalesce_rules(struct sk_buff *msg,
9027 struct cfg80211_registered_device *rdev)
9028{
9029 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
9030 int i, j, pat_len;
9031 struct cfg80211_coalesce_rules *rule;
9032
9033 if (!rdev->coalesce->n_rules)
9034 return 0;
9035
9036 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
9037 if (!nl_rules)
9038 return -ENOBUFS;
9039
9040 for (i = 0; i < rdev->coalesce->n_rules; i++) {
9041 nl_rule = nla_nest_start(msg, i + 1);
9042 if (!nl_rule)
9043 return -ENOBUFS;
9044
9045 rule = &rdev->coalesce->rules[i];
9046 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
9047 rule->delay))
9048 return -ENOBUFS;
9049
9050 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
9051 rule->condition))
9052 return -ENOBUFS;
9053
9054 nl_pats = nla_nest_start(msg,
9055 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
9056 if (!nl_pats)
9057 return -ENOBUFS;
9058
9059 for (j = 0; j < rule->n_patterns; j++) {
9060 nl_pat = nla_nest_start(msg, j + 1);
9061 if (!nl_pat)
9062 return -ENOBUFS;
9063 pat_len = rule->patterns[j].pattern_len;
9064 if (nla_put(msg, NL80211_PKTPAT_MASK,
9065 DIV_ROUND_UP(pat_len, 8),
9066 rule->patterns[j].mask) ||
9067 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
9068 rule->patterns[j].pattern) ||
9069 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
9070 rule->patterns[j].pkt_offset))
9071 return -ENOBUFS;
9072 nla_nest_end(msg, nl_pat);
9073 }
9074 nla_nest_end(msg, nl_pats);
9075 nla_nest_end(msg, nl_rule);
9076 }
9077 nla_nest_end(msg, nl_rules);
9078
9079 return 0;
9080}
9081
9082static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
9083{
9084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9085 struct sk_buff *msg;
9086 void *hdr;
9087
9088 if (!rdev->wiphy.coalesce)
9089 return -EOPNOTSUPP;
9090
9091 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9092 if (!msg)
9093 return -ENOMEM;
9094
9095 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9096 NL80211_CMD_GET_COALESCE);
9097 if (!hdr)
9098 goto nla_put_failure;
9099
9100 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
9101 goto nla_put_failure;
9102
9103 genlmsg_end(msg, hdr);
9104 return genlmsg_reply(msg, info);
9105
9106nla_put_failure:
9107 nlmsg_free(msg);
9108 return -ENOBUFS;
9109}
9110
9111void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
9112{
9113 struct cfg80211_coalesce *coalesce = rdev->coalesce;
9114 int i, j;
9115 struct cfg80211_coalesce_rules *rule;
9116
9117 if (!coalesce)
9118 return;
9119
9120 for (i = 0; i < coalesce->n_rules; i++) {
9121 rule = &coalesce->rules[i];
9122 for (j = 0; j < rule->n_patterns; j++)
9123 kfree(rule->patterns[j].mask);
9124 kfree(rule->patterns);
9125 }
9126 kfree(coalesce->rules);
9127 kfree(coalesce);
9128 rdev->coalesce = NULL;
9129}
9130
9131static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
9132 struct nlattr *rule,
9133 struct cfg80211_coalesce_rules *new_rule)
9134{
9135 int err, i;
9136 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9137 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
9138 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
9139 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9140
9141 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
9142 nla_len(rule), nl80211_coalesce_policy);
9143 if (err)
9144 return err;
9145
9146 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
9147 new_rule->delay =
9148 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
9149 if (new_rule->delay > coalesce->max_delay)
9150 return -EINVAL;
9151
9152 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
9153 new_rule->condition =
9154 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
9155 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
9156 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
9157 return -EINVAL;
9158
9159 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
9160 return -EINVAL;
9161
9162 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9163 rem)
9164 n_patterns++;
9165 if (n_patterns > coalesce->n_patterns)
9166 return -EINVAL;
9167
9168 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
9169 GFP_KERNEL);
9170 if (!new_rule->patterns)
9171 return -ENOMEM;
9172
9173 new_rule->n_patterns = n_patterns;
9174 i = 0;
9175
9176 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9177 rem) {
Johannes Berg922bd802014-05-19 17:59:50 +02009178 u8 *mask_pat;
9179
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009180 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
9181 nla_len(pat), NULL);
9182 if (!pat_tb[NL80211_PKTPAT_MASK] ||
9183 !pat_tb[NL80211_PKTPAT_PATTERN])
9184 return -EINVAL;
9185 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
9186 mask_len = DIV_ROUND_UP(pat_len, 8);
9187 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
9188 return -EINVAL;
9189 if (pat_len > coalesce->pattern_max_len ||
9190 pat_len < coalesce->pattern_min_len)
9191 return -EINVAL;
9192
9193 if (!pat_tb[NL80211_PKTPAT_OFFSET])
9194 pkt_offset = 0;
9195 else
9196 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
9197 if (pkt_offset > coalesce->max_pkt_offset)
9198 return -EINVAL;
9199 new_rule->patterns[i].pkt_offset = pkt_offset;
9200
Johannes Berg922bd802014-05-19 17:59:50 +02009201 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
9202 if (!mask_pat)
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009203 return -ENOMEM;
Johannes Berg922bd802014-05-19 17:59:50 +02009204
9205 new_rule->patterns[i].mask = mask_pat;
9206 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
9207 mask_len);
9208
9209 mask_pat += mask_len;
9210 new_rule->patterns[i].pattern = mask_pat;
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009211 new_rule->patterns[i].pattern_len = pat_len;
Johannes Berg922bd802014-05-19 17:59:50 +02009212 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
9213 pat_len);
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009214 i++;
9215 }
9216
9217 return 0;
9218}
9219
9220static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
9221{
9222 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9223 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9224 struct cfg80211_coalesce new_coalesce = {};
9225 struct cfg80211_coalesce *n_coalesce;
9226 int err, rem_rule, n_rules = 0, i, j;
9227 struct nlattr *rule;
9228 struct cfg80211_coalesce_rules *tmp_rule;
9229
9230 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
9231 return -EOPNOTSUPP;
9232
9233 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
9234 cfg80211_rdev_free_coalesce(rdev);
9235 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
9236 return 0;
9237 }
9238
9239 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9240 rem_rule)
9241 n_rules++;
9242 if (n_rules > coalesce->n_rules)
9243 return -EINVAL;
9244
9245 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
9246 GFP_KERNEL);
9247 if (!new_coalesce.rules)
9248 return -ENOMEM;
9249
9250 new_coalesce.n_rules = n_rules;
9251 i = 0;
9252
9253 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9254 rem_rule) {
9255 err = nl80211_parse_coalesce_rule(rdev, rule,
9256 &new_coalesce.rules[i]);
9257 if (err)
9258 goto error;
9259
9260 i++;
9261 }
9262
9263 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
9264 if (err)
9265 goto error;
9266
9267 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
9268 if (!n_coalesce) {
9269 err = -ENOMEM;
9270 goto error;
9271 }
9272 cfg80211_rdev_free_coalesce(rdev);
9273 rdev->coalesce = n_coalesce;
9274
9275 return 0;
9276error:
9277 for (i = 0; i < new_coalesce.n_rules; i++) {
9278 tmp_rule = &new_coalesce.rules[i];
9279 for (j = 0; j < tmp_rule->n_patterns; j++)
9280 kfree(tmp_rule->patterns[j].mask);
9281 kfree(tmp_rule->patterns);
9282 }
9283 kfree(new_coalesce.rules);
9284
9285 return err;
9286}
9287
Johannes Berge5497d72011-07-05 16:35:40 +02009288static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
9289{
9290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9291 struct net_device *dev = info->user_ptr[1];
9292 struct wireless_dev *wdev = dev->ieee80211_ptr;
9293 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
9294 struct cfg80211_gtk_rekey_data rekey_data;
9295 int err;
9296
9297 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
9298 return -EINVAL;
9299
9300 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
9301 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
9302 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
9303 nl80211_rekey_policy);
9304 if (err)
9305 return err;
9306
9307 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
9308 return -ERANGE;
9309 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
9310 return -ERANGE;
9311 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
9312 return -ERANGE;
9313
Johannes Berg78f686c2014-09-10 22:28:06 +03009314 rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
9315 rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
9316 rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
Johannes Berge5497d72011-07-05 16:35:40 +02009317
9318 wdev_lock(wdev);
9319 if (!wdev->current_bss) {
9320 err = -ENOTCONN;
9321 goto out;
9322 }
9323
9324 if (!rdev->ops->set_rekey_data) {
9325 err = -EOPNOTSUPP;
9326 goto out;
9327 }
9328
Hila Gonene35e4d22012-06-27 17:19:42 +03009329 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02009330 out:
9331 wdev_unlock(wdev);
9332 return err;
9333}
9334
Johannes Berg28946da2011-11-04 11:18:12 +01009335static int nl80211_register_unexpected_frame(struct sk_buff *skb,
9336 struct genl_info *info)
9337{
9338 struct net_device *dev = info->user_ptr[1];
9339 struct wireless_dev *wdev = dev->ieee80211_ptr;
9340
9341 if (wdev->iftype != NL80211_IFTYPE_AP &&
9342 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9343 return -EINVAL;
9344
Eric W. Biederman15e47302012-09-07 20:12:54 +00009345 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009346 return -EBUSY;
9347
Eric W. Biederman15e47302012-09-07 20:12:54 +00009348 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01009349 return 0;
9350}
9351
Johannes Berg7f6cf312011-11-04 11:18:15 +01009352static int nl80211_probe_client(struct sk_buff *skb,
9353 struct genl_info *info)
9354{
9355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9356 struct net_device *dev = info->user_ptr[1];
9357 struct wireless_dev *wdev = dev->ieee80211_ptr;
9358 struct sk_buff *msg;
9359 void *hdr;
9360 const u8 *addr;
9361 u64 cookie;
9362 int err;
9363
9364 if (wdev->iftype != NL80211_IFTYPE_AP &&
9365 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9366 return -EOPNOTSUPP;
9367
9368 if (!info->attrs[NL80211_ATTR_MAC])
9369 return -EINVAL;
9370
9371 if (!rdev->ops->probe_client)
9372 return -EOPNOTSUPP;
9373
9374 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9375 if (!msg)
9376 return -ENOMEM;
9377
Eric W. Biederman15e47302012-09-07 20:12:54 +00009378 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01009379 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03009380 if (!hdr) {
9381 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009382 goto free_msg;
9383 }
9384
9385 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9386
Hila Gonene35e4d22012-06-27 17:19:42 +03009387 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01009388 if (err)
9389 goto free_msg;
9390
David S. Miller9360ffd2012-03-29 04:41:26 -04009391 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9392 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01009393
9394 genlmsg_end(msg, hdr);
9395
9396 return genlmsg_reply(msg, info);
9397
9398 nla_put_failure:
9399 err = -ENOBUFS;
9400 free_msg:
9401 nlmsg_free(msg);
9402 return err;
9403}
9404
Johannes Berg5e760232011-11-04 11:18:17 +01009405static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
9406{
9407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07009408 struct cfg80211_beacon_registration *reg, *nreg;
9409 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01009410
9411 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
9412 return -EOPNOTSUPP;
9413
Ben Greear37c73b52012-10-26 14:49:25 -07009414 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
9415 if (!nreg)
9416 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01009417
Ben Greear37c73b52012-10-26 14:49:25 -07009418 /* First, check if already registered. */
9419 spin_lock_bh(&rdev->beacon_registrations_lock);
9420 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9421 if (reg->nlportid == info->snd_portid) {
9422 rv = -EALREADY;
9423 goto out_err;
9424 }
9425 }
9426 /* Add it to the list */
9427 nreg->nlportid = info->snd_portid;
9428 list_add(&nreg->list, &rdev->beacon_registrations);
9429
9430 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009431
9432 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07009433out_err:
9434 spin_unlock_bh(&rdev->beacon_registrations_lock);
9435 kfree(nreg);
9436 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01009437}
9438
Johannes Berg98104fde2012-06-16 00:19:54 +02009439static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
9440{
9441 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9442 struct wireless_dev *wdev = info->user_ptr[1];
9443 int err;
9444
9445 if (!rdev->ops->start_p2p_device)
9446 return -EOPNOTSUPP;
9447
9448 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9449 return -EOPNOTSUPP;
9450
9451 if (wdev->p2p_started)
9452 return 0;
9453
Luciano Coelhob6a55012014-02-27 11:07:21 +02009454 if (rfkill_blocked(rdev->rfkill))
9455 return -ERFKILL;
Johannes Berg98104fde2012-06-16 00:19:54 +02009456
Johannes Bergeeb126e2012-10-23 15:16:50 +02009457 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009458 if (err)
9459 return err;
9460
9461 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02009462 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02009463
9464 return 0;
9465}
9466
9467static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9468{
9469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9470 struct wireless_dev *wdev = info->user_ptr[1];
9471
9472 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9473 return -EOPNOTSUPP;
9474
9475 if (!rdev->ops->stop_p2p_device)
9476 return -EOPNOTSUPP;
9477
Johannes Bergf9f47522013-03-19 15:04:07 +01009478 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009479
9480 return 0;
9481}
9482
Johannes Berg3713b4e2013-02-14 16:19:38 +01009483static int nl80211_get_protocol_features(struct sk_buff *skb,
9484 struct genl_info *info)
9485{
9486 void *hdr;
9487 struct sk_buff *msg;
9488
9489 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9490 if (!msg)
9491 return -ENOMEM;
9492
9493 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9494 NL80211_CMD_GET_PROTOCOL_FEATURES);
9495 if (!hdr)
9496 goto nla_put_failure;
9497
9498 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9499 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9500 goto nla_put_failure;
9501
9502 genlmsg_end(msg, hdr);
9503 return genlmsg_reply(msg, info);
9504
9505 nla_put_failure:
9506 kfree_skb(msg);
9507 return -ENOBUFS;
9508}
9509
Jouni Malinen355199e2013-02-27 17:14:27 +02009510static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9511{
9512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9513 struct cfg80211_update_ft_ies_params ft_params;
9514 struct net_device *dev = info->user_ptr[1];
9515
9516 if (!rdev->ops->update_ft_ies)
9517 return -EOPNOTSUPP;
9518
9519 if (!info->attrs[NL80211_ATTR_MDID] ||
9520 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9521 return -EINVAL;
9522
9523 memset(&ft_params, 0, sizeof(ft_params));
9524 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9525 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9526 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9527
9528 return rdev_update_ft_ies(rdev, dev, &ft_params);
9529}
9530
Arend van Spriel5de17982013-04-18 15:49:00 +02009531static int nl80211_crit_protocol_start(struct sk_buff *skb,
9532 struct genl_info *info)
9533{
9534 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9535 struct wireless_dev *wdev = info->user_ptr[1];
9536 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9537 u16 duration;
9538 int ret;
9539
9540 if (!rdev->ops->crit_proto_start)
9541 return -EOPNOTSUPP;
9542
9543 if (WARN_ON(!rdev->ops->crit_proto_stop))
9544 return -EINVAL;
9545
9546 if (rdev->crit_proto_nlportid)
9547 return -EBUSY;
9548
9549 /* determine protocol if provided */
9550 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9551 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9552
9553 if (proto >= NUM_NL80211_CRIT_PROTO)
9554 return -EINVAL;
9555
9556 /* timeout must be provided */
9557 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9558 return -EINVAL;
9559
9560 duration =
9561 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9562
9563 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9564 return -ERANGE;
9565
9566 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9567 if (!ret)
9568 rdev->crit_proto_nlportid = info->snd_portid;
9569
9570 return ret;
9571}
9572
9573static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9574 struct genl_info *info)
9575{
9576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9577 struct wireless_dev *wdev = info->user_ptr[1];
9578
9579 if (!rdev->ops->crit_proto_stop)
9580 return -EOPNOTSUPP;
9581
9582 if (rdev->crit_proto_nlportid) {
9583 rdev->crit_proto_nlportid = 0;
9584 rdev_crit_proto_stop(rdev, wdev);
9585 }
9586 return 0;
9587}
9588
Johannes Bergad7e7182013-11-13 13:37:47 +01009589static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9590{
9591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9592 struct wireless_dev *wdev =
9593 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9594 int i, err;
9595 u32 vid, subcmd;
9596
9597 if (!rdev->wiphy.vendor_commands)
9598 return -EOPNOTSUPP;
9599
9600 if (IS_ERR(wdev)) {
9601 err = PTR_ERR(wdev);
9602 if (err != -EINVAL)
9603 return err;
9604 wdev = NULL;
9605 } else if (wdev->wiphy != &rdev->wiphy) {
9606 return -EINVAL;
9607 }
9608
9609 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9610 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9611 return -EINVAL;
9612
9613 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9614 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9615 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9616 const struct wiphy_vendor_command *vcmd;
9617 void *data = NULL;
9618 int len = 0;
9619
9620 vcmd = &rdev->wiphy.vendor_commands[i];
9621
9622 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9623 continue;
9624
9625 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9626 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9627 if (!wdev)
9628 return -EINVAL;
9629 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9630 !wdev->netdev)
9631 return -EINVAL;
9632
9633 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9634 if (wdev->netdev &&
9635 !netif_running(wdev->netdev))
9636 return -ENETDOWN;
9637 if (!wdev->netdev && !wdev->p2p_started)
9638 return -ENETDOWN;
9639 }
9640 } else {
9641 wdev = NULL;
9642 }
9643
9644 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9645 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9646 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9647 }
9648
9649 rdev->cur_cmd_info = info;
9650 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9651 data, len);
9652 rdev->cur_cmd_info = NULL;
9653 return err;
9654 }
9655
9656 return -EOPNOTSUPP;
9657}
9658
9659struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9660 enum nl80211_commands cmd,
9661 enum nl80211_attrs attr,
9662 int approxlen)
9663{
Zhao, Gangf26cbf42014-04-21 12:53:03 +08009664 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Bergad7e7182013-11-13 13:37:47 +01009665
9666 if (WARN_ON(!rdev->cur_cmd_info))
9667 return NULL;
9668
9669 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9670 rdev->cur_cmd_info->snd_portid,
9671 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009672 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009673}
9674EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9675
9676int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9677{
9678 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9679 void *hdr = ((void **)skb->cb)[1];
9680 struct nlattr *data = ((void **)skb->cb)[2];
9681
Johannes Bergbd8c78e2014-07-30 14:55:26 +02009682 /* clear CB data for netlink core to own from now on */
9683 memset(skb->cb, 0, sizeof(skb->cb));
9684
Johannes Bergad7e7182013-11-13 13:37:47 +01009685 if (WARN_ON(!rdev->cur_cmd_info)) {
9686 kfree_skb(skb);
9687 return -EINVAL;
9688 }
9689
9690 nla_nest_end(skb, data);
9691 genlmsg_end(skb, hdr);
9692 return genlmsg_reply(skb, rdev->cur_cmd_info);
9693}
9694EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9695
9696
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009697static int nl80211_set_qos_map(struct sk_buff *skb,
9698 struct genl_info *info)
9699{
9700 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9701 struct cfg80211_qos_map *qos_map = NULL;
9702 struct net_device *dev = info->user_ptr[1];
9703 u8 *pos, len, num_des, des_len, des;
9704 int ret;
9705
9706 if (!rdev->ops->set_qos_map)
9707 return -EOPNOTSUPP;
9708
9709 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9710 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9711 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9712
9713 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9714 len > IEEE80211_QOS_MAP_LEN_MAX)
9715 return -EINVAL;
9716
9717 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9718 if (!qos_map)
9719 return -ENOMEM;
9720
9721 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9722 if (num_des) {
9723 des_len = num_des *
9724 sizeof(struct cfg80211_dscp_exception);
9725 memcpy(qos_map->dscp_exception, pos, des_len);
9726 qos_map->num_des = num_des;
9727 for (des = 0; des < num_des; des++) {
9728 if (qos_map->dscp_exception[des].up > 7) {
9729 kfree(qos_map);
9730 return -EINVAL;
9731 }
9732 }
9733 pos += des_len;
9734 }
9735 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9736 }
9737
9738 wdev_lock(dev->ieee80211_ptr);
9739 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9740 if (!ret)
9741 ret = rdev_set_qos_map(rdev, dev, qos_map);
9742 wdev_unlock(dev->ieee80211_ptr);
9743
9744 kfree(qos_map);
9745 return ret;
9746}
9747
Johannes Berg960d01a2014-09-09 22:55:35 +03009748static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
9749{
9750 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9751 struct net_device *dev = info->user_ptr[1];
9752 struct wireless_dev *wdev = dev->ieee80211_ptr;
9753 const u8 *peer;
9754 u8 tsid, up;
9755 u16 admitted_time = 0;
9756 int err;
9757
Johannes Berg723e73a2014-10-22 09:25:06 +02009758 if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
Johannes Berg960d01a2014-09-09 22:55:35 +03009759 return -EOPNOTSUPP;
9760
9761 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
9762 !info->attrs[NL80211_ATTR_USER_PRIO])
9763 return -EINVAL;
9764
9765 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
9766 if (tsid >= IEEE80211_NUM_TIDS)
9767 return -EINVAL;
9768
9769 up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
9770 if (up >= IEEE80211_NUM_UPS)
9771 return -EINVAL;
9772
9773 /* WMM uses TIDs 0-7 even for TSPEC */
Johannes Berg723e73a2014-10-22 09:25:06 +02009774 if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
Johannes Berg960d01a2014-09-09 22:55:35 +03009775 /* TODO: handle 802.11 TSPEC/admission control
Johannes Berg723e73a2014-10-22 09:25:06 +02009776 * need more attributes for that (e.g. BA session requirement);
9777 * change the WMM adminssion test above to allow both then
Johannes Berg960d01a2014-09-09 22:55:35 +03009778 */
9779 return -EINVAL;
9780 }
9781
9782 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9783
9784 if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
9785 admitted_time =
9786 nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
9787 if (!admitted_time)
9788 return -EINVAL;
9789 }
9790
9791 wdev_lock(wdev);
9792 switch (wdev->iftype) {
9793 case NL80211_IFTYPE_STATION:
9794 case NL80211_IFTYPE_P2P_CLIENT:
9795 if (wdev->current_bss)
9796 break;
9797 err = -ENOTCONN;
9798 goto out;
9799 default:
9800 err = -EOPNOTSUPP;
9801 goto out;
9802 }
9803
9804 err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
9805
9806 out:
9807 wdev_unlock(wdev);
9808 return err;
9809}
9810
9811static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
9812{
9813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9814 struct net_device *dev = info->user_ptr[1];
9815 struct wireless_dev *wdev = dev->ieee80211_ptr;
9816 const u8 *peer;
9817 u8 tsid;
9818 int err;
9819
9820 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
9821 return -EINVAL;
9822
9823 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
9824 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9825
9826 wdev_lock(wdev);
9827 err = rdev_del_tx_ts(rdev, dev, tsid, peer);
9828 wdev_unlock(wdev);
9829
9830 return err;
9831}
9832
Arik Nemtsov1057d352014-11-19 12:54:26 +02009833static int nl80211_tdls_channel_switch(struct sk_buff *skb,
9834 struct genl_info *info)
9835{
9836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9837 struct net_device *dev = info->user_ptr[1];
9838 struct wireless_dev *wdev = dev->ieee80211_ptr;
9839 struct cfg80211_chan_def chandef = {};
9840 const u8 *addr;
9841 u8 oper_class;
9842 int err;
9843
9844 if (!rdev->ops->tdls_channel_switch ||
9845 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
9846 return -EOPNOTSUPP;
9847
9848 switch (dev->ieee80211_ptr->iftype) {
9849 case NL80211_IFTYPE_STATION:
9850 case NL80211_IFTYPE_P2P_CLIENT:
9851 break;
9852 default:
9853 return -EOPNOTSUPP;
9854 }
9855
9856 if (!info->attrs[NL80211_ATTR_MAC] ||
9857 !info->attrs[NL80211_ATTR_OPER_CLASS])
9858 return -EINVAL;
9859
9860 err = nl80211_parse_chandef(rdev, info, &chandef);
9861 if (err)
9862 return err;
9863
9864 /*
9865 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
9866 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
9867 * specification is not defined for them.
9868 */
9869 if (chandef.chan->band == IEEE80211_BAND_2GHZ &&
9870 chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
9871 chandef.width != NL80211_CHAN_WIDTH_20)
9872 return -EINVAL;
9873
9874 /* we will be active on the TDLS link */
9875 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, wdev->iftype))
9876 return -EINVAL;
9877
9878 /* don't allow switching to DFS channels */
9879 if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
9880 return -EINVAL;
9881
9882 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9883 oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
9884
9885 wdev_lock(wdev);
9886 err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
9887 wdev_unlock(wdev);
9888
9889 return err;
9890}
9891
9892static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
9893 struct genl_info *info)
9894{
9895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9896 struct net_device *dev = info->user_ptr[1];
9897 struct wireless_dev *wdev = dev->ieee80211_ptr;
9898 const u8 *addr;
9899
9900 if (!rdev->ops->tdls_channel_switch ||
9901 !rdev->ops->tdls_cancel_channel_switch ||
9902 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
9903 return -EOPNOTSUPP;
9904
9905 switch (dev->ieee80211_ptr->iftype) {
9906 case NL80211_IFTYPE_STATION:
9907 case NL80211_IFTYPE_P2P_CLIENT:
9908 break;
9909 default:
9910 return -EOPNOTSUPP;
9911 }
9912
9913 if (!info->attrs[NL80211_ATTR_MAC])
9914 return -EINVAL;
9915
9916 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9917
9918 wdev_lock(wdev);
9919 rdev_tdls_cancel_channel_switch(rdev, dev, addr);
9920 wdev_unlock(wdev);
9921
9922 return 0;
9923}
9924
Johannes Berg4c476992010-10-04 21:36:35 +02009925#define NL80211_FLAG_NEED_WIPHY 0x01
9926#define NL80211_FLAG_NEED_NETDEV 0x02
9927#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009928#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9929#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9930 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009931#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009932/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009933#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9934 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg5393b912014-09-10 15:00:16 +03009935#define NL80211_FLAG_CLEAR_SKB 0x20
Johannes Berg4c476992010-10-04 21:36:35 +02009936
Johannes Bergf84f7712013-11-14 17:14:45 +01009937static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009938 struct genl_info *info)
9939{
9940 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009941 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009942 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009943 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9944
9945 if (rtnl)
9946 rtnl_lock();
9947
9948 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009949 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009950 if (IS_ERR(rdev)) {
9951 if (rtnl)
9952 rtnl_unlock();
9953 return PTR_ERR(rdev);
9954 }
9955 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009956 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9957 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009958 ASSERT_RTNL();
9959
Johannes Berg89a54e42012-06-15 14:33:17 +02009960 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9961 info->attrs);
9962 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009963 if (rtnl)
9964 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009965 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009966 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009967
Johannes Berg89a54e42012-06-15 14:33:17 +02009968 dev = wdev->netdev;
Zhao, Gangf26cbf42014-04-21 12:53:03 +08009969 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg89a54e42012-06-15 14:33:17 +02009970
Johannes Berg1bf614e2012-06-15 15:23:36 +02009971 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9972 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009973 if (rtnl)
9974 rtnl_unlock();
9975 return -EINVAL;
9976 }
9977
9978 info->user_ptr[1] = dev;
9979 } else {
9980 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009981 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009982
Johannes Berg1bf614e2012-06-15 15:23:36 +02009983 if (dev) {
9984 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9985 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009986 if (rtnl)
9987 rtnl_unlock();
9988 return -ENETDOWN;
9989 }
9990
9991 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009992 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9993 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009994 if (rtnl)
9995 rtnl_unlock();
9996 return -ENETDOWN;
9997 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009998 }
9999
Johannes Berg4c476992010-10-04 21:36:35 +020010000 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +020010001 }
10002
10003 return 0;
10004}
10005
Johannes Bergf84f7712013-11-14 17:14:45 +010010006static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020010007 struct genl_info *info)
10008{
Johannes Berg1bf614e2012-06-15 15:23:36 +020010009 if (info->user_ptr[1]) {
10010 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
10011 struct wireless_dev *wdev = info->user_ptr[1];
10012
10013 if (wdev->netdev)
10014 dev_put(wdev->netdev);
10015 } else {
10016 dev_put(info->user_ptr[1]);
10017 }
10018 }
Johannes Berg5393b912014-09-10 15:00:16 +030010019
Johannes Berg4c476992010-10-04 21:36:35 +020010020 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
10021 rtnl_unlock();
Johannes Berg5393b912014-09-10 15:00:16 +030010022
10023 /* If needed, clear the netlink message payload from the SKB
10024 * as it might contain key data that shouldn't stick around on
10025 * the heap after the SKB is freed. The netlink message header
10026 * is still needed for further processing, so leave it intact.
10027 */
10028 if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
10029 struct nlmsghdr *nlh = nlmsg_hdr(skb);
10030
10031 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
10032 }
Johannes Berg4c476992010-10-04 21:36:35 +020010033}
10034
Johannes Berg4534de82013-11-14 17:14:46 +010010035static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -040010036 {
10037 .cmd = NL80211_CMD_GET_WIPHY,
10038 .doit = nl80211_get_wiphy,
10039 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +020010040 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -040010041 .policy = nl80211_policy,
10042 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +020010043 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10044 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010045 },
10046 {
10047 .cmd = NL80211_CMD_SET_WIPHY,
10048 .doit = nl80211_set_wiphy,
10049 .policy = nl80211_policy,
10050 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010051 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010052 },
10053 {
10054 .cmd = NL80211_CMD_GET_INTERFACE,
10055 .doit = nl80211_get_interface,
10056 .dumpit = nl80211_dump_interface,
10057 .policy = nl80211_policy,
10058 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +020010059 .internal_flags = NL80211_FLAG_NEED_WDEV |
10060 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010061 },
10062 {
10063 .cmd = NL80211_CMD_SET_INTERFACE,
10064 .doit = nl80211_set_interface,
10065 .policy = nl80211_policy,
10066 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010067 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10068 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010069 },
10070 {
10071 .cmd = NL80211_CMD_NEW_INTERFACE,
10072 .doit = nl80211_new_interface,
10073 .policy = nl80211_policy,
10074 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010075 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10076 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010077 },
10078 {
10079 .cmd = NL80211_CMD_DEL_INTERFACE,
10080 .doit = nl80211_del_interface,
10081 .policy = nl80211_policy,
10082 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +020010083 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +020010084 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -040010085 },
Johannes Berg41ade002007-12-19 02:03:29 +010010086 {
10087 .cmd = NL80211_CMD_GET_KEY,
10088 .doit = nl80211_get_key,
10089 .policy = nl80211_policy,
10090 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010092 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +010010093 },
10094 {
10095 .cmd = NL80211_CMD_SET_KEY,
10096 .doit = nl80211_set_key,
10097 .policy = nl80211_policy,
10098 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010099 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010100 NL80211_FLAG_NEED_RTNL |
10101 NL80211_FLAG_CLEAR_SKB,
Johannes Berg41ade002007-12-19 02:03:29 +010010102 },
10103 {
10104 .cmd = NL80211_CMD_NEW_KEY,
10105 .doit = nl80211_new_key,
10106 .policy = nl80211_policy,
10107 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010108 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010109 NL80211_FLAG_NEED_RTNL |
10110 NL80211_FLAG_CLEAR_SKB,
Johannes Berg41ade002007-12-19 02:03:29 +010010111 },
10112 {
10113 .cmd = NL80211_CMD_DEL_KEY,
10114 .doit = nl80211_del_key,
10115 .policy = nl80211_policy,
10116 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010117 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010118 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +010010119 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010010120 {
10121 .cmd = NL80211_CMD_SET_BEACON,
10122 .policy = nl80211_policy,
10123 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010010124 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010125 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010126 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010127 },
10128 {
Johannes Berg88600202012-02-13 15:17:18 +010010129 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010130 .policy = nl80211_policy,
10131 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010010132 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010133 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010134 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010135 },
10136 {
Johannes Berg88600202012-02-13 15:17:18 +010010137 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010138 .policy = nl80211_policy,
10139 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +010010140 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010141 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010142 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +010010143 },
Johannes Berg5727ef12007-12-19 02:03:34 +010010144 {
10145 .cmd = NL80211_CMD_GET_STATION,
10146 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010147 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +010010148 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +020010149 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10150 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010151 },
10152 {
10153 .cmd = NL80211_CMD_SET_STATION,
10154 .doit = nl80211_set_station,
10155 .policy = nl80211_policy,
10156 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010157 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010158 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010159 },
10160 {
10161 .cmd = NL80211_CMD_NEW_STATION,
10162 .doit = nl80211_new_station,
10163 .policy = nl80211_policy,
10164 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010165 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010166 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010167 },
10168 {
10169 .cmd = NL80211_CMD_DEL_STATION,
10170 .doit = nl80211_del_station,
10171 .policy = nl80211_policy,
10172 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010173 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010174 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +010010175 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010176 {
10177 .cmd = NL80211_CMD_GET_MPATH,
10178 .doit = nl80211_get_mpath,
10179 .dumpit = nl80211_dump_mpath,
10180 .policy = nl80211_policy,
10181 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010182 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010183 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010184 },
10185 {
Henning Rogge66be7d22014-09-12 08:58:49 +020010186 .cmd = NL80211_CMD_GET_MPP,
10187 .doit = nl80211_get_mpp,
10188 .dumpit = nl80211_dump_mpp,
10189 .policy = nl80211_policy,
10190 .flags = GENL_ADMIN_PERM,
10191 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10192 NL80211_FLAG_NEED_RTNL,
10193 },
10194 {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010195 .cmd = NL80211_CMD_SET_MPATH,
10196 .doit = nl80211_set_mpath,
10197 .policy = nl80211_policy,
10198 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010199 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010200 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010201 },
10202 {
10203 .cmd = NL80211_CMD_NEW_MPATH,
10204 .doit = nl80211_new_mpath,
10205 .policy = nl80211_policy,
10206 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010207 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010208 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010209 },
10210 {
10211 .cmd = NL80211_CMD_DEL_MPATH,
10212 .doit = nl80211_del_mpath,
10213 .policy = nl80211_policy,
10214 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010216 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010010217 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030010218 {
10219 .cmd = NL80211_CMD_SET_BSS,
10220 .doit = nl80211_set_bss,
10221 .policy = nl80211_policy,
10222 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010223 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010224 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +030010225 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070010226 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080010227 .cmd = NL80211_CMD_GET_REG,
10228 .doit = nl80211_get_reg,
10229 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +020010230 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -080010231 /* can be retrieved by unprivileged users */
10232 },
10233 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070010234 .cmd = NL80211_CMD_SET_REG,
10235 .doit = nl80211_set_reg,
10236 .policy = nl80211_policy,
10237 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +020010238 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070010239 },
10240 {
10241 .cmd = NL80211_CMD_REQ_SET_REG,
10242 .doit = nl80211_req_set_reg,
10243 .policy = nl80211_policy,
10244 .flags = GENL_ADMIN_PERM,
10245 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010246 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -080010247 .cmd = NL80211_CMD_GET_MESH_CONFIG,
10248 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010249 .policy = nl80211_policy,
10250 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010251 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010252 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010253 },
10254 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -080010255 .cmd = NL80211_CMD_SET_MESH_CONFIG,
10256 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010257 .policy = nl80211_policy,
10258 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +010010259 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010260 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -070010261 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +020010262 {
Johannes Berg2a519312009-02-10 21:25:55 +010010263 .cmd = NL80211_CMD_TRIGGER_SCAN,
10264 .doit = nl80211_trigger_scan,
10265 .policy = nl80211_policy,
10266 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +020010267 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010268 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +010010269 },
10270 {
10271 .cmd = NL80211_CMD_GET_SCAN,
10272 .policy = nl80211_policy,
10273 .dumpit = nl80211_dump_scan,
10274 },
Jouni Malinen636a5d32009-03-19 13:39:22 +020010275 {
Luciano Coelho807f8a82011-05-11 17:09:35 +030010276 .cmd = NL80211_CMD_START_SCHED_SCAN,
10277 .doit = nl80211_start_sched_scan,
10278 .policy = nl80211_policy,
10279 .flags = GENL_ADMIN_PERM,
10280 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10281 NL80211_FLAG_NEED_RTNL,
10282 },
10283 {
10284 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
10285 .doit = nl80211_stop_sched_scan,
10286 .policy = nl80211_policy,
10287 .flags = GENL_ADMIN_PERM,
10288 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10289 NL80211_FLAG_NEED_RTNL,
10290 },
10291 {
Jouni Malinen636a5d32009-03-19 13:39:22 +020010292 .cmd = NL80211_CMD_AUTHENTICATE,
10293 .doit = nl80211_authenticate,
10294 .policy = nl80211_policy,
10295 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010296 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010297 NL80211_FLAG_NEED_RTNL |
10298 NL80211_FLAG_CLEAR_SKB,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010299 },
10300 {
10301 .cmd = NL80211_CMD_ASSOCIATE,
10302 .doit = nl80211_associate,
10303 .policy = nl80211_policy,
10304 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010305 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010306 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010307 },
10308 {
10309 .cmd = NL80211_CMD_DEAUTHENTICATE,
10310 .doit = nl80211_deauthenticate,
10311 .policy = nl80211_policy,
10312 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010313 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010314 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010315 },
10316 {
10317 .cmd = NL80211_CMD_DISASSOCIATE,
10318 .doit = nl80211_disassociate,
10319 .policy = nl80211_policy,
10320 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010321 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010322 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +020010323 },
Johannes Berg04a773a2009-04-19 21:24:32 +020010324 {
10325 .cmd = NL80211_CMD_JOIN_IBSS,
10326 .doit = nl80211_join_ibss,
10327 .policy = nl80211_policy,
10328 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010329 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010330 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +020010331 },
10332 {
10333 .cmd = NL80211_CMD_LEAVE_IBSS,
10334 .doit = nl80211_leave_ibss,
10335 .policy = nl80211_policy,
10336 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010337 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010338 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +020010339 },
Johannes Bergaff89a92009-07-01 21:26:51 +020010340#ifdef CONFIG_NL80211_TESTMODE
10341 {
10342 .cmd = NL80211_CMD_TESTMODE,
10343 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -070010344 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +020010345 .policy = nl80211_policy,
10346 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010347 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10348 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +020010349 },
10350#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +020010351 {
10352 .cmd = NL80211_CMD_CONNECT,
10353 .doit = nl80211_connect,
10354 .policy = nl80211_policy,
10355 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010356 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010357 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +020010358 },
10359 {
10360 .cmd = NL80211_CMD_DISCONNECT,
10361 .doit = nl80211_disconnect,
10362 .policy = nl80211_policy,
10363 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +020010364 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010365 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +020010366 },
Johannes Berg463d0182009-07-14 00:33:35 +020010367 {
10368 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
10369 .doit = nl80211_wiphy_netns,
10370 .policy = nl80211_policy,
10371 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010372 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10373 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +020010374 },
Holger Schurig61fa7132009-11-11 12:25:40 +010010375 {
10376 .cmd = NL80211_CMD_GET_SURVEY,
10377 .policy = nl80211_policy,
10378 .dumpit = nl80211_dump_survey,
10379 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010380 {
10381 .cmd = NL80211_CMD_SET_PMKSA,
10382 .doit = nl80211_setdel_pmksa,
10383 .policy = nl80211_policy,
10384 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010385 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010386 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010387 },
10388 {
10389 .cmd = NL80211_CMD_DEL_PMKSA,
10390 .doit = nl80211_setdel_pmksa,
10391 .policy = nl80211_policy,
10392 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010393 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010394 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010395 },
10396 {
10397 .cmd = NL80211_CMD_FLUSH_PMKSA,
10398 .doit = nl80211_flush_pmksa,
10399 .policy = nl80211_policy,
10400 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010401 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010402 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +010010403 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010404 {
10405 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
10406 .doit = nl80211_remain_on_channel,
10407 .policy = nl80211_policy,
10408 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010409 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010410 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010411 },
10412 {
10413 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
10414 .doit = nl80211_cancel_remain_on_channel,
10415 .policy = nl80211_policy,
10416 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010417 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010418 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010419 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +020010420 {
10421 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
10422 .doit = nl80211_set_tx_bitrate_mask,
10423 .policy = nl80211_policy,
10424 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010425 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10426 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +020010427 },
Jouni Malinen026331c2010-02-15 12:53:10 +020010428 {
Johannes Berg2e161f72010-08-12 15:38:38 +020010429 .cmd = NL80211_CMD_REGISTER_FRAME,
10430 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +020010431 .policy = nl80211_policy,
10432 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010433 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +020010434 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +020010435 },
10436 {
Johannes Berg2e161f72010-08-12 15:38:38 +020010437 .cmd = NL80211_CMD_FRAME,
10438 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +020010439 .policy = nl80211_policy,
10440 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010441 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +020010442 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +020010443 },
Kalle Valoffb9eb32010-02-17 17:58:10 +020010444 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +010010445 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
10446 .doit = nl80211_tx_mgmt_cancel_wait,
10447 .policy = nl80211_policy,
10448 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +020010449 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +010010450 NL80211_FLAG_NEED_RTNL,
10451 },
10452 {
Kalle Valoffb9eb32010-02-17 17:58:10 +020010453 .cmd = NL80211_CMD_SET_POWER_SAVE,
10454 .doit = nl80211_set_power_save,
10455 .policy = nl80211_policy,
10456 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010457 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10458 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +020010459 },
10460 {
10461 .cmd = NL80211_CMD_GET_POWER_SAVE,
10462 .doit = nl80211_get_power_save,
10463 .policy = nl80211_policy,
10464 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +020010465 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10466 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +020010467 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010468 {
10469 .cmd = NL80211_CMD_SET_CQM,
10470 .doit = nl80211_set_cqm,
10471 .policy = nl80211_policy,
10472 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010473 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10474 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010475 },
Johannes Bergf444de02010-05-05 15:25:02 +020010476 {
10477 .cmd = NL80211_CMD_SET_CHANNEL,
10478 .doit = nl80211_set_channel,
10479 .policy = nl80211_policy,
10480 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +020010481 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10482 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +020010483 },
Bill Jordane8347eb2010-10-01 13:54:28 -040010484 {
10485 .cmd = NL80211_CMD_SET_WDS_PEER,
10486 .doit = nl80211_set_wds_peer,
10487 .policy = nl80211_policy,
10488 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +020010489 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10490 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -040010491 },
Johannes Berg29cbe682010-12-03 09:20:44 +010010492 {
10493 .cmd = NL80211_CMD_JOIN_MESH,
10494 .doit = nl80211_join_mesh,
10495 .policy = nl80211_policy,
10496 .flags = GENL_ADMIN_PERM,
10497 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10498 NL80211_FLAG_NEED_RTNL,
10499 },
10500 {
10501 .cmd = NL80211_CMD_LEAVE_MESH,
10502 .doit = nl80211_leave_mesh,
10503 .policy = nl80211_policy,
10504 .flags = GENL_ADMIN_PERM,
10505 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10506 NL80211_FLAG_NEED_RTNL,
10507 },
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +010010508 {
10509 .cmd = NL80211_CMD_JOIN_OCB,
10510 .doit = nl80211_join_ocb,
10511 .policy = nl80211_policy,
10512 .flags = GENL_ADMIN_PERM,
10513 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10514 NL80211_FLAG_NEED_RTNL,
10515 },
10516 {
10517 .cmd = NL80211_CMD_LEAVE_OCB,
10518 .doit = nl80211_leave_ocb,
10519 .policy = nl80211_policy,
10520 .flags = GENL_ADMIN_PERM,
10521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10522 NL80211_FLAG_NEED_RTNL,
10523 },
Johannes Bergdfb89c52012-06-27 09:23:48 +020010524#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +020010525 {
10526 .cmd = NL80211_CMD_GET_WOWLAN,
10527 .doit = nl80211_get_wowlan,
10528 .policy = nl80211_policy,
10529 /* can be retrieved by unprivileged users */
10530 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10531 NL80211_FLAG_NEED_RTNL,
10532 },
10533 {
10534 .cmd = NL80211_CMD_SET_WOWLAN,
10535 .doit = nl80211_set_wowlan,
10536 .policy = nl80211_policy,
10537 .flags = GENL_ADMIN_PERM,
10538 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10539 NL80211_FLAG_NEED_RTNL,
10540 },
Johannes Bergdfb89c52012-06-27 09:23:48 +020010541#endif
Johannes Berge5497d72011-07-05 16:35:40 +020010542 {
10543 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
10544 .doit = nl80211_set_rekey_data,
10545 .policy = nl80211_policy,
10546 .flags = GENL_ADMIN_PERM,
10547 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg5393b912014-09-10 15:00:16 +030010548 NL80211_FLAG_NEED_RTNL |
10549 NL80211_FLAG_CLEAR_SKB,
Johannes Berge5497d72011-07-05 16:35:40 +020010550 },
Arik Nemtsov109086c2011-09-28 14:12:50 +030010551 {
10552 .cmd = NL80211_CMD_TDLS_MGMT,
10553 .doit = nl80211_tdls_mgmt,
10554 .policy = nl80211_policy,
10555 .flags = GENL_ADMIN_PERM,
10556 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10557 NL80211_FLAG_NEED_RTNL,
10558 },
10559 {
10560 .cmd = NL80211_CMD_TDLS_OPER,
10561 .doit = nl80211_tdls_oper,
10562 .policy = nl80211_policy,
10563 .flags = GENL_ADMIN_PERM,
10564 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10565 NL80211_FLAG_NEED_RTNL,
10566 },
Johannes Berg28946da2011-11-04 11:18:12 +010010567 {
10568 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
10569 .doit = nl80211_register_unexpected_frame,
10570 .policy = nl80211_policy,
10571 .flags = GENL_ADMIN_PERM,
10572 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10573 NL80211_FLAG_NEED_RTNL,
10574 },
Johannes Berg7f6cf312011-11-04 11:18:15 +010010575 {
10576 .cmd = NL80211_CMD_PROBE_CLIENT,
10577 .doit = nl80211_probe_client,
10578 .policy = nl80211_policy,
10579 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +020010580 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +010010581 NL80211_FLAG_NEED_RTNL,
10582 },
Johannes Berg5e760232011-11-04 11:18:17 +010010583 {
10584 .cmd = NL80211_CMD_REGISTER_BEACONS,
10585 .doit = nl80211_register_beacons,
10586 .policy = nl80211_policy,
10587 .flags = GENL_ADMIN_PERM,
10588 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10589 NL80211_FLAG_NEED_RTNL,
10590 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +010010591 {
10592 .cmd = NL80211_CMD_SET_NOACK_MAP,
10593 .doit = nl80211_set_noack_map,
10594 .policy = nl80211_policy,
10595 .flags = GENL_ADMIN_PERM,
10596 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10597 NL80211_FLAG_NEED_RTNL,
10598 },
Johannes Berg98104fde2012-06-16 00:19:54 +020010599 {
10600 .cmd = NL80211_CMD_START_P2P_DEVICE,
10601 .doit = nl80211_start_p2p_device,
10602 .policy = nl80211_policy,
10603 .flags = GENL_ADMIN_PERM,
10604 .internal_flags = NL80211_FLAG_NEED_WDEV |
10605 NL80211_FLAG_NEED_RTNL,
10606 },
10607 {
10608 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
10609 .doit = nl80211_stop_p2p_device,
10610 .policy = nl80211_policy,
10611 .flags = GENL_ADMIN_PERM,
10612 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10613 NL80211_FLAG_NEED_RTNL,
10614 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +010010615 {
10616 .cmd = NL80211_CMD_SET_MCAST_RATE,
10617 .doit = nl80211_set_mcast_rate,
10618 .policy = nl80211_policy,
10619 .flags = GENL_ADMIN_PERM,
10620 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10621 NL80211_FLAG_NEED_RTNL,
10622 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +053010623 {
10624 .cmd = NL80211_CMD_SET_MAC_ACL,
10625 .doit = nl80211_set_mac_acl,
10626 .policy = nl80211_policy,
10627 .flags = GENL_ADMIN_PERM,
10628 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10629 NL80211_FLAG_NEED_RTNL,
10630 },
Simon Wunderlich04f39042013-02-08 18:16:19 +010010631 {
10632 .cmd = NL80211_CMD_RADAR_DETECT,
10633 .doit = nl80211_start_radar_detection,
10634 .policy = nl80211_policy,
10635 .flags = GENL_ADMIN_PERM,
10636 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10637 NL80211_FLAG_NEED_RTNL,
10638 },
Johannes Berg3713b4e2013-02-14 16:19:38 +010010639 {
10640 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
10641 .doit = nl80211_get_protocol_features,
10642 .policy = nl80211_policy,
10643 },
Jouni Malinen355199e2013-02-27 17:14:27 +020010644 {
10645 .cmd = NL80211_CMD_UPDATE_FT_IES,
10646 .doit = nl80211_update_ft_ies,
10647 .policy = nl80211_policy,
10648 .flags = GENL_ADMIN_PERM,
10649 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10650 NL80211_FLAG_NEED_RTNL,
10651 },
Arend van Spriel5de17982013-04-18 15:49:00 +020010652 {
10653 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
10654 .doit = nl80211_crit_protocol_start,
10655 .policy = nl80211_policy,
10656 .flags = GENL_ADMIN_PERM,
10657 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10658 NL80211_FLAG_NEED_RTNL,
10659 },
10660 {
10661 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
10662 .doit = nl80211_crit_protocol_stop,
10663 .policy = nl80211_policy,
10664 .flags = GENL_ADMIN_PERM,
10665 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10666 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -070010667 },
10668 {
10669 .cmd = NL80211_CMD_GET_COALESCE,
10670 .doit = nl80211_get_coalesce,
10671 .policy = nl80211_policy,
10672 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10673 NL80211_FLAG_NEED_RTNL,
10674 },
10675 {
10676 .cmd = NL80211_CMD_SET_COALESCE,
10677 .doit = nl80211_set_coalesce,
10678 .policy = nl80211_policy,
10679 .flags = GENL_ADMIN_PERM,
10680 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10681 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020010682 },
10683 {
10684 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10685 .doit = nl80211_channel_switch,
10686 .policy = nl80211_policy,
10687 .flags = GENL_ADMIN_PERM,
10688 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10689 NL80211_FLAG_NEED_RTNL,
10690 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010691 {
10692 .cmd = NL80211_CMD_VENDOR,
10693 .doit = nl80211_vendor_cmd,
10694 .policy = nl80211_policy,
10695 .flags = GENL_ADMIN_PERM,
10696 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10697 NL80211_FLAG_NEED_RTNL,
10698 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010699 {
10700 .cmd = NL80211_CMD_SET_QOS_MAP,
10701 .doit = nl80211_set_qos_map,
10702 .policy = nl80211_policy,
10703 .flags = GENL_ADMIN_PERM,
10704 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10705 NL80211_FLAG_NEED_RTNL,
10706 },
Johannes Berg960d01a2014-09-09 22:55:35 +030010707 {
10708 .cmd = NL80211_CMD_ADD_TX_TS,
10709 .doit = nl80211_add_tx_ts,
10710 .policy = nl80211_policy,
10711 .flags = GENL_ADMIN_PERM,
10712 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10713 NL80211_FLAG_NEED_RTNL,
10714 },
10715 {
10716 .cmd = NL80211_CMD_DEL_TX_TS,
10717 .doit = nl80211_del_tx_ts,
10718 .policy = nl80211_policy,
10719 .flags = GENL_ADMIN_PERM,
10720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10721 NL80211_FLAG_NEED_RTNL,
10722 },
Arik Nemtsov1057d352014-11-19 12:54:26 +020010723 {
10724 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
10725 .doit = nl80211_tdls_channel_switch,
10726 .policy = nl80211_policy,
10727 .flags = GENL_ADMIN_PERM,
10728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10729 NL80211_FLAG_NEED_RTNL,
10730 },
10731 {
10732 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
10733 .doit = nl80211_tdls_cancel_channel_switch,
10734 .policy = nl80211_policy,
10735 .flags = GENL_ADMIN_PERM,
10736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10737 NL80211_FLAG_NEED_RTNL,
10738 },
Johannes Berg55682962007-09-20 13:09:35 -040010739};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010740
Johannes Berg55682962007-09-20 13:09:35 -040010741/* notification functions */
10742
Johannes Berg3bb20552014-05-26 13:52:25 +020010743void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
10744 enum nl80211_commands cmd)
Johannes Berg55682962007-09-20 13:09:35 -040010745{
10746 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010747 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010748
Johannes Berg3bb20552014-05-26 13:52:25 +020010749 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
10750 cmd != NL80211_CMD_DEL_WIPHY);
10751
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010752 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010753 if (!msg)
10754 return;
10755
Johannes Berg3bb20552014-05-26 13:52:25 +020010756 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010757 nlmsg_free(msg);
10758 return;
10759 }
10760
Johannes Berg68eb5502013-11-19 15:19:38 +010010761 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010762 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010763}
10764
Johannes Berg362a4152009-05-24 16:43:15 +020010765static int nl80211_add_scan_req(struct sk_buff *msg,
10766 struct cfg80211_registered_device *rdev)
10767{
10768 struct cfg80211_scan_request *req = rdev->scan_req;
10769 struct nlattr *nest;
10770 int i;
10771
10772 if (WARN_ON(!req))
10773 return 0;
10774
10775 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10776 if (!nest)
10777 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010778 for (i = 0; i < req->n_ssids; i++) {
10779 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10780 goto nla_put_failure;
10781 }
Johannes Berg362a4152009-05-24 16:43:15 +020010782 nla_nest_end(msg, nest);
10783
10784 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10785 if (!nest)
10786 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010787 for (i = 0; i < req->n_channels; i++) {
10788 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10789 goto nla_put_failure;
10790 }
Johannes Berg362a4152009-05-24 16:43:15 +020010791 nla_nest_end(msg, nest);
10792
David S. Miller9360ffd2012-03-29 04:41:26 -040010793 if (req->ie &&
10794 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10795 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010796
Johannes Bergae917c92013-10-25 11:05:22 +020010797 if (req->flags &&
10798 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10799 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010800
Johannes Berg362a4152009-05-24 16:43:15 +020010801 return 0;
10802 nla_put_failure:
10803 return -ENOBUFS;
10804}
10805
Johannes Berga538e2d2009-06-16 19:56:42 +020010806static int nl80211_send_scan_msg(struct sk_buff *msg,
10807 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010808 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010809 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010810 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010811{
10812 void *hdr;
10813
Eric W. Biederman15e47302012-09-07 20:12:54 +000010814 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010815 if (!hdr)
10816 return -1;
10817
David S. Miller9360ffd2012-03-29 04:41:26 -040010818 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010819 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10820 wdev->netdev->ifindex)) ||
10821 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010822 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010823
Johannes Berg362a4152009-05-24 16:43:15 +020010824 /* ignore errors and send incomplete event anyway */
10825 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010826
10827 return genlmsg_end(msg, hdr);
10828
10829 nla_put_failure:
10830 genlmsg_cancel(msg, hdr);
10831 return -EMSGSIZE;
10832}
10833
Luciano Coelho807f8a82011-05-11 17:09:35 +030010834static int
10835nl80211_send_sched_scan_msg(struct sk_buff *msg,
10836 struct cfg80211_registered_device *rdev,
10837 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010838 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010839{
10840 void *hdr;
10841
Eric W. Biederman15e47302012-09-07 20:12:54 +000010842 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010843 if (!hdr)
10844 return -1;
10845
David S. Miller9360ffd2012-03-29 04:41:26 -040010846 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10847 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10848 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010849
10850 return genlmsg_end(msg, hdr);
10851
10852 nla_put_failure:
10853 genlmsg_cancel(msg, hdr);
10854 return -EMSGSIZE;
10855}
10856
Johannes Berga538e2d2009-06-16 19:56:42 +020010857void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010858 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010859{
10860 struct sk_buff *msg;
10861
Thomas Graf58050fc2012-06-28 03:57:45 +000010862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010863 if (!msg)
10864 return;
10865
Johannes Bergfd014282012-06-18 19:17:03 +020010866 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010867 NL80211_CMD_TRIGGER_SCAN) < 0) {
10868 nlmsg_free(msg);
10869 return;
10870 }
10871
Johannes Berg68eb5502013-11-19 15:19:38 +010010872 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010873 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010874}
10875
Johannes Bergf9d15d12014-01-22 11:14:19 +020010876struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10877 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010878{
10879 struct sk_buff *msg;
10880
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010882 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010883 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010884
Johannes Bergfd014282012-06-18 19:17:03 +020010885 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010886 aborted ? NL80211_CMD_SCAN_ABORTED :
10887 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010888 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010889 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010890 }
10891
Johannes Bergf9d15d12014-01-22 11:14:19 +020010892 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010893}
10894
Johannes Bergf9d15d12014-01-22 11:14:19 +020010895void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10896 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010897{
Johannes Berg2a519312009-02-10 21:25:55 +010010898 if (!msg)
10899 return;
10900
Johannes Berg68eb5502013-11-19 15:19:38 +010010901 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010902 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010903}
10904
Luciano Coelho807f8a82011-05-11 17:09:35 +030010905void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10906 struct net_device *netdev)
10907{
10908 struct sk_buff *msg;
10909
10910 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10911 if (!msg)
10912 return;
10913
10914 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10915 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10916 nlmsg_free(msg);
10917 return;
10918 }
10919
Johannes Berg68eb5502013-11-19 15:19:38 +010010920 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010921 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010922}
10923
10924void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10925 struct net_device *netdev, u32 cmd)
10926{
10927 struct sk_buff *msg;
10928
Thomas Graf58050fc2012-06-28 03:57:45 +000010929 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010930 if (!msg)
10931 return;
10932
10933 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10934 nlmsg_free(msg);
10935 return;
10936 }
10937
Johannes Berg68eb5502013-11-19 15:19:38 +010010938 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010939 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010940}
10941
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010942/*
10943 * This can happen on global regulatory changes or device specific settings
10944 * based on custom world regulatory domains.
10945 */
10946void nl80211_send_reg_change_event(struct regulatory_request *request)
10947{
10948 struct sk_buff *msg;
10949 void *hdr;
10950
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010951 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010952 if (!msg)
10953 return;
10954
10955 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10956 if (!hdr) {
10957 nlmsg_free(msg);
10958 return;
10959 }
10960
10961 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010962 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10963 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010964
David S. Miller9360ffd2012-03-29 04:41:26 -040010965 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10966 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10967 NL80211_REGDOM_TYPE_WORLD))
10968 goto nla_put_failure;
10969 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10970 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10971 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10972 goto nla_put_failure;
10973 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10974 request->intersect) {
10975 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10976 NL80211_REGDOM_TYPE_INTERSECTION))
10977 goto nla_put_failure;
10978 } else {
10979 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10980 NL80211_REGDOM_TYPE_COUNTRY) ||
10981 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10982 request->alpha2))
10983 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010984 }
10985
Johannes Bergf4173762012-12-03 18:23:37 +010010986 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010987 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10988 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010989
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010990 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010991
Johannes Bergbc43b282009-07-25 10:54:13 +020010992 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010993 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010994 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010995 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010996
10997 return;
10998
10999nla_put_failure:
11000 genlmsg_cancel(msg, hdr);
11001 nlmsg_free(msg);
11002}
11003
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011004static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
11005 struct net_device *netdev,
11006 const u8 *buf, size_t len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011007 enum nl80211_commands cmd, gfp_t gfp,
11008 int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011009{
11010 struct sk_buff *msg;
11011 void *hdr;
11012
Johannes Berge6d6e342009-07-01 21:26:47 +020011013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011014 if (!msg)
11015 return;
11016
11017 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11018 if (!hdr) {
11019 nlmsg_free(msg);
11020 return;
11021 }
11022
David S. Miller9360ffd2012-03-29 04:41:26 -040011023 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11024 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11025 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
11026 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011027
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011028 if (uapsd_queues >= 0) {
11029 struct nlattr *nla_wmm =
11030 nla_nest_start(msg, NL80211_ATTR_STA_WME);
11031 if (!nla_wmm)
11032 goto nla_put_failure;
11033
11034 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
11035 uapsd_queues))
11036 goto nla_put_failure;
11037
11038 nla_nest_end(msg, nla_wmm);
11039 }
11040
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011041 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011042
Johannes Berg68eb5502013-11-19 15:19:38 +010011043 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011044 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011045 return;
11046
11047 nla_put_failure:
11048 genlmsg_cancel(msg, hdr);
11049 nlmsg_free(msg);
11050}
11051
11052void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011053 struct net_device *netdev, const u8 *buf,
11054 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011055{
11056 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011057 NL80211_CMD_AUTHENTICATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011058}
11059
11060void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
11061 struct net_device *netdev, const u8 *buf,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011062 size_t len, gfp_t gfp, int uapsd_queues)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011063{
Johannes Berge6d6e342009-07-01 21:26:47 +020011064 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011065 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011066}
11067
Jouni Malinen53b46b82009-03-27 20:53:56 +020011068void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011069 struct net_device *netdev, const u8 *buf,
11070 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011071{
11072 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011073 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011074}
11075
Jouni Malinen53b46b82009-03-27 20:53:56 +020011076void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
11077 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020011078 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011079{
11080 nl80211_send_mlme_event(rdev, netdev, buf, len,
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011081 NL80211_CMD_DISASSOCIATE, gfp, -1);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011082}
11083
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011084void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
11085 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020011086{
Johannes Berg947add32013-02-22 22:05:20 +010011087 struct wireless_dev *wdev = dev->ieee80211_ptr;
11088 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011089 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011090 const struct ieee80211_mgmt *mgmt = (void *)buf;
11091 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020011092
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011093 if (WARN_ON(len < 2))
11094 return;
11095
11096 if (ieee80211_is_deauth(mgmt->frame_control))
11097 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
11098 else
11099 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
11100
11101 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
Eliad Pellerb0b6aa22014-09-09 17:09:45 +030011102 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
Jouni Malinencf4e5942010-12-16 00:52:40 +020011103}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020011104EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020011105
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040011106static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
11107 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020011108 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030011109{
11110 struct sk_buff *msg;
11111 void *hdr;
11112
Johannes Berge6d6e342009-07-01 21:26:47 +020011113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011114 if (!msg)
11115 return;
11116
11117 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11118 if (!hdr) {
11119 nlmsg_free(msg);
11120 return;
11121 }
11122
David S. Miller9360ffd2012-03-29 04:41:26 -040011123 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11124 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11125 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
11126 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11127 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030011128
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011129 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030011130
Johannes Berg68eb5502013-11-19 15:19:38 +010011131 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011132 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011133 return;
11134
11135 nla_put_failure:
11136 genlmsg_cancel(msg, hdr);
11137 nlmsg_free(msg);
11138}
11139
11140void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011141 struct net_device *netdev, const u8 *addr,
11142 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030011143{
11144 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020011145 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011146}
11147
11148void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020011149 struct net_device *netdev, const u8 *addr,
11150 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030011151{
Johannes Berge6d6e342009-07-01 21:26:47 +020011152 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
11153 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030011154}
11155
Samuel Ortizb23aa672009-07-01 21:26:54 +020011156void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
11157 struct net_device *netdev, const u8 *bssid,
11158 const u8 *req_ie, size_t req_ie_len,
11159 const u8 *resp_ie, size_t resp_ie_len,
11160 u16 status, gfp_t gfp)
11161{
11162 struct sk_buff *msg;
11163 void *hdr;
11164
Thomas Graf58050fc2012-06-28 03:57:45 +000011165 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011166 if (!msg)
11167 return;
11168
11169 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
11170 if (!hdr) {
11171 nlmsg_free(msg);
11172 return;
11173 }
11174
David S. Miller9360ffd2012-03-29 04:41:26 -040011175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11176 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11177 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
11178 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
11179 (req_ie &&
11180 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11181 (resp_ie &&
11182 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11183 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020011184
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011185 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011186
Johannes Berg68eb5502013-11-19 15:19:38 +010011187 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011188 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011189 return;
11190
11191 nla_put_failure:
11192 genlmsg_cancel(msg, hdr);
11193 nlmsg_free(msg);
11194
11195}
11196
11197void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
11198 struct net_device *netdev, const u8 *bssid,
11199 const u8 *req_ie, size_t req_ie_len,
11200 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
11201{
11202 struct sk_buff *msg;
11203 void *hdr;
11204
Thomas Graf58050fc2012-06-28 03:57:45 +000011205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011206 if (!msg)
11207 return;
11208
11209 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
11210 if (!hdr) {
11211 nlmsg_free(msg);
11212 return;
11213 }
11214
David S. Miller9360ffd2012-03-29 04:41:26 -040011215 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11216 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11217 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
11218 (req_ie &&
11219 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11220 (resp_ie &&
11221 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11222 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020011223
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011224 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011225
Johannes Berg68eb5502013-11-19 15:19:38 +010011226 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011227 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011228 return;
11229
11230 nla_put_failure:
11231 genlmsg_cancel(msg, hdr);
11232 nlmsg_free(msg);
11233
11234}
11235
11236void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
11237 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020011238 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020011239{
11240 struct sk_buff *msg;
11241 void *hdr;
11242
Thomas Graf58050fc2012-06-28 03:57:45 +000011243 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011244 if (!msg)
11245 return;
11246
11247 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
11248 if (!hdr) {
11249 nlmsg_free(msg);
11250 return;
11251 }
11252
David S. Miller9360ffd2012-03-29 04:41:26 -040011253 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11254 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11255 (from_ap && reason &&
11256 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
11257 (from_ap &&
11258 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
11259 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
11260 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020011261
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011262 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011263
Johannes Berg68eb5502013-11-19 15:19:38 +010011264 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011265 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020011266 return;
11267
11268 nla_put_failure:
11269 genlmsg_cancel(msg, hdr);
11270 nlmsg_free(msg);
11271
11272}
11273
Johannes Berg04a773a2009-04-19 21:24:32 +020011274void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
11275 struct net_device *netdev, const u8 *bssid,
11276 gfp_t gfp)
11277{
11278 struct sk_buff *msg;
11279 void *hdr;
11280
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070011281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020011282 if (!msg)
11283 return;
11284
11285 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
11286 if (!hdr) {
11287 nlmsg_free(msg);
11288 return;
11289 }
11290
David S. Miller9360ffd2012-03-29 04:41:26 -040011291 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11292 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11293 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11294 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020011295
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011296 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020011297
Johannes Berg68eb5502013-11-19 15:19:38 +010011298 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011299 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020011300 return;
11301
11302 nla_put_failure:
11303 genlmsg_cancel(msg, hdr);
11304 nlmsg_free(msg);
11305}
11306
Johannes Berg947add32013-02-22 22:05:20 +010011307void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
11308 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070011309{
Johannes Berg947add32013-02-22 22:05:20 +010011310 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011311 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011312 struct sk_buff *msg;
11313 void *hdr;
11314
Johannes Berg947add32013-02-22 22:05:20 +010011315 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
11316 return;
11317
11318 trace_cfg80211_notify_new_peer_candidate(dev, addr);
11319
Javier Cardonac93b5e72011-04-07 15:08:34 -070011320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11321 if (!msg)
11322 return;
11323
11324 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
11325 if (!hdr) {
11326 nlmsg_free(msg);
11327 return;
11328 }
11329
David S. Miller9360ffd2012-03-29 04:41:26 -040011330 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011331 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11332 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011333 (ie_len && ie &&
11334 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
11335 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070011336
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011337 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011338
Johannes Berg68eb5502013-11-19 15:19:38 +010011339 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011340 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011341 return;
11342
11343 nla_put_failure:
11344 genlmsg_cancel(msg, hdr);
11345 nlmsg_free(msg);
11346}
Johannes Berg947add32013-02-22 22:05:20 +010011347EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070011348
Jouni Malinena3b8b052009-03-27 21:59:49 +020011349void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
11350 struct net_device *netdev, const u8 *addr,
11351 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020011352 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020011353{
11354 struct sk_buff *msg;
11355 void *hdr;
11356
Johannes Berge6d6e342009-07-01 21:26:47 +020011357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020011358 if (!msg)
11359 return;
11360
11361 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
11362 if (!hdr) {
11363 nlmsg_free(msg);
11364 return;
11365 }
11366
David S. Miller9360ffd2012-03-29 04:41:26 -040011367 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11368 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11369 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
11370 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
11371 (key_id != -1 &&
11372 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
11373 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
11374 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020011375
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011376 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020011377
Johannes Berg68eb5502013-11-19 15:19:38 +010011378 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011379 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020011380 return;
11381
11382 nla_put_failure:
11383 genlmsg_cancel(msg, hdr);
11384 nlmsg_free(msg);
11385}
11386
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011387void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
11388 struct ieee80211_channel *channel_before,
11389 struct ieee80211_channel *channel_after)
11390{
11391 struct sk_buff *msg;
11392 void *hdr;
11393 struct nlattr *nl_freq;
11394
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070011395 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011396 if (!msg)
11397 return;
11398
11399 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
11400 if (!hdr) {
11401 nlmsg_free(msg);
11402 return;
11403 }
11404
11405 /*
11406 * Since we are applying the beacon hint to a wiphy we know its
11407 * wiphy_idx is valid
11408 */
David S. Miller9360ffd2012-03-29 04:41:26 -040011409 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
11410 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011411
11412 /* Before */
11413 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
11414 if (!nl_freq)
11415 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010011416 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011417 goto nla_put_failure;
11418 nla_nest_end(msg, nl_freq);
11419
11420 /* After */
11421 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
11422 if (!nl_freq)
11423 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010011424 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011425 goto nla_put_failure;
11426 nla_nest_end(msg, nl_freq);
11427
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011428 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011429
Johannes Berg463d0182009-07-14 00:33:35 +020011430 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010011431 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011432 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020011433 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040011434
11435 return;
11436
11437nla_put_failure:
11438 genlmsg_cancel(msg, hdr);
11439 nlmsg_free(msg);
11440}
11441
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011442static void nl80211_send_remain_on_chan_event(
11443 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020011444 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011445 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011446 unsigned int duration, gfp_t gfp)
11447{
11448 struct sk_buff *msg;
11449 void *hdr;
11450
11451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11452 if (!msg)
11453 return;
11454
11455 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11456 if (!hdr) {
11457 nlmsg_free(msg);
11458 return;
11459 }
11460
David S. Miller9360ffd2012-03-29 04:41:26 -040011461 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011462 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11463 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020011464 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011465 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010011466 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
11467 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011468 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
11469 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011470
David S. Miller9360ffd2012-03-29 04:41:26 -040011471 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
11472 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
11473 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011474
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011475 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011476
Johannes Berg68eb5502013-11-19 15:19:38 +010011477 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011478 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011479 return;
11480
11481 nla_put_failure:
11482 genlmsg_cancel(msg, hdr);
11483 nlmsg_free(msg);
11484}
11485
Johannes Berg947add32013-02-22 22:05:20 +010011486void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
11487 struct ieee80211_channel *chan,
11488 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011489{
Johannes Berg947add32013-02-22 22:05:20 +010011490 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011491 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011492
11493 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011494 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020011495 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010011496 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011497}
Johannes Berg947add32013-02-22 22:05:20 +010011498EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011499
Johannes Berg947add32013-02-22 22:05:20 +010011500void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
11501 struct ieee80211_channel *chan,
11502 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011503{
Johannes Berg947add32013-02-22 22:05:20 +010011504 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011505 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011506
11507 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011508 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010011509 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011510}
Johannes Berg947add32013-02-22 22:05:20 +010011511EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010011512
Johannes Berg947add32013-02-22 22:05:20 +010011513void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
11514 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010011515{
Johannes Berg947add32013-02-22 22:05:20 +010011516 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011517 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010011518 struct sk_buff *msg;
11519
Johannes Berg947add32013-02-22 22:05:20 +010011520 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
11521
Thomas Graf58050fc2012-06-28 03:57:45 +000011522 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010011523 if (!msg)
11524 return;
11525
John W. Linville66266b32012-03-15 13:25:41 -040011526 if (nl80211_send_station(msg, 0, 0, 0,
11527 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010011528 nlmsg_free(msg);
11529 return;
11530 }
11531
Johannes Berg68eb5502013-11-19 15:19:38 +010011532 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011533 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010011534}
Johannes Berg947add32013-02-22 22:05:20 +010011535EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010011536
Johannes Berg947add32013-02-22 22:05:20 +010011537void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020011538{
Johannes Berg947add32013-02-22 22:05:20 +010011539 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011540 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020011541 struct sk_buff *msg;
11542 void *hdr;
11543
Johannes Berg947add32013-02-22 22:05:20 +010011544 trace_cfg80211_del_sta(dev, mac_addr);
11545
Thomas Graf58050fc2012-06-28 03:57:45 +000011546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020011547 if (!msg)
11548 return;
11549
11550 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
11551 if (!hdr) {
11552 nlmsg_free(msg);
11553 return;
11554 }
11555
David S. Miller9360ffd2012-03-29 04:41:26 -040011556 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11557 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
11558 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020011559
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011560 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020011561
Johannes Berg68eb5502013-11-19 15:19:38 +010011562 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011563 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020011564 return;
11565
11566 nla_put_failure:
11567 genlmsg_cancel(msg, hdr);
11568 nlmsg_free(msg);
11569}
Johannes Berg947add32013-02-22 22:05:20 +010011570EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020011571
Johannes Berg947add32013-02-22 22:05:20 +010011572void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
11573 enum nl80211_connect_failed_reason reason,
11574 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011575{
Johannes Berg947add32013-02-22 22:05:20 +010011576 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011577 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011578 struct sk_buff *msg;
11579 void *hdr;
11580
11581 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11582 if (!msg)
11583 return;
11584
11585 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
11586 if (!hdr) {
11587 nlmsg_free(msg);
11588 return;
11589 }
11590
11591 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11592 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
11593 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
11594 goto nla_put_failure;
11595
11596 genlmsg_end(msg, hdr);
11597
Johannes Berg68eb5502013-11-19 15:19:38 +010011598 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011599 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011600 return;
11601
11602 nla_put_failure:
11603 genlmsg_cancel(msg, hdr);
11604 nlmsg_free(msg);
11605}
Johannes Berg947add32013-02-22 22:05:20 +010011606EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053011607
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011608static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
11609 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010011610{
11611 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011612 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg28946da2011-11-04 11:18:12 +010011613 struct sk_buff *msg;
11614 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000011615 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010011616
Eric W. Biederman15e47302012-09-07 20:12:54 +000011617 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010011618 return false;
11619
11620 msg = nlmsg_new(100, gfp);
11621 if (!msg)
11622 return true;
11623
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011624 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010011625 if (!hdr) {
11626 nlmsg_free(msg);
11627 return true;
11628 }
11629
David S. Miller9360ffd2012-03-29 04:41:26 -040011630 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11631 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11632 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11633 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010011634
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011635 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000011636 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010011637 return true;
11638
11639 nla_put_failure:
11640 genlmsg_cancel(msg, hdr);
11641 nlmsg_free(msg);
11642 return true;
11643}
11644
Johannes Berg947add32013-02-22 22:05:20 +010011645bool cfg80211_rx_spurious_frame(struct net_device *dev,
11646 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011647{
Johannes Berg947add32013-02-22 22:05:20 +010011648 struct wireless_dev *wdev = dev->ieee80211_ptr;
11649 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011650
Johannes Berg947add32013-02-22 22:05:20 +010011651 trace_cfg80211_rx_spurious_frame(dev, addr);
11652
11653 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11654 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
11655 trace_cfg80211_return_bool(false);
11656 return false;
11657 }
11658 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
11659 addr, gfp);
11660 trace_cfg80211_return_bool(ret);
11661 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011662}
Johannes Berg947add32013-02-22 22:05:20 +010011663EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
11664
11665bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
11666 const u8 *addr, gfp_t gfp)
11667{
11668 struct wireless_dev *wdev = dev->ieee80211_ptr;
11669 bool ret;
11670
11671 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
11672
11673 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11674 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
11675 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
11676 trace_cfg80211_return_bool(false);
11677 return false;
11678 }
11679 ret = __nl80211_unexpected_frame(dev,
11680 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
11681 addr, gfp);
11682 trace_cfg80211_return_bool(ret);
11683 return ret;
11684}
11685EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010011686
Johannes Berg2e161f72010-08-12 15:38:38 +020011687int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000011688 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010011689 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030011690 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011691{
Johannes Berg71bbc992012-06-15 15:30:18 +020011692 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011693 struct sk_buff *msg;
11694 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020011695
11696 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11697 if (!msg)
11698 return -ENOMEM;
11699
Johannes Berg2e161f72010-08-12 15:38:38 +020011700 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020011701 if (!hdr) {
11702 nlmsg_free(msg);
11703 return -ENOMEM;
11704 }
11705
David S. Miller9360ffd2012-03-29 04:41:26 -040011706 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011707 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11708 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011709 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011710 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
11711 (sig_dbm &&
11712 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030011713 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11714 (flags &&
11715 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040011716 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011717
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011718 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011719
Eric W. Biederman15e47302012-09-07 20:12:54 +000011720 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020011721
11722 nla_put_failure:
11723 genlmsg_cancel(msg, hdr);
11724 nlmsg_free(msg);
11725 return -ENOBUFS;
11726}
11727
Johannes Berg947add32013-02-22 22:05:20 +010011728void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
11729 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011730{
Johannes Berg947add32013-02-22 22:05:20 +010011731 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011732 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020011733 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011734 struct sk_buff *msg;
11735 void *hdr;
11736
Johannes Berg947add32013-02-22 22:05:20 +010011737 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11738
Jouni Malinen026331c2010-02-15 12:53:10 +020011739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11740 if (!msg)
11741 return;
11742
Johannes Berg2e161f72010-08-12 15:38:38 +020011743 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011744 if (!hdr) {
11745 nlmsg_free(msg);
11746 return;
11747 }
11748
David S. Miller9360ffd2012-03-29 04:41:26 -040011749 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011750 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11751 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011752 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011753 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11754 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11755 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11756 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011757
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011758 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011759
Johannes Berg68eb5502013-11-19 15:19:38 +010011760 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011761 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011762 return;
11763
11764 nla_put_failure:
11765 genlmsg_cancel(msg, hdr);
11766 nlmsg_free(msg);
11767}
Johannes Berg947add32013-02-22 22:05:20 +010011768EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011769
Johannes Berg5b97f492014-11-26 12:37:43 +010011770static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
11771 const char *mac, gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011772{
Johannes Berg947add32013-02-22 22:05:20 +010011773 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg5b97f492014-11-26 12:37:43 +010011774 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
11775 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11776 void **cb;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011777
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011778 if (!msg)
Johannes Berg5b97f492014-11-26 12:37:43 +010011779 return NULL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011780
Johannes Berg5b97f492014-11-26 12:37:43 +010011781 cb = (void **)msg->cb;
11782
11783 cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11784 if (!cb[0]) {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011785 nlmsg_free(msg);
Johannes Berg5b97f492014-11-26 12:37:43 +010011786 return NULL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011787 }
11788
David S. Miller9360ffd2012-03-29 04:41:26 -040011789 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011790 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011791 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011792
Johannes Berg5b97f492014-11-26 12:37:43 +010011793 if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011794 goto nla_put_failure;
11795
Johannes Berg5b97f492014-11-26 12:37:43 +010011796 cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
11797 if (!cb[1])
11798 goto nla_put_failure;
11799
11800 cb[2] = rdev;
11801
11802 return msg;
11803 nla_put_failure:
11804 nlmsg_free(msg);
11805 return NULL;
11806}
11807
11808static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
11809{
11810 void **cb = (void **)msg->cb;
11811 struct cfg80211_registered_device *rdev = cb[2];
11812
11813 nla_nest_end(msg, cb[1]);
11814 genlmsg_end(msg, cb[0]);
11815
11816 memset(msg->cb, 0, sizeof(msg->cb));
11817
11818 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11819 NL80211_MCGRP_MLME, gfp);
11820}
11821
11822void cfg80211_cqm_rssi_notify(struct net_device *dev,
11823 enum nl80211_cqm_rssi_threshold_event rssi_event,
11824 gfp_t gfp)
11825{
11826 struct sk_buff *msg;
11827
11828 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11829
Johannes Berg98f03342014-11-26 12:42:02 +010011830 if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
11831 rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
11832 return;
11833
Johannes Berg5b97f492014-11-26 12:37:43 +010011834 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
11835 if (!msg)
11836 return;
11837
David S. Miller9360ffd2012-03-29 04:41:26 -040011838 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11839 rssi_event))
11840 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011841
Johannes Berg5b97f492014-11-26 12:37:43 +010011842 cfg80211_send_cqm(msg, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011843
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011844 return;
11845
11846 nla_put_failure:
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011847 nlmsg_free(msg);
11848}
Johannes Berg947add32013-02-22 22:05:20 +010011849EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011850
Johannes Berg5b97f492014-11-26 12:37:43 +010011851void cfg80211_cqm_txe_notify(struct net_device *dev,
11852 const u8 *peer, u32 num_packets,
11853 u32 rate, u32 intvl, gfp_t gfp)
11854{
11855 struct sk_buff *msg;
11856
11857 msg = cfg80211_prepare_cqm(dev, peer, gfp);
11858 if (!msg)
11859 return;
11860
11861 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11862 goto nla_put_failure;
11863
11864 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11865 goto nla_put_failure;
11866
11867 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11868 goto nla_put_failure;
11869
11870 cfg80211_send_cqm(msg, gfp);
11871 return;
11872
11873 nla_put_failure:
11874 nlmsg_free(msg);
11875}
11876EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
11877
11878void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11879 const u8 *peer, u32 num_packets, gfp_t gfp)
11880{
11881 struct sk_buff *msg;
11882
11883 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11884
11885 msg = cfg80211_prepare_cqm(dev, peer, gfp);
11886 if (!msg)
11887 return;
11888
11889 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11890 goto nla_put_failure;
11891
11892 cfg80211_send_cqm(msg, gfp);
11893 return;
11894
11895 nla_put_failure:
11896 nlmsg_free(msg);
11897}
11898EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
11899
Johannes Berg98f03342014-11-26 12:42:02 +010011900void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
11901{
11902 struct sk_buff *msg;
11903
11904 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
11905 if (!msg)
11906 return;
11907
11908 if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
11909 goto nla_put_failure;
11910
11911 cfg80211_send_cqm(msg, gfp);
11912 return;
11913
11914 nla_put_failure:
11915 nlmsg_free(msg);
11916}
11917EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
11918
Johannes Berg947add32013-02-22 22:05:20 +010011919static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11920 struct net_device *netdev, const u8 *bssid,
11921 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011922{
11923 struct sk_buff *msg;
11924 struct nlattr *rekey_attr;
11925 void *hdr;
11926
Thomas Graf58050fc2012-06-28 03:57:45 +000011927 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011928 if (!msg)
11929 return;
11930
11931 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11932 if (!hdr) {
11933 nlmsg_free(msg);
11934 return;
11935 }
11936
David S. Miller9360ffd2012-03-29 04:41:26 -040011937 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11938 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11939 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11940 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011941
11942 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11943 if (!rekey_attr)
11944 goto nla_put_failure;
11945
David S. Miller9360ffd2012-03-29 04:41:26 -040011946 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11947 NL80211_REPLAY_CTR_LEN, replay_ctr))
11948 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011949
11950 nla_nest_end(msg, rekey_attr);
11951
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011952 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011953
Johannes Berg68eb5502013-11-19 15:19:38 +010011954 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011955 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011956 return;
11957
11958 nla_put_failure:
11959 genlmsg_cancel(msg, hdr);
11960 nlmsg_free(msg);
11961}
11962
Johannes Berg947add32013-02-22 22:05:20 +010011963void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11964 const u8 *replay_ctr, gfp_t gfp)
11965{
11966 struct wireless_dev *wdev = dev->ieee80211_ptr;
11967 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080011968 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010011969
11970 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11971 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11972}
11973EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11974
11975static void
11976nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11977 struct net_device *netdev, int index,
11978 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011979{
11980 struct sk_buff *msg;
11981 struct nlattr *attr;
11982 void *hdr;
11983
Thomas Graf58050fc2012-06-28 03:57:45 +000011984 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011985 if (!msg)
11986 return;
11987
11988 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11989 if (!hdr) {
11990 nlmsg_free(msg);
11991 return;
11992 }
11993
David S. Miller9360ffd2012-03-29 04:41:26 -040011994 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11995 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11996 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011997
11998 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11999 if (!attr)
12000 goto nla_put_failure;
12001
David S. Miller9360ffd2012-03-29 04:41:26 -040012002 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
12003 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
12004 (preauth &&
12005 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
12006 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030012007
12008 nla_nest_end(msg, attr);
12009
Johannes Berg3b7b72e2011-10-22 19:05:51 +020012010 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030012011
Johannes Berg68eb5502013-11-19 15:19:38 +010012012 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012013 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030012014 return;
12015
12016 nla_put_failure:
12017 genlmsg_cancel(msg, hdr);
12018 nlmsg_free(msg);
12019}
12020
Johannes Berg947add32013-02-22 22:05:20 +010012021void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
12022 const u8 *bssid, bool preauth, gfp_t gfp)
12023{
12024 struct wireless_dev *wdev = dev->ieee80211_ptr;
12025 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012026 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010012027
12028 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
12029 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
12030}
12031EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
12032
12033static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
12034 struct net_device *netdev,
12035 struct cfg80211_chan_def *chandef,
Luciano Coelhof8d75522014-11-07 14:31:35 +020012036 gfp_t gfp,
12037 enum nl80211_commands notif,
12038 u8 count)
Thomas Pedersen53145262012-04-06 13:35:47 -070012039{
12040 struct sk_buff *msg;
12041 void *hdr;
12042
Thomas Graf58050fc2012-06-28 03:57:45 +000012043 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070012044 if (!msg)
12045 return;
12046
Luciano Coelhof8d75522014-11-07 14:31:35 +020012047 hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
Thomas Pedersen53145262012-04-06 13:35:47 -070012048 if (!hdr) {
12049 nlmsg_free(msg);
12050 return;
12051 }
12052
Johannes Berg683b6d32012-11-08 21:25:48 +010012053 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
12054 goto nla_put_failure;
12055
12056 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040012057 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070012058
Luciano Coelhof8d75522014-11-07 14:31:35 +020012059 if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
12060 (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
12061 goto nla_put_failure;
12062
Thomas Pedersen53145262012-04-06 13:35:47 -070012063 genlmsg_end(msg, hdr);
12064
Johannes Berg68eb5502013-11-19 15:19:38 +010012065 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012066 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070012067 return;
12068
12069 nla_put_failure:
12070 genlmsg_cancel(msg, hdr);
12071 nlmsg_free(msg);
12072}
12073
Johannes Berg947add32013-02-22 22:05:20 +010012074void cfg80211_ch_switch_notify(struct net_device *dev,
12075 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070012076{
Johannes Berg947add32013-02-22 22:05:20 +010012077 struct wireless_dev *wdev = dev->ieee80211_ptr;
12078 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012079 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg947add32013-02-22 22:05:20 +010012080
Simon Wunderliche487eae2013-11-21 18:19:51 +010012081 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010012082
Simon Wunderliche487eae2013-11-21 18:19:51 +010012083 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010012084
Michal Kazior9e0e2962014-01-29 14:22:27 +010012085 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010012086 wdev->preset_chandef = *chandef;
Luciano Coelhof8d75522014-11-07 14:31:35 +020012087 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
12088 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
Johannes Berg947add32013-02-22 22:05:20 +010012089}
12090EXPORT_SYMBOL(cfg80211_ch_switch_notify);
12091
Luciano Coelhof8d75522014-11-07 14:31:35 +020012092void cfg80211_ch_switch_started_notify(struct net_device *dev,
12093 struct cfg80211_chan_def *chandef,
12094 u8 count)
12095{
12096 struct wireless_dev *wdev = dev->ieee80211_ptr;
12097 struct wiphy *wiphy = wdev->wiphy;
12098 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12099
12100 trace_cfg80211_ch_switch_started_notify(dev, chandef);
12101
12102 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
12103 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
12104}
12105EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
12106
Thomas Pedersen84f10702012-07-12 16:17:33 -070012107void
Simon Wunderlich04f39042013-02-08 18:16:19 +010012108nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010012109 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010012110 enum nl80211_radar_event event,
12111 struct net_device *netdev, gfp_t gfp)
12112{
12113 struct sk_buff *msg;
12114 void *hdr;
12115
12116 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12117 if (!msg)
12118 return;
12119
12120 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
12121 if (!hdr) {
12122 nlmsg_free(msg);
12123 return;
12124 }
12125
12126 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
12127 goto nla_put_failure;
12128
12129 /* NOP and radar events don't need a netdev parameter */
12130 if (netdev) {
12131 struct wireless_dev *wdev = netdev->ieee80211_ptr;
12132
12133 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12134 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12135 goto nla_put_failure;
12136 }
12137
12138 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
12139 goto nla_put_failure;
12140
12141 if (nl80211_send_chandef(msg, chandef))
12142 goto nla_put_failure;
12143
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012144 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010012145
Johannes Berg68eb5502013-11-19 15:19:38 +010012146 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012147 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010012148 return;
12149
12150 nla_put_failure:
12151 genlmsg_cancel(msg, hdr);
12152 nlmsg_free(msg);
12153}
12154
Johannes Berg7f6cf312011-11-04 11:18:15 +010012155void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
12156 u64 cookie, bool acked, gfp_t gfp)
12157{
12158 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012159 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg7f6cf312011-11-04 11:18:15 +010012160 struct sk_buff *msg;
12161 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010012162
Beni Lev4ee3e062012-08-27 12:49:39 +030012163 trace_cfg80211_probe_status(dev, addr, cookie, acked);
12164
Thomas Graf58050fc2012-06-28 03:57:45 +000012165 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030012166
Johannes Berg7f6cf312011-11-04 11:18:15 +010012167 if (!msg)
12168 return;
12169
12170 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
12171 if (!hdr) {
12172 nlmsg_free(msg);
12173 return;
12174 }
12175
David S. Miller9360ffd2012-03-29 04:41:26 -040012176 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12177 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12178 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12179 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
12180 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
12181 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010012182
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012183 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010012184
Johannes Berg68eb5502013-11-19 15:19:38 +010012185 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012186 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010012187 return;
12188
12189 nla_put_failure:
12190 genlmsg_cancel(msg, hdr);
12191 nlmsg_free(msg);
12192}
12193EXPORT_SYMBOL(cfg80211_probe_status);
12194
Johannes Berg5e760232011-11-04 11:18:17 +010012195void cfg80211_report_obss_beacon(struct wiphy *wiphy,
12196 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070012197 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010012198{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012199 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg5e760232011-11-04 11:18:17 +010012200 struct sk_buff *msg;
12201 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070012202 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010012203
Beni Lev4ee3e062012-08-27 12:49:39 +030012204 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
12205
Ben Greear37c73b52012-10-26 14:49:25 -070012206 spin_lock_bh(&rdev->beacon_registrations_lock);
12207 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12208 msg = nlmsg_new(len + 100, GFP_ATOMIC);
12209 if (!msg) {
12210 spin_unlock_bh(&rdev->beacon_registrations_lock);
12211 return;
12212 }
Johannes Berg5e760232011-11-04 11:18:17 +010012213
Ben Greear37c73b52012-10-26 14:49:25 -070012214 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
12215 if (!hdr)
12216 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010012217
Ben Greear37c73b52012-10-26 14:49:25 -070012218 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12219 (freq &&
12220 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
12221 (sig_dbm &&
12222 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
12223 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
12224 goto nla_put_failure;
12225
12226 genlmsg_end(msg, hdr);
12227
12228 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010012229 }
Ben Greear37c73b52012-10-26 14:49:25 -070012230 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010012231 return;
12232
12233 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070012234 spin_unlock_bh(&rdev->beacon_registrations_lock);
12235 if (hdr)
12236 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010012237 nlmsg_free(msg);
12238}
12239EXPORT_SYMBOL(cfg80211_report_obss_beacon);
12240
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012241#ifdef CONFIG_PM
Luciano Coelho8cd4d452014-09-17 11:55:28 +030012242static int cfg80211_net_detect_results(struct sk_buff *msg,
12243 struct cfg80211_wowlan_wakeup *wakeup)
12244{
12245 struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
12246 struct nlattr *nl_results, *nl_match, *nl_freqs;
12247 int i, j;
12248
12249 nl_results = nla_nest_start(
12250 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
12251 if (!nl_results)
12252 return -EMSGSIZE;
12253
12254 for (i = 0; i < nd->n_matches; i++) {
12255 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
12256
12257 nl_match = nla_nest_start(msg, i);
12258 if (!nl_match)
12259 break;
12260
12261 /* The SSID attribute is optional in nl80211, but for
12262 * simplicity reasons it's always present in the
12263 * cfg80211 structure. If a driver can't pass the
12264 * SSID, that needs to be changed. A zero length SSID
12265 * is still a valid SSID (wildcard), so it cannot be
12266 * used for this purpose.
12267 */
12268 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
12269 match->ssid.ssid)) {
12270 nla_nest_cancel(msg, nl_match);
12271 goto out;
12272 }
12273
12274 if (match->n_channels) {
12275 nl_freqs = nla_nest_start(
12276 msg, NL80211_ATTR_SCAN_FREQUENCIES);
12277 if (!nl_freqs) {
12278 nla_nest_cancel(msg, nl_match);
12279 goto out;
12280 }
12281
12282 for (j = 0; j < match->n_channels; j++) {
12283 if (nla_put_u32(msg,
12284 NL80211_ATTR_WIPHY_FREQ,
12285 match->channels[j])) {
12286 nla_nest_cancel(msg, nl_freqs);
12287 nla_nest_cancel(msg, nl_match);
12288 goto out;
12289 }
12290 }
12291
12292 nla_nest_end(msg, nl_freqs);
12293 }
12294
12295 nla_nest_end(msg, nl_match);
12296 }
12297
12298out:
12299 nla_nest_end(msg, nl_results);
12300 return 0;
12301}
12302
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012303void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
12304 struct cfg80211_wowlan_wakeup *wakeup,
12305 gfp_t gfp)
12306{
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012307 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012308 struct sk_buff *msg;
12309 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012310 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012311
12312 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
12313
12314 if (wakeup)
12315 size += wakeup->packet_present_len;
12316
12317 msg = nlmsg_new(size, gfp);
12318 if (!msg)
12319 return;
12320
12321 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
12322 if (!hdr)
12323 goto free_msg;
12324
12325 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12326 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12327 goto free_msg;
12328
12329 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12330 wdev->netdev->ifindex))
12331 goto free_msg;
12332
12333 if (wakeup) {
12334 struct nlattr *reasons;
12335
12336 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020012337 if (!reasons)
12338 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012339
12340 if (wakeup->disconnect &&
12341 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
12342 goto free_msg;
12343 if (wakeup->magic_pkt &&
12344 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
12345 goto free_msg;
12346 if (wakeup->gtk_rekey_failure &&
12347 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
12348 goto free_msg;
12349 if (wakeup->eap_identity_req &&
12350 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
12351 goto free_msg;
12352 if (wakeup->four_way_handshake &&
12353 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
12354 goto free_msg;
12355 if (wakeup->rfkill_release &&
12356 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
12357 goto free_msg;
12358
12359 if (wakeup->pattern_idx >= 0 &&
12360 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
12361 wakeup->pattern_idx))
12362 goto free_msg;
12363
Johannes Bergae917c92013-10-25 11:05:22 +020012364 if (wakeup->tcp_match &&
12365 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
12366 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010012367
Johannes Bergae917c92013-10-25 11:05:22 +020012368 if (wakeup->tcp_connlost &&
12369 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
12370 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010012371
Johannes Bergae917c92013-10-25 11:05:22 +020012372 if (wakeup->tcp_nomoretokens &&
12373 nla_put_flag(msg,
12374 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
12375 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010012376
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012377 if (wakeup->packet) {
12378 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
12379 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
12380
12381 if (!wakeup->packet_80211) {
12382 pkt_attr =
12383 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
12384 len_attr =
12385 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
12386 }
12387
12388 if (wakeup->packet_len &&
12389 nla_put_u32(msg, len_attr, wakeup->packet_len))
12390 goto free_msg;
12391
12392 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
12393 wakeup->packet))
12394 goto free_msg;
12395 }
12396
Luciano Coelho8cd4d452014-09-17 11:55:28 +030012397 if (wakeup->net_detect &&
12398 cfg80211_net_detect_results(msg, wakeup))
12399 goto free_msg;
12400
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012401 nla_nest_end(msg, reasons);
12402 }
12403
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012404 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012405
Johannes Berg68eb5502013-11-19 15:19:38 +010012406 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012407 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010012408 return;
12409
12410 free_msg:
12411 nlmsg_free(msg);
12412}
12413EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
12414#endif
12415
Jouni Malinen3475b092012-11-16 22:49:57 +020012416void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
12417 enum nl80211_tdls_operation oper,
12418 u16 reason_code, gfp_t gfp)
12419{
12420 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012421 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Jouni Malinen3475b092012-11-16 22:49:57 +020012422 struct sk_buff *msg;
12423 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020012424
12425 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
12426 reason_code);
12427
12428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12429 if (!msg)
12430 return;
12431
12432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
12433 if (!hdr) {
12434 nlmsg_free(msg);
12435 return;
12436 }
12437
12438 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12439 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12440 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
12441 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
12442 (reason_code > 0 &&
12443 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
12444 goto nla_put_failure;
12445
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012446 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020012447
Johannes Berg68eb5502013-11-19 15:19:38 +010012448 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012449 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020012450 return;
12451
12452 nla_put_failure:
12453 genlmsg_cancel(msg, hdr);
12454 nlmsg_free(msg);
12455}
12456EXPORT_SYMBOL(cfg80211_tdls_oper_request);
12457
Jouni Malinen026331c2010-02-15 12:53:10 +020012458static int nl80211_netlink_notify(struct notifier_block * nb,
12459 unsigned long state,
12460 void *_notify)
12461{
12462 struct netlink_notify *notify = _notify;
12463 struct cfg80211_registered_device *rdev;
12464 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070012465 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020012466
12467 if (state != NETLINK_URELEASE)
12468 return NOTIFY_DONE;
12469
12470 rcu_read_lock();
12471
Johannes Berg5e760232011-11-04 11:18:17 +010012472 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010012473 bool schedule_destroy_work = false;
12474
12475 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000012476 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070012477
Johannes Berg78f22b62014-03-24 17:57:27 +010012478 if (wdev->owner_nlportid == notify->portid)
12479 schedule_destroy_work = true;
12480 }
12481
Ben Greear37c73b52012-10-26 14:49:25 -070012482 spin_lock_bh(&rdev->beacon_registrations_lock);
12483 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
12484 list) {
12485 if (reg->nlportid == notify->portid) {
12486 list_del(&reg->list);
12487 kfree(reg);
12488 break;
12489 }
12490 }
12491 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010012492
12493 if (schedule_destroy_work) {
12494 struct cfg80211_iface_destroy *destroy;
12495
12496 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
12497 if (destroy) {
12498 destroy->nlportid = notify->portid;
12499 spin_lock(&rdev->destroy_list_lock);
12500 list_add(&destroy->list, &rdev->destroy_list);
12501 spin_unlock(&rdev->destroy_list_lock);
12502 schedule_work(&rdev->destroy_work);
12503 }
12504 }
Johannes Berg5e760232011-11-04 11:18:17 +010012505 }
Jouni Malinen026331c2010-02-15 12:53:10 +020012506
12507 rcu_read_unlock();
12508
Zhao, Gang6784c7d2014-04-21 12:53:04 +080012509 return NOTIFY_OK;
Jouni Malinen026331c2010-02-15 12:53:10 +020012510}
12511
12512static struct notifier_block nl80211_netlink_notifier = {
12513 .notifier_call = nl80211_netlink_notify,
12514};
12515
Jouni Malinen355199e2013-02-27 17:14:27 +020012516void cfg80211_ft_event(struct net_device *netdev,
12517 struct cfg80211_ft_event_params *ft_event)
12518{
12519 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012520 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Jouni Malinen355199e2013-02-27 17:14:27 +020012521 struct sk_buff *msg;
12522 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020012523
12524 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
12525
12526 if (!ft_event->target_ap)
12527 return;
12528
12529 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12530 if (!msg)
12531 return;
12532
12533 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020012534 if (!hdr)
12535 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020012536
Johannes Bergae917c92013-10-25 11:05:22 +020012537 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12538 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12539 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
12540 goto out;
12541
12542 if (ft_event->ies &&
12543 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
12544 goto out;
12545 if (ft_event->ric_ies &&
12546 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
12547 ft_event->ric_ies))
12548 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020012549
Johannes Berg9c90a9f2013-06-04 12:46:03 +020012550 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020012551
Johannes Berg68eb5502013-11-19 15:19:38 +010012552 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010012553 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020012554 return;
12555 out:
12556 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020012557}
12558EXPORT_SYMBOL(cfg80211_ft_event);
12559
Arend van Spriel5de17982013-04-18 15:49:00 +020012560void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
12561{
12562 struct cfg80211_registered_device *rdev;
12563 struct sk_buff *msg;
12564 void *hdr;
12565 u32 nlportid;
12566
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012567 rdev = wiphy_to_rdev(wdev->wiphy);
Arend van Spriel5de17982013-04-18 15:49:00 +020012568 if (!rdev->crit_proto_nlportid)
12569 return;
12570
12571 nlportid = rdev->crit_proto_nlportid;
12572 rdev->crit_proto_nlportid = 0;
12573
12574 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12575 if (!msg)
12576 return;
12577
12578 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
12579 if (!hdr)
12580 goto nla_put_failure;
12581
12582 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12583 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12584 goto nla_put_failure;
12585
12586 genlmsg_end(msg, hdr);
12587
12588 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
12589 return;
12590
12591 nla_put_failure:
12592 if (hdr)
12593 genlmsg_cancel(msg, hdr);
12594 nlmsg_free(msg);
12595
12596}
12597EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
12598
Johannes Berg348baf02014-01-24 14:06:29 +010012599void nl80211_send_ap_stopped(struct wireless_dev *wdev)
12600{
12601 struct wiphy *wiphy = wdev->wiphy;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080012602 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
Johannes Berg348baf02014-01-24 14:06:29 +010012603 struct sk_buff *msg;
12604 void *hdr;
12605
12606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12607 if (!msg)
12608 return;
12609
12610 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
12611 if (!hdr)
12612 goto out;
12613
12614 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12615 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
12616 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12617 goto out;
12618
12619 genlmsg_end(msg, hdr);
12620
12621 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
12622 NL80211_MCGRP_MLME, GFP_KERNEL);
12623 return;
12624 out:
12625 nlmsg_free(msg);
12626}
12627
Johannes Berg55682962007-09-20 13:09:35 -040012628/* initialisation/exit functions */
12629
12630int nl80211_init(void)
12631{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000012632 int err;
Johannes Berg55682962007-09-20 13:09:35 -040012633
Johannes Berg2a94fe42013-11-19 15:19:39 +010012634 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
12635 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040012636 if (err)
12637 return err;
12638
Jouni Malinen026331c2010-02-15 12:53:10 +020012639 err = netlink_register_notifier(&nl80211_netlink_notifier);
12640 if (err)
12641 goto err_out;
12642
Johannes Berg55682962007-09-20 13:09:35 -040012643 return 0;
12644 err_out:
12645 genl_unregister_family(&nl80211_fam);
12646 return err;
12647}
12648
12649void nl80211_exit(void)
12650{
Jouni Malinen026331c2010-02-15 12:53:10 +020012651 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040012652 genl_unregister_family(&nl80211_fam);
12653}