blob: 1e5a434e42249871b3e2e8a315bd68d2392c120c [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Bergf84f7712013-11-14 17:14:45 +010033static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020034 struct genl_info *info);
Johannes Bergf84f7712013-11-14 17:14:45 +010035static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020036 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070040 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040044 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg2a94fe42013-11-19 15:19:39 +010050/* multicast groups */
51enum nl80211_multicast_groups {
52 NL80211_MCGRP_CONFIG,
53 NL80211_MCGRP_SCAN,
54 NL80211_MCGRP_REGULATORY,
55 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010056 NL80211_MCGRP_VENDOR,
Johannes Berg2a94fe42013-11-19 15:19:39 +010057 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
58};
59
60static const struct genl_multicast_group nl80211_mcgrps[] = {
61 [NL80211_MCGRP_CONFIG] = { .name = "config", },
62 [NL80211_MCGRP_SCAN] = { .name = "scan", },
63 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
64 [NL80211_MCGRP_MLME] = { .name = "mlme", },
Johannes Berg567ffc32013-12-18 14:43:31 +010065 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
Johannes Berg2a94fe42013-11-19 15:19:39 +010066#ifdef CONFIG_NL80211_TESTMODE
67 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
68#endif
69};
70
Johannes Berg89a54e42012-06-15 14:33:17 +020071/* returns ERR_PTR values */
72static struct wireless_dev *
73__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040074{
Johannes Berg89a54e42012-06-15 14:33:17 +020075 struct cfg80211_registered_device *rdev;
76 struct wireless_dev *result = NULL;
77 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
78 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
79 u64 wdev_id;
80 int wiphy_idx = -1;
81 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040082
Johannes Berg5fe231e2013-05-08 21:45:15 +020083 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040084
Johannes Berg89a54e42012-06-15 14:33:17 +020085 if (!have_ifidx && !have_wdev_id)
86 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040087
Johannes Berg89a54e42012-06-15 14:33:17 +020088 if (have_ifidx)
89 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
90 if (have_wdev_id) {
91 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
92 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040093 }
94
Johannes Berg89a54e42012-06-15 14:33:17 +020095 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
96 struct wireless_dev *wdev;
97
98 if (wiphy_net(&rdev->wiphy) != netns)
99 continue;
100
101 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
102 continue;
103
Johannes Berg89a54e42012-06-15 14:33:17 +0200104 list_for_each_entry(wdev, &rdev->wdev_list, list) {
105 if (have_ifidx && wdev->netdev &&
106 wdev->netdev->ifindex == ifidx) {
107 result = wdev;
108 break;
109 }
110 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
111 result = wdev;
112 break;
113 }
114 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200115
116 if (result)
117 break;
118 }
119
120 if (result)
121 return result;
122 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400123}
124
Johannes Berga9455402012-06-15 13:32:49 +0200125static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200126__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200127{
Johannes Berg7fee4772012-06-15 14:09:58 +0200128 struct cfg80211_registered_device *rdev = NULL, *tmp;
129 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200130
Johannes Berg5fe231e2013-05-08 21:45:15 +0200131 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200132
Johannes Berg878d9ec2012-06-15 14:18:32 +0200133 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200134 !attrs[NL80211_ATTR_IFINDEX] &&
135 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200136 return ERR_PTR(-EINVAL);
137
Johannes Berg878d9ec2012-06-15 14:18:32 +0200138 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200139 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200140 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200141
Johannes Berg89a54e42012-06-15 14:33:17 +0200142 if (attrs[NL80211_ATTR_WDEV]) {
143 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
144 struct wireless_dev *wdev;
145 bool found = false;
146
147 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
148 if (tmp) {
149 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200150 list_for_each_entry(wdev, &tmp->wdev_list, list) {
151 if (wdev->identifier != (u32)wdev_id)
152 continue;
153 found = true;
154 break;
155 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200156
157 if (!found)
158 tmp = NULL;
159
160 if (rdev && tmp != rdev)
161 return ERR_PTR(-EINVAL);
162 rdev = tmp;
163 }
164 }
165
Johannes Berg878d9ec2012-06-15 14:18:32 +0200166 if (attrs[NL80211_ATTR_IFINDEX]) {
167 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Ying Xue7f2b8562014-01-15 10:23:45 +0800168 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200169 if (netdev) {
170 if (netdev->ieee80211_ptr)
171 tmp = wiphy_to_dev(
172 netdev->ieee80211_ptr->wiphy);
173 else
174 tmp = NULL;
175
Johannes Berg7fee4772012-06-15 14:09:58 +0200176 /* not wireless device -- return error */
177 if (!tmp)
178 return ERR_PTR(-EINVAL);
179
180 /* mismatch -- return error */
181 if (rdev && tmp != rdev)
182 return ERR_PTR(-EINVAL);
183
184 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200185 }
Johannes Berga9455402012-06-15 13:32:49 +0200186 }
187
Johannes Berg4f7eff12012-06-15 14:14:22 +0200188 if (!rdev)
189 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200190
Johannes Berg4f7eff12012-06-15 14:14:22 +0200191 if (netns != wiphy_net(&rdev->wiphy))
192 return ERR_PTR(-ENODEV);
193
194 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200195}
196
197/*
198 * This function returns a pointer to the driver
199 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200200 *
201 * The result of this can be a PTR_ERR and hence must
202 * be checked with IS_ERR() for errors.
203 */
204static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200205cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200206{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200207 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208}
209
Johannes Berg55682962007-09-20 13:09:35 -0400210/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000211static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400212 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
213 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700214 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200215 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100216
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200217 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530218 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100219 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
220 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
222
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200223 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
224 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
226 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100227 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400228
229 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
230 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
231 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100232
Eliad Pellere007b852011-11-24 18:13:56 +0200233 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
234 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100235
Johannes Bergb9454e82009-07-08 13:29:08 +0200236 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100237 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
238 .len = WLAN_MAX_KEY_LEN },
239 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
240 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
241 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200242 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200243 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100244
245 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
246 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
247 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
248 .len = IEEE80211_MAX_DATA_LEN },
249 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100251 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
252 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
253 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
255 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100256 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100257 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200258 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100259 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800260 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300262
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
264 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
265
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300266 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
267 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
268 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200269 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
270 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100271 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300272
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800273 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700274 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700275
Johannes Berg6c739412011-11-03 09:27:01 +0100276 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200277
278 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
279 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
280 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100281 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
282 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200283
284 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_SSID_LEN },
286 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
287 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200288 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300289 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300290 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300291 [NL80211_ATTR_STA_FLAGS2] = {
292 .len = sizeof(struct nl80211_sta_flag_update),
293 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300294 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300295 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
296 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200297 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
299 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200300 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100301 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100302 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
303 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100304 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
305 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200306 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200307 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
308 .len = IEEE80211_MAX_DATA_LEN },
309 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200310 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200311 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300312 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200313 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300314 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
315 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200316 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900317 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100319 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100320 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100321 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200322 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700323 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300324 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200325 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200326 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300327 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300328 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
329 .len = IEEE80211_MAX_DATA_LEN },
330 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530332 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300333 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530334 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300335 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
336 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
337 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
339 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100340 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200341 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700343 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800344 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
346 .len = NL80211_HT_CAPABILITY_LEN
347 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100348 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530349 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530350 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200351 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700352 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300353 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000354 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700355 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100356 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
357 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530358 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
359 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200360 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
361 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100362 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100363 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
364 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
365 .len = NL80211_VHT_CAPABILITY_LEN,
366 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200367 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
368 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
369 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300370 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200371 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
372 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100379 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100380 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
381 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
382 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800383 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
384 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200385 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
386 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530387 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400388};
389
Johannes Berge31b8212010-10-05 19:39:30 +0200390/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000391static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200392 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200393 [NL80211_KEY_IDX] = { .type = NLA_U8 },
394 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200395 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200396 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
397 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200398 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100399 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
400};
401
402/* policy for the key default flags */
403static const struct nla_policy
404nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
405 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
406 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200407};
408
Johannes Bergff1b6e62011-05-04 15:37:28 +0200409/* policy for WoWLAN attributes */
410static const struct nla_policy
411nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
412 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
413 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200416 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
417 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
418 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100420 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
421};
422
423static const struct nla_policy
424nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
425 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
426 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
427 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
428 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
429 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
430 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
432 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
433 },
434 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
435 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
436 },
437 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
438 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
439 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200440};
441
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700442/* policy for coalesce rule attributes */
443static const struct nla_policy
444nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
445 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
446 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
447 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
448};
449
Johannes Berge5497d72011-07-05 16:35:40 +0200450/* policy for GTK rekey offload attributes */
451static const struct nla_policy
452nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
453 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
454 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
455 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
456};
457
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300458static const struct nla_policy
459nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200460 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300461 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700462 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300463};
464
Johannes Berg97990a02013-04-19 01:02:55 +0200465static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
466 struct netlink_callback *cb,
467 struct cfg80211_registered_device **rdev,
468 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100469{
Johannes Berg67748892010-10-04 21:14:06 +0200470 int err;
471
Johannes Berg67748892010-10-04 21:14:06 +0200472 rtnl_lock();
473
Johannes Berg97990a02013-04-19 01:02:55 +0200474 if (!cb->args[0]) {
475 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
476 nl80211_fam.attrbuf, nl80211_fam.maxattr,
477 nl80211_policy);
478 if (err)
479 goto out_unlock;
480
481 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
482 nl80211_fam.attrbuf);
483 if (IS_ERR(*wdev)) {
484 err = PTR_ERR(*wdev);
485 goto out_unlock;
486 }
487 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200488 /* 0 is the first index - add 1 to parse only once */
489 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200490 cb->args[1] = (*wdev)->identifier;
491 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200492 /* subtract the 1 again here */
493 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200494 struct wireless_dev *tmp;
495
496 if (!wiphy) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
500 *rdev = wiphy_to_dev(wiphy);
501 *wdev = NULL;
502
Johannes Berg97990a02013-04-19 01:02:55 +0200503 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
504 if (tmp->identifier == cb->args[1]) {
505 *wdev = tmp;
506 break;
507 }
508 }
Johannes Berg97990a02013-04-19 01:02:55 +0200509
510 if (!*wdev) {
511 err = -ENODEV;
512 goto out_unlock;
513 }
Johannes Berg67748892010-10-04 21:14:06 +0200514 }
515
Johannes Berg67748892010-10-04 21:14:06 +0200516 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200517 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200518 rtnl_unlock();
519 return err;
520}
521
Johannes Berg97990a02013-04-19 01:02:55 +0200522static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200523{
Johannes Berg67748892010-10-04 21:14:06 +0200524 rtnl_unlock();
525}
526
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100527/* IE validation */
528static bool is_valid_ie_attr(const struct nlattr *attr)
529{
530 const u8 *pos;
531 int len;
532
533 if (!attr)
534 return true;
535
536 pos = nla_data(attr);
537 len = nla_len(attr);
538
539 while (len) {
540 u8 elemlen;
541
542 if (len < 2)
543 return false;
544 len -= 2;
545
546 elemlen = pos[1];
547 if (elemlen > len)
548 return false;
549
550 len -= elemlen;
551 pos += 2 + elemlen;
552 }
553
554 return true;
555}
556
Johannes Berg55682962007-09-20 13:09:35 -0400557/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000558static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400559 int flags, u8 cmd)
560{
561 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000562 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400563}
564
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400565static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100566 struct ieee80211_channel *chan,
567 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400568{
David S. Miller9360ffd2012-03-29 04:41:26 -0400569 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
570 chan->center_freq))
571 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400572
David S. Miller9360ffd2012-03-29 04:41:26 -0400573 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
574 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
575 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200576 if (chan->flags & IEEE80211_CHAN_NO_IR) {
577 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
578 goto nla_put_failure;
579 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
580 goto nla_put_failure;
581 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100582 if (chan->flags & IEEE80211_CHAN_RADAR) {
583 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
584 goto nla_put_failure;
585 if (large) {
586 u32 time;
587
588 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
589
590 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
591 chan->dfs_state))
592 goto nla_put_failure;
593 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
594 time))
595 goto nla_put_failure;
596 }
597 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400598
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100599 if (large) {
600 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
602 goto nla_put_failure;
603 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
604 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
605 goto nla_put_failure;
606 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
607 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
608 goto nla_put_failure;
609 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
610 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
611 goto nla_put_failure;
612 }
613
David S. Miller9360ffd2012-03-29 04:41:26 -0400614 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
615 DBM_TO_MBM(chan->max_power)))
616 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400617
618 return 0;
619
620 nla_put_failure:
621 return -ENOBUFS;
622}
623
Johannes Berg55682962007-09-20 13:09:35 -0400624/* netlink command implementations */
625
Johannes Bergb9454e82009-07-08 13:29:08 +0200626struct key_parse {
627 struct key_params p;
628 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200629 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200630 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100631 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200632};
633
634static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
635{
636 struct nlattr *tb[NL80211_KEY_MAX + 1];
637 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
638 nl80211_key_policy);
639 if (err)
640 return err;
641
642 k->def = !!tb[NL80211_KEY_DEFAULT];
643 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
644
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (k->def) {
646 k->def_uni = true;
647 k->def_multi = true;
648 }
649 if (k->defmgmt)
650 k->def_multi = true;
651
Johannes Bergb9454e82009-07-08 13:29:08 +0200652 if (tb[NL80211_KEY_IDX])
653 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
654
655 if (tb[NL80211_KEY_DATA]) {
656 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
657 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
658 }
659
660 if (tb[NL80211_KEY_SEQ]) {
661 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
662 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
663 }
664
665 if (tb[NL80211_KEY_CIPHER])
666 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
667
Johannes Berge31b8212010-10-05 19:39:30 +0200668 if (tb[NL80211_KEY_TYPE]) {
669 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
670 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
671 return -EINVAL;
672 }
673
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100674 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
675 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100676 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
677 tb[NL80211_KEY_DEFAULT_TYPES],
678 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100679 if (err)
680 return err;
681
682 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
683 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
684 }
685
Johannes Bergb9454e82009-07-08 13:29:08 +0200686 return 0;
687}
688
689static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
690{
691 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
692 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
693 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
694 }
695
696 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
697 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
698 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
699 }
700
701 if (info->attrs[NL80211_ATTR_KEY_IDX])
702 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
703
704 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
705 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
706
707 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
708 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
709
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100710 if (k->def) {
711 k->def_uni = true;
712 k->def_multi = true;
713 }
714 if (k->defmgmt)
715 k->def_multi = true;
716
Johannes Berge31b8212010-10-05 19:39:30 +0200717 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
718 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
719 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
720 return -EINVAL;
721 }
722
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
724 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
725 int err = nla_parse_nested(
726 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
727 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
728 nl80211_key_default_policy);
729 if (err)
730 return err;
731
732 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
733 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
734 }
735
Johannes Bergb9454e82009-07-08 13:29:08 +0200736 return 0;
737}
738
739static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
740{
741 int err;
742
743 memset(k, 0, sizeof(*k));
744 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200745 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200746
747 if (info->attrs[NL80211_ATTR_KEY])
748 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
749 else
750 err = nl80211_parse_key_old(info, k);
751
752 if (err)
753 return err;
754
755 if (k->def && k->defmgmt)
756 return -EINVAL;
757
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100758 if (k->defmgmt) {
759 if (k->def_uni || !k->def_multi)
760 return -EINVAL;
761 }
762
Johannes Bergb9454e82009-07-08 13:29:08 +0200763 if (k->idx != -1) {
764 if (k->defmgmt) {
765 if (k->idx < 4 || k->idx > 5)
766 return -EINVAL;
767 } else if (k->def) {
768 if (k->idx < 0 || k->idx > 3)
769 return -EINVAL;
770 } else {
771 if (k->idx < 0 || k->idx > 5)
772 return -EINVAL;
773 }
774 }
775
776 return 0;
777}
778
Johannes Bergfffd0932009-07-08 14:22:54 +0200779static struct cfg80211_cached_keys *
780nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530781 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200782{
783 struct key_parse parse;
784 struct nlattr *key;
785 struct cfg80211_cached_keys *result;
786 int rem, err, def = 0;
787
788 result = kzalloc(sizeof(*result), GFP_KERNEL);
789 if (!result)
790 return ERR_PTR(-ENOMEM);
791
792 result->def = -1;
793 result->defmgmt = -1;
794
795 nla_for_each_nested(key, keys, rem) {
796 memset(&parse, 0, sizeof(parse));
797 parse.idx = -1;
798
799 err = nl80211_parse_key_new(key, &parse);
800 if (err)
801 goto error;
802 err = -EINVAL;
803 if (!parse.p.key)
804 goto error;
805 if (parse.idx < 0 || parse.idx > 4)
806 goto error;
807 if (parse.def) {
808 if (def)
809 goto error;
810 def = 1;
811 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100812 if (!parse.def_uni || !parse.def_multi)
813 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200814 } else if (parse.defmgmt)
815 goto error;
816 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200817 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 if (err)
819 goto error;
820 result->params[parse.idx].cipher = parse.p.cipher;
821 result->params[parse.idx].key_len = parse.p.key_len;
822 result->params[parse.idx].key = result->data[parse.idx];
823 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530824
825 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
826 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
827 if (no_ht)
828 *no_ht = true;
829 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200830 }
831
832 return result;
833 error:
834 kfree(result);
835 return ERR_PTR(err);
836}
837
838static int nl80211_key_allowed(struct wireless_dev *wdev)
839{
840 ASSERT_WDEV_LOCK(wdev);
841
Johannes Bergfffd0932009-07-08 14:22:54 +0200842 switch (wdev->iftype) {
843 case NL80211_IFTYPE_AP:
844 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200845 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700846 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200847 break;
848 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200850 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200851 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200852 return -ENOLINK;
853 break;
854 default:
855 return -EINVAL;
856 }
857
858 return 0;
859}
860
Jouni Malinen664834d2014-01-15 00:01:44 +0200861static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
862 struct nlattr *tb)
863{
864 struct ieee80211_channel *chan;
865
866 if (tb == NULL)
867 return NULL;
868 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
869 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
870 return NULL;
871 return chan;
872}
873
Johannes Berg7527a782011-05-13 10:58:57 +0200874static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
875{
876 struct nlattr *nl_modes = nla_nest_start(msg, attr);
877 int i;
878
879 if (!nl_modes)
880 goto nla_put_failure;
881
882 i = 0;
883 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if ((ifmodes & 1) && nla_put_flag(msg, i))
885 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200886 ifmodes >>= 1;
887 i++;
888 }
889
890 nla_nest_end(msg, nl_modes);
891 return 0;
892
893nla_put_failure:
894 return -ENOBUFS;
895}
896
897static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100898 struct sk_buff *msg,
899 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200900{
901 struct nlattr *nl_combis;
902 int i, j;
903
904 nl_combis = nla_nest_start(msg,
905 NL80211_ATTR_INTERFACE_COMBINATIONS);
906 if (!nl_combis)
907 goto nla_put_failure;
908
909 for (i = 0; i < wiphy->n_iface_combinations; i++) {
910 const struct ieee80211_iface_combination *c;
911 struct nlattr *nl_combi, *nl_limits;
912
913 c = &wiphy->iface_combinations[i];
914
915 nl_combi = nla_nest_start(msg, i + 1);
916 if (!nl_combi)
917 goto nla_put_failure;
918
919 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
920 if (!nl_limits)
921 goto nla_put_failure;
922
923 for (j = 0; j < c->n_limits; j++) {
924 struct nlattr *nl_limit;
925
926 nl_limit = nla_nest_start(msg, j + 1);
927 if (!nl_limit)
928 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400929 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
930 c->limits[j].max))
931 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200932 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
933 c->limits[j].types))
934 goto nla_put_failure;
935 nla_nest_end(msg, nl_limit);
936 }
937
938 nla_nest_end(msg, nl_limits);
939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (c->beacon_int_infra_match &&
941 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
942 goto nla_put_failure;
943 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
944 c->num_different_channels) ||
945 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
946 c->max_interfaces))
947 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100948 if (large &&
949 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
950 c->radar_detect_widths))
951 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200952
953 nla_nest_end(msg, nl_combi);
954 }
955
956 nla_nest_end(msg, nl_combis);
957
958 return 0;
959nla_put_failure:
960 return -ENOBUFS;
961}
962
Johannes Berg3713b4e2013-02-14 16:19:38 +0100963#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100964static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
965 struct sk_buff *msg)
966{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200967 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100968 struct nlattr *nl_tcp;
969
970 if (!tcp)
971 return 0;
972
973 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
974 if (!nl_tcp)
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
978 tcp->data_payload_max))
979 return -ENOBUFS;
980
981 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
982 tcp->data_payload_max))
983 return -ENOBUFS;
984
985 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
986 return -ENOBUFS;
987
988 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
989 sizeof(*tcp->tok), tcp->tok))
990 return -ENOBUFS;
991
992 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
993 tcp->data_interval_max))
994 return -ENOBUFS;
995
996 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
997 tcp->wake_payload_max))
998 return -ENOBUFS;
999
1000 nla_nest_end(msg, nl_tcp);
1001 return 0;
1002}
1003
Johannes Berg3713b4e2013-02-14 16:19:38 +01001004static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001005 struct cfg80211_registered_device *dev,
1006 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001007{
1008 struct nlattr *nl_wowlan;
1009
Johannes Berg964dc9e2013-06-03 17:25:34 +02001010 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001011 return 0;
1012
1013 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1014 if (!nl_wowlan)
1015 return -ENOBUFS;
1016
Johannes Berg964dc9e2013-06-03 17:25:34 +02001017 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001018 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001019 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001020 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001023 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001024 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001025 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001026 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001027 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001028 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001029 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001031 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1033 return -ENOBUFS;
1034
Johannes Berg964dc9e2013-06-03 17:25:34 +02001035 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001036 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001037 .max_patterns = dev->wiphy.wowlan->n_patterns,
1038 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1039 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1040 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041 };
1042
1043 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1044 sizeof(pat), &pat))
1045 return -ENOBUFS;
1046 }
1047
Johannes Bergb56cf722013-02-20 01:02:38 +01001048 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1049 return -ENOBUFS;
1050
Johannes Berg3713b4e2013-02-14 16:19:38 +01001051 nla_nest_end(msg, nl_wowlan);
1052
1053 return 0;
1054}
1055#endif
1056
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001057static int nl80211_send_coalesce(struct sk_buff *msg,
1058 struct cfg80211_registered_device *dev)
1059{
1060 struct nl80211_coalesce_rule_support rule;
1061
1062 if (!dev->wiphy.coalesce)
1063 return 0;
1064
1065 rule.max_rules = dev->wiphy.coalesce->n_rules;
1066 rule.max_delay = dev->wiphy.coalesce->max_delay;
1067 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1068 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1069 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1070 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1071
1072 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1073 return -ENOBUFS;
1074
1075 return 0;
1076}
1077
Johannes Berg3713b4e2013-02-14 16:19:38 +01001078static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1079 struct ieee80211_supported_band *sband)
1080{
1081 struct nlattr *nl_rates, *nl_rate;
1082 struct ieee80211_rate *rate;
1083 int i;
1084
1085 /* add HT info */
1086 if (sband->ht_cap.ht_supported &&
1087 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1088 sizeof(sband->ht_cap.mcs),
1089 &sband->ht_cap.mcs) ||
1090 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1091 sband->ht_cap.cap) ||
1092 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1093 sband->ht_cap.ampdu_factor) ||
1094 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1095 sband->ht_cap.ampdu_density)))
1096 return -ENOBUFS;
1097
1098 /* add VHT info */
1099 if (sband->vht_cap.vht_supported &&
1100 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1101 sizeof(sband->vht_cap.vht_mcs),
1102 &sband->vht_cap.vht_mcs) ||
1103 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1104 sband->vht_cap.cap)))
1105 return -ENOBUFS;
1106
1107 /* add bitrates */
1108 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1109 if (!nl_rates)
1110 return -ENOBUFS;
1111
1112 for (i = 0; i < sband->n_bitrates; i++) {
1113 nl_rate = nla_nest_start(msg, i);
1114 if (!nl_rate)
1115 return -ENOBUFS;
1116
1117 rate = &sband->bitrates[i];
1118 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1119 rate->bitrate))
1120 return -ENOBUFS;
1121 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1122 nla_put_flag(msg,
1123 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1124 return -ENOBUFS;
1125
1126 nla_nest_end(msg, nl_rate);
1127 }
1128
1129 nla_nest_end(msg, nl_rates);
1130
1131 return 0;
1132}
1133
1134static int
1135nl80211_send_mgmt_stypes(struct sk_buff *msg,
1136 const struct ieee80211_txrx_stypes *mgmt_stypes)
1137{
1138 u16 stypes;
1139 struct nlattr *nl_ftypes, *nl_ifs;
1140 enum nl80211_iftype ift;
1141 int i;
1142
1143 if (!mgmt_stypes)
1144 return 0;
1145
1146 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1147 if (!nl_ifs)
1148 return -ENOBUFS;
1149
1150 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1151 nl_ftypes = nla_nest_start(msg, ift);
1152 if (!nl_ftypes)
1153 return -ENOBUFS;
1154 i = 0;
1155 stypes = mgmt_stypes[ift].tx;
1156 while (stypes) {
1157 if ((stypes & 1) &&
1158 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1159 (i << 4) | IEEE80211_FTYPE_MGMT))
1160 return -ENOBUFS;
1161 stypes >>= 1;
1162 i++;
1163 }
1164 nla_nest_end(msg, nl_ftypes);
1165 }
1166
1167 nla_nest_end(msg, nl_ifs);
1168
1169 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1170 if (!nl_ifs)
1171 return -ENOBUFS;
1172
1173 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1174 nl_ftypes = nla_nest_start(msg, ift);
1175 if (!nl_ftypes)
1176 return -ENOBUFS;
1177 i = 0;
1178 stypes = mgmt_stypes[ift].rx;
1179 while (stypes) {
1180 if ((stypes & 1) &&
1181 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1182 (i << 4) | IEEE80211_FTYPE_MGMT))
1183 return -ENOBUFS;
1184 stypes >>= 1;
1185 i++;
1186 }
1187 nla_nest_end(msg, nl_ftypes);
1188 }
1189 nla_nest_end(msg, nl_ifs);
1190
1191 return 0;
1192}
1193
Johannes Berg86e8cf92013-06-19 10:57:22 +02001194struct nl80211_dump_wiphy_state {
1195 s64 filter_wiphy;
1196 long start;
1197 long split_start, band_start, chan_start;
1198 bool split;
1199};
1200
Johannes Berg3713b4e2013-02-14 16:19:38 +01001201static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1202 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001203 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001204{
1205 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001206 struct nlattr *nl_bands, *nl_band;
1207 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001208 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001209 enum ieee80211_band band;
1210 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001211 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001212 const struct ieee80211_txrx_stypes *mgmt_stypes =
1213 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001214 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001215
Eric W. Biederman15e47302012-09-07 20:12:54 +00001216 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001217 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 return -ENOBUFS;
1219
Johannes Berg86e8cf92013-06-19 10:57:22 +02001220 if (WARN_ON(!state))
1221 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001222
David S. Miller9360ffd2012-03-29 04:41:26 -04001223 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001224 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1225 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001226 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001227 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001228 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001229
Johannes Berg86e8cf92013-06-19 10:57:22 +02001230 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001231 case 0:
1232 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1233 dev->wiphy.retry_short) ||
1234 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1235 dev->wiphy.retry_long) ||
1236 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1237 dev->wiphy.frag_threshold) ||
1238 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1239 dev->wiphy.rts_threshold) ||
1240 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1241 dev->wiphy.coverage_class) ||
1242 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1243 dev->wiphy.max_scan_ssids) ||
1244 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1245 dev->wiphy.max_sched_scan_ssids) ||
1246 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1247 dev->wiphy.max_scan_ie_len) ||
1248 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1249 dev->wiphy.max_sched_scan_ie_len) ||
1250 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1251 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001252 goto nla_put_failure;
1253
Johannes Berg3713b4e2013-02-14 16:19:38 +01001254 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1255 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1256 goto nla_put_failure;
1257 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1258 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1259 goto nla_put_failure;
1260 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1261 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1262 goto nla_put_failure;
1263 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1264 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1265 goto nla_put_failure;
1266 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1267 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1268 goto nla_put_failure;
1269 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1270 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001271 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001272 state->split_start++;
1273 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001274 break;
1275 case 1:
1276 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1277 sizeof(u32) * dev->wiphy.n_cipher_suites,
1278 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001279 goto nla_put_failure;
1280
Johannes Berg3713b4e2013-02-14 16:19:38 +01001281 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1282 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001283 goto nla_put_failure;
1284
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1286 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1287 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001288
Johannes Berg3713b4e2013-02-14 16:19:38 +01001289 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1290 dev->wiphy.available_antennas_tx) ||
1291 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1292 dev->wiphy.available_antennas_rx))
1293 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001294
Johannes Berg3713b4e2013-02-14 16:19:38 +01001295 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1296 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1297 dev->wiphy.probe_resp_offload))
1298 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001299
Johannes Berg3713b4e2013-02-14 16:19:38 +01001300 if ((dev->wiphy.available_antennas_tx ||
1301 dev->wiphy.available_antennas_rx) &&
1302 dev->ops->get_antenna) {
1303 u32 tx_ant = 0, rx_ant = 0;
1304 int res;
1305 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1306 if (!res) {
1307 if (nla_put_u32(msg,
1308 NL80211_ATTR_WIPHY_ANTENNA_TX,
1309 tx_ant) ||
1310 nla_put_u32(msg,
1311 NL80211_ATTR_WIPHY_ANTENNA_RX,
1312 rx_ant))
1313 goto nla_put_failure;
1314 }
Johannes Bergee688b002008-01-24 19:38:39 +01001315 }
1316
Johannes Berg86e8cf92013-06-19 10:57:22 +02001317 state->split_start++;
1318 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001319 break;
1320 case 2:
1321 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1322 dev->wiphy.interface_modes))
1323 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001324 state->split_start++;
1325 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001326 break;
1327 case 3:
1328 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1329 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001330 goto nla_put_failure;
1331
Johannes Berg86e8cf92013-06-19 10:57:22 +02001332 for (band = state->band_start;
1333 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001334 struct ieee80211_supported_band *sband;
1335
1336 sband = dev->wiphy.bands[band];
1337
1338 if (!sband)
1339 continue;
1340
1341 nl_band = nla_nest_start(msg, band);
1342 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001343 goto nla_put_failure;
1344
Johannes Berg86e8cf92013-06-19 10:57:22 +02001345 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001346 case 0:
1347 if (nl80211_send_band_rateinfo(msg, sband))
1348 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001349 state->chan_start++;
1350 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 break;
1352 default:
1353 /* add frequencies */
1354 nl_freqs = nla_nest_start(
1355 msg, NL80211_BAND_ATTR_FREQS);
1356 if (!nl_freqs)
1357 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001358
Johannes Berg86e8cf92013-06-19 10:57:22 +02001359 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001360 i < sband->n_channels;
1361 i++) {
1362 nl_freq = nla_nest_start(msg, i);
1363 if (!nl_freq)
1364 goto nla_put_failure;
1365
1366 chan = &sband->channels[i];
1367
Johannes Berg86e8cf92013-06-19 10:57:22 +02001368 if (nl80211_msg_put_channel(
1369 msg, chan,
1370 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 goto nla_put_failure;
1372
1373 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001374 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001375 break;
1376 }
1377 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001378 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001379 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001380 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001381 nla_nest_end(msg, nl_freqs);
1382 }
1383
1384 nla_nest_end(msg, nl_band);
1385
Johannes Berg86e8cf92013-06-19 10:57:22 +02001386 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001387 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001388 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001389 band--;
1390 break;
1391 }
Johannes Bergee688b002008-01-24 19:38:39 +01001392 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001393 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001394
Johannes Berg3713b4e2013-02-14 16:19:38 +01001395 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001396 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001397 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001398 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001399
Johannes Berg3713b4e2013-02-14 16:19:38 +01001400 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001401 if (state->band_start == 0 && state->chan_start == 0)
1402 state->split_start++;
1403 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 break;
1405 case 4:
1406 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1407 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001408 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001409
1410 i = 0;
1411#define CMD(op, n) \
1412 do { \
1413 if (dev->ops->op) { \
1414 i++; \
1415 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1416 goto nla_put_failure; \
1417 } \
1418 } while (0)
1419
1420 CMD(add_virtual_intf, NEW_INTERFACE);
1421 CMD(change_virtual_intf, SET_INTERFACE);
1422 CMD(add_key, NEW_KEY);
1423 CMD(start_ap, START_AP);
1424 CMD(add_station, NEW_STATION);
1425 CMD(add_mpath, NEW_MPATH);
1426 CMD(update_mesh_config, SET_MESH_CONFIG);
1427 CMD(change_bss, SET_BSS);
1428 CMD(auth, AUTHENTICATE);
1429 CMD(assoc, ASSOCIATE);
1430 CMD(deauth, DEAUTHENTICATE);
1431 CMD(disassoc, DISASSOCIATE);
1432 CMD(join_ibss, JOIN_IBSS);
1433 CMD(join_mesh, JOIN_MESH);
1434 CMD(set_pmksa, SET_PMKSA);
1435 CMD(del_pmksa, DEL_PMKSA);
1436 CMD(flush_pmksa, FLUSH_PMKSA);
1437 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1438 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1439 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1440 CMD(mgmt_tx, FRAME);
1441 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1442 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1443 i++;
1444 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1445 goto nla_put_failure;
1446 }
1447 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1448 dev->ops->join_mesh) {
1449 i++;
1450 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1451 goto nla_put_failure;
1452 }
1453 CMD(set_wds_peer, SET_WDS_PEER);
1454 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1455 CMD(tdls_mgmt, TDLS_MGMT);
1456 CMD(tdls_oper, TDLS_OPER);
1457 }
1458 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1459 CMD(sched_scan_start, START_SCHED_SCAN);
1460 CMD(probe_client, PROBE_CLIENT);
1461 CMD(set_noack_map, SET_NOACK_MAP);
1462 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1463 i++;
1464 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1465 goto nla_put_failure;
1466 }
1467 CMD(start_p2p_device, START_P2P_DEVICE);
1468 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001469 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001470 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1471 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001472 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1473 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001474 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001475 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001476
Kalle Valo4745fc02011-11-17 19:06:10 +02001477#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001478 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001479#endif
1480
Johannes Berg8fdc6212009-03-14 09:34:01 +01001481#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001482
Johannes Berg3713b4e2013-02-14 16:19:38 +01001483 if (dev->ops->connect || dev->ops->auth) {
1484 i++;
1485 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001486 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001487 }
1488
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 if (dev->ops->disconnect || dev->ops->deauth) {
1490 i++;
1491 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1492 goto nla_put_failure;
1493 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001494
Johannes Berg3713b4e2013-02-14 16:19:38 +01001495 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001496 state->split_start++;
1497 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001498 break;
1499 case 5:
1500 if (dev->ops->remain_on_channel &&
1501 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1502 nla_put_u32(msg,
1503 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1504 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001505 goto nla_put_failure;
1506
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1508 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1509 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001510
Johannes Berg3713b4e2013-02-14 16:19:38 +01001511 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1512 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001513 state->split_start++;
1514 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001515 break;
1516 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001517#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001518 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001519 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001520 state->split_start++;
1521 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001522 break;
1523#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001525#endif
1526 case 7:
1527 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1528 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001529 goto nla_put_failure;
1530
Johannes Berg86e8cf92013-06-19 10:57:22 +02001531 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1532 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001534
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 state->split_start++;
1536 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001537 break;
1538 case 8:
1539 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1540 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1541 dev->wiphy.ap_sme_capa))
1542 goto nla_put_failure;
1543
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001544 features = dev->wiphy.features;
1545 /*
1546 * We can only add the per-channel limit information if the
1547 * dump is split, otherwise it makes it too big. Therefore
1548 * only advertise it in that case.
1549 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001550 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001551 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1552 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001553 goto nla_put_failure;
1554
1555 if (dev->wiphy.ht_capa_mod_mask &&
1556 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1557 sizeof(*dev->wiphy.ht_capa_mod_mask),
1558 dev->wiphy.ht_capa_mod_mask))
1559 goto nla_put_failure;
1560
1561 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1562 dev->wiphy.max_acl_mac_addrs &&
1563 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1564 dev->wiphy.max_acl_mac_addrs))
1565 goto nla_put_failure;
1566
1567 /*
1568 * Any information below this point is only available to
1569 * applications that can deal with it being split. This
1570 * helps ensure that newly added capabilities don't break
1571 * older tools by overrunning their buffers.
1572 *
1573 * We still increment split_start so that in the split
1574 * case we'll continue with more data in the next round,
1575 * but break unconditionally so unsplit data stops here.
1576 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001577 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001578 break;
1579 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001580 if (dev->wiphy.extended_capabilities &&
1581 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1582 dev->wiphy.extended_capabilities_len,
1583 dev->wiphy.extended_capabilities) ||
1584 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1585 dev->wiphy.extended_capabilities_len,
1586 dev->wiphy.extended_capabilities_mask)))
1587 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001588
Johannes Bergee2aca32013-02-21 17:36:01 +01001589 if (dev->wiphy.vht_capa_mod_mask &&
1590 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1591 sizeof(*dev->wiphy.vht_capa_mod_mask),
1592 dev->wiphy.vht_capa_mod_mask))
1593 goto nla_put_failure;
1594
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001595 state->split_start++;
1596 break;
1597 case 10:
1598 if (nl80211_send_coalesce(msg, dev))
1599 goto nla_put_failure;
1600
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001601 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1602 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1603 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1604 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001605
1606 if (dev->wiphy.max_ap_assoc_sta &&
1607 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1608 dev->wiphy.max_ap_assoc_sta))
1609 goto nla_put_failure;
1610
Johannes Bergad7e7182013-11-13 13:37:47 +01001611 state->split_start++;
1612 break;
1613 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001614 if (dev->wiphy.n_vendor_commands) {
1615 const struct nl80211_vendor_cmd_info *info;
1616 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001617
Johannes Berg567ffc32013-12-18 14:43:31 +01001618 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1619 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001620 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001621
1622 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1623 info = &dev->wiphy.vendor_commands[i].info;
1624 if (nla_put(msg, i + 1, sizeof(*info), info))
1625 goto nla_put_failure;
1626 }
1627 nla_nest_end(msg, nested);
1628 }
1629
1630 if (dev->wiphy.n_vendor_events) {
1631 const struct nl80211_vendor_cmd_info *info;
1632 struct nlattr *nested;
1633
1634 nested = nla_nest_start(msg,
1635 NL80211_ATTR_VENDOR_EVENTS);
1636 if (!nested)
1637 goto nla_put_failure;
1638
1639 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1640 info = &dev->wiphy.vendor_events[i];
1641 if (nla_put(msg, i + 1, sizeof(*info), info))
1642 goto nla_put_failure;
1643 }
1644 nla_nest_end(msg, nested);
1645 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001646
Johannes Berg3713b4e2013-02-14 16:19:38 +01001647 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001648 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001649 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001650 }
Johannes Berg55682962007-09-20 13:09:35 -04001651 return genlmsg_end(msg, hdr);
1652
1653 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001654 genlmsg_cancel(msg, hdr);
1655 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001656}
1657
Johannes Berg86e8cf92013-06-19 10:57:22 +02001658static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1659 struct netlink_callback *cb,
1660 struct nl80211_dump_wiphy_state *state)
1661{
1662 struct nlattr **tb = nl80211_fam.attrbuf;
1663 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1664 tb, nl80211_fam.maxattr, nl80211_policy);
1665 /* ignore parse errors for backward compatibility */
1666 if (ret)
1667 return 0;
1668
1669 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1670 if (tb[NL80211_ATTR_WIPHY])
1671 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1672 if (tb[NL80211_ATTR_WDEV])
1673 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1674 if (tb[NL80211_ATTR_IFINDEX]) {
1675 struct net_device *netdev;
1676 struct cfg80211_registered_device *rdev;
1677 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1678
Ying Xue7f2b8562014-01-15 10:23:45 +08001679 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001680 if (!netdev)
1681 return -ENODEV;
1682 if (netdev->ieee80211_ptr) {
1683 rdev = wiphy_to_dev(
1684 netdev->ieee80211_ptr->wiphy);
1685 state->filter_wiphy = rdev->wiphy_idx;
1686 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001687 }
1688
1689 return 0;
1690}
1691
Johannes Berg55682962007-09-20 13:09:35 -04001692static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1693{
Johannes Berg645e77d2013-03-01 14:03:49 +01001694 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001695 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001696 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001697
Johannes Berg5fe231e2013-05-08 21:45:15 +02001698 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699 if (!state) {
1700 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001701 if (!state) {
1702 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001703 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001704 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001705 state->filter_wiphy = -1;
1706 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1707 if (ret) {
1708 kfree(state);
1709 rtnl_unlock();
1710 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001711 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001712 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001713 }
1714
Johannes Berg79c97e92009-07-07 03:56:12 +02001715 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001716 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1717 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001718 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001719 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001720 if (state->filter_wiphy != -1 &&
1721 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001722 continue;
1723 /* attempt to fit multiple wiphy data chunks into the skb */
1724 do {
1725 ret = nl80211_send_wiphy(dev, skb,
1726 NETLINK_CB(cb->skb).portid,
1727 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001728 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001729 if (ret < 0) {
1730 /*
1731 * If sending the wiphy data didn't fit (ENOBUFS
1732 * or EMSGSIZE returned), this SKB is still
1733 * empty (so it's not too big because another
1734 * wiphy dataset is already in the skb) and
1735 * we've not tried to adjust the dump allocation
1736 * yet ... then adjust the alloc size to be
1737 * bigger, and return 1 but with the empty skb.
1738 * This results in an empty message being RX'ed
1739 * in userspace, but that is ignored.
1740 *
1741 * We can then retry with the larger buffer.
1742 */
1743 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001744 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001745 cb->min_dump_alloc < 4096) {
1746 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001747 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001748 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001749 return 1;
1750 }
1751 idx--;
1752 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001753 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001754 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001755 break;
Johannes Berg55682962007-09-20 13:09:35 -04001756 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001757 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001758
Johannes Berg86e8cf92013-06-19 10:57:22 +02001759 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001760
1761 return skb->len;
1762}
1763
Johannes Berg86e8cf92013-06-19 10:57:22 +02001764static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1765{
1766 kfree((void *)cb->args[0]);
1767 return 0;
1768}
1769
Johannes Berg55682962007-09-20 13:09:35 -04001770static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1771{
1772 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001773 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001774 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001775
Johannes Berg645e77d2013-03-01 14:03:49 +01001776 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001777 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001778 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001779
Johannes Berg3713b4e2013-02-14 16:19:38 +01001780 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001781 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001782 nlmsg_free(msg);
1783 return -ENOBUFS;
1784 }
Johannes Berg55682962007-09-20 13:09:35 -04001785
Johannes Berg134e6372009-07-10 09:51:34 +00001786 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001787}
1788
Jouni Malinen31888482008-10-30 16:59:24 +02001789static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1790 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1791 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1792 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1793 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1794 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1795};
1796
1797static int parse_txq_params(struct nlattr *tb[],
1798 struct ieee80211_txq_params *txq_params)
1799{
Johannes Berga3304b02012-03-28 11:04:24 +02001800 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001801 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1802 !tb[NL80211_TXQ_ATTR_AIFS])
1803 return -EINVAL;
1804
Johannes Berga3304b02012-03-28 11:04:24 +02001805 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001806 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1807 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1808 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1809 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1810
Johannes Berga3304b02012-03-28 11:04:24 +02001811 if (txq_params->ac >= NL80211_NUM_ACS)
1812 return -EINVAL;
1813
Jouni Malinen31888482008-10-30 16:59:24 +02001814 return 0;
1815}
1816
Johannes Bergf444de02010-05-05 15:25:02 +02001817static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1818{
1819 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001820 * You can only set the channel explicitly for WDS interfaces,
1821 * all others have their channel managed via their respective
1822 * "establish a connection" command (connect, join, ...)
1823 *
1824 * For AP/GO and mesh mode, the channel can be set with the
1825 * channel userspace API, but is only stored and passed to the
1826 * low-level driver when the AP starts or the mesh is joined.
1827 * This is for backward compatibility, userspace can also give
1828 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001829 *
1830 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001831 * whatever else is going on, so they have their own special
1832 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001833 */
1834 return !wdev ||
1835 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001836 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001837 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1838 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001839}
1840
Johannes Berg683b6d32012-11-08 21:25:48 +01001841static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1842 struct genl_info *info,
1843 struct cfg80211_chan_def *chandef)
1844{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301845 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001846
1847 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1848 return -EINVAL;
1849
1850 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1851
1852 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001853 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1854 chandef->center_freq1 = control_freq;
1855 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001856
1857 /* Primary channel not allowed */
1858 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1859 return -EINVAL;
1860
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001861 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1862 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001863
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001864 chantype = nla_get_u32(
1865 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1866
1867 switch (chantype) {
1868 case NL80211_CHAN_NO_HT:
1869 case NL80211_CHAN_HT20:
1870 case NL80211_CHAN_HT40PLUS:
1871 case NL80211_CHAN_HT40MINUS:
1872 cfg80211_chandef_create(chandef, chandef->chan,
1873 chantype);
1874 break;
1875 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001876 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001877 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001878 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1879 chandef->width =
1880 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1881 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1882 chandef->center_freq1 =
1883 nla_get_u32(
1884 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1885 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1886 chandef->center_freq2 =
1887 nla_get_u32(
1888 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1889 }
1890
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001891 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001892 return -EINVAL;
1893
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001894 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1895 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001896 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001897
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001898 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1899 chandef->width == NL80211_CHAN_WIDTH_10) &&
1900 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1901 return -EINVAL;
1902
Johannes Berg683b6d32012-11-08 21:25:48 +01001903 return 0;
1904}
1905
Johannes Bergf444de02010-05-05 15:25:02 +02001906static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1907 struct wireless_dev *wdev,
1908 struct genl_info *info)
1909{
Johannes Berg683b6d32012-11-08 21:25:48 +01001910 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001911 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001912 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1913
1914 if (wdev)
1915 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001916
Johannes Bergf444de02010-05-05 15:25:02 +02001917 if (!nl80211_can_set_dev_channel(wdev))
1918 return -EOPNOTSUPP;
1919
Johannes Berg683b6d32012-11-08 21:25:48 +01001920 result = nl80211_parse_chandef(rdev, info, &chandef);
1921 if (result)
1922 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001923
Johannes Berge8c9bd52012-06-06 08:18:22 +02001924 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001925 case NL80211_IFTYPE_AP:
1926 case NL80211_IFTYPE_P2P_GO:
1927 if (wdev->beacon_interval) {
1928 result = -EBUSY;
1929 break;
1930 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001931 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001932 result = -EINVAL;
1933 break;
1934 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001935 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 result = 0;
1937 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001938 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001939 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001940 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001941 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001942 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001943 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001944 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001945 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001946 }
Johannes Bergf444de02010-05-05 15:25:02 +02001947
1948 return result;
1949}
1950
1951static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1952{
Johannes Berg4c476992010-10-04 21:36:35 +02001953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1954 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001955
Johannes Berg4c476992010-10-04 21:36:35 +02001956 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001957}
1958
Bill Jordane8347eb2010-10-01 13:54:28 -04001959static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1960{
Johannes Berg43b19952010-10-07 13:10:30 +02001961 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1962 struct net_device *dev = info->user_ptr[1];
1963 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001964 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001965
1966 if (!info->attrs[NL80211_ATTR_MAC])
1967 return -EINVAL;
1968
Johannes Berg43b19952010-10-07 13:10:30 +02001969 if (netif_running(dev))
1970 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001971
Johannes Berg43b19952010-10-07 13:10:30 +02001972 if (!rdev->ops->set_wds_peer)
1973 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001974
Johannes Berg43b19952010-10-07 13:10:30 +02001975 if (wdev->iftype != NL80211_IFTYPE_WDS)
1976 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001977
1978 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001979 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001980}
1981
1982
Johannes Berg55682962007-09-20 13:09:35 -04001983static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1984{
1985 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001986 struct net_device *netdev = NULL;
1987 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001988 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001989 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001990 u32 changed;
1991 u8 retry_short = 0, retry_long = 0;
1992 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001993 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001994
Johannes Berg5fe231e2013-05-08 21:45:15 +02001995 ASSERT_RTNL();
1996
Johannes Bergf444de02010-05-05 15:25:02 +02001997 /*
1998 * Try to find the wiphy and netdev. Normally this
1999 * function shouldn't need the netdev, but this is
2000 * done for backward compatibility -- previously
2001 * setting the channel was done per wiphy, but now
2002 * it is per netdev. Previous userland like hostapd
2003 * also passed a netdev to set_wiphy, so that it is
2004 * possible to let that go to the right netdev!
2005 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002006
Johannes Bergf444de02010-05-05 15:25:02 +02002007 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2008 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2009
Ying Xue7f2b8562014-01-15 10:23:45 +08002010 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002011 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002012 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002013 else
Johannes Bergf444de02010-05-05 15:25:02 +02002014 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002015 }
2016
Johannes Bergf444de02010-05-05 15:25:02 +02002017 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002018 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2019 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002020 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002021 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002022 wdev = NULL;
2023 netdev = NULL;
2024 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002025 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002026 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002027
2028 /*
2029 * end workaround code, by now the rdev is available
2030 * and locked, and wdev may or may not be NULL.
2031 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002032
2033 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002034 result = cfg80211_dev_rename(
2035 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002036
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002037 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002038 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002039
Jouni Malinen31888482008-10-30 16:59:24 +02002040 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2041 struct ieee80211_txq_params txq_params;
2042 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2043
Ying Xue7f2b8562014-01-15 10:23:45 +08002044 if (!rdev->ops->set_txq_params)
2045 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002046
Ying Xue7f2b8562014-01-15 10:23:45 +08002047 if (!netdev)
2048 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002049
Johannes Berg133a3ff2011-11-03 14:50:13 +01002050 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002051 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2052 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002053
Ying Xue7f2b8562014-01-15 10:23:45 +08002054 if (!netif_running(netdev))
2055 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002056
Jouni Malinen31888482008-10-30 16:59:24 +02002057 nla_for_each_nested(nl_txq_params,
2058 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2059 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002060 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2061 nla_data(nl_txq_params),
2062 nla_len(nl_txq_params),
2063 txq_params_policy);
2064 if (result)
2065 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002066 result = parse_txq_params(tb, &txq_params);
2067 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002068 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002069
Hila Gonene35e4d22012-06-27 17:19:42 +03002070 result = rdev_set_txq_params(rdev, netdev,
2071 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002072 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002073 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002074 }
2075 }
2076
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002077 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002078 result = __nl80211_set_channel(rdev,
2079 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2080 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002081 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002082 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002083 }
2084
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002085 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002086 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002087 enum nl80211_tx_power_setting type;
2088 int idx, mbm = 0;
2089
Johannes Bergc8442112012-10-24 10:17:18 +02002090 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2091 txp_wdev = NULL;
2092
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 if (!rdev->ops->set_tx_power)
2094 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002095
2096 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2097 type = nla_get_u32(info->attrs[idx]);
2098
2099 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002100 (type != NL80211_TX_POWER_AUTOMATIC))
2101 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002102
2103 if (type != NL80211_TX_POWER_AUTOMATIC) {
2104 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2105 mbm = nla_get_u32(info->attrs[idx]);
2106 }
2107
Johannes Bergc8442112012-10-24 10:17:18 +02002108 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002109 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002110 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002111 }
2112
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002113 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2114 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2115 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002116 if ((!rdev->wiphy.available_antennas_tx &&
2117 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002118 !rdev->ops->set_antenna)
2119 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002120
2121 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2122 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2123
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002124 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002125 * available antenna masks, except for the "all" mask */
2126 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002127 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2128 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002129
Bruno Randolf7f531e02010-12-16 11:30:22 +09002130 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2131 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002132
Hila Gonene35e4d22012-06-27 17:19:42 +03002133 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002134 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002135 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002136 }
2137
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002138 changed = 0;
2139
2140 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2141 retry_short = nla_get_u8(
2142 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002143 if (retry_short == 0)
2144 return -EINVAL;
2145
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002146 changed |= WIPHY_PARAM_RETRY_SHORT;
2147 }
2148
2149 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2150 retry_long = nla_get_u8(
2151 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002152 if (retry_long == 0)
2153 return -EINVAL;
2154
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002155 changed |= WIPHY_PARAM_RETRY_LONG;
2156 }
2157
2158 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2159 frag_threshold = nla_get_u32(
2160 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002161 if (frag_threshold < 256)
2162 return -EINVAL;
2163
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002164 if (frag_threshold != (u32) -1) {
2165 /*
2166 * Fragments (apart from the last one) are required to
2167 * have even length. Make the fragmentation code
2168 * simpler by stripping LSB should someone try to use
2169 * odd threshold value.
2170 */
2171 frag_threshold &= ~0x1;
2172 }
2173 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2174 }
2175
2176 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2177 rts_threshold = nla_get_u32(
2178 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2179 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2180 }
2181
Lukáš Turek81077e82009-12-21 22:50:47 +01002182 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2183 coverage_class = nla_get_u8(
2184 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2185 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2186 }
2187
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002188 if (changed) {
2189 u8 old_retry_short, old_retry_long;
2190 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002191 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002192
Ying Xue7f2b8562014-01-15 10:23:45 +08002193 if (!rdev->ops->set_wiphy_params)
2194 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002195
2196 old_retry_short = rdev->wiphy.retry_short;
2197 old_retry_long = rdev->wiphy.retry_long;
2198 old_frag_threshold = rdev->wiphy.frag_threshold;
2199 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002200 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002201
2202 if (changed & WIPHY_PARAM_RETRY_SHORT)
2203 rdev->wiphy.retry_short = retry_short;
2204 if (changed & WIPHY_PARAM_RETRY_LONG)
2205 rdev->wiphy.retry_long = retry_long;
2206 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2207 rdev->wiphy.frag_threshold = frag_threshold;
2208 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2209 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002210 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2211 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002212
Hila Gonene35e4d22012-06-27 17:19:42 +03002213 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002214 if (result) {
2215 rdev->wiphy.retry_short = old_retry_short;
2216 rdev->wiphy.retry_long = old_retry_long;
2217 rdev->wiphy.frag_threshold = old_frag_threshold;
2218 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002219 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002220 }
2221 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002222 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002223}
2224
Johannes Berg71bbc992012-06-15 15:30:18 +02002225static inline u64 wdev_id(struct wireless_dev *wdev)
2226{
2227 return (u64)wdev->identifier |
2228 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2229}
Johannes Berg55682962007-09-20 13:09:35 -04002230
Johannes Berg683b6d32012-11-08 21:25:48 +01002231static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002232 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002233{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002234 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002235
Johannes Berg683b6d32012-11-08 21:25:48 +01002236 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2237 chandef->chan->center_freq))
2238 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002239 switch (chandef->width) {
2240 case NL80211_CHAN_WIDTH_20_NOHT:
2241 case NL80211_CHAN_WIDTH_20:
2242 case NL80211_CHAN_WIDTH_40:
2243 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2244 cfg80211_get_chandef_type(chandef)))
2245 return -ENOBUFS;
2246 break;
2247 default:
2248 break;
2249 }
2250 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2251 return -ENOBUFS;
2252 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2253 return -ENOBUFS;
2254 if (chandef->center_freq2 &&
2255 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002256 return -ENOBUFS;
2257 return 0;
2258}
2259
Eric W. Biederman15e47302012-09-07 20:12:54 +00002260static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002261 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002262 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002263{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002264 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002265 void *hdr;
2266
Eric W. Biederman15e47302012-09-07 20:12:54 +00002267 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002268 if (!hdr)
2269 return -1;
2270
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002271 if (dev &&
2272 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002273 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002274 goto nla_put_failure;
2275
2276 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2277 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002278 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002279 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002280 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2281 rdev->devlist_generation ^
2282 (cfg80211_rdev_list_generation << 2)))
2283 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002284
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002285 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002286 int ret;
2287 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002288
Johannes Berg683b6d32012-11-08 21:25:48 +01002289 ret = rdev_get_channel(rdev, wdev, &chandef);
2290 if (ret == 0) {
2291 if (nl80211_send_chandef(msg, &chandef))
2292 goto nla_put_failure;
2293 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002294 }
2295
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002296 if (wdev->ssid_len) {
2297 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2298 goto nla_put_failure;
2299 }
2300
Johannes Berg55682962007-09-20 13:09:35 -04002301 return genlmsg_end(msg, hdr);
2302
2303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002304 genlmsg_cancel(msg, hdr);
2305 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002306}
2307
2308static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2309{
2310 int wp_idx = 0;
2311 int if_idx = 0;
2312 int wp_start = cb->args[0];
2313 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002314 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002315 struct wireless_dev *wdev;
2316
Johannes Berg5fe231e2013-05-08 21:45:15 +02002317 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002318 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2319 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002320 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002321 if (wp_idx < wp_start) {
2322 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002323 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002324 }
Johannes Berg55682962007-09-20 13:09:35 -04002325 if_idx = 0;
2326
Johannes Berg89a54e42012-06-15 14:33:17 +02002327 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002328 if (if_idx < if_start) {
2329 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002330 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002331 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002332 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002333 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002334 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 goto out;
2336 }
2337 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002338 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339
2340 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002341 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002343 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002344
2345 cb->args[0] = wp_idx;
2346 cb->args[1] = if_idx;
2347
2348 return skb->len;
2349}
2350
2351static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2352{
2353 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002354 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002355 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002356
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002358 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002359 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002360
Eric W. Biederman15e47302012-09-07 20:12:54 +00002361 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002362 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002363 nlmsg_free(msg);
2364 return -ENOBUFS;
2365 }
Johannes Berg55682962007-09-20 13:09:35 -04002366
Johannes Berg134e6372009-07-10 09:51:34 +00002367 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002368}
2369
Michael Wu66f7ac52008-01-31 19:48:22 +01002370static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2371 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2372 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2373 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2374 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2375 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002376 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002377};
2378
2379static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2380{
2381 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2382 int flag;
2383
2384 *mntrflags = 0;
2385
2386 if (!nla)
2387 return -EINVAL;
2388
2389 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2390 nla, mntr_flags_policy))
2391 return -EINVAL;
2392
2393 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2394 if (flags[flag])
2395 *mntrflags |= (1<<flag);
2396
2397 return 0;
2398}
2399
Johannes Berg9bc383d2009-11-19 11:55:19 +01002400static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002401 struct net_device *netdev, u8 use_4addr,
2402 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002403{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002404 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002405 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002406 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002407 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002408 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002409
2410 switch (iftype) {
2411 case NL80211_IFTYPE_AP_VLAN:
2412 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2413 return 0;
2414 break;
2415 case NL80211_IFTYPE_STATION:
2416 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2417 return 0;
2418 break;
2419 default:
2420 break;
2421 }
2422
2423 return -EOPNOTSUPP;
2424}
2425
Johannes Berg55682962007-09-20 13:09:35 -04002426static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2427{
Johannes Berg4c476992010-10-04 21:36:35 +02002428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002429 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002430 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002431 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002433 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002434 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002435
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002436 memset(&params, 0, sizeof(params));
2437
Johannes Berg04a773a2009-04-19 21:24:32 +02002438 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002439
Johannes Berg723b0382008-09-16 20:22:09 +02002440 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002441 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002443 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 if (ntype > NL80211_IFTYPE_MAX)
2445 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002446 }
2447
Johannes Berg92ffe052008-09-16 20:39:36 +02002448 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002449 struct wireless_dev *wdev = dev->ieee80211_ptr;
2450
Johannes Berg4c476992010-10-04 21:36:35 +02002451 if (ntype != NL80211_IFTYPE_MESH_POINT)
2452 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002453 if (netif_running(dev))
2454 return -EBUSY;
2455
2456 wdev_lock(wdev);
2457 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2458 IEEE80211_MAX_MESH_ID_LEN);
2459 wdev->mesh_id_up_len =
2460 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2461 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2462 wdev->mesh_id_up_len);
2463 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002464 }
2465
Felix Fietkau8b787642009-11-10 18:53:10 +01002466 if (info->attrs[NL80211_ATTR_4ADDR]) {
2467 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2468 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002469 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002470 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002471 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002472 } else {
2473 params.use_4addr = -1;
2474 }
2475
Johannes Berg92ffe052008-09-16 20:39:36 +02002476 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002477 if (ntype != NL80211_IFTYPE_MONITOR)
2478 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002479 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2480 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002483
2484 flags = &_flags;
2485 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002486 }
Johannes Berg3b858752009-03-12 09:55:09 +01002487
Luciano Coelho18003292013-08-29 13:26:57 +03002488 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002489 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2490 return -EOPNOTSUPP;
2491
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002492 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002493 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002494 else
2495 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002496
Johannes Berg9bc383d2009-11-19 11:55:19 +01002497 if (!err && params.use_4addr != -1)
2498 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2499
Johannes Berg55682962007-09-20 13:09:35 -04002500 return err;
2501}
2502
2503static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2504{
Johannes Berg4c476992010-10-04 21:36:35 +02002505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002506 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002507 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002508 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002509 int err;
2510 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002511 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002512
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002513 memset(&params, 0, sizeof(params));
2514
Johannes Berg55682962007-09-20 13:09:35 -04002515 if (!info->attrs[NL80211_ATTR_IFNAME])
2516 return -EINVAL;
2517
2518 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2519 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2520 if (type > NL80211_IFTYPE_MAX)
2521 return -EINVAL;
2522 }
2523
Johannes Berg79c97e92009-07-07 03:56:12 +02002524 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002525 !(rdev->wiphy.interface_modes & (1 << type)))
2526 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002527
Arend van Spriel1c18f142013-01-08 10:17:27 +01002528 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2529 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2530 ETH_ALEN);
2531 if (!is_valid_ether_addr(params.macaddr))
2532 return -EADDRNOTAVAIL;
2533 }
2534
Johannes Berg9bc383d2009-11-19 11:55:19 +01002535 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002536 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002537 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002538 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002539 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002540 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002541
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002542 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2543 if (!msg)
2544 return -ENOMEM;
2545
Michael Wu66f7ac52008-01-31 19:48:22 +01002546 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2547 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2548 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002549
Luciano Coelho18003292013-08-29 13:26:57 +03002550 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002551 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2552 return -EOPNOTSUPP;
2553
Hila Gonene35e4d22012-06-27 17:19:42 +03002554 wdev = rdev_add_virtual_intf(rdev,
2555 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2556 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002557 if (IS_ERR(wdev)) {
2558 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002559 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002560 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002561
Johannes Berg98104fde2012-06-16 00:19:54 +02002562 switch (type) {
2563 case NL80211_IFTYPE_MESH_POINT:
2564 if (!info->attrs[NL80211_ATTR_MESH_ID])
2565 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002566 wdev_lock(wdev);
2567 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2568 IEEE80211_MAX_MESH_ID_LEN);
2569 wdev->mesh_id_up_len =
2570 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2571 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2572 wdev->mesh_id_up_len);
2573 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002574 break;
2575 case NL80211_IFTYPE_P2P_DEVICE:
2576 /*
2577 * P2P Device doesn't have a netdev, so doesn't go
2578 * through the netdev notifier and must be added here
2579 */
2580 mutex_init(&wdev->mtx);
2581 INIT_LIST_HEAD(&wdev->event_list);
2582 spin_lock_init(&wdev->event_lock);
2583 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2584 spin_lock_init(&wdev->mgmt_registrations_lock);
2585
Johannes Berg98104fde2012-06-16 00:19:54 +02002586 wdev->identifier = ++rdev->wdev_id;
2587 list_add_rcu(&wdev->list, &rdev->wdev_list);
2588 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002589 break;
2590 default:
2591 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002592 }
2593
Eric W. Biederman15e47302012-09-07 20:12:54 +00002594 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002595 rdev, wdev) < 0) {
2596 nlmsg_free(msg);
2597 return -ENOBUFS;
2598 }
2599
2600 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002601}
2602
2603static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2604{
Johannes Berg4c476992010-10-04 21:36:35 +02002605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002606 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002607
Johannes Berg4c476992010-10-04 21:36:35 +02002608 if (!rdev->ops->del_virtual_intf)
2609 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002610
Johannes Berg84efbb82012-06-16 00:00:26 +02002611 /*
2612 * If we remove a wireless device without a netdev then clear
2613 * user_ptr[1] so that nl80211_post_doit won't dereference it
2614 * to check if it needs to do dev_put(). Otherwise it crashes
2615 * since the wdev has been freed, unlike with a netdev where
2616 * we need the dev_put() for the netdev to really be freed.
2617 */
2618 if (!wdev->netdev)
2619 info->user_ptr[1] = NULL;
2620
Hila Gonene35e4d22012-06-27 17:19:42 +03002621 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002622}
2623
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002624static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2625{
2626 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2627 struct net_device *dev = info->user_ptr[1];
2628 u16 noack_map;
2629
2630 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2631 return -EINVAL;
2632
2633 if (!rdev->ops->set_noack_map)
2634 return -EOPNOTSUPP;
2635
2636 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2637
Hila Gonene35e4d22012-06-27 17:19:42 +03002638 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002639}
2640
Johannes Berg41ade002007-12-19 02:03:29 +01002641struct get_key_cookie {
2642 struct sk_buff *msg;
2643 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002644 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002645};
2646
2647static void get_key_callback(void *c, struct key_params *params)
2648{
Johannes Bergb9454e82009-07-08 13:29:08 +02002649 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002650 struct get_key_cookie *cookie = c;
2651
David S. Miller9360ffd2012-03-29 04:41:26 -04002652 if ((params->key &&
2653 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2654 params->key_len, params->key)) ||
2655 (params->seq &&
2656 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2657 params->seq_len, params->seq)) ||
2658 (params->cipher &&
2659 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2660 params->cipher)))
2661 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002662
Johannes Bergb9454e82009-07-08 13:29:08 +02002663 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2664 if (!key)
2665 goto nla_put_failure;
2666
David S. Miller9360ffd2012-03-29 04:41:26 -04002667 if ((params->key &&
2668 nla_put(cookie->msg, NL80211_KEY_DATA,
2669 params->key_len, params->key)) ||
2670 (params->seq &&
2671 nla_put(cookie->msg, NL80211_KEY_SEQ,
2672 params->seq_len, params->seq)) ||
2673 (params->cipher &&
2674 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2675 params->cipher)))
2676 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002677
David S. Miller9360ffd2012-03-29 04:41:26 -04002678 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2679 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002680
2681 nla_nest_end(cookie->msg, key);
2682
Johannes Berg41ade002007-12-19 02:03:29 +01002683 return;
2684 nla_put_failure:
2685 cookie->error = 1;
2686}
2687
2688static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2689{
Johannes Berg4c476992010-10-04 21:36:35 +02002690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002691 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002692 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002693 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002694 const u8 *mac_addr = NULL;
2695 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002696 struct get_key_cookie cookie = {
2697 .error = 0,
2698 };
2699 void *hdr;
2700 struct sk_buff *msg;
2701
2702 if (info->attrs[NL80211_ATTR_KEY_IDX])
2703 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2704
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002705 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002706 return -EINVAL;
2707
2708 if (info->attrs[NL80211_ATTR_MAC])
2709 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2710
Johannes Berge31b8212010-10-05 19:39:30 +02002711 pairwise = !!mac_addr;
2712 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2713 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2714 if (kt >= NUM_NL80211_KEYTYPES)
2715 return -EINVAL;
2716 if (kt != NL80211_KEYTYPE_GROUP &&
2717 kt != NL80211_KEYTYPE_PAIRWISE)
2718 return -EINVAL;
2719 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2720 }
2721
Johannes Berg4c476992010-10-04 21:36:35 +02002722 if (!rdev->ops->get_key)
2723 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002724
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002726 if (!msg)
2727 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002728
Eric W. Biederman15e47302012-09-07 20:12:54 +00002729 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002730 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002731 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002732 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002733
2734 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002735 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002736
David S. Miller9360ffd2012-03-29 04:41:26 -04002737 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2738 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2739 goto nla_put_failure;
2740 if (mac_addr &&
2741 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2742 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002743
Johannes Berge31b8212010-10-05 19:39:30 +02002744 if (pairwise && mac_addr &&
2745 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2746 return -ENOENT;
2747
Hila Gonene35e4d22012-06-27 17:19:42 +03002748 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2749 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002752 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002753
2754 if (cookie.error)
2755 goto nla_put_failure;
2756
2757 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002758 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002759
2760 nla_put_failure:
2761 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002762 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002763 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002764 return err;
2765}
2766
2767static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2768{
Johannes Berg4c476992010-10-04 21:36:35 +02002769 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002770 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002771 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002772 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002773
Johannes Bergb9454e82009-07-08 13:29:08 +02002774 err = nl80211_parse_key(info, &key);
2775 if (err)
2776 return err;
2777
2778 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002779 return -EINVAL;
2780
Johannes Bergb9454e82009-07-08 13:29:08 +02002781 /* only support setting default key */
2782 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002783 return -EINVAL;
2784
Johannes Bergfffd0932009-07-08 14:22:54 +02002785 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002786
2787 if (key.def) {
2788 if (!rdev->ops->set_default_key) {
2789 err = -EOPNOTSUPP;
2790 goto out;
2791 }
2792
2793 err = nl80211_key_allowed(dev->ieee80211_ptr);
2794 if (err)
2795 goto out;
2796
Hila Gonene35e4d22012-06-27 17:19:42 +03002797 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002798 key.def_uni, key.def_multi);
2799
2800 if (err)
2801 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002802
Johannes Berg3d23e342009-09-29 23:27:28 +02002803#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002804 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002805#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002806 } else {
2807 if (key.def_uni || !key.def_multi) {
2808 err = -EINVAL;
2809 goto out;
2810 }
2811
2812 if (!rdev->ops->set_default_mgmt_key) {
2813 err = -EOPNOTSUPP;
2814 goto out;
2815 }
2816
2817 err = nl80211_key_allowed(dev->ieee80211_ptr);
2818 if (err)
2819 goto out;
2820
Hila Gonene35e4d22012-06-27 17:19:42 +03002821 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002822 if (err)
2823 goto out;
2824
2825#ifdef CONFIG_CFG80211_WEXT
2826 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2827#endif
2828 }
2829
2830 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002831 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002832
Johannes Berg41ade002007-12-19 02:03:29 +01002833 return err;
2834}
2835
2836static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2837{
Johannes Berg4c476992010-10-04 21:36:35 +02002838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002839 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002840 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002841 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002842 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002843
Johannes Bergb9454e82009-07-08 13:29:08 +02002844 err = nl80211_parse_key(info, &key);
2845 if (err)
2846 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002847
Johannes Bergb9454e82009-07-08 13:29:08 +02002848 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002849 return -EINVAL;
2850
Johannes Berg41ade002007-12-19 02:03:29 +01002851 if (info->attrs[NL80211_ATTR_MAC])
2852 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2853
Johannes Berge31b8212010-10-05 19:39:30 +02002854 if (key.type == -1) {
2855 if (mac_addr)
2856 key.type = NL80211_KEYTYPE_PAIRWISE;
2857 else
2858 key.type = NL80211_KEYTYPE_GROUP;
2859 }
2860
2861 /* for now */
2862 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2863 key.type != NL80211_KEYTYPE_GROUP)
2864 return -EINVAL;
2865
Johannes Berg4c476992010-10-04 21:36:35 +02002866 if (!rdev->ops->add_key)
2867 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002868
Johannes Berge31b8212010-10-05 19:39:30 +02002869 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2870 key.type == NL80211_KEYTYPE_PAIRWISE,
2871 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002872 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002873
2874 wdev_lock(dev->ieee80211_ptr);
2875 err = nl80211_key_allowed(dev->ieee80211_ptr);
2876 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002877 err = rdev_add_key(rdev, dev, key.idx,
2878 key.type == NL80211_KEYTYPE_PAIRWISE,
2879 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002880 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002881
Johannes Berg41ade002007-12-19 02:03:29 +01002882 return err;
2883}
2884
2885static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2886{
Johannes Berg4c476992010-10-04 21:36:35 +02002887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002888 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002889 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002890 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002891 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002892
Johannes Bergb9454e82009-07-08 13:29:08 +02002893 err = nl80211_parse_key(info, &key);
2894 if (err)
2895 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002896
2897 if (info->attrs[NL80211_ATTR_MAC])
2898 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2899
Johannes Berge31b8212010-10-05 19:39:30 +02002900 if (key.type == -1) {
2901 if (mac_addr)
2902 key.type = NL80211_KEYTYPE_PAIRWISE;
2903 else
2904 key.type = NL80211_KEYTYPE_GROUP;
2905 }
2906
2907 /* for now */
2908 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2909 key.type != NL80211_KEYTYPE_GROUP)
2910 return -EINVAL;
2911
Johannes Berg4c476992010-10-04 21:36:35 +02002912 if (!rdev->ops->del_key)
2913 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002914
Johannes Bergfffd0932009-07-08 14:22:54 +02002915 wdev_lock(dev->ieee80211_ptr);
2916 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002917
2918 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2919 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2920 err = -ENOENT;
2921
Johannes Bergfffd0932009-07-08 14:22:54 +02002922 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002923 err = rdev_del_key(rdev, dev, key.idx,
2924 key.type == NL80211_KEYTYPE_PAIRWISE,
2925 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002926
Johannes Berg3d23e342009-09-29 23:27:28 +02002927#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002928 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002929 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002930 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002931 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002932 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2933 }
2934#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002935 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002936
Johannes Berg41ade002007-12-19 02:03:29 +01002937 return err;
2938}
2939
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302940/* This function returns an error or the number of nested attributes */
2941static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2942{
2943 struct nlattr *attr;
2944 int n_entries = 0, tmp;
2945
2946 nla_for_each_nested(attr, nl_attr, tmp) {
2947 if (nla_len(attr) != ETH_ALEN)
2948 return -EINVAL;
2949
2950 n_entries++;
2951 }
2952
2953 return n_entries;
2954}
2955
2956/*
2957 * This function parses ACL information and allocates memory for ACL data.
2958 * On successful return, the calling function is responsible to free the
2959 * ACL buffer returned by this function.
2960 */
2961static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2962 struct genl_info *info)
2963{
2964 enum nl80211_acl_policy acl_policy;
2965 struct nlattr *attr;
2966 struct cfg80211_acl_data *acl;
2967 int i = 0, n_entries, tmp;
2968
2969 if (!wiphy->max_acl_mac_addrs)
2970 return ERR_PTR(-EOPNOTSUPP);
2971
2972 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2973 return ERR_PTR(-EINVAL);
2974
2975 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2976 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2977 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2978 return ERR_PTR(-EINVAL);
2979
2980 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2981 return ERR_PTR(-EINVAL);
2982
2983 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2984 if (n_entries < 0)
2985 return ERR_PTR(n_entries);
2986
2987 if (n_entries > wiphy->max_acl_mac_addrs)
2988 return ERR_PTR(-ENOTSUPP);
2989
2990 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2991 GFP_KERNEL);
2992 if (!acl)
2993 return ERR_PTR(-ENOMEM);
2994
2995 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2996 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2997 i++;
2998 }
2999
3000 acl->n_acl_entries = n_entries;
3001 acl->acl_policy = acl_policy;
3002
3003 return acl;
3004}
3005
3006static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3007{
3008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3009 struct net_device *dev = info->user_ptr[1];
3010 struct cfg80211_acl_data *acl;
3011 int err;
3012
3013 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3014 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3015 return -EOPNOTSUPP;
3016
3017 if (!dev->ieee80211_ptr->beacon_interval)
3018 return -EINVAL;
3019
3020 acl = parse_acl_data(&rdev->wiphy, info);
3021 if (IS_ERR(acl))
3022 return PTR_ERR(acl);
3023
3024 err = rdev_set_mac_acl(rdev, dev, acl);
3025
3026 kfree(acl);
3027
3028 return err;
3029}
3030
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003031static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003032 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003033{
Johannes Berg88600202012-02-13 15:17:18 +01003034 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003035
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003036 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3037 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3038 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3039 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003040 return -EINVAL;
3041
Johannes Berg88600202012-02-13 15:17:18 +01003042 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003043
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003044 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3045 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3046 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003047 if (!bcn->head_len)
3048 return -EINVAL;
3049 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003050 }
3051
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003052 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3053 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3054 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003055 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003056 }
3057
Johannes Berg4c476992010-10-04 21:36:35 +02003058 if (!haveinfo)
3059 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003061 if (attrs[NL80211_ATTR_IE]) {
3062 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3063 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003064 }
3065
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003066 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003067 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003068 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003069 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003070 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003071 }
3072
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003073 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003074 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003075 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003076 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003077 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003078 }
3079
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003080 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3081 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3082 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003083 }
3084
Johannes Berg88600202012-02-13 15:17:18 +01003085 return 0;
3086}
3087
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003088static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3089 struct cfg80211_ap_settings *params)
3090{
3091 struct wireless_dev *wdev;
3092 bool ret = false;
3093
Johannes Berg89a54e42012-06-15 14:33:17 +02003094 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003095 if (wdev->iftype != NL80211_IFTYPE_AP &&
3096 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3097 continue;
3098
Johannes Berg683b6d32012-11-08 21:25:48 +01003099 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003100 continue;
3101
Johannes Berg683b6d32012-11-08 21:25:48 +01003102 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003103 ret = true;
3104 break;
3105 }
3106
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003107 return ret;
3108}
3109
Jouni Malinene39e5b52012-09-30 19:29:39 +03003110static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3111 enum nl80211_auth_type auth_type,
3112 enum nl80211_commands cmd)
3113{
3114 if (auth_type > NL80211_AUTHTYPE_MAX)
3115 return false;
3116
3117 switch (cmd) {
3118 case NL80211_CMD_AUTHENTICATE:
3119 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3120 auth_type == NL80211_AUTHTYPE_SAE)
3121 return false;
3122 return true;
3123 case NL80211_CMD_CONNECT:
3124 case NL80211_CMD_START_AP:
3125 /* SAE not supported yet */
3126 if (auth_type == NL80211_AUTHTYPE_SAE)
3127 return false;
3128 return true;
3129 default:
3130 return false;
3131 }
3132}
3133
Johannes Berg88600202012-02-13 15:17:18 +01003134static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3135{
3136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3137 struct net_device *dev = info->user_ptr[1];
3138 struct wireless_dev *wdev = dev->ieee80211_ptr;
3139 struct cfg80211_ap_settings params;
3140 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003141 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003142
3143 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3144 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3145 return -EOPNOTSUPP;
3146
3147 if (!rdev->ops->start_ap)
3148 return -EOPNOTSUPP;
3149
3150 if (wdev->beacon_interval)
3151 return -EALREADY;
3152
3153 memset(&params, 0, sizeof(params));
3154
3155 /* these are required for START_AP */
3156 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3157 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3158 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3159 return -EINVAL;
3160
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003161 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003162 if (err)
3163 return err;
3164
3165 params.beacon_interval =
3166 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3167 params.dtim_period =
3168 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3169
3170 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3171 if (err)
3172 return err;
3173
3174 /*
3175 * In theory, some of these attributes should be required here
3176 * but since they were not used when the command was originally
3177 * added, keep them optional for old user space programs to let
3178 * them continue to work with drivers that do not need the
3179 * additional information -- drivers must check!
3180 */
3181 if (info->attrs[NL80211_ATTR_SSID]) {
3182 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3183 params.ssid_len =
3184 nla_len(info->attrs[NL80211_ATTR_SSID]);
3185 if (params.ssid_len == 0 ||
3186 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3187 return -EINVAL;
3188 }
3189
3190 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3191 params.hidden_ssid = nla_get_u32(
3192 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3193 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3194 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3195 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3196 return -EINVAL;
3197 }
3198
3199 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3200
3201 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3202 params.auth_type = nla_get_u32(
3203 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003204 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3205 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003206 return -EINVAL;
3207 } else
3208 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3209
3210 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3211 NL80211_MAX_NR_CIPHER_SUITES);
3212 if (err)
3213 return err;
3214
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303215 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3216 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3217 return -EOPNOTSUPP;
3218 params.inactivity_timeout = nla_get_u16(
3219 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3220 }
3221
Johannes Berg53cabad2012-11-14 15:17:28 +01003222 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3223 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3224 return -EINVAL;
3225 params.p2p_ctwindow =
3226 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3227 if (params.p2p_ctwindow > 127)
3228 return -EINVAL;
3229 if (params.p2p_ctwindow != 0 &&
3230 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3231 return -EINVAL;
3232 }
3233
3234 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3235 u8 tmp;
3236
3237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3238 return -EINVAL;
3239 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3240 if (tmp > 1)
3241 return -EINVAL;
3242 params.p2p_opp_ps = tmp;
3243 if (params.p2p_opp_ps != 0 &&
3244 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3245 return -EINVAL;
3246 }
3247
Johannes Bergaa430da2012-05-16 23:50:18 +02003248 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003249 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3250 if (err)
3251 return err;
3252 } else if (wdev->preset_chandef.chan) {
3253 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003254 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003255 return -EINVAL;
3256
Johannes Berg683b6d32012-11-08 21:25:48 +01003257 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003258 return -EINVAL;
3259
Simon Wunderlich04f39042013-02-08 18:16:19 +01003260 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3261 if (err < 0)
3262 return err;
3263 if (err) {
3264 radar_detect_width = BIT(params.chandef.width);
3265 params.radar_required = true;
3266 }
3267
Simon Wunderlich04f39042013-02-08 18:16:19 +01003268 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3269 params.chandef.chan,
3270 CHAN_MODE_SHARED,
3271 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003272 if (err)
3273 return err;
3274
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303275 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3276 params.acl = parse_acl_data(&rdev->wiphy, info);
3277 if (IS_ERR(params.acl))
3278 return PTR_ERR(params.acl);
3279 }
3280
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003281 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003282 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003283 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003284 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003285 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003286 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003287 wdev->ssid_len = params.ssid_len;
3288 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003289 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003290 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303291
3292 kfree(params.acl);
3293
Johannes Berg56d18932011-05-09 18:41:15 +02003294 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003295}
3296
Johannes Berg88600202012-02-13 15:17:18 +01003297static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3298{
3299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3300 struct net_device *dev = info->user_ptr[1];
3301 struct wireless_dev *wdev = dev->ieee80211_ptr;
3302 struct cfg80211_beacon_data params;
3303 int err;
3304
3305 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3306 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3307 return -EOPNOTSUPP;
3308
3309 if (!rdev->ops->change_beacon)
3310 return -EOPNOTSUPP;
3311
3312 if (!wdev->beacon_interval)
3313 return -EINVAL;
3314
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003315 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003316 if (err)
3317 return err;
3318
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003319 wdev_lock(wdev);
3320 err = rdev_change_beacon(rdev, dev, &params);
3321 wdev_unlock(wdev);
3322
3323 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003324}
3325
3326static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003327{
Johannes Berg4c476992010-10-04 21:36:35 +02003328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3329 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003330
Michal Kazior60771782012-06-29 12:46:56 +02003331 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003332}
3333
Johannes Berg5727ef12007-12-19 02:03:34 +01003334static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3335 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3336 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3337 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003338 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003339 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003340 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003341};
3342
Johannes Bergeccb8e82009-05-11 21:57:56 +03003343static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003344 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003345 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003346{
3347 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003348 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003349 int flag;
3350
Johannes Bergeccb8e82009-05-11 21:57:56 +03003351 /*
3352 * Try parsing the new attribute first so userspace
3353 * can specify both for older kernels.
3354 */
3355 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3356 if (nla) {
3357 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003358
Johannes Bergeccb8e82009-05-11 21:57:56 +03003359 sta_flags = nla_data(nla);
3360 params->sta_flags_mask = sta_flags->mask;
3361 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003362 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003363 if ((params->sta_flags_mask |
3364 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3365 return -EINVAL;
3366 return 0;
3367 }
3368
3369 /* if present, parse the old attribute */
3370
3371 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003372 if (!nla)
3373 return 0;
3374
3375 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3376 nla, sta_flags_policy))
3377 return -EINVAL;
3378
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003379 /*
3380 * Only allow certain flags for interface types so that
3381 * other attributes are silently ignored. Remember that
3382 * this is backward compatibility code with old userspace
3383 * and shouldn't be hit in other cases anyway.
3384 */
3385 switch (iftype) {
3386 case NL80211_IFTYPE_AP:
3387 case NL80211_IFTYPE_AP_VLAN:
3388 case NL80211_IFTYPE_P2P_GO:
3389 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3390 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3391 BIT(NL80211_STA_FLAG_WME) |
3392 BIT(NL80211_STA_FLAG_MFP);
3393 break;
3394 case NL80211_IFTYPE_P2P_CLIENT:
3395 case NL80211_IFTYPE_STATION:
3396 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3397 BIT(NL80211_STA_FLAG_TDLS_PEER);
3398 break;
3399 case NL80211_IFTYPE_MESH_POINT:
3400 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3401 BIT(NL80211_STA_FLAG_MFP) |
3402 BIT(NL80211_STA_FLAG_AUTHORIZED);
3403 default:
3404 return -EINVAL;
3405 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003406
Johannes Berg3383b5a2012-05-10 20:14:43 +02003407 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3408 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003409 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003410
Johannes Berg3383b5a2012-05-10 20:14:43 +02003411 /* no longer support new API additions in old API */
3412 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3413 return -EINVAL;
3414 }
3415 }
3416
Johannes Berg5727ef12007-12-19 02:03:34 +01003417 return 0;
3418}
3419
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003420static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3421 int attr)
3422{
3423 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003424 u32 bitrate;
3425 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003426
3427 rate = nla_nest_start(msg, attr);
3428 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003429 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003430
3431 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3432 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003433 /* report 16-bit bitrate only if we can */
3434 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003435 if (bitrate > 0 &&
3436 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3437 return false;
3438 if (bitrate_compat > 0 &&
3439 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3440 return false;
3441
3442 if (info->flags & RATE_INFO_FLAGS_MCS) {
3443 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3444 return false;
3445 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3446 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3447 return false;
3448 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3449 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3450 return false;
3451 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3452 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3453 return false;
3454 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3455 return false;
3456 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3457 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3458 return false;
3459 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3460 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3461 return false;
3462 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3463 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3464 return false;
3465 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3466 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3467 return false;
3468 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3469 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3470 return false;
3471 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003472
3473 nla_nest_end(msg, rate);
3474 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003475}
3476
Felix Fietkau119363c2013-04-22 16:29:30 +02003477static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3478 int id)
3479{
3480 void *attr;
3481 int i = 0;
3482
3483 if (!mask)
3484 return true;
3485
3486 attr = nla_nest_start(msg, id);
3487 if (!attr)
3488 return false;
3489
3490 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3491 if (!(mask & BIT(i)))
3492 continue;
3493
3494 if (nla_put_u8(msg, i, signal[i]))
3495 return false;
3496 }
3497
3498 nla_nest_end(msg, attr);
3499
3500 return true;
3501}
3502
Eric W. Biederman15e47302012-09-07 20:12:54 +00003503static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003504 int flags,
3505 struct cfg80211_registered_device *rdev,
3506 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003507 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003508{
3509 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003510 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003511
Eric W. Biederman15e47302012-09-07 20:12:54 +00003512 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003513 if (!hdr)
3514 return -1;
3515
David S. Miller9360ffd2012-03-29 04:41:26 -04003516 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3517 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3518 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3519 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003520
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003521 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3522 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003523 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003524 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3525 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3526 sinfo->connected_time))
3527 goto nla_put_failure;
3528 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3529 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3530 sinfo->inactive_time))
3531 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003532 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3533 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003534 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003535 (u32)sinfo->rx_bytes))
3536 goto nla_put_failure;
3537 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003538 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003539 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3540 (u32)sinfo->tx_bytes))
3541 goto nla_put_failure;
3542 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3543 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003544 sinfo->rx_bytes))
3545 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003546 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3547 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003548 sinfo->tx_bytes))
3549 goto nla_put_failure;
3550 if ((sinfo->filled & STATION_INFO_LLID) &&
3551 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3552 goto nla_put_failure;
3553 if ((sinfo->filled & STATION_INFO_PLID) &&
3554 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3555 goto nla_put_failure;
3556 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3557 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3558 sinfo->plink_state))
3559 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003560 switch (rdev->wiphy.signal_type) {
3561 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003562 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3563 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3564 sinfo->signal))
3565 goto nla_put_failure;
3566 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3567 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3568 sinfo->signal_avg))
3569 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003570 break;
3571 default:
3572 break;
3573 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003574 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3575 if (!nl80211_put_signal(msg, sinfo->chains,
3576 sinfo->chain_signal,
3577 NL80211_STA_INFO_CHAIN_SIGNAL))
3578 goto nla_put_failure;
3579 }
3580 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3581 if (!nl80211_put_signal(msg, sinfo->chains,
3582 sinfo->chain_signal_avg,
3583 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3584 goto nla_put_failure;
3585 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003586 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003587 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3588 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003589 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003590 }
3591 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3592 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3593 NL80211_STA_INFO_RX_BITRATE))
3594 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003595 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003596 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3597 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3598 sinfo->rx_packets))
3599 goto nla_put_failure;
3600 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3601 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3602 sinfo->tx_packets))
3603 goto nla_put_failure;
3604 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3605 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3606 sinfo->tx_retries))
3607 goto nla_put_failure;
3608 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3609 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3610 sinfo->tx_failed))
3611 goto nla_put_failure;
3612 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3613 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3614 sinfo->beacon_loss_count))
3615 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003616 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3617 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3618 sinfo->local_pm))
3619 goto nla_put_failure;
3620 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3621 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3622 sinfo->peer_pm))
3623 goto nla_put_failure;
3624 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3625 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3626 sinfo->nonpeer_pm))
3627 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003628 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3629 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3630 if (!bss_param)
3631 goto nla_put_failure;
3632
David S. Miller9360ffd2012-03-29 04:41:26 -04003633 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3634 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3635 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3636 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3637 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3638 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3639 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3640 sinfo->bss_param.dtim_period) ||
3641 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3642 sinfo->bss_param.beacon_interval))
3643 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003644
3645 nla_nest_end(msg, bss_param);
3646 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003647 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3648 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3649 sizeof(struct nl80211_sta_flag_update),
3650 &sinfo->sta_flags))
3651 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003652 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3653 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3654 sinfo->t_offset))
3655 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003656 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003657
David S. Miller9360ffd2012-03-29 04:41:26 -04003658 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3659 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3660 sinfo->assoc_req_ies))
3661 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003662
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003663 return genlmsg_end(msg, hdr);
3664
3665 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003666 genlmsg_cancel(msg, hdr);
3667 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003668}
3669
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003670static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003671 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003672{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003673 struct station_info sinfo;
3674 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003675 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003676 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003677 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003678 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003679
Johannes Berg97990a02013-04-19 01:02:55 +02003680 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003681 if (err)
3682 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003683
Johannes Berg97990a02013-04-19 01:02:55 +02003684 if (!wdev->netdev) {
3685 err = -EINVAL;
3686 goto out_err;
3687 }
3688
Johannes Bergbba95fe2008-07-29 13:22:51 +02003689 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003690 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003691 goto out_err;
3692 }
3693
Johannes Bergbba95fe2008-07-29 13:22:51 +02003694 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003695 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003696 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003697 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003698 if (err == -ENOENT)
3699 break;
3700 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003701 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003702
3703 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003704 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003705 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003706 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003707 &sinfo) < 0)
3708 goto out;
3709
3710 sta_idx++;
3711 }
3712
3713
3714 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003715 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003716 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003717 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003718 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003719
3720 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003721}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003722
Johannes Berg5727ef12007-12-19 02:03:34 +01003723static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3724{
Johannes Berg4c476992010-10-04 21:36:35 +02003725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3726 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003727 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003728 struct sk_buff *msg;
3729 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003730 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003731
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003732 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003733
3734 if (!info->attrs[NL80211_ATTR_MAC])
3735 return -EINVAL;
3736
3737 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3738
Johannes Berg4c476992010-10-04 21:36:35 +02003739 if (!rdev->ops->get_station)
3740 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003741
Hila Gonene35e4d22012-06-27 17:19:42 +03003742 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003743 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003744 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003745
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003747 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003748 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003749
Eric W. Biederman15e47302012-09-07 20:12:54 +00003750 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003751 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003752 nlmsg_free(msg);
3753 return -ENOBUFS;
3754 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003755
Johannes Berg4c476992010-10-04 21:36:35 +02003756 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003757}
3758
Johannes Berg77ee7c82013-02-15 00:48:33 +01003759int cfg80211_check_station_change(struct wiphy *wiphy,
3760 struct station_parameters *params,
3761 enum cfg80211_station_type statype)
3762{
3763 if (params->listen_interval != -1)
3764 return -EINVAL;
3765 if (params->aid)
3766 return -EINVAL;
3767
3768 /* When you run into this, adjust the code below for the new flag */
3769 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3770
3771 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003772 case CFG80211_STA_MESH_PEER_KERNEL:
3773 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003774 /*
3775 * No ignoring the TDLS flag here -- the userspace mesh
3776 * code doesn't have the bug of including TDLS in the
3777 * mask everywhere.
3778 */
3779 if (params->sta_flags_mask &
3780 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3781 BIT(NL80211_STA_FLAG_MFP) |
3782 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3783 return -EINVAL;
3784 break;
3785 case CFG80211_STA_TDLS_PEER_SETUP:
3786 case CFG80211_STA_TDLS_PEER_ACTIVE:
3787 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3788 return -EINVAL;
3789 /* ignore since it can't change */
3790 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3791 break;
3792 default:
3793 /* disallow mesh-specific things */
3794 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3795 return -EINVAL;
3796 if (params->local_pm)
3797 return -EINVAL;
3798 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3799 return -EINVAL;
3800 }
3801
3802 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3803 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3804 /* TDLS can't be set, ... */
3805 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3806 return -EINVAL;
3807 /*
3808 * ... but don't bother the driver with it. This works around
3809 * a hostapd/wpa_supplicant issue -- it always includes the
3810 * TLDS_PEER flag in the mask even for AP mode.
3811 */
3812 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3813 }
3814
3815 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3816 /* reject other things that can't change */
3817 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3818 return -EINVAL;
3819 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3820 return -EINVAL;
3821 if (params->supported_rates)
3822 return -EINVAL;
3823 if (params->ext_capab || params->ht_capa || params->vht_capa)
3824 return -EINVAL;
3825 }
3826
3827 if (statype != CFG80211_STA_AP_CLIENT) {
3828 if (params->vlan)
3829 return -EINVAL;
3830 }
3831
3832 switch (statype) {
3833 case CFG80211_STA_AP_MLME_CLIENT:
3834 /* Use this only for authorizing/unauthorizing a station */
3835 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3836 return -EOPNOTSUPP;
3837 break;
3838 case CFG80211_STA_AP_CLIENT:
3839 /* accept only the listed bits */
3840 if (params->sta_flags_mask &
3841 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3842 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3843 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3844 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3845 BIT(NL80211_STA_FLAG_WME) |
3846 BIT(NL80211_STA_FLAG_MFP)))
3847 return -EINVAL;
3848
3849 /* but authenticated/associated only if driver handles it */
3850 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3851 params->sta_flags_mask &
3852 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3853 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3854 return -EINVAL;
3855 break;
3856 case CFG80211_STA_IBSS:
3857 case CFG80211_STA_AP_STA:
3858 /* reject any changes other than AUTHORIZED */
3859 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3860 return -EINVAL;
3861 break;
3862 case CFG80211_STA_TDLS_PEER_SETUP:
3863 /* reject any changes other than AUTHORIZED or WME */
3864 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3865 BIT(NL80211_STA_FLAG_WME)))
3866 return -EINVAL;
3867 /* force (at least) rates when authorizing */
3868 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3869 !params->supported_rates)
3870 return -EINVAL;
3871 break;
3872 case CFG80211_STA_TDLS_PEER_ACTIVE:
3873 /* reject any changes */
3874 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003875 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003876 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3877 return -EINVAL;
3878 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003879 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003880 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3881 return -EINVAL;
3882 break;
3883 }
3884
3885 return 0;
3886}
3887EXPORT_SYMBOL(cfg80211_check_station_change);
3888
Johannes Berg5727ef12007-12-19 02:03:34 +01003889/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003890 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003891 */
Johannes Berg80b99892011-11-18 16:23:01 +01003892static struct net_device *get_vlan(struct genl_info *info,
3893 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003894{
Johannes Berg463d0182009-07-14 00:33:35 +02003895 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003896 struct net_device *v;
3897 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003898
Johannes Berg80b99892011-11-18 16:23:01 +01003899 if (!vlanattr)
3900 return NULL;
3901
3902 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3903 if (!v)
3904 return ERR_PTR(-ENODEV);
3905
3906 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3907 ret = -EINVAL;
3908 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003909 }
Johannes Berg80b99892011-11-18 16:23:01 +01003910
Johannes Berg77ee7c82013-02-15 00:48:33 +01003911 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3912 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3913 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3914 ret = -EINVAL;
3915 goto error;
3916 }
3917
Johannes Berg80b99892011-11-18 16:23:01 +01003918 if (!netif_running(v)) {
3919 ret = -ENETDOWN;
3920 goto error;
3921 }
3922
3923 return v;
3924 error:
3925 dev_put(v);
3926 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003927}
3928
Johannes Berg94e860f2014-01-20 23:58:15 +01003929static const struct nla_policy
3930nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003931 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3932 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3933};
3934
Johannes Bergff276692013-02-15 00:09:01 +01003935static int nl80211_parse_sta_wme(struct genl_info *info,
3936 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003937{
Jouni Malinendf881292013-02-14 21:10:54 +02003938 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3939 struct nlattr *nla;
3940 int err;
3941
Jouni Malinendf881292013-02-14 21:10:54 +02003942 /* parse WME attributes if present */
3943 if (!info->attrs[NL80211_ATTR_STA_WME])
3944 return 0;
3945
3946 nla = info->attrs[NL80211_ATTR_STA_WME];
3947 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3948 nl80211_sta_wme_policy);
3949 if (err)
3950 return err;
3951
3952 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3953 params->uapsd_queues = nla_get_u8(
3954 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3955 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3956 return -EINVAL;
3957
3958 if (tb[NL80211_STA_WME_MAX_SP])
3959 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3960
3961 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3962 return -EINVAL;
3963
3964 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3965
3966 return 0;
3967}
3968
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303969static int nl80211_parse_sta_channel_info(struct genl_info *info,
3970 struct station_parameters *params)
3971{
3972 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3973 params->supported_channels =
3974 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3975 params->supported_channels_len =
3976 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3977 /*
3978 * Need to include at least one (first channel, number of
3979 * channels) tuple for each subband, and must have proper
3980 * tuples for the rest of the data as well.
3981 */
3982 if (params->supported_channels_len < 2)
3983 return -EINVAL;
3984 if (params->supported_channels_len % 2)
3985 return -EINVAL;
3986 }
3987
3988 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3989 params->supported_oper_classes =
3990 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3991 params->supported_oper_classes_len =
3992 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3993 /*
3994 * The value of the Length field of the Supported Operating
3995 * Classes element is between 2 and 253.
3996 */
3997 if (params->supported_oper_classes_len < 2 ||
3998 params->supported_oper_classes_len > 253)
3999 return -EINVAL;
4000 }
4001 return 0;
4002}
4003
Johannes Bergff276692013-02-15 00:09:01 +01004004static int nl80211_set_station_tdls(struct genl_info *info,
4005 struct station_parameters *params)
4006{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304007 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004008 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004009 if (info->attrs[NL80211_ATTR_PEER_AID])
4010 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004011 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4012 params->ht_capa =
4013 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4014 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4015 params->vht_capa =
4016 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4017
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304018 err = nl80211_parse_sta_channel_info(info, params);
4019 if (err)
4020 return err;
4021
Johannes Bergff276692013-02-15 00:09:01 +01004022 return nl80211_parse_sta_wme(info, params);
4023}
4024
Johannes Berg5727ef12007-12-19 02:03:34 +01004025static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4026{
Johannes Berg4c476992010-10-04 21:36:35 +02004027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004028 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004029 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004030 u8 *mac_addr;
4031 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004032
4033 memset(&params, 0, sizeof(params));
4034
4035 params.listen_interval = -1;
4036
Johannes Berg77ee7c82013-02-15 00:48:33 +01004037 if (!rdev->ops->change_station)
4038 return -EOPNOTSUPP;
4039
Johannes Berg5727ef12007-12-19 02:03:34 +01004040 if (info->attrs[NL80211_ATTR_STA_AID])
4041 return -EINVAL;
4042
4043 if (!info->attrs[NL80211_ATTR_MAC])
4044 return -EINVAL;
4045
4046 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4047
4048 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4049 params.supported_rates =
4050 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4051 params.supported_rates_len =
4052 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4053 }
4054
Jouni Malinen9d62a982013-02-14 21:10:13 +02004055 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4056 params.capability =
4057 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4058 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4059 }
4060
4061 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4062 params.ext_capab =
4063 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4064 params.ext_capab_len =
4065 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4066 }
4067
Jouni Malinendf881292013-02-14 21:10:54 +02004068 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004069 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004070
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004071 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004072 return -EINVAL;
4073
Johannes Bergf8bacc22013-02-14 23:27:01 +01004074 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004075 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004076 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4077 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4078 return -EINVAL;
4079 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004080
Johannes Bergf8bacc22013-02-14 23:27:01 +01004081 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004082 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004083 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4084 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4085 return -EINVAL;
4086 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4087 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004088
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004089 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4090 enum nl80211_mesh_power_mode pm = nla_get_u32(
4091 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4092
4093 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4094 pm > NL80211_MESH_POWER_MAX)
4095 return -EINVAL;
4096
4097 params.local_pm = pm;
4098 }
4099
Johannes Berg77ee7c82013-02-15 00:48:33 +01004100 /* Include parameters for TDLS peer (will check later) */
4101 err = nl80211_set_station_tdls(info, &params);
4102 if (err)
4103 return err;
4104
4105 params.vlan = get_vlan(info, rdev);
4106 if (IS_ERR(params.vlan))
4107 return PTR_ERR(params.vlan);
4108
Johannes Berga97f4422009-06-18 17:23:43 +02004109 switch (dev->ieee80211_ptr->iftype) {
4110 case NL80211_IFTYPE_AP:
4111 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004112 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004113 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004114 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004115 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004116 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004117 break;
4118 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004119 err = -EOPNOTSUPP;
4120 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004121 }
4122
Johannes Berg77ee7c82013-02-15 00:48:33 +01004123 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004124 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004125
Johannes Berg77ee7c82013-02-15 00:48:33 +01004126 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004127 if (params.vlan)
4128 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004129
Johannes Berg5727ef12007-12-19 02:03:34 +01004130 return err;
4131}
4132
4133static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4134{
Johannes Berg4c476992010-10-04 21:36:35 +02004135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004136 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004137 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004138 struct station_parameters params;
4139 u8 *mac_addr = NULL;
4140
4141 memset(&params, 0, sizeof(params));
4142
Johannes Berg984c3112013-02-14 23:43:25 +01004143 if (!rdev->ops->add_station)
4144 return -EOPNOTSUPP;
4145
Johannes Berg5727ef12007-12-19 02:03:34 +01004146 if (!info->attrs[NL80211_ATTR_MAC])
4147 return -EINVAL;
4148
Johannes Berg5727ef12007-12-19 02:03:34 +01004149 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4150 return -EINVAL;
4151
4152 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4153 return -EINVAL;
4154
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004155 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4156 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004157 return -EINVAL;
4158
Johannes Berg5727ef12007-12-19 02:03:34 +01004159 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4160 params.supported_rates =
4161 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4162 params.supported_rates_len =
4163 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4164 params.listen_interval =
4165 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004166
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004167 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004168 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004169 else
4170 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004171 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4172 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004173
Jouni Malinen9d62a982013-02-14 21:10:13 +02004174 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4175 params.capability =
4176 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4177 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4178 }
4179
4180 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4181 params.ext_capab =
4182 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4183 params.ext_capab_len =
4184 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4185 }
4186
Jouni Malinen36aedc902008-08-25 11:58:58 +03004187 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4188 params.ht_capa =
4189 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004190
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004191 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4192 params.vht_capa =
4193 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4194
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004195 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4196 params.opmode_notif_used = true;
4197 params.opmode_notif =
4198 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4199 }
4200
Johannes Bergf8bacc22013-02-14 23:27:01 +01004201 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004202 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004203 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4204 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4205 return -EINVAL;
4206 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004207
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304208 err = nl80211_parse_sta_channel_info(info, &params);
4209 if (err)
4210 return err;
4211
Johannes Bergff276692013-02-15 00:09:01 +01004212 err = nl80211_parse_sta_wme(info, &params);
4213 if (err)
4214 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004215
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 Berg77ee7c82013-02-15 00:48:33 +01004219 /* When you run into this, adjust the code below for the new flag */
4220 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4221
Johannes Bergbdd90d52011-12-14 12:20:27 +01004222 switch (dev->ieee80211_ptr->iftype) {
4223 case NL80211_IFTYPE_AP:
4224 case NL80211_IFTYPE_AP_VLAN:
4225 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004226 /* ignore WME attributes if iface/sta is not capable */
4227 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4228 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4229 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004230
Johannes Bergbdd90d52011-12-14 12:20:27 +01004231 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004232 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4233 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004234 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004235 /* but don't bother the driver with it */
4236 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004237
Johannes Bergd582cff2012-10-26 17:53:44 +02004238 /* allow authenticated/associated only if driver handles it */
4239 if (!(rdev->wiphy.features &
4240 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4241 params.sta_flags_mask &
4242 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4243 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4244 return -EINVAL;
4245
Johannes Bergbdd90d52011-12-14 12:20:27 +01004246 /* must be last in here for error handling */
4247 params.vlan = get_vlan(info, rdev);
4248 if (IS_ERR(params.vlan))
4249 return PTR_ERR(params.vlan);
4250 break;
4251 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004252 /* ignore uAPSD data */
4253 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4254
Johannes Bergd582cff2012-10-26 17:53:44 +02004255 /* associated is disallowed */
4256 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4257 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004258 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004259 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4260 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004261 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004262 break;
4263 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004264 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004265 /* ignore uAPSD data */
4266 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4267
Johannes Berg77ee7c82013-02-15 00:48:33 +01004268 /* these are disallowed */
4269 if (params.sta_flags_mask &
4270 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4271 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004272 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004273 /* Only TDLS peers can be added */
4274 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4275 return -EINVAL;
4276 /* Can only add if TDLS ... */
4277 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4278 return -EOPNOTSUPP;
4279 /* ... with external setup is supported */
4280 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4281 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004282 /*
4283 * Older wpa_supplicant versions always mark the TDLS peer
4284 * as authorized, but it shouldn't yet be.
4285 */
4286 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004287 break;
4288 default:
4289 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004290 }
4291
Johannes Bergbdd90d52011-12-14 12:20:27 +01004292 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004293
Hila Gonene35e4d22012-06-27 17:19:42 +03004294 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004295
Johannes Berg5727ef12007-12-19 02:03:34 +01004296 if (params.vlan)
4297 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004298 return err;
4299}
4300
4301static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4302{
Johannes Berg4c476992010-10-04 21:36:35 +02004303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4304 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004305 u8 *mac_addr = NULL;
4306
4307 if (info->attrs[NL80211_ATTR_MAC])
4308 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4309
Johannes Berge80cf852009-05-11 14:43:13 +02004310 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004311 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004312 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4314 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004315
Johannes Berg4c476992010-10-04 21:36:35 +02004316 if (!rdev->ops->del_station)
4317 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004318
Hila Gonene35e4d22012-06-27 17:19:42 +03004319 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004320}
4321
Eric W. Biederman15e47302012-09-07 20:12:54 +00004322static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004323 int flags, struct net_device *dev,
4324 u8 *dst, u8 *next_hop,
4325 struct mpath_info *pinfo)
4326{
4327 void *hdr;
4328 struct nlattr *pinfoattr;
4329
Eric W. Biederman15e47302012-09-07 20:12:54 +00004330 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331 if (!hdr)
4332 return -1;
4333
David S. Miller9360ffd2012-03-29 04:41:26 -04004334 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4335 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4336 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4337 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4338 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004339
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004340 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4341 if (!pinfoattr)
4342 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004343 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4344 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4345 pinfo->frame_qlen))
4346 goto nla_put_failure;
4347 if (((pinfo->filled & MPATH_INFO_SN) &&
4348 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4349 ((pinfo->filled & MPATH_INFO_METRIC) &&
4350 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4351 pinfo->metric)) ||
4352 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4353 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4354 pinfo->exptime)) ||
4355 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4356 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4357 pinfo->flags)) ||
4358 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4359 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4360 pinfo->discovery_timeout)) ||
4361 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4362 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4363 pinfo->discovery_retries)))
4364 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004365
4366 nla_nest_end(msg, pinfoattr);
4367
4368 return genlmsg_end(msg, hdr);
4369
4370 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004371 genlmsg_cancel(msg, hdr);
4372 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004373}
4374
4375static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004376 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004377{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004378 struct mpath_info pinfo;
4379 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004380 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004381 u8 dst[ETH_ALEN];
4382 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004383 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004384 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004385
Johannes Berg97990a02013-04-19 01:02:55 +02004386 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004387 if (err)
4388 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004389
4390 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004391 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004392 goto out_err;
4393 }
4394
Johannes Berg97990a02013-04-19 01:02:55 +02004395 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004396 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004397 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004398 }
4399
Johannes Bergbba95fe2008-07-29 13:22:51 +02004400 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004401 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4402 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004403 if (err == -ENOENT)
4404 break;
4405 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004406 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004407
Eric W. Biederman15e47302012-09-07 20:12:54 +00004408 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004409 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004410 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004411 &pinfo) < 0)
4412 goto out;
4413
4414 path_idx++;
4415 }
4416
4417
4418 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004419 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004420 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004421 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004422 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004423 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004424}
4425
4426static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4427{
Johannes Berg4c476992010-10-04 21:36:35 +02004428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004429 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004430 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004431 struct mpath_info pinfo;
4432 struct sk_buff *msg;
4433 u8 *dst = NULL;
4434 u8 next_hop[ETH_ALEN];
4435
4436 memset(&pinfo, 0, sizeof(pinfo));
4437
4438 if (!info->attrs[NL80211_ATTR_MAC])
4439 return -EINVAL;
4440
4441 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4442
Johannes Berg4c476992010-10-04 21:36:35 +02004443 if (!rdev->ops->get_mpath)
4444 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004445
Johannes Berg4c476992010-10-04 21:36:35 +02004446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4447 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004448
Hila Gonene35e4d22012-06-27 17:19:42 +03004449 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004450 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004451 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004452
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004454 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004455 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004456
Eric W. Biederman15e47302012-09-07 20:12:54 +00004457 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004458 dev, dst, next_hop, &pinfo) < 0) {
4459 nlmsg_free(msg);
4460 return -ENOBUFS;
4461 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004464}
4465
4466static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4467{
Johannes Berg4c476992010-10-04 21:36:35 +02004468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4469 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470 u8 *dst = NULL;
4471 u8 *next_hop = NULL;
4472
4473 if (!info->attrs[NL80211_ATTR_MAC])
4474 return -EINVAL;
4475
4476 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4477 return -EINVAL;
4478
4479 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4480 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4481
Johannes Berg4c476992010-10-04 21:36:35 +02004482 if (!rdev->ops->change_mpath)
4483 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004484
Johannes Berg4c476992010-10-04 21:36:35 +02004485 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4486 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487
Hila Gonene35e4d22012-06-27 17:19:42 +03004488 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004489}
Johannes Berg4c476992010-10-04 21:36:35 +02004490
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4494 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004495 u8 *dst = NULL;
4496 u8 *next_hop = NULL;
4497
4498 if (!info->attrs[NL80211_ATTR_MAC])
4499 return -EINVAL;
4500
4501 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4502 return -EINVAL;
4503
4504 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4505 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4506
Johannes Berg4c476992010-10-04 21:36:35 +02004507 if (!rdev->ops->add_mpath)
4508 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004509
Johannes Berg4c476992010-10-04 21:36:35 +02004510 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4511 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004512
Hila Gonene35e4d22012-06-27 17:19:42 +03004513 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004514}
4515
4516static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4517{
Johannes Berg4c476992010-10-04 21:36:35 +02004518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4519 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004520 u8 *dst = NULL;
4521
4522 if (info->attrs[NL80211_ATTR_MAC])
4523 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4524
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (!rdev->ops->del_mpath)
4526 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004527
Hila Gonene35e4d22012-06-27 17:19:42 +03004528 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004529}
4530
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004531static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4532{
Johannes Berg4c476992010-10-04 21:36:35 +02004533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4534 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004535 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004536 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004537 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004538
4539 memset(&params, 0, sizeof(params));
4540 /* default to not changing parameters */
4541 params.use_cts_prot = -1;
4542 params.use_short_preamble = -1;
4543 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004544 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004545 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004546 params.p2p_ctwindow = -1;
4547 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004548
4549 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4550 params.use_cts_prot =
4551 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4552 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4553 params.use_short_preamble =
4554 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4555 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4556 params.use_short_slot_time =
4557 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004558 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4559 params.basic_rates =
4560 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4561 params.basic_rates_len =
4562 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4563 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004564 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4565 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004566 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4567 params.ht_opmode =
4568 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004569
Johannes Berg53cabad2012-11-14 15:17:28 +01004570 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4571 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4572 return -EINVAL;
4573 params.p2p_ctwindow =
4574 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4575 if (params.p2p_ctwindow < 0)
4576 return -EINVAL;
4577 if (params.p2p_ctwindow != 0 &&
4578 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4579 return -EINVAL;
4580 }
4581
4582 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4583 u8 tmp;
4584
4585 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4586 return -EINVAL;
4587 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4588 if (tmp > 1)
4589 return -EINVAL;
4590 params.p2p_opp_ps = tmp;
4591 if (params.p2p_opp_ps &&
4592 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4593 return -EINVAL;
4594 }
4595
Johannes Berg4c476992010-10-04 21:36:35 +02004596 if (!rdev->ops->change_bss)
4597 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004598
Johannes Berg074ac8d2010-09-16 14:58:22 +02004599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004600 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4601 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004602
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004603 wdev_lock(wdev);
4604 err = rdev_change_bss(rdev, dev, &params);
4605 wdev_unlock(wdev);
4606
4607 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004608}
4609
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004610static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004611 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4612 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4613 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4614 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4615 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4616 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4617};
4618
4619static int parse_reg_rule(struct nlattr *tb[],
4620 struct ieee80211_reg_rule *reg_rule)
4621{
4622 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4623 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4624
4625 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4626 return -EINVAL;
4627 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4628 return -EINVAL;
4629 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4630 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004631 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4632 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004633 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4634 return -EINVAL;
4635
4636 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4637
4638 freq_range->start_freq_khz =
4639 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4640 freq_range->end_freq_khz =
4641 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004642 freq_range->max_bandwidth_khz =
4643 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004644
4645 power_rule->max_eirp =
4646 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4647
4648 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4649 power_rule->max_antenna_gain =
4650 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4651
4652 return 0;
4653}
4654
4655static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4656{
4657 int r;
4658 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004659 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004660
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004661 /*
4662 * You should only get this when cfg80211 hasn't yet initialized
4663 * completely when built-in to the kernel right between the time
4664 * window between nl80211_init() and regulatory_init(), if that is
4665 * even possible.
4666 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004667 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004668 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004669
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004670 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4671 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004672
4673 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4674
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004675 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4676 user_reg_hint_type =
4677 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4678 else
4679 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4680
4681 switch (user_reg_hint_type) {
4682 case NL80211_USER_REG_HINT_USER:
4683 case NL80211_USER_REG_HINT_CELL_BASE:
4684 break;
4685 default:
4686 return -EINVAL;
4687 }
4688
4689 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004690
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004691 return r;
4692}
4693
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004694static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004695 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004696{
Johannes Berg4c476992010-10-04 21:36:35 +02004697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004698 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004699 struct wireless_dev *wdev = dev->ieee80211_ptr;
4700 struct mesh_config cur_params;
4701 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004702 void *hdr;
4703 struct nlattr *pinfoattr;
4704 struct sk_buff *msg;
4705
Johannes Berg29cbe682010-12-03 09:20:44 +01004706 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4707 return -EOPNOTSUPP;
4708
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004709 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004710 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004711
Johannes Berg29cbe682010-12-03 09:20:44 +01004712 wdev_lock(wdev);
4713 /* If not connected, get default parameters */
4714 if (!wdev->mesh_id_len)
4715 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4716 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004717 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004718 wdev_unlock(wdev);
4719
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004720 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004721 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004722
4723 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004724 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004725 if (!msg)
4726 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004727 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004728 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004729 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004730 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004731 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004732 if (!pinfoattr)
4733 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004734 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4735 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4736 cur_params.dot11MeshRetryTimeout) ||
4737 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4738 cur_params.dot11MeshConfirmTimeout) ||
4739 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4740 cur_params.dot11MeshHoldingTimeout) ||
4741 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4742 cur_params.dot11MeshMaxPeerLinks) ||
4743 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4744 cur_params.dot11MeshMaxRetries) ||
4745 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4746 cur_params.dot11MeshTTL) ||
4747 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4748 cur_params.element_ttl) ||
4749 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4750 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004751 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4752 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004753 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4754 cur_params.dot11MeshHWMPmaxPREQretries) ||
4755 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4756 cur_params.path_refresh_time) ||
4757 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4758 cur_params.min_discovery_timeout) ||
4759 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4760 cur_params.dot11MeshHWMPactivePathTimeout) ||
4761 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4762 cur_params.dot11MeshHWMPpreqMinInterval) ||
4763 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4764 cur_params.dot11MeshHWMPperrMinInterval) ||
4765 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4766 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4767 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4768 cur_params.dot11MeshHWMPRootMode) ||
4769 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4770 cur_params.dot11MeshHWMPRannInterval) ||
4771 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4772 cur_params.dot11MeshGateAnnouncementProtocol) ||
4773 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4774 cur_params.dot11MeshForwarding) ||
4775 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004776 cur_params.rssi_threshold) ||
4777 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004778 cur_params.ht_opmode) ||
4779 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4780 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4781 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004782 cur_params.dot11MeshHWMProotInterval) ||
4783 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004784 cur_params.dot11MeshHWMPconfirmationInterval) ||
4785 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4786 cur_params.power_mode) ||
4787 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004788 cur_params.dot11MeshAwakeWindowDuration) ||
4789 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4790 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004791 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004792 nla_nest_end(msg, pinfoattr);
4793 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004794 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004795
Johannes Berg3b858752009-03-12 09:55:09 +01004796 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004797 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004798 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004799 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004800 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004801}
4802
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004803static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004804 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4805 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4806 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4807 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4808 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4809 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004810 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004811 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004812 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004813 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4814 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4815 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4816 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4817 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004818 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004819 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004820 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004821 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004822 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004823 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004824 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4825 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004826 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4827 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004828 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004829 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4830 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004831 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004832};
4833
Javier Cardonac80d5452010-12-16 17:37:49 -08004834static const struct nla_policy
4835 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004836 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004837 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4838 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004839 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004840 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004841 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004842 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004843 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004844 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004845};
4846
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004847static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004848 struct mesh_config *cfg,
4849 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004850{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004851 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004852 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004853
Marco Porschea54fba2013-01-07 16:04:48 +01004854#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4855do { \
4856 if (tb[attr]) { \
4857 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4858 return -EINVAL; \
4859 cfg->param = fn(tb[attr]); \
4860 mask |= (1 << (attr - 1)); \
4861 } \
4862} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004863
4864
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004865 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004866 return -EINVAL;
4867 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004868 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004869 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004870 return -EINVAL;
4871
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004872 /* This makes sure that there aren't more than 32 mesh config
4873 * parameters (otherwise our bitfield scheme would not work.) */
4874 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4875
4876 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004877 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004878 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4879 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004880 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004881 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4882 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004883 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004884 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4885 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004886 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004887 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4888 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004889 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004890 mask, NL80211_MESHCONF_MAX_RETRIES,
4891 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004892 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004893 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004895 mask, NL80211_MESHCONF_ELEMENT_TTL,
4896 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004897 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004898 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4899 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4901 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004902 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4903 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004904 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004905 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4906 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004907 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004908 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4909 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004910 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004911 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4912 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4914 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004915 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4916 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004917 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004918 1, 65535, mask,
4919 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004920 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004921 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004922 1, 65535, mask,
4923 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004924 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004926 dot11MeshHWMPnetDiameterTraversalTime,
4927 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004928 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4929 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004930 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4931 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4932 nla_get_u8);
4933 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4934 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004935 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004936 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004937 dot11MeshGateAnnouncementProtocol, 0, 1,
4938 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004939 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004941 mask, NL80211_MESHCONF_FORWARDING,
4942 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004944 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004945 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004946 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004947 mask, NL80211_MESHCONF_HT_OPMODE,
4948 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004949 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004950 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004951 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4952 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004953 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004954 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4955 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004956 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004957 dot11MeshHWMPconfirmationInterval,
4958 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004959 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4960 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004961 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4962 NL80211_MESH_POWER_ACTIVE,
4963 NL80211_MESH_POWER_MAX,
4964 mask, NL80211_MESHCONF_POWER_MODE,
4965 nla_get_u32);
4966 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4967 0, 65535, mask,
4968 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004969 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4970 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4971 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004972 if (mask_out)
4973 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004974
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004975 return 0;
4976
4977#undef FILL_IN_MESH_PARAM_IF_SET
4978}
4979
Javier Cardonac80d5452010-12-16 17:37:49 -08004980static int nl80211_parse_mesh_setup(struct genl_info *info,
4981 struct mesh_setup *setup)
4982{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004984 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4985
4986 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4987 return -EINVAL;
4988 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4989 info->attrs[NL80211_ATTR_MESH_SETUP],
4990 nl80211_mesh_setup_params_policy))
4991 return -EINVAL;
4992
Javier Cardonad299a1f2012-03-31 11:31:33 -07004993 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4994 setup->sync_method =
4995 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4996 IEEE80211_SYNC_METHOD_VENDOR :
4997 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4998
Javier Cardonac80d5452010-12-16 17:37:49 -08004999 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5000 setup->path_sel_proto =
5001 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5002 IEEE80211_PATH_PROTOCOL_VENDOR :
5003 IEEE80211_PATH_PROTOCOL_HWMP;
5004
5005 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5006 setup->path_metric =
5007 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5008 IEEE80211_PATH_METRIC_VENDOR :
5009 IEEE80211_PATH_METRIC_AIRTIME;
5010
Javier Cardona581a8b02011-04-07 15:08:27 -07005011
5012 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005013 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005014 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005015 if (!is_valid_ie_attr(ieattr))
5016 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005017 setup->ie = nla_data(ieattr);
5018 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005019 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005020 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5021 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5022 return -EINVAL;
5023 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005024 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5025 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005026 if (setup->is_secure)
5027 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005028
Colleen Twitty6e16d902013-05-08 11:45:59 -07005029 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5030 if (!setup->user_mpm)
5031 return -EINVAL;
5032 setup->auth_id =
5033 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5034 }
5035
Javier Cardonac80d5452010-12-16 17:37:49 -08005036 return 0;
5037}
5038
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005039static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005040 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005041{
5042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5043 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005044 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005045 struct mesh_config cfg;
5046 u32 mask;
5047 int err;
5048
Johannes Berg29cbe682010-12-03 09:20:44 +01005049 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5050 return -EOPNOTSUPP;
5051
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005052 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005053 return -EOPNOTSUPP;
5054
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005055 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005056 if (err)
5057 return err;
5058
Johannes Berg29cbe682010-12-03 09:20:44 +01005059 wdev_lock(wdev);
5060 if (!wdev->mesh_id_len)
5061 err = -ENOLINK;
5062
5063 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005064 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005065
5066 wdev_unlock(wdev);
5067
5068 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005069}
5070
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005071static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5072{
Johannes Berg458f4f92012-12-06 15:47:38 +01005073 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005074 struct sk_buff *msg;
5075 void *hdr = NULL;
5076 struct nlattr *nl_reg_rules;
5077 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005078
5079 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005080 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005081
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005082 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005083 if (!msg)
5084 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005085
Eric W. Biederman15e47302012-09-07 20:12:54 +00005086 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005087 NL80211_CMD_GET_REG);
5088 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005089 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005090
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005091 if (reg_last_request_cell_base() &&
5092 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5093 NL80211_USER_REG_HINT_CELL_BASE))
5094 goto nla_put_failure;
5095
Johannes Berg458f4f92012-12-06 15:47:38 +01005096 rcu_read_lock();
5097 regdom = rcu_dereference(cfg80211_regdomain);
5098
5099 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5100 (regdom->dfs_region &&
5101 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5102 goto nla_put_failure_rcu;
5103
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005104 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5105 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005106 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005107
Johannes Berg458f4f92012-12-06 15:47:38 +01005108 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005109 struct nlattr *nl_reg_rule;
5110 const struct ieee80211_reg_rule *reg_rule;
5111 const struct ieee80211_freq_range *freq_range;
5112 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005113 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005114
Johannes Berg458f4f92012-12-06 15:47:38 +01005115 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005116 freq_range = &reg_rule->freq_range;
5117 power_rule = &reg_rule->power_rule;
5118
5119 nl_reg_rule = nla_nest_start(msg, i);
5120 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005121 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005122
Janusz Dziedzic97524822014-01-30 09:52:20 +01005123 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5124 if (!max_bandwidth_khz)
5125 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5126 reg_rule);
5127
David S. Miller9360ffd2012-03-29 04:41:26 -04005128 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5129 reg_rule->flags) ||
5130 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5131 freq_range->start_freq_khz) ||
5132 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5133 freq_range->end_freq_khz) ||
5134 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005135 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005136 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5137 power_rule->max_antenna_gain) ||
5138 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5139 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005140 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005141
5142 nla_nest_end(msg, nl_reg_rule);
5143 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005144 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005145
5146 nla_nest_end(msg, nl_reg_rules);
5147
5148 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005149 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005150
Johannes Berg458f4f92012-12-06 15:47:38 +01005151nla_put_failure_rcu:
5152 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005153nla_put_failure:
5154 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005155put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005156 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005157 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005158}
5159
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005160static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5161{
5162 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5163 struct nlattr *nl_reg_rule;
5164 char *alpha2 = NULL;
5165 int rem_reg_rules = 0, r = 0;
5166 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005167 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005168 struct ieee80211_regdomain *rd = NULL;
5169
5170 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5171 return -EINVAL;
5172
5173 if (!info->attrs[NL80211_ATTR_REG_RULES])
5174 return -EINVAL;
5175
5176 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5177
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005178 if (info->attrs[NL80211_ATTR_DFS_REGION])
5179 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5180
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005181 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005182 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005183 num_rules++;
5184 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005185 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005186 }
5187
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005188 if (!reg_is_valid_request(alpha2))
5189 return -EINVAL;
5190
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005191 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005192 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005193
5194 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005195 if (!rd)
5196 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005197
5198 rd->n_reg_rules = num_rules;
5199 rd->alpha2[0] = alpha2[0];
5200 rd->alpha2[1] = alpha2[1];
5201
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005202 /*
5203 * Disable DFS master mode if the DFS region was
5204 * not supported or known on this kernel.
5205 */
5206 if (reg_supported_dfs_region(dfs_region))
5207 rd->dfs_region = dfs_region;
5208
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005209 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005210 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005211 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5212 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5213 reg_rule_policy);
5214 if (r)
5215 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005216 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5217 if (r)
5218 goto bad_reg;
5219
5220 rule_idx++;
5221
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005222 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5223 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005224 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005225 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005226 }
5227
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005228 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005229 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005230 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005231
Johannes Bergd2372b32008-10-24 20:32:20 +02005232 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005233 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005234 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005235}
5236
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005237static int validate_scan_freqs(struct nlattr *freqs)
5238{
5239 struct nlattr *attr1, *attr2;
5240 int n_channels = 0, tmp1, tmp2;
5241
5242 nla_for_each_nested(attr1, freqs, tmp1) {
5243 n_channels++;
5244 /*
5245 * Some hardware has a limited channel list for
5246 * scanning, and it is pretty much nonsensical
5247 * to scan for a channel twice, so disallow that
5248 * and don't require drivers to check that the
5249 * channel list they get isn't longer than what
5250 * they can scan, as long as they can scan all
5251 * the channels they registered at once.
5252 */
5253 nla_for_each_nested(attr2, freqs, tmp2)
5254 if (attr1 != attr2 &&
5255 nla_get_u32(attr1) == nla_get_u32(attr2))
5256 return 0;
5257 }
5258
5259 return n_channels;
5260}
5261
Johannes Berg2a519312009-02-10 21:25:55 +01005262static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5263{
Johannes Berg4c476992010-10-04 21:36:35 +02005264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005265 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005266 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005267 struct nlattr *attr;
5268 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005269 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005270 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005271
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005272 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5273 return -EINVAL;
5274
Johannes Berg79c97e92009-07-07 03:56:12 +02005275 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005276
Johannes Berg4c476992010-10-04 21:36:35 +02005277 if (!rdev->ops->scan)
5278 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005279
Johannes Bergf9d15d12014-01-22 11:14:19 +02005280 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005281 err = -EBUSY;
5282 goto unlock;
5283 }
Johannes Berg2a519312009-02-10 21:25:55 +01005284
5285 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005286 n_channels = validate_scan_freqs(
5287 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005288 if (!n_channels) {
5289 err = -EINVAL;
5290 goto unlock;
5291 }
Johannes Berg2a519312009-02-10 21:25:55 +01005292 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005293 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005294 }
5295
5296 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5297 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5298 n_ssids++;
5299
Johannes Bergf9f47522013-03-19 15:04:07 +01005300 if (n_ssids > wiphy->max_scan_ssids) {
5301 err = -EINVAL;
5302 goto unlock;
5303 }
Johannes Berg2a519312009-02-10 21:25:55 +01005304
Jouni Malinen70692ad2009-02-16 19:39:13 +02005305 if (info->attrs[NL80211_ATTR_IE])
5306 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5307 else
5308 ie_len = 0;
5309
Johannes Bergf9f47522013-03-19 15:04:07 +01005310 if (ie_len > wiphy->max_scan_ie_len) {
5311 err = -EINVAL;
5312 goto unlock;
5313 }
Johannes Berg18a83652009-03-31 12:12:05 +02005314
Johannes Berg2a519312009-02-10 21:25:55 +01005315 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005316 + sizeof(*request->ssids) * n_ssids
5317 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005318 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005319 if (!request) {
5320 err = -ENOMEM;
5321 goto unlock;
5322 }
Johannes Berg2a519312009-02-10 21:25:55 +01005323
Johannes Berg2a519312009-02-10 21:25:55 +01005324 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005325 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005326 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005327 if (ie_len) {
5328 if (request->ssids)
5329 request->ie = (void *)(request->ssids + n_ssids);
5330 else
5331 request->ie = (void *)(request->channels + n_channels);
5332 }
Johannes Berg2a519312009-02-10 21:25:55 +01005333
Johannes Berg584991d2009-11-02 13:32:03 +01005334 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005335 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5336 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005337 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005338 struct ieee80211_channel *chan;
5339
5340 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5341
5342 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005343 err = -EINVAL;
5344 goto out_free;
5345 }
Johannes Berg584991d2009-11-02 13:32:03 +01005346
5347 /* ignore disabled channels */
5348 if (chan->flags & IEEE80211_CHAN_DISABLED)
5349 continue;
5350
5351 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005352 i++;
5353 }
5354 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005355 enum ieee80211_band band;
5356
Johannes Berg2a519312009-02-10 21:25:55 +01005357 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005358 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5359 int j;
5360 if (!wiphy->bands[band])
5361 continue;
5362 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005363 struct ieee80211_channel *chan;
5364
5365 chan = &wiphy->bands[band]->channels[j];
5366
5367 if (chan->flags & IEEE80211_CHAN_DISABLED)
5368 continue;
5369
5370 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005371 i++;
5372 }
5373 }
5374 }
5375
Johannes Berg584991d2009-11-02 13:32:03 +01005376 if (!i) {
5377 err = -EINVAL;
5378 goto out_free;
5379 }
5380
5381 request->n_channels = i;
5382
Johannes Berg2a519312009-02-10 21:25:55 +01005383 i = 0;
5384 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5385 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005386 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005387 err = -EINVAL;
5388 goto out_free;
5389 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005390 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005391 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005392 i++;
5393 }
5394 }
5395
Jouni Malinen70692ad2009-02-16 19:39:13 +02005396 if (info->attrs[NL80211_ATTR_IE]) {
5397 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005398 memcpy((void *)request->ie,
5399 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005400 request->ie_len);
5401 }
5402
Johannes Berg34850ab2011-07-18 18:08:35 +02005403 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005404 if (wiphy->bands[i])
5405 request->rates[i] =
5406 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005407
5408 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5409 nla_for_each_nested(attr,
5410 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5411 tmp) {
5412 enum ieee80211_band band = nla_type(attr);
5413
Dan Carpenter84404622011-07-29 11:52:18 +03005414 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005415 err = -EINVAL;
5416 goto out_free;
5417 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005418
5419 if (!wiphy->bands[band])
5420 continue;
5421
Johannes Berg34850ab2011-07-18 18:08:35 +02005422 err = ieee80211_get_ratemask(wiphy->bands[band],
5423 nla_data(attr),
5424 nla_len(attr),
5425 &request->rates[band]);
5426 if (err)
5427 goto out_free;
5428 }
5429 }
5430
Sam Leffler46856bb2012-10-11 21:03:32 -07005431 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005432 request->flags = nla_get_u32(
5433 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005434 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5435 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005436 err = -EOPNOTSUPP;
5437 goto out_free;
5438 }
5439 }
Sam Lefflered4737712012-10-11 21:03:31 -07005440
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305441 request->no_cck =
5442 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5443
Johannes Bergfd014282012-06-18 19:17:03 +02005444 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005445 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005446 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005447
Johannes Berg79c97e92009-07-07 03:56:12 +02005448 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005449 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005450
Johannes Berg463d0182009-07-14 00:33:35 +02005451 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005452 nl80211_send_scan_start(rdev, wdev);
5453 if (wdev->netdev)
5454 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005455 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005456 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005457 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005458 kfree(request);
5459 }
Johannes Berg3b858752009-03-12 09:55:09 +01005460
Johannes Bergf9f47522013-03-19 15:04:07 +01005461 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005462 return err;
5463}
5464
Luciano Coelho807f8a82011-05-11 17:09:35 +03005465static int nl80211_start_sched_scan(struct sk_buff *skb,
5466 struct genl_info *info)
5467{
5468 struct cfg80211_sched_scan_request *request;
5469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5470 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005471 struct nlattr *attr;
5472 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005473 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005474 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005475 enum ieee80211_band band;
5476 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005477 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005478 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005479
5480 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5481 !rdev->ops->sched_scan_start)
5482 return -EOPNOTSUPP;
5483
5484 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5485 return -EINVAL;
5486
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005487 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5488 return -EINVAL;
5489
5490 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5491 if (interval == 0)
5492 return -EINVAL;
5493
Luciano Coelho807f8a82011-05-11 17:09:35 +03005494 wiphy = &rdev->wiphy;
5495
5496 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5497 n_channels = validate_scan_freqs(
5498 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5499 if (!n_channels)
5500 return -EINVAL;
5501 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005502 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005503 }
5504
5505 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5506 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5507 tmp)
5508 n_ssids++;
5509
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005510 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005511 return -EINVAL;
5512
Johannes Bergea73cbc2014-01-24 10:53:53 +01005513 /*
5514 * First, count the number of 'real' matchsets. Due to an issue with
5515 * the old implementation, matchsets containing only the RSSI attribute
5516 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5517 * RSSI for all matchsets, rather than their own matchset for reporting
5518 * all APs with a strong RSSI. This is needed to be compatible with
5519 * older userspace that treated a matchset with only the RSSI as the
5520 * global RSSI for all other matchsets - if there are other matchsets.
5521 */
5522 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005523 nla_for_each_nested(attr,
5524 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005525 tmp) {
5526 struct nlattr *rssi;
5527
5528 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5529 nla_data(attr), nla_len(attr),
5530 nl80211_match_policy);
5531 if (err)
5532 return err;
5533 /* add other standalone attributes here */
5534 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5535 n_match_sets++;
5536 continue;
5537 }
5538 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5539 if (rssi)
5540 default_match_rssi = nla_get_s32(rssi);
5541 }
5542 }
5543
5544 /* However, if there's no other matchset, add the RSSI one */
5545 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5546 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005547
5548 if (n_match_sets > wiphy->max_match_sets)
5549 return -EINVAL;
5550
Luciano Coelho807f8a82011-05-11 17:09:35 +03005551 if (info->attrs[NL80211_ATTR_IE])
5552 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5553 else
5554 ie_len = 0;
5555
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005556 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005557 return -EINVAL;
5558
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005559 if (rdev->sched_scan_req) {
5560 err = -EINPROGRESS;
5561 goto out;
5562 }
5563
Luciano Coelho807f8a82011-05-11 17:09:35 +03005564 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005565 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005566 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005567 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005568 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005569 if (!request) {
5570 err = -ENOMEM;
5571 goto out;
5572 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005573
5574 if (n_ssids)
5575 request->ssids = (void *)&request->channels[n_channels];
5576 request->n_ssids = n_ssids;
5577 if (ie_len) {
5578 if (request->ssids)
5579 request->ie = (void *)(request->ssids + n_ssids);
5580 else
5581 request->ie = (void *)(request->channels + n_channels);
5582 }
5583
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005584 if (n_match_sets) {
5585 if (request->ie)
5586 request->match_sets = (void *)(request->ie + ie_len);
5587 else if (request->ssids)
5588 request->match_sets =
5589 (void *)(request->ssids + n_ssids);
5590 else
5591 request->match_sets =
5592 (void *)(request->channels + n_channels);
5593 }
5594 request->n_match_sets = n_match_sets;
5595
Luciano Coelho807f8a82011-05-11 17:09:35 +03005596 i = 0;
5597 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5598 /* user specified, bail out if channel not found */
5599 nla_for_each_nested(attr,
5600 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5601 tmp) {
5602 struct ieee80211_channel *chan;
5603
5604 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5605
5606 if (!chan) {
5607 err = -EINVAL;
5608 goto out_free;
5609 }
5610
5611 /* ignore disabled channels */
5612 if (chan->flags & IEEE80211_CHAN_DISABLED)
5613 continue;
5614
5615 request->channels[i] = chan;
5616 i++;
5617 }
5618 } else {
5619 /* all channels */
5620 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5621 int j;
5622 if (!wiphy->bands[band])
5623 continue;
5624 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5625 struct ieee80211_channel *chan;
5626
5627 chan = &wiphy->bands[band]->channels[j];
5628
5629 if (chan->flags & IEEE80211_CHAN_DISABLED)
5630 continue;
5631
5632 request->channels[i] = chan;
5633 i++;
5634 }
5635 }
5636 }
5637
5638 if (!i) {
5639 err = -EINVAL;
5640 goto out_free;
5641 }
5642
5643 request->n_channels = i;
5644
5645 i = 0;
5646 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5647 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5648 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005649 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005650 err = -EINVAL;
5651 goto out_free;
5652 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005653 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005654 memcpy(request->ssids[i].ssid, nla_data(attr),
5655 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005656 i++;
5657 }
5658 }
5659
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005660 i = 0;
5661 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5662 nla_for_each_nested(attr,
5663 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5664 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005665 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005666
Johannes Bergae811e22014-01-24 10:17:47 +01005667 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5668 nla_data(attr), nla_len(attr),
5669 nl80211_match_policy);
5670 if (err)
5671 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005672 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005673 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005674 if (WARN_ON(i >= n_match_sets)) {
5675 /* this indicates a programming error,
5676 * the loop above should have verified
5677 * things properly
5678 */
5679 err = -EINVAL;
5680 goto out_free;
5681 }
5682
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005683 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5684 err = -EINVAL;
5685 goto out_free;
5686 }
5687 memcpy(request->match_sets[i].ssid.ssid,
5688 nla_data(ssid), nla_len(ssid));
5689 request->match_sets[i].ssid.ssid_len =
5690 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005691 /* special attribute - old implemenation w/a */
5692 request->match_sets[i].rssi_thold =
5693 default_match_rssi;
5694 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5695 if (rssi)
5696 request->match_sets[i].rssi_thold =
5697 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005698 }
5699 i++;
5700 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005701
5702 /* there was no other matchset, so the RSSI one is alone */
5703 if (i == 0)
5704 request->match_sets[0].rssi_thold = default_match_rssi;
5705
5706 request->min_rssi_thold = INT_MAX;
5707 for (i = 0; i < n_match_sets; i++)
5708 request->min_rssi_thold =
5709 min(request->match_sets[i].rssi_thold,
5710 request->min_rssi_thold);
5711 } else {
5712 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005713 }
5714
Johannes Berg9900e482014-02-04 21:01:25 +01005715 if (ie_len) {
5716 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005717 memcpy((void *)request->ie,
5718 nla_data(info->attrs[NL80211_ATTR_IE]),
5719 request->ie_len);
5720 }
5721
Sam Leffler46856bb2012-10-11 21:03:32 -07005722 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005723 request->flags = nla_get_u32(
5724 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005725 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5726 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005727 err = -EOPNOTSUPP;
5728 goto out_free;
5729 }
5730 }
Sam Lefflered4737712012-10-11 21:03:31 -07005731
Luciano Coelho807f8a82011-05-11 17:09:35 +03005732 request->dev = dev;
5733 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005734 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005735 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005736
Hila Gonene35e4d22012-06-27 17:19:42 +03005737 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005738 if (!err) {
5739 rdev->sched_scan_req = request;
5740 nl80211_send_sched_scan(rdev, dev,
5741 NL80211_CMD_START_SCHED_SCAN);
5742 goto out;
5743 }
5744
5745out_free:
5746 kfree(request);
5747out:
5748 return err;
5749}
5750
5751static int nl80211_stop_sched_scan(struct sk_buff *skb,
5752 struct genl_info *info)
5753{
5754 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5755
5756 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5757 !rdev->ops->sched_scan_stop)
5758 return -EOPNOTSUPP;
5759
Johannes Berg5fe231e2013-05-08 21:45:15 +02005760 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005761}
5762
Simon Wunderlich04f39042013-02-08 18:16:19 +01005763static int nl80211_start_radar_detection(struct sk_buff *skb,
5764 struct genl_info *info)
5765{
5766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5767 struct net_device *dev = info->user_ptr[1];
5768 struct wireless_dev *wdev = dev->ieee80211_ptr;
5769 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005770 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005771 int err;
5772
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005773 dfs_region = reg_get_dfs_region(wdev->wiphy);
5774 if (dfs_region == NL80211_DFS_UNSET)
5775 return -EINVAL;
5776
Simon Wunderlich04f39042013-02-08 18:16:19 +01005777 err = nl80211_parse_chandef(rdev, info, &chandef);
5778 if (err)
5779 return err;
5780
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005781 if (netif_carrier_ok(dev))
5782 return -EBUSY;
5783
Simon Wunderlich04f39042013-02-08 18:16:19 +01005784 if (wdev->cac_started)
5785 return -EBUSY;
5786
5787 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5788 if (err < 0)
5789 return err;
5790
5791 if (err == 0)
5792 return -EINVAL;
5793
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005794 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005795 return -EINVAL;
5796
5797 if (!rdev->ops->start_radar_detection)
5798 return -EOPNOTSUPP;
5799
Simon Wunderlich04f39042013-02-08 18:16:19 +01005800 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5801 chandef.chan, CHAN_MODE_SHARED,
5802 BIT(chandef.width));
5803 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005804 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005805
5806 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5807 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005808 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005809 wdev->cac_started = true;
5810 wdev->cac_start_time = jiffies;
5811 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005812 return err;
5813}
5814
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005815static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5816{
5817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5818 struct net_device *dev = info->user_ptr[1];
5819 struct wireless_dev *wdev = dev->ieee80211_ptr;
5820 struct cfg80211_csa_settings params;
5821 /* csa_attrs is defined static to avoid waste of stack size - this
5822 * function is called under RTNL lock, so this should not be a problem.
5823 */
5824 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5825 u8 radar_detect_width = 0;
5826 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005827 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005828
5829 if (!rdev->ops->channel_switch ||
5830 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5831 return -EOPNOTSUPP;
5832
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005833 switch (dev->ieee80211_ptr->iftype) {
5834 case NL80211_IFTYPE_AP:
5835 case NL80211_IFTYPE_P2P_GO:
5836 need_new_beacon = true;
5837
5838 /* useless if AP is not running */
5839 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005840 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005841 break;
5842 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005843 if (!wdev->ssid_len)
5844 return -ENOTCONN;
5845 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005846 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005847 if (!wdev->mesh_id_len)
5848 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005849 break;
5850 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005851 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005852 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005853
5854 memset(&params, 0, sizeof(params));
5855
5856 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5857 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5858 return -EINVAL;
5859
5860 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005861 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005862 return -EINVAL;
5863
5864 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5865
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005866 if (!need_new_beacon)
5867 goto skip_beacons;
5868
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005869 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5870 if (err)
5871 return err;
5872
5873 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5874 info->attrs[NL80211_ATTR_CSA_IES],
5875 nl80211_policy);
5876 if (err)
5877 return err;
5878
5879 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5880 if (err)
5881 return err;
5882
5883 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5884 return -EINVAL;
5885
5886 params.counter_offset_beacon =
5887 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5888 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5889 return -EINVAL;
5890
5891 /* sanity check - counters should be the same */
5892 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5893 params.count)
5894 return -EINVAL;
5895
5896 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5897 params.counter_offset_presp =
5898 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5899 if (params.counter_offset_presp >=
5900 params.beacon_csa.probe_resp_len)
5901 return -EINVAL;
5902
5903 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5904 params.count)
5905 return -EINVAL;
5906 }
5907
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005908skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005909 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5910 if (err)
5911 return err;
5912
5913 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5914 return -EINVAL;
5915
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005916 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
Simon Wunderlich5336fa82013-10-07 18:41:05 +02005917 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5918 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005919 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5920 &params.chandef);
5921 if (err < 0) {
5922 return err;
5923 } else if (err) {
5924 radar_detect_width = BIT(params.chandef.width);
5925 params.radar_required = true;
5926 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005927 }
5928
5929 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5930 params.chandef.chan,
5931 CHAN_MODE_SHARED,
5932 radar_detect_width);
5933 if (err)
5934 return err;
5935
5936 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5937 params.block_tx = true;
5938
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005939 wdev_lock(wdev);
5940 err = rdev_channel_switch(rdev, dev, &params);
5941 wdev_unlock(wdev);
5942
5943 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005944}
5945
Johannes Berg9720bb32011-06-21 09:45:33 +02005946static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5947 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005948 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005949 struct wireless_dev *wdev,
5950 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005951{
Johannes Berg48ab9052009-07-10 18:42:31 +02005952 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005953 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005954 void *hdr;
5955 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005956 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005957
5958 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005959
Eric W. Biederman15e47302012-09-07 20:12:54 +00005960 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005961 NL80211_CMD_NEW_SCAN_RESULTS);
5962 if (!hdr)
5963 return -1;
5964
Johannes Berg9720bb32011-06-21 09:45:33 +02005965 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5966
Johannes Berg97990a02013-04-19 01:02:55 +02005967 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5968 goto nla_put_failure;
5969 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005970 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5971 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005972 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5973 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005974
5975 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5976 if (!bss)
5977 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005978 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005979 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005980 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005981
5982 rcu_read_lock();
5983 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005984 if (ies) {
5985 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5986 goto fail_unlock_rcu;
5987 tsf = true;
5988 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5989 ies->len, ies->data))
5990 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005991 }
5992 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005993 if (ies) {
5994 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5995 goto fail_unlock_rcu;
5996 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5997 ies->len, ies->data))
5998 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005999 }
6000 rcu_read_unlock();
6001
David S. Miller9360ffd2012-03-29 04:41:26 -04006002 if (res->beacon_interval &&
6003 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6004 goto nla_put_failure;
6005 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6006 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006007 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006008 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6009 jiffies_to_msecs(jiffies - intbss->ts)))
6010 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006011
Johannes Berg77965c92009-02-18 18:45:06 +01006012 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006013 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006014 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6015 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006016 break;
6017 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006018 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6019 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006020 break;
6021 default:
6022 break;
6023 }
6024
Johannes Berg48ab9052009-07-10 18:42:31 +02006025 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006026 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006027 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006028 if (intbss == wdev->current_bss &&
6029 nla_put_u32(msg, NL80211_BSS_STATUS,
6030 NL80211_BSS_STATUS_ASSOCIATED))
6031 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006032 break;
6033 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006034 if (intbss == wdev->current_bss &&
6035 nla_put_u32(msg, NL80211_BSS_STATUS,
6036 NL80211_BSS_STATUS_IBSS_JOINED))
6037 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006038 break;
6039 default:
6040 break;
6041 }
6042
Johannes Berg2a519312009-02-10 21:25:55 +01006043 nla_nest_end(msg, bss);
6044
6045 return genlmsg_end(msg, hdr);
6046
Johannes Berg8cef2c92013-02-05 16:54:31 +01006047 fail_unlock_rcu:
6048 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006049 nla_put_failure:
6050 genlmsg_cancel(msg, hdr);
6051 return -EMSGSIZE;
6052}
6053
Johannes Berg97990a02013-04-19 01:02:55 +02006054static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006055{
Johannes Berg48ab9052009-07-10 18:42:31 +02006056 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006057 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006058 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006059 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006060 int err;
6061
Johannes Berg97990a02013-04-19 01:02:55 +02006062 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006063 if (err)
6064 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006065
Johannes Berg48ab9052009-07-10 18:42:31 +02006066 wdev_lock(wdev);
6067 spin_lock_bh(&rdev->bss_lock);
6068 cfg80211_bss_expire(rdev);
6069
Johannes Berg9720bb32011-06-21 09:45:33 +02006070 cb->seq = rdev->bss_generation;
6071
Johannes Berg48ab9052009-07-10 18:42:31 +02006072 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006073 if (++idx <= start)
6074 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006075 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006076 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006077 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006078 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006079 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006080 }
6081 }
6082
Johannes Berg48ab9052009-07-10 18:42:31 +02006083 spin_unlock_bh(&rdev->bss_lock);
6084 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006085
Johannes Berg97990a02013-04-19 01:02:55 +02006086 cb->args[2] = idx;
6087 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006088
Johannes Berg67748892010-10-04 21:14:06 +02006089 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006090}
6091
Eric W. Biederman15e47302012-09-07 20:12:54 +00006092static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006093 int flags, struct net_device *dev,
6094 struct survey_info *survey)
6095{
6096 void *hdr;
6097 struct nlattr *infoattr;
6098
Eric W. Biederman15e47302012-09-07 20:12:54 +00006099 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006100 NL80211_CMD_NEW_SURVEY_RESULTS);
6101 if (!hdr)
6102 return -ENOMEM;
6103
David S. Miller9360ffd2012-03-29 04:41:26 -04006104 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6105 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006106
6107 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6108 if (!infoattr)
6109 goto nla_put_failure;
6110
David S. Miller9360ffd2012-03-29 04:41:26 -04006111 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6112 survey->channel->center_freq))
6113 goto nla_put_failure;
6114
6115 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6116 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6117 goto nla_put_failure;
6118 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6119 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6120 goto nla_put_failure;
6121 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6122 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6123 survey->channel_time))
6124 goto nla_put_failure;
6125 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6126 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6127 survey->channel_time_busy))
6128 goto nla_put_failure;
6129 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6130 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6131 survey->channel_time_ext_busy))
6132 goto nla_put_failure;
6133 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6134 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6135 survey->channel_time_rx))
6136 goto nla_put_failure;
6137 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6138 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6139 survey->channel_time_tx))
6140 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006141
6142 nla_nest_end(msg, infoattr);
6143
6144 return genlmsg_end(msg, hdr);
6145
6146 nla_put_failure:
6147 genlmsg_cancel(msg, hdr);
6148 return -EMSGSIZE;
6149}
6150
6151static int nl80211_dump_survey(struct sk_buff *skb,
6152 struct netlink_callback *cb)
6153{
6154 struct survey_info survey;
6155 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006156 struct wireless_dev *wdev;
6157 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006158 int res;
6159
Johannes Berg97990a02013-04-19 01:02:55 +02006160 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006161 if (res)
6162 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006163
Johannes Berg97990a02013-04-19 01:02:55 +02006164 if (!wdev->netdev) {
6165 res = -EINVAL;
6166 goto out_err;
6167 }
6168
Holger Schurig61fa7132009-11-11 12:25:40 +01006169 if (!dev->ops->dump_survey) {
6170 res = -EOPNOTSUPP;
6171 goto out_err;
6172 }
6173
6174 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006175 struct ieee80211_channel *chan;
6176
Johannes Berg97990a02013-04-19 01:02:55 +02006177 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006178 if (res == -ENOENT)
6179 break;
6180 if (res)
6181 goto out_err;
6182
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006183 /* Survey without a channel doesn't make sense */
6184 if (!survey.channel) {
6185 res = -EINVAL;
6186 goto out;
6187 }
6188
6189 chan = ieee80211_get_channel(&dev->wiphy,
6190 survey.channel->center_freq);
6191 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6192 survey_idx++;
6193 continue;
6194 }
6195
Holger Schurig61fa7132009-11-11 12:25:40 +01006196 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006197 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006198 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006199 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006200 goto out;
6201 survey_idx++;
6202 }
6203
6204 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006205 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006206 res = skb->len;
6207 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006208 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006209 return res;
6210}
6211
Samuel Ortizb23aa672009-07-01 21:26:54 +02006212static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6213{
6214 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6215 NL80211_WPA_VERSION_2));
6216}
6217
Jouni Malinen636a5d32009-03-19 13:39:22 +02006218static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6219{
Johannes Berg4c476992010-10-04 21:36:35 +02006220 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6221 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006222 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006223 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6224 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006225 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006226 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006227 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006228
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006229 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6230 return -EINVAL;
6231
6232 if (!info->attrs[NL80211_ATTR_MAC])
6233 return -EINVAL;
6234
Jouni Malinen17780922009-03-27 20:52:47 +02006235 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6236 return -EINVAL;
6237
Johannes Berg19957bb2009-07-02 17:20:43 +02006238 if (!info->attrs[NL80211_ATTR_SSID])
6239 return -EINVAL;
6240
6241 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6242 return -EINVAL;
6243
Johannes Bergfffd0932009-07-08 14:22:54 +02006244 err = nl80211_parse_key(info, &key);
6245 if (err)
6246 return err;
6247
6248 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006249 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6250 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006251 if (!key.p.key || !key.p.key_len)
6252 return -EINVAL;
6253 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6254 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6255 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6256 key.p.key_len != WLAN_KEY_LEN_WEP104))
6257 return -EINVAL;
6258 if (key.idx > 4)
6259 return -EINVAL;
6260 } else {
6261 key.p.key_len = 0;
6262 key.p.key = NULL;
6263 }
6264
Johannes Bergafea0b72010-08-10 09:46:42 +02006265 if (key.idx >= 0) {
6266 int i;
6267 bool ok = false;
6268 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6269 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6270 ok = true;
6271 break;
6272 }
6273 }
Johannes Berg4c476992010-10-04 21:36:35 +02006274 if (!ok)
6275 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006276 }
6277
Johannes Berg4c476992010-10-04 21:36:35 +02006278 if (!rdev->ops->auth)
6279 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006280
Johannes Berg074ac8d2010-09-16 14:58:22 +02006281 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006282 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6283 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006284
Johannes Berg19957bb2009-07-02 17:20:43 +02006285 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006286 chan = nl80211_get_valid_chan(&rdev->wiphy,
6287 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6288 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006289 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006290
Johannes Berg19957bb2009-07-02 17:20:43 +02006291 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6292 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6293
6294 if (info->attrs[NL80211_ATTR_IE]) {
6295 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6296 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6297 }
6298
6299 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006300 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006301 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006302
Jouni Malinene39e5b52012-09-30 19:29:39 +03006303 if (auth_type == NL80211_AUTHTYPE_SAE &&
6304 !info->attrs[NL80211_ATTR_SAE_DATA])
6305 return -EINVAL;
6306
6307 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6308 if (auth_type != NL80211_AUTHTYPE_SAE)
6309 return -EINVAL;
6310 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6311 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6312 /* need to include at least Auth Transaction and Status Code */
6313 if (sae_data_len < 4)
6314 return -EINVAL;
6315 }
6316
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006317 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6318
Johannes Berg95de8172012-01-20 13:55:25 +01006319 /*
6320 * Since we no longer track auth state, ignore
6321 * requests to only change local state.
6322 */
6323 if (local_state_change)
6324 return 0;
6325
Johannes Berg91bf9b22013-05-15 17:44:01 +02006326 wdev_lock(dev->ieee80211_ptr);
6327 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6328 ssid, ssid_len, ie, ie_len,
6329 key.p.key, key.p.key_len, key.idx,
6330 sae_data, sae_data_len);
6331 wdev_unlock(dev->ieee80211_ptr);
6332 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006333}
6334
Johannes Bergc0692b82010-08-27 14:26:53 +03006335static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6336 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006337 struct cfg80211_crypto_settings *settings,
6338 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006339{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006340 memset(settings, 0, sizeof(*settings));
6341
Samuel Ortizb23aa672009-07-01 21:26:54 +02006342 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6343
Johannes Bergc0692b82010-08-27 14:26:53 +03006344 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6345 u16 proto;
6346 proto = nla_get_u16(
6347 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6348 settings->control_port_ethertype = cpu_to_be16(proto);
6349 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6350 proto != ETH_P_PAE)
6351 return -EINVAL;
6352 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6353 settings->control_port_no_encrypt = true;
6354 } else
6355 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6356
Samuel Ortizb23aa672009-07-01 21:26:54 +02006357 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6358 void *data;
6359 int len, i;
6360
6361 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6362 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6363 settings->n_ciphers_pairwise = len / sizeof(u32);
6364
6365 if (len % sizeof(u32))
6366 return -EINVAL;
6367
Johannes Berg3dc27d22009-07-02 21:36:37 +02006368 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006369 return -EINVAL;
6370
6371 memcpy(settings->ciphers_pairwise, data, len);
6372
6373 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006374 if (!cfg80211_supported_cipher_suite(
6375 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006376 settings->ciphers_pairwise[i]))
6377 return -EINVAL;
6378 }
6379
6380 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6381 settings->cipher_group =
6382 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006383 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6384 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006385 return -EINVAL;
6386 }
6387
6388 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6389 settings->wpa_versions =
6390 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6391 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6392 return -EINVAL;
6393 }
6394
6395 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6396 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006397 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006398
6399 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6400 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6401 settings->n_akm_suites = len / sizeof(u32);
6402
6403 if (len % sizeof(u32))
6404 return -EINVAL;
6405
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006406 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6407 return -EINVAL;
6408
Samuel Ortizb23aa672009-07-01 21:26:54 +02006409 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006410 }
6411
6412 return 0;
6413}
6414
Jouni Malinen636a5d32009-03-19 13:39:22 +02006415static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6416{
Johannes Berg4c476992010-10-04 21:36:35 +02006417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6418 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006419 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006420 struct cfg80211_assoc_request req = {};
6421 const u8 *bssid, *ssid;
6422 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006423
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006424 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6425 return -EINVAL;
6426
6427 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006428 !info->attrs[NL80211_ATTR_SSID] ||
6429 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006430 return -EINVAL;
6431
Johannes Berg4c476992010-10-04 21:36:35 +02006432 if (!rdev->ops->assoc)
6433 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006434
Johannes Berg074ac8d2010-09-16 14:58:22 +02006435 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006436 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6437 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006438
Johannes Berg19957bb2009-07-02 17:20:43 +02006439 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006440
Jouni Malinen664834d2014-01-15 00:01:44 +02006441 chan = nl80211_get_valid_chan(&rdev->wiphy,
6442 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6443 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006444 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006445
Johannes Berg19957bb2009-07-02 17:20:43 +02006446 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6447 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006448
6449 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006450 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6451 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006452 }
6453
Jouni Malinendc6382c2009-05-06 22:09:37 +03006454 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006455 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006456 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006457 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006458 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006459 else if (mfp != NL80211_MFP_NO)
6460 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006461 }
6462
Johannes Berg3e5d7642009-07-07 14:37:26 +02006463 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006464 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006465
Ben Greear7e7c8922011-11-18 11:31:59 -08006466 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006467 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006468
6469 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006470 memcpy(&req.ht_capa_mask,
6471 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6472 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006473
6474 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006475 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006476 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006477 memcpy(&req.ht_capa,
6478 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6479 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006480 }
6481
Johannes Bergee2aca32013-02-21 17:36:01 +01006482 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006483 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006484
6485 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006486 memcpy(&req.vht_capa_mask,
6487 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6488 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006489
6490 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006491 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006492 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006493 memcpy(&req.vht_capa,
6494 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6495 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006496 }
6497
Johannes Bergf62fab72013-02-21 20:09:09 +01006498 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006499 if (!err) {
6500 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006501 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6502 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006503 wdev_unlock(dev->ieee80211_ptr);
6504 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006505
Jouni Malinen636a5d32009-03-19 13:39:22 +02006506 return err;
6507}
6508
6509static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6510{
Johannes Berg4c476992010-10-04 21:36:35 +02006511 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6512 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006513 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006514 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006515 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006516 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006517
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006518 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6519 return -EINVAL;
6520
6521 if (!info->attrs[NL80211_ATTR_MAC])
6522 return -EINVAL;
6523
6524 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6525 return -EINVAL;
6526
Johannes Berg4c476992010-10-04 21:36:35 +02006527 if (!rdev->ops->deauth)
6528 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006529
Johannes Berg074ac8d2010-09-16 14:58:22 +02006530 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006531 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6532 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006533
Johannes Berg19957bb2009-07-02 17:20:43 +02006534 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006535
Johannes Berg19957bb2009-07-02 17:20:43 +02006536 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6537 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006538 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006539 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006540 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006541
6542 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006543 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6544 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006545 }
6546
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006547 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6548
Johannes Berg91bf9b22013-05-15 17:44:01 +02006549 wdev_lock(dev->ieee80211_ptr);
6550 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6551 local_state_change);
6552 wdev_unlock(dev->ieee80211_ptr);
6553 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006554}
6555
6556static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6557{
Johannes Berg4c476992010-10-04 21:36:35 +02006558 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6559 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006560 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006561 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006562 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006563 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006564
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006565 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6566 return -EINVAL;
6567
6568 if (!info->attrs[NL80211_ATTR_MAC])
6569 return -EINVAL;
6570
6571 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6572 return -EINVAL;
6573
Johannes Berg4c476992010-10-04 21:36:35 +02006574 if (!rdev->ops->disassoc)
6575 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006576
Johannes Berg074ac8d2010-09-16 14:58:22 +02006577 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006578 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6579 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006580
Johannes Berg19957bb2009-07-02 17:20:43 +02006581 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006582
Johannes Berg19957bb2009-07-02 17:20:43 +02006583 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6584 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006585 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006586 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006587 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006588
6589 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006590 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6591 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006592 }
6593
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006594 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6595
Johannes Berg91bf9b22013-05-15 17:44:01 +02006596 wdev_lock(dev->ieee80211_ptr);
6597 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6598 local_state_change);
6599 wdev_unlock(dev->ieee80211_ptr);
6600 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006601}
6602
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006603static bool
6604nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6605 int mcast_rate[IEEE80211_NUM_BANDS],
6606 int rateval)
6607{
6608 struct wiphy *wiphy = &rdev->wiphy;
6609 bool found = false;
6610 int band, i;
6611
6612 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6613 struct ieee80211_supported_band *sband;
6614
6615 sband = wiphy->bands[band];
6616 if (!sband)
6617 continue;
6618
6619 for (i = 0; i < sband->n_bitrates; i++) {
6620 if (sband->bitrates[i].bitrate == rateval) {
6621 mcast_rate[band] = i + 1;
6622 found = true;
6623 break;
6624 }
6625 }
6626 }
6627
6628 return found;
6629}
6630
Johannes Berg04a773a2009-04-19 21:24:32 +02006631static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6632{
Johannes Berg4c476992010-10-04 21:36:35 +02006633 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6634 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006635 struct cfg80211_ibss_params ibss;
6636 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006637 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006638 int err;
6639
Johannes Berg8e30bc52009-04-22 17:45:38 +02006640 memset(&ibss, 0, sizeof(ibss));
6641
Johannes Berg04a773a2009-04-19 21:24:32 +02006642 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6643 return -EINVAL;
6644
Johannes Berg683b6d32012-11-08 21:25:48 +01006645 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006646 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6647 return -EINVAL;
6648
Johannes Berg8e30bc52009-04-22 17:45:38 +02006649 ibss.beacon_interval = 100;
6650
6651 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6652 ibss.beacon_interval =
6653 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6654 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6655 return -EINVAL;
6656 }
6657
Johannes Berg4c476992010-10-04 21:36:35 +02006658 if (!rdev->ops->join_ibss)
6659 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006660
Johannes Berg4c476992010-10-04 21:36:35 +02006661 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6662 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006663
Johannes Berg79c97e92009-07-07 03:56:12 +02006664 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006665
Johannes Berg39193492011-09-16 13:45:25 +02006666 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006667 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006668
6669 if (!is_valid_ether_addr(ibss.bssid))
6670 return -EINVAL;
6671 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006672 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6673 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6674
6675 if (info->attrs[NL80211_ATTR_IE]) {
6676 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6677 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6678 }
6679
Johannes Berg683b6d32012-11-08 21:25:48 +01006680 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6681 if (err)
6682 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006683
Johannes Berg683b6d32012-11-08 21:25:48 +01006684 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006685 return -EINVAL;
6686
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006687 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006688 case NL80211_CHAN_WIDTH_5:
6689 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006690 case NL80211_CHAN_WIDTH_20_NOHT:
6691 break;
6692 case NL80211_CHAN_WIDTH_20:
6693 case NL80211_CHAN_WIDTH_40:
6694 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6695 break;
6696 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006697 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006698 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006699
Johannes Berg04a773a2009-04-19 21:24:32 +02006700 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006701 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006702
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006703 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6704 u8 *rates =
6705 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6706 int n_rates =
6707 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6708 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006709 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006710
Johannes Berg34850ab2011-07-18 18:08:35 +02006711 err = ieee80211_get_ratemask(sband, rates, n_rates,
6712 &ibss.basic_rates);
6713 if (err)
6714 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006715 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006716
Simon Wunderlich803768f2013-06-28 10:39:58 +02006717 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6718 memcpy(&ibss.ht_capa_mask,
6719 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6720 sizeof(ibss.ht_capa_mask));
6721
6722 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6723 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6724 return -EINVAL;
6725 memcpy(&ibss.ht_capa,
6726 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6727 sizeof(ibss.ht_capa));
6728 }
6729
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006730 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6731 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6732 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6733 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006734
Johannes Berg4c476992010-10-04 21:36:35 +02006735 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306736 bool no_ht = false;
6737
Johannes Berg4c476992010-10-04 21:36:35 +02006738 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306739 info->attrs[NL80211_ATTR_KEYS],
6740 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006741 if (IS_ERR(connkeys))
6742 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306743
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006744 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6745 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306746 kfree(connkeys);
6747 return -EINVAL;
6748 }
Johannes Berg4c476992010-10-04 21:36:35 +02006749 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006750
Antonio Quartulli267335d2012-01-31 20:25:47 +01006751 ibss.control_port =
6752 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6753
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006754 ibss.userspace_handles_dfs =
6755 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6756
Johannes Berg4c476992010-10-04 21:36:35 +02006757 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006758 if (err)
6759 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006760 return err;
6761}
6762
6763static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6764{
Johannes Berg4c476992010-10-04 21:36:35 +02006765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6766 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006767
Johannes Berg4c476992010-10-04 21:36:35 +02006768 if (!rdev->ops->leave_ibss)
6769 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006770
Johannes Berg4c476992010-10-04 21:36:35 +02006771 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6772 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006773
Johannes Berg4c476992010-10-04 21:36:35 +02006774 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006775}
6776
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006777static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6778{
6779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6780 struct net_device *dev = info->user_ptr[1];
6781 int mcast_rate[IEEE80211_NUM_BANDS];
6782 u32 nla_rate;
6783 int err;
6784
6785 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6786 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6787 return -EOPNOTSUPP;
6788
6789 if (!rdev->ops->set_mcast_rate)
6790 return -EOPNOTSUPP;
6791
6792 memset(mcast_rate, 0, sizeof(mcast_rate));
6793
6794 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6795 return -EINVAL;
6796
6797 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6798 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6799 return -EINVAL;
6800
6801 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6802
6803 return err;
6804}
6805
Johannes Bergad7e7182013-11-13 13:37:47 +01006806static struct sk_buff *
6807__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6808 int approxlen, u32 portid, u32 seq,
6809 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006810 enum nl80211_attrs attr,
6811 const struct nl80211_vendor_cmd_info *info,
6812 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006813{
6814 struct sk_buff *skb;
6815 void *hdr;
6816 struct nlattr *data;
6817
6818 skb = nlmsg_new(approxlen + 100, gfp);
6819 if (!skb)
6820 return NULL;
6821
6822 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6823 if (!hdr) {
6824 kfree_skb(skb);
6825 return NULL;
6826 }
6827
6828 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6829 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006830
6831 if (info) {
6832 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6833 info->vendor_id))
6834 goto nla_put_failure;
6835 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6836 info->subcmd))
6837 goto nla_put_failure;
6838 }
6839
Johannes Bergad7e7182013-11-13 13:37:47 +01006840 data = nla_nest_start(skb, attr);
6841
6842 ((void **)skb->cb)[0] = rdev;
6843 ((void **)skb->cb)[1] = hdr;
6844 ((void **)skb->cb)[2] = data;
6845
6846 return skb;
6847
6848 nla_put_failure:
6849 kfree_skb(skb);
6850 return NULL;
6851}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006852
Johannes Berge03ad6e2014-01-01 17:22:30 +01006853struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6854 enum nl80211_commands cmd,
6855 enum nl80211_attrs attr,
6856 int vendor_event_idx,
6857 int approxlen, gfp_t gfp)
6858{
6859 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6860 const struct nl80211_vendor_cmd_info *info;
6861
6862 switch (cmd) {
6863 case NL80211_CMD_TESTMODE:
6864 if (WARN_ON(vendor_event_idx != -1))
6865 return NULL;
6866 info = NULL;
6867 break;
6868 case NL80211_CMD_VENDOR:
6869 if (WARN_ON(vendor_event_idx < 0 ||
6870 vendor_event_idx >= wiphy->n_vendor_events))
6871 return NULL;
6872 info = &wiphy->vendor_events[vendor_event_idx];
6873 break;
6874 default:
6875 WARN_ON(1);
6876 return NULL;
6877 }
6878
6879 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6880 cmd, attr, info, gfp);
6881}
6882EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6883
6884void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6885{
6886 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6887 void *hdr = ((void **)skb->cb)[1];
6888 struct nlattr *data = ((void **)skb->cb)[2];
6889 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6890
6891 nla_nest_end(skb, data);
6892 genlmsg_end(skb, hdr);
6893
6894 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6895 mcgrp = NL80211_MCGRP_VENDOR;
6896
6897 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6898 mcgrp, gfp);
6899}
6900EXPORT_SYMBOL(__cfg80211_send_event_skb);
6901
Johannes Bergaff89a92009-07-01 21:26:51 +02006902#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006903static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6904{
Johannes Berg4c476992010-10-04 21:36:35 +02006905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006906 struct wireless_dev *wdev =
6907 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006908 int err;
6909
David Spinadelfc73f112013-07-31 18:04:15 +03006910 if (!rdev->ops->testmode_cmd)
6911 return -EOPNOTSUPP;
6912
6913 if (IS_ERR(wdev)) {
6914 err = PTR_ERR(wdev);
6915 if (err != -EINVAL)
6916 return err;
6917 wdev = NULL;
6918 } else if (wdev->wiphy != &rdev->wiphy) {
6919 return -EINVAL;
6920 }
6921
Johannes Bergaff89a92009-07-01 21:26:51 +02006922 if (!info->attrs[NL80211_ATTR_TESTDATA])
6923 return -EINVAL;
6924
Johannes Bergad7e7182013-11-13 13:37:47 +01006925 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006926 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006927 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6928 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006929 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006930
Johannes Bergaff89a92009-07-01 21:26:51 +02006931 return err;
6932}
6933
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006934static int nl80211_testmode_dump(struct sk_buff *skb,
6935 struct netlink_callback *cb)
6936{
Johannes Berg00918d32011-12-13 17:22:05 +01006937 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006938 int err;
6939 long phy_idx;
6940 void *data = NULL;
6941 int data_len = 0;
6942
Johannes Berg5fe231e2013-05-08 21:45:15 +02006943 rtnl_lock();
6944
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006945 if (cb->args[0]) {
6946 /*
6947 * 0 is a valid index, but not valid for args[0],
6948 * so we need to offset by 1.
6949 */
6950 phy_idx = cb->args[0] - 1;
6951 } else {
6952 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6953 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6954 nl80211_policy);
6955 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006956 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006957
Johannes Berg2bd7e352012-06-15 14:23:16 +02006958 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6959 nl80211_fam.attrbuf);
6960 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006961 err = PTR_ERR(rdev);
6962 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006963 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006964 phy_idx = rdev->wiphy_idx;
6965 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006966
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006967 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6968 cb->args[1] =
6969 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6970 }
6971
6972 if (cb->args[1]) {
6973 data = nla_data((void *)cb->args[1]);
6974 data_len = nla_len((void *)cb->args[1]);
6975 }
6976
Johannes Berg00918d32011-12-13 17:22:05 +01006977 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6978 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006979 err = -ENOENT;
6980 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006981 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006982
Johannes Berg00918d32011-12-13 17:22:05 +01006983 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006984 err = -EOPNOTSUPP;
6985 goto out_err;
6986 }
6987
6988 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006989 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006990 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6991 NL80211_CMD_TESTMODE);
6992 struct nlattr *tmdata;
6993
Dan Carpentercb35fba2013-08-14 14:50:01 +03006994 if (!hdr)
6995 break;
6996
David S. Miller9360ffd2012-03-29 04:41:26 -04006997 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006998 genlmsg_cancel(skb, hdr);
6999 break;
7000 }
7001
7002 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7003 if (!tmdata) {
7004 genlmsg_cancel(skb, hdr);
7005 break;
7006 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007007 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007008 nla_nest_end(skb, tmdata);
7009
7010 if (err == -ENOBUFS || err == -ENOENT) {
7011 genlmsg_cancel(skb, hdr);
7012 break;
7013 } else if (err) {
7014 genlmsg_cancel(skb, hdr);
7015 goto out_err;
7016 }
7017
7018 genlmsg_end(skb, hdr);
7019 }
7020
7021 err = skb->len;
7022 /* see above */
7023 cb->args[0] = phy_idx + 1;
7024 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007025 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007026 return err;
7027}
Johannes Bergaff89a92009-07-01 21:26:51 +02007028#endif
7029
Samuel Ortizb23aa672009-07-01 21:26:54 +02007030static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7031{
Johannes Berg4c476992010-10-04 21:36:35 +02007032 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7033 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007034 struct cfg80211_connect_params connect;
7035 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007036 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007037 int err;
7038
7039 memset(&connect, 0, sizeof(connect));
7040
7041 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7042 return -EINVAL;
7043
7044 if (!info->attrs[NL80211_ATTR_SSID] ||
7045 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7046 return -EINVAL;
7047
7048 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7049 connect.auth_type =
7050 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007051 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7052 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007053 return -EINVAL;
7054 } else
7055 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7056
7057 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7058
Johannes Bergc0692b82010-08-27 14:26:53 +03007059 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007060 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007061 if (err)
7062 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007063
Johannes Berg074ac8d2010-09-16 14:58:22 +02007064 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007065 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7066 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007067
Johannes Berg79c97e92009-07-07 03:56:12 +02007068 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007069
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307070 connect.bg_scan_period = -1;
7071 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7072 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7073 connect.bg_scan_period =
7074 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7075 }
7076
Samuel Ortizb23aa672009-07-01 21:26:54 +02007077 if (info->attrs[NL80211_ATTR_MAC])
7078 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007079 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7080 connect.bssid_hint =
7081 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007082 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7083 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7084
7085 if (info->attrs[NL80211_ATTR_IE]) {
7086 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7087 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7088 }
7089
Jouni Malinencee00a92013-01-15 17:15:57 +02007090 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7091 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7092 if (connect.mfp != NL80211_MFP_REQUIRED &&
7093 connect.mfp != NL80211_MFP_NO)
7094 return -EINVAL;
7095 } else {
7096 connect.mfp = NL80211_MFP_NO;
7097 }
7098
Samuel Ortizb23aa672009-07-01 21:26:54 +02007099 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007100 connect.channel = nl80211_get_valid_chan(
7101 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7102 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007103 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007104 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007105 connect.channel_hint = nl80211_get_valid_chan(
7106 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7107 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007108 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007109 }
7110
Johannes Bergfffd0932009-07-08 14:22:54 +02007111 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7112 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307113 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007114 if (IS_ERR(connkeys))
7115 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007116 }
7117
Ben Greear7e7c8922011-11-18 11:31:59 -08007118 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7119 connect.flags |= ASSOC_REQ_DISABLE_HT;
7120
7121 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7122 memcpy(&connect.ht_capa_mask,
7123 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7124 sizeof(connect.ht_capa_mask));
7125
7126 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007127 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7128 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007129 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007130 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007131 memcpy(&connect.ht_capa,
7132 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7133 sizeof(connect.ht_capa));
7134 }
7135
Johannes Bergee2aca32013-02-21 17:36:01 +01007136 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7137 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7138
7139 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7140 memcpy(&connect.vht_capa_mask,
7141 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7142 sizeof(connect.vht_capa_mask));
7143
7144 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7145 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7146 kfree(connkeys);
7147 return -EINVAL;
7148 }
7149 memcpy(&connect.vht_capa,
7150 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7151 sizeof(connect.vht_capa));
7152 }
7153
Johannes Berg83739b02013-05-15 17:44:01 +02007154 wdev_lock(dev->ieee80211_ptr);
7155 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7156 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007157 if (err)
7158 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007159 return err;
7160}
7161
7162static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7163{
Johannes Berg4c476992010-10-04 21:36:35 +02007164 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7165 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007166 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007167 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007168
7169 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7170 reason = WLAN_REASON_DEAUTH_LEAVING;
7171 else
7172 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7173
7174 if (reason == 0)
7175 return -EINVAL;
7176
Johannes Berg074ac8d2010-09-16 14:58:22 +02007177 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007178 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7179 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007180
Johannes Berg83739b02013-05-15 17:44:01 +02007181 wdev_lock(dev->ieee80211_ptr);
7182 ret = cfg80211_disconnect(rdev, dev, reason, true);
7183 wdev_unlock(dev->ieee80211_ptr);
7184 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007185}
7186
Johannes Berg463d0182009-07-14 00:33:35 +02007187static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7188{
Johannes Berg4c476992010-10-04 21:36:35 +02007189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007190 struct net *net;
7191 int err;
7192 u32 pid;
7193
7194 if (!info->attrs[NL80211_ATTR_PID])
7195 return -EINVAL;
7196
7197 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7198
Johannes Berg463d0182009-07-14 00:33:35 +02007199 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007200 if (IS_ERR(net))
7201 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007202
7203 err = 0;
7204
7205 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007206 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7207 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007208
Johannes Berg463d0182009-07-14 00:33:35 +02007209 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007210 return err;
7211}
7212
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007213static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7214{
Johannes Berg4c476992010-10-04 21:36:35 +02007215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007216 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7217 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007218 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007219 struct cfg80211_pmksa pmksa;
7220
7221 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7222
7223 if (!info->attrs[NL80211_ATTR_MAC])
7224 return -EINVAL;
7225
7226 if (!info->attrs[NL80211_ATTR_PMKID])
7227 return -EINVAL;
7228
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007229 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7230 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7231
Johannes Berg074ac8d2010-09-16 14:58:22 +02007232 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007233 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7234 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007235
7236 switch (info->genlhdr->cmd) {
7237 case NL80211_CMD_SET_PMKSA:
7238 rdev_ops = rdev->ops->set_pmksa;
7239 break;
7240 case NL80211_CMD_DEL_PMKSA:
7241 rdev_ops = rdev->ops->del_pmksa;
7242 break;
7243 default:
7244 WARN_ON(1);
7245 break;
7246 }
7247
Johannes Berg4c476992010-10-04 21:36:35 +02007248 if (!rdev_ops)
7249 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007250
Johannes Berg4c476992010-10-04 21:36:35 +02007251 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007252}
7253
7254static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7255{
Johannes Berg4c476992010-10-04 21:36:35 +02007256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7257 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007258
Johannes Berg074ac8d2010-09-16 14:58:22 +02007259 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007260 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7261 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007262
Johannes Berg4c476992010-10-04 21:36:35 +02007263 if (!rdev->ops->flush_pmksa)
7264 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007265
Hila Gonene35e4d22012-06-27 17:19:42 +03007266 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007267}
7268
Arik Nemtsov109086c2011-09-28 14:12:50 +03007269static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7270{
7271 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7272 struct net_device *dev = info->user_ptr[1];
7273 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307274 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007275 u16 status_code;
7276 u8 *peer;
7277
7278 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7279 !rdev->ops->tdls_mgmt)
7280 return -EOPNOTSUPP;
7281
7282 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7283 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7284 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7285 !info->attrs[NL80211_ATTR_IE] ||
7286 !info->attrs[NL80211_ATTR_MAC])
7287 return -EINVAL;
7288
7289 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7290 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7291 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7292 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307293 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7294 peer_capability =
7295 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007296
Hila Gonene35e4d22012-06-27 17:19:42 +03007297 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307298 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007299 nla_data(info->attrs[NL80211_ATTR_IE]),
7300 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007301}
7302
7303static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7304{
7305 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7306 struct net_device *dev = info->user_ptr[1];
7307 enum nl80211_tdls_operation operation;
7308 u8 *peer;
7309
7310 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7311 !rdev->ops->tdls_oper)
7312 return -EOPNOTSUPP;
7313
7314 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7315 !info->attrs[NL80211_ATTR_MAC])
7316 return -EINVAL;
7317
7318 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7319 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7320
Hila Gonene35e4d22012-06-27 17:19:42 +03007321 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007322}
7323
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007324static int nl80211_remain_on_channel(struct sk_buff *skb,
7325 struct genl_info *info)
7326{
Johannes Berg4c476992010-10-04 21:36:35 +02007327 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007328 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007329 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007330 struct sk_buff *msg;
7331 void *hdr;
7332 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007333 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007334 int err;
7335
7336 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7337 !info->attrs[NL80211_ATTR_DURATION])
7338 return -EINVAL;
7339
7340 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7341
Johannes Berg7c4ef712011-11-18 15:33:48 +01007342 if (!rdev->ops->remain_on_channel ||
7343 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007344 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007345
Johannes Bergebf348f2012-06-01 12:50:54 +02007346 /*
7347 * We should be on that channel for at least a minimum amount of
7348 * time (10ms) but no longer than the driver supports.
7349 */
7350 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7351 duration > rdev->wiphy.max_remain_on_channel_duration)
7352 return -EINVAL;
7353
Johannes Berg683b6d32012-11-08 21:25:48 +01007354 err = nl80211_parse_chandef(rdev, info, &chandef);
7355 if (err)
7356 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007357
7358 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007359 if (!msg)
7360 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007361
Eric W. Biederman15e47302012-09-07 20:12:54 +00007362 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007363 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007364 if (!hdr) {
7365 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007366 goto free_msg;
7367 }
7368
Johannes Berg683b6d32012-11-08 21:25:48 +01007369 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7370 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007371
7372 if (err)
7373 goto free_msg;
7374
David S. Miller9360ffd2012-03-29 04:41:26 -04007375 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7376 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007377
7378 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007379
7380 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007381
7382 nla_put_failure:
7383 err = -ENOBUFS;
7384 free_msg:
7385 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007386 return err;
7387}
7388
7389static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7390 struct genl_info *info)
7391{
Johannes Berg4c476992010-10-04 21:36:35 +02007392 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007393 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007394 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007395
7396 if (!info->attrs[NL80211_ATTR_COOKIE])
7397 return -EINVAL;
7398
Johannes Berg4c476992010-10-04 21:36:35 +02007399 if (!rdev->ops->cancel_remain_on_channel)
7400 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007401
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007402 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7403
Hila Gonene35e4d22012-06-27 17:19:42 +03007404 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007405}
7406
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007407static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7408 u8 *rates, u8 rates_len)
7409{
7410 u8 i;
7411 u32 mask = 0;
7412
7413 for (i = 0; i < rates_len; i++) {
7414 int rate = (rates[i] & 0x7f) * 5;
7415 int ridx;
7416 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7417 struct ieee80211_rate *srate =
7418 &sband->bitrates[ridx];
7419 if (rate == srate->bitrate) {
7420 mask |= 1 << ridx;
7421 break;
7422 }
7423 }
7424 if (ridx == sband->n_bitrates)
7425 return 0; /* rate not found */
7426 }
7427
7428 return mask;
7429}
7430
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007431static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7432 u8 *rates, u8 rates_len,
7433 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7434{
7435 u8 i;
7436
7437 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7438
7439 for (i = 0; i < rates_len; i++) {
7440 int ridx, rbit;
7441
7442 ridx = rates[i] / 8;
7443 rbit = BIT(rates[i] % 8);
7444
7445 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007446 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007447 return false;
7448
7449 /* check availability */
7450 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7451 mcs[ridx] |= rbit;
7452 else
7453 return false;
7454 }
7455
7456 return true;
7457}
7458
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007459static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7460{
7461 u16 mcs_mask = 0;
7462
7463 switch (vht_mcs_map) {
7464 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7465 break;
7466 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7467 mcs_mask = 0x00FF;
7468 break;
7469 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7470 mcs_mask = 0x01FF;
7471 break;
7472 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7473 mcs_mask = 0x03FF;
7474 break;
7475 default:
7476 break;
7477 }
7478
7479 return mcs_mask;
7480}
7481
7482static void vht_build_mcs_mask(u16 vht_mcs_map,
7483 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7484{
7485 u8 nss;
7486
7487 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7488 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7489 vht_mcs_map >>= 2;
7490 }
7491}
7492
7493static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7494 struct nl80211_txrate_vht *txrate,
7495 u16 mcs[NL80211_VHT_NSS_MAX])
7496{
7497 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7498 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7499 u8 i;
7500
7501 if (!sband->vht_cap.vht_supported)
7502 return false;
7503
7504 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7505
7506 /* Build vht_mcs_mask from VHT capabilities */
7507 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7508
7509 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7510 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7511 mcs[i] = txrate->mcs[i];
7512 else
7513 return false;
7514 }
7515
7516 return true;
7517}
7518
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007519static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007520 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7521 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007522 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7523 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007524 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007525 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007526};
7527
7528static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7529 struct genl_info *info)
7530{
7531 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007533 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007534 int rem, i;
7535 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007536 struct nlattr *tx_rates;
7537 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007538 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007539
Johannes Berg4c476992010-10-04 21:36:35 +02007540 if (!rdev->ops->set_bitrate_mask)
7541 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007542
7543 memset(&mask, 0, sizeof(mask));
7544 /* Default to all rates enabled */
7545 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7546 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007547
7548 if (!sband)
7549 continue;
7550
7551 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007552 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007553 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007554 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007555
7556 if (!sband->vht_cap.vht_supported)
7557 continue;
7558
7559 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7560 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007561 }
7562
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007563 /* if no rates are given set it back to the defaults */
7564 if (!info->attrs[NL80211_ATTR_TX_RATES])
7565 goto out;
7566
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007567 /*
7568 * The nested attribute uses enum nl80211_band as the index. This maps
7569 * directly to the enum ieee80211_band values used in cfg80211.
7570 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007571 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007572 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007573 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007574 int err;
7575
Johannes Berg4c476992010-10-04 21:36:35 +02007576 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7577 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007578 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007579 if (sband == NULL)
7580 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007581 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7582 nla_len(tx_rates), nl80211_txattr_policy);
7583 if (err)
7584 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007585 if (tb[NL80211_TXRATE_LEGACY]) {
7586 mask.control[band].legacy = rateset_to_mask(
7587 sband,
7588 nla_data(tb[NL80211_TXRATE_LEGACY]),
7589 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307590 if ((mask.control[band].legacy == 0) &&
7591 nla_len(tb[NL80211_TXRATE_LEGACY]))
7592 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007593 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007594 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007595 if (!ht_rateset_to_mask(
7596 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007597 nla_data(tb[NL80211_TXRATE_HT]),
7598 nla_len(tb[NL80211_TXRATE_HT]),
7599 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007600 return -EINVAL;
7601 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007602 if (tb[NL80211_TXRATE_VHT]) {
7603 if (!vht_set_mcs_mask(
7604 sband,
7605 nla_data(tb[NL80211_TXRATE_VHT]),
7606 mask.control[band].vht_mcs))
7607 return -EINVAL;
7608 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007609 if (tb[NL80211_TXRATE_GI]) {
7610 mask.control[band].gi =
7611 nla_get_u8(tb[NL80211_TXRATE_GI]);
7612 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7613 return -EINVAL;
7614 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007615
7616 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007617 /* don't allow empty legacy rates if HT or VHT
7618 * are not even supported.
7619 */
7620 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7621 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007622 return -EINVAL;
7623
7624 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007625 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007626 goto out;
7627
7628 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7629 if (mask.control[band].vht_mcs[i])
7630 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007631
7632 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007633 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007634 }
7635 }
7636
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007637out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007638 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007639}
7640
Johannes Berg2e161f72010-08-12 15:38:38 +02007641static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007642{
Johannes Berg4c476992010-10-04 21:36:35 +02007643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007644 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007645 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007646
7647 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7648 return -EINVAL;
7649
Johannes Berg2e161f72010-08-12 15:38:38 +02007650 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7651 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007652
Johannes Berg71bbc992012-06-15 15:30:18 +02007653 switch (wdev->iftype) {
7654 case NL80211_IFTYPE_STATION:
7655 case NL80211_IFTYPE_ADHOC:
7656 case NL80211_IFTYPE_P2P_CLIENT:
7657 case NL80211_IFTYPE_AP:
7658 case NL80211_IFTYPE_AP_VLAN:
7659 case NL80211_IFTYPE_MESH_POINT:
7660 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007661 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007662 break;
7663 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007664 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007665 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007666
7667 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007668 if (!rdev->ops->mgmt_tx)
7669 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007670
Eric W. Biederman15e47302012-09-07 20:12:54 +00007671 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007672 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7673 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007674}
7675
Johannes Berg2e161f72010-08-12 15:38:38 +02007676static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007677{
Johannes Berg4c476992010-10-04 21:36:35 +02007678 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007679 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007680 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007681 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007682 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007683 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007684 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007685 struct cfg80211_mgmt_tx_params params = {
7686 .dont_wait_for_ack =
7687 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7688 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007689
Johannes Berg683b6d32012-11-08 21:25:48 +01007690 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007691 return -EINVAL;
7692
Johannes Berg4c476992010-10-04 21:36:35 +02007693 if (!rdev->ops->mgmt_tx)
7694 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007695
Johannes Berg71bbc992012-06-15 15:30:18 +02007696 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007697 case NL80211_IFTYPE_P2P_DEVICE:
7698 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7699 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007700 case NL80211_IFTYPE_STATION:
7701 case NL80211_IFTYPE_ADHOC:
7702 case NL80211_IFTYPE_P2P_CLIENT:
7703 case NL80211_IFTYPE_AP:
7704 case NL80211_IFTYPE_AP_VLAN:
7705 case NL80211_IFTYPE_MESH_POINT:
7706 case NL80211_IFTYPE_P2P_GO:
7707 break;
7708 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007709 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007710 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007711
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007712 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007713 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007714 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007715 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007716
7717 /*
7718 * We should wait on the channel for at least a minimum amount
7719 * of time (10ms) but no longer than the driver supports.
7720 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007721 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7722 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007723 return -EINVAL;
7724
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007725 }
7726
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007727 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007728
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007729 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007730 return -EINVAL;
7731
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007732 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307733
Antonio Quartulliea141b752013-06-11 14:20:03 +02007734 /* get the channel if any has been specified, otherwise pass NULL to
7735 * the driver. The latter will use the current one
7736 */
7737 chandef.chan = NULL;
7738 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7739 err = nl80211_parse_chandef(rdev, info, &chandef);
7740 if (err)
7741 return err;
7742 }
7743
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007744 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007745 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007746
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007747 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007748 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7749 if (!msg)
7750 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007751
Eric W. Biederman15e47302012-09-07 20:12:54 +00007752 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007753 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007754 if (!hdr) {
7755 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007756 goto free_msg;
7757 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007758 }
Johannes Berge247bd902011-11-04 11:18:21 +01007759
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007760 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7761 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7762 params.chan = chandef.chan;
7763 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007764 if (err)
7765 goto free_msg;
7766
Johannes Berge247bd902011-11-04 11:18:21 +01007767 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007768 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7769 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007770
Johannes Berge247bd902011-11-04 11:18:21 +01007771 genlmsg_end(msg, hdr);
7772 return genlmsg_reply(msg, info);
7773 }
7774
7775 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007776
7777 nla_put_failure:
7778 err = -ENOBUFS;
7779 free_msg:
7780 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007781 return err;
7782}
7783
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007784static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7785{
7786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007787 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007788 u64 cookie;
7789
7790 if (!info->attrs[NL80211_ATTR_COOKIE])
7791 return -EINVAL;
7792
7793 if (!rdev->ops->mgmt_tx_cancel_wait)
7794 return -EOPNOTSUPP;
7795
Johannes Berg71bbc992012-06-15 15:30:18 +02007796 switch (wdev->iftype) {
7797 case NL80211_IFTYPE_STATION:
7798 case NL80211_IFTYPE_ADHOC:
7799 case NL80211_IFTYPE_P2P_CLIENT:
7800 case NL80211_IFTYPE_AP:
7801 case NL80211_IFTYPE_AP_VLAN:
7802 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007803 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007804 break;
7805 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007806 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007807 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007808
7809 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7810
Hila Gonene35e4d22012-06-27 17:19:42 +03007811 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007812}
7813
Kalle Valoffb9eb32010-02-17 17:58:10 +02007814static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7815{
Johannes Berg4c476992010-10-04 21:36:35 +02007816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007817 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007818 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007819 u8 ps_state;
7820 bool state;
7821 int err;
7822
Johannes Berg4c476992010-10-04 21:36:35 +02007823 if (!info->attrs[NL80211_ATTR_PS_STATE])
7824 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007825
7826 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7827
Johannes Berg4c476992010-10-04 21:36:35 +02007828 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7829 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007830
7831 wdev = dev->ieee80211_ptr;
7832
Johannes Berg4c476992010-10-04 21:36:35 +02007833 if (!rdev->ops->set_power_mgmt)
7834 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007835
7836 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7837
7838 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007839 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007840
Hila Gonene35e4d22012-06-27 17:19:42 +03007841 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007842 if (!err)
7843 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007844 return err;
7845}
7846
7847static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7848{
Johannes Berg4c476992010-10-04 21:36:35 +02007849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007850 enum nl80211_ps_state ps_state;
7851 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007852 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007853 struct sk_buff *msg;
7854 void *hdr;
7855 int err;
7856
Kalle Valoffb9eb32010-02-17 17:58:10 +02007857 wdev = dev->ieee80211_ptr;
7858
Johannes Berg4c476992010-10-04 21:36:35 +02007859 if (!rdev->ops->set_power_mgmt)
7860 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007861
7862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007863 if (!msg)
7864 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007865
Eric W. Biederman15e47302012-09-07 20:12:54 +00007866 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007867 NL80211_CMD_GET_POWER_SAVE);
7868 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007869 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007870 goto free_msg;
7871 }
7872
7873 if (wdev->ps)
7874 ps_state = NL80211_PS_ENABLED;
7875 else
7876 ps_state = NL80211_PS_DISABLED;
7877
David S. Miller9360ffd2012-03-29 04:41:26 -04007878 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7879 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007880
7881 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007882 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007883
Johannes Berg4c476992010-10-04 21:36:35 +02007884 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007885 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007886 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007887 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007888 return err;
7889}
7890
Johannes Berg94e860f2014-01-20 23:58:15 +01007891static const struct nla_policy
7892nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007893 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7894 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7895 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007896 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7897 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7898 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007899};
7900
Thomas Pedersen84f10702012-07-12 16:17:33 -07007901static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007902 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007903{
7904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007905 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007906 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007907
Johannes Bergd9d8b012012-11-26 12:51:52 +01007908 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007909 return -EINVAL;
7910
Thomas Pedersen84f10702012-07-12 16:17:33 -07007911 if (!rdev->ops->set_cqm_txe_config)
7912 return -EOPNOTSUPP;
7913
7914 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7915 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7916 return -EOPNOTSUPP;
7917
Hila Gonene35e4d22012-06-27 17:19:42 +03007918 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007919}
7920
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007921static int nl80211_set_cqm_rssi(struct genl_info *info,
7922 s32 threshold, u32 hysteresis)
7923{
Johannes Berg4c476992010-10-04 21:36:35 +02007924 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007925 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007926 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007927
7928 if (threshold > 0)
7929 return -EINVAL;
7930
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007931 /* disabling - hysteresis should also be zero then */
7932 if (threshold == 0)
7933 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007934
Johannes Berg4c476992010-10-04 21:36:35 +02007935 if (!rdev->ops->set_cqm_rssi_config)
7936 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007937
Johannes Berg074ac8d2010-09-16 14:58:22 +02007938 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007939 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7940 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007941
Hila Gonene35e4d22012-06-27 17:19:42 +03007942 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007943}
7944
7945static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7946{
7947 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7948 struct nlattr *cqm;
7949 int err;
7950
7951 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007952 if (!cqm)
7953 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007954
7955 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7956 nl80211_attr_cqm_policy);
7957 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007958 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007959
7960 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7961 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007962 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7963 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007964
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007965 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7966 }
7967
7968 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7969 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7970 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7971 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7972 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7973 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7974
7975 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7976 }
7977
7978 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007979}
7980
Johannes Berg29cbe682010-12-03 09:20:44 +01007981static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7982{
7983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7984 struct net_device *dev = info->user_ptr[1];
7985 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007986 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007987 int err;
7988
7989 /* start with default */
7990 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007991 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007992
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007993 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007994 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007995 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007996 if (err)
7997 return err;
7998 }
7999
8000 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8001 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8002 return -EINVAL;
8003
Javier Cardonac80d5452010-12-16 17:37:49 -08008004 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8005 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8006
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008007 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8008 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8009 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8010 return -EINVAL;
8011
Marco Porsch9bdbf042013-01-07 16:04:51 +01008012 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8013 setup.beacon_interval =
8014 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8015 if (setup.beacon_interval < 10 ||
8016 setup.beacon_interval > 10000)
8017 return -EINVAL;
8018 }
8019
8020 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8021 setup.dtim_period =
8022 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8023 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8024 return -EINVAL;
8025 }
8026
Javier Cardonac80d5452010-12-16 17:37:49 -08008027 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8028 /* parse additional setup parameters if given */
8029 err = nl80211_parse_mesh_setup(info, &setup);
8030 if (err)
8031 return err;
8032 }
8033
Thomas Pedersend37bb182013-03-04 13:06:13 -08008034 if (setup.user_mpm)
8035 cfg.auto_open_plinks = false;
8036
Johannes Bergcc1d2802012-05-16 23:50:20 +02008037 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008038 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8039 if (err)
8040 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008041 } else {
8042 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008043 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008044 }
8045
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008046 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8047 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8048 int n_rates =
8049 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8050 struct ieee80211_supported_band *sband;
8051
8052 if (!setup.chandef.chan)
8053 return -EINVAL;
8054
8055 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8056
8057 err = ieee80211_get_ratemask(sband, rates, n_rates,
8058 &setup.basic_rates);
8059 if (err)
8060 return err;
8061 }
8062
Javier Cardonac80d5452010-12-16 17:37:49 -08008063 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008064}
8065
8066static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8067{
8068 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8069 struct net_device *dev = info->user_ptr[1];
8070
8071 return cfg80211_leave_mesh(rdev, dev);
8072}
8073
Johannes Bergdfb89c52012-06-27 09:23:48 +02008074#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008075static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8076 struct cfg80211_registered_device *rdev)
8077{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008078 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008079 struct nlattr *nl_pats, *nl_pat;
8080 int i, pat_len;
8081
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008082 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008083 return 0;
8084
8085 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8086 if (!nl_pats)
8087 return -ENOBUFS;
8088
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008089 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008090 nl_pat = nla_nest_start(msg, i + 1);
8091 if (!nl_pat)
8092 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008093 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008094 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008095 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008096 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8097 wowlan->patterns[i].pattern) ||
8098 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008099 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008100 return -ENOBUFS;
8101 nla_nest_end(msg, nl_pat);
8102 }
8103 nla_nest_end(msg, nl_pats);
8104
8105 return 0;
8106}
8107
Johannes Berg2a0e0472013-01-23 22:57:40 +01008108static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8109 struct cfg80211_wowlan_tcp *tcp)
8110{
8111 struct nlattr *nl_tcp;
8112
8113 if (!tcp)
8114 return 0;
8115
8116 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8117 if (!nl_tcp)
8118 return -ENOBUFS;
8119
8120 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8121 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8122 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8123 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8124 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8125 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8126 tcp->payload_len, tcp->payload) ||
8127 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8128 tcp->data_interval) ||
8129 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8130 tcp->wake_len, tcp->wake_data) ||
8131 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8132 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8133 return -ENOBUFS;
8134
8135 if (tcp->payload_seq.len &&
8136 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8137 sizeof(tcp->payload_seq), &tcp->payload_seq))
8138 return -ENOBUFS;
8139
8140 if (tcp->payload_tok.len &&
8141 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8142 sizeof(tcp->payload_tok) + tcp->tokens_size,
8143 &tcp->payload_tok))
8144 return -ENOBUFS;
8145
Johannes Berge248ad32013-05-16 10:24:28 +02008146 nla_nest_end(msg, nl_tcp);
8147
Johannes Berg2a0e0472013-01-23 22:57:40 +01008148 return 0;
8149}
8150
Johannes Bergff1b6e62011-05-04 15:37:28 +02008151static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8152{
8153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8154 struct sk_buff *msg;
8155 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008156 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008157
Johannes Berg964dc9e2013-06-03 17:25:34 +02008158 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008159 return -EOPNOTSUPP;
8160
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008161 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008162 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008163 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8164 rdev->wiphy.wowlan_config->tcp->payload_len +
8165 rdev->wiphy.wowlan_config->tcp->wake_len +
8166 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008167 }
8168
8169 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008170 if (!msg)
8171 return -ENOMEM;
8172
Eric W. Biederman15e47302012-09-07 20:12:54 +00008173 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008174 NL80211_CMD_GET_WOWLAN);
8175 if (!hdr)
8176 goto nla_put_failure;
8177
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008178 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008179 struct nlattr *nl_wowlan;
8180
8181 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8182 if (!nl_wowlan)
8183 goto nla_put_failure;
8184
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008185 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008186 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008187 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008188 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008189 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008190 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008191 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008192 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008193 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008194 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008195 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008196 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008197 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008198 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8199 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008200
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008201 if (nl80211_send_wowlan_patterns(msg, rdev))
8202 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008203
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008204 if (nl80211_send_wowlan_tcp(msg,
8205 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008206 goto nla_put_failure;
8207
Johannes Bergff1b6e62011-05-04 15:37:28 +02008208 nla_nest_end(msg, nl_wowlan);
8209 }
8210
8211 genlmsg_end(msg, hdr);
8212 return genlmsg_reply(msg, info);
8213
8214nla_put_failure:
8215 nlmsg_free(msg);
8216 return -ENOBUFS;
8217}
8218
Johannes Berg2a0e0472013-01-23 22:57:40 +01008219static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8220 struct nlattr *attr,
8221 struct cfg80211_wowlan *trig)
8222{
8223 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8224 struct cfg80211_wowlan_tcp *cfg;
8225 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8226 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8227 u32 size;
8228 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8229 int err, port;
8230
Johannes Berg964dc9e2013-06-03 17:25:34 +02008231 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008232 return -EINVAL;
8233
8234 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8235 nla_data(attr), nla_len(attr),
8236 nl80211_wowlan_tcp_policy);
8237 if (err)
8238 return err;
8239
8240 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8241 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8242 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8243 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8244 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8245 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8246 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8247 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8248 return -EINVAL;
8249
8250 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008251 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008252 return -EINVAL;
8253
8254 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008255 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008256 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008257 return -EINVAL;
8258
8259 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008260 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008261 return -EINVAL;
8262
8263 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8264 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8265 return -EINVAL;
8266
8267 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8268 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8269
8270 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8271 tokens_size = tokln - sizeof(*tok);
8272
8273 if (!tok->len || tokens_size % tok->len)
8274 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008275 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008276 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008277 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008278 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008279 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008280 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008281 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008282 return -EINVAL;
8283 if (tok->offset + tok->len > data_size)
8284 return -EINVAL;
8285 }
8286
8287 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8288 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008289 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008290 return -EINVAL;
8291 if (seq->len == 0 || seq->len > 4)
8292 return -EINVAL;
8293 if (seq->len + seq->offset > data_size)
8294 return -EINVAL;
8295 }
8296
8297 size = sizeof(*cfg);
8298 size += data_size;
8299 size += wake_size + wake_mask_size;
8300 size += tokens_size;
8301
8302 cfg = kzalloc(size, GFP_KERNEL);
8303 if (!cfg)
8304 return -ENOMEM;
8305 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8306 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8307 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8308 ETH_ALEN);
8309 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8310 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8311 else
8312 port = 0;
8313#ifdef CONFIG_INET
8314 /* allocate a socket and port for it and use it */
8315 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8316 IPPROTO_TCP, &cfg->sock, 1);
8317 if (err) {
8318 kfree(cfg);
8319 return err;
8320 }
8321 if (inet_csk_get_port(cfg->sock->sk, port)) {
8322 sock_release(cfg->sock);
8323 kfree(cfg);
8324 return -EADDRINUSE;
8325 }
8326 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8327#else
8328 if (!port) {
8329 kfree(cfg);
8330 return -EINVAL;
8331 }
8332 cfg->src_port = port;
8333#endif
8334
8335 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8336 cfg->payload_len = data_size;
8337 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8338 memcpy((void *)cfg->payload,
8339 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8340 data_size);
8341 if (seq)
8342 cfg->payload_seq = *seq;
8343 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8344 cfg->wake_len = wake_size;
8345 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8346 memcpy((void *)cfg->wake_data,
8347 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8348 wake_size);
8349 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8350 data_size + wake_size;
8351 memcpy((void *)cfg->wake_mask,
8352 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8353 wake_mask_size);
8354 if (tok) {
8355 cfg->tokens_size = tokens_size;
8356 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8357 }
8358
8359 trig->tcp = cfg;
8360
8361 return 0;
8362}
8363
Johannes Bergff1b6e62011-05-04 15:37:28 +02008364static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8365{
8366 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8367 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008368 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008369 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008370 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008371 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008372 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008373
Johannes Berg964dc9e2013-06-03 17:25:34 +02008374 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008375 return -EOPNOTSUPP;
8376
Johannes Bergae33bd82012-07-12 16:25:02 +02008377 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8378 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008379 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008380 goto set_wakeup;
8381 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008382
8383 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8384 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8385 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8386 nl80211_wowlan_policy);
8387 if (err)
8388 return err;
8389
8390 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8391 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8392 return -EINVAL;
8393 new_triggers.any = true;
8394 }
8395
8396 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8397 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8398 return -EINVAL;
8399 new_triggers.disconnect = true;
8400 }
8401
8402 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8403 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8404 return -EINVAL;
8405 new_triggers.magic_pkt = true;
8406 }
8407
Johannes Berg77dbbb12011-07-13 10:48:55 +02008408 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8409 return -EINVAL;
8410
8411 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8412 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8413 return -EINVAL;
8414 new_triggers.gtk_rekey_failure = true;
8415 }
8416
8417 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8418 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8419 return -EINVAL;
8420 new_triggers.eap_identity_req = true;
8421 }
8422
8423 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8424 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8425 return -EINVAL;
8426 new_triggers.four_way_handshake = true;
8427 }
8428
8429 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8430 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8431 return -EINVAL;
8432 new_triggers.rfkill_release = true;
8433 }
8434
Johannes Bergff1b6e62011-05-04 15:37:28 +02008435 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8436 struct nlattr *pat;
8437 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008438 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008439 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008440
8441 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8442 rem)
8443 n_patterns++;
8444 if (n_patterns > wowlan->n_patterns)
8445 return -EINVAL;
8446
8447 new_triggers.patterns = kcalloc(n_patterns,
8448 sizeof(new_triggers.patterns[0]),
8449 GFP_KERNEL);
8450 if (!new_triggers.patterns)
8451 return -ENOMEM;
8452
8453 new_triggers.n_patterns = n_patterns;
8454 i = 0;
8455
8456 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8457 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008458 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8459 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008460 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008461 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8462 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008463 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008464 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008465 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008466 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008467 goto error;
8468 if (pat_len > wowlan->pattern_max_len ||
8469 pat_len < wowlan->pattern_min_len)
8470 goto error;
8471
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008472 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008473 pkt_offset = 0;
8474 else
8475 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008476 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008477 if (pkt_offset > wowlan->max_pkt_offset)
8478 goto error;
8479 new_triggers.patterns[i].pkt_offset = pkt_offset;
8480
Johannes Bergff1b6e62011-05-04 15:37:28 +02008481 new_triggers.patterns[i].mask =
8482 kmalloc(mask_len + pat_len, GFP_KERNEL);
8483 if (!new_triggers.patterns[i].mask) {
8484 err = -ENOMEM;
8485 goto error;
8486 }
8487 new_triggers.patterns[i].pattern =
8488 new_triggers.patterns[i].mask + mask_len;
8489 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008490 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008491 mask_len);
8492 new_triggers.patterns[i].pattern_len = pat_len;
8493 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008494 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008495 pat_len);
8496 i++;
8497 }
8498 }
8499
Johannes Berg2a0e0472013-01-23 22:57:40 +01008500 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8501 err = nl80211_parse_wowlan_tcp(
8502 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8503 &new_triggers);
8504 if (err)
8505 goto error;
8506 }
8507
Johannes Bergae33bd82012-07-12 16:25:02 +02008508 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8509 if (!ntrig) {
8510 err = -ENOMEM;
8511 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008512 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008513 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008514 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008515
Johannes Bergae33bd82012-07-12 16:25:02 +02008516 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008517 if (rdev->ops->set_wakeup &&
8518 prev_enabled != !!rdev->wiphy.wowlan_config)
8519 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008520
Johannes Bergff1b6e62011-05-04 15:37:28 +02008521 return 0;
8522 error:
8523 for (i = 0; i < new_triggers.n_patterns; i++)
8524 kfree(new_triggers.patterns[i].mask);
8525 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008526 if (new_triggers.tcp && new_triggers.tcp->sock)
8527 sock_release(new_triggers.tcp->sock);
8528 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008529 return err;
8530}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008531#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008532
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008533static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8534 struct cfg80211_registered_device *rdev)
8535{
8536 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8537 int i, j, pat_len;
8538 struct cfg80211_coalesce_rules *rule;
8539
8540 if (!rdev->coalesce->n_rules)
8541 return 0;
8542
8543 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8544 if (!nl_rules)
8545 return -ENOBUFS;
8546
8547 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8548 nl_rule = nla_nest_start(msg, i + 1);
8549 if (!nl_rule)
8550 return -ENOBUFS;
8551
8552 rule = &rdev->coalesce->rules[i];
8553 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8554 rule->delay))
8555 return -ENOBUFS;
8556
8557 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8558 rule->condition))
8559 return -ENOBUFS;
8560
8561 nl_pats = nla_nest_start(msg,
8562 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8563 if (!nl_pats)
8564 return -ENOBUFS;
8565
8566 for (j = 0; j < rule->n_patterns; j++) {
8567 nl_pat = nla_nest_start(msg, j + 1);
8568 if (!nl_pat)
8569 return -ENOBUFS;
8570 pat_len = rule->patterns[j].pattern_len;
8571 if (nla_put(msg, NL80211_PKTPAT_MASK,
8572 DIV_ROUND_UP(pat_len, 8),
8573 rule->patterns[j].mask) ||
8574 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8575 rule->patterns[j].pattern) ||
8576 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8577 rule->patterns[j].pkt_offset))
8578 return -ENOBUFS;
8579 nla_nest_end(msg, nl_pat);
8580 }
8581 nla_nest_end(msg, nl_pats);
8582 nla_nest_end(msg, nl_rule);
8583 }
8584 nla_nest_end(msg, nl_rules);
8585
8586 return 0;
8587}
8588
8589static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8590{
8591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8592 struct sk_buff *msg;
8593 void *hdr;
8594
8595 if (!rdev->wiphy.coalesce)
8596 return -EOPNOTSUPP;
8597
8598 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8599 if (!msg)
8600 return -ENOMEM;
8601
8602 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8603 NL80211_CMD_GET_COALESCE);
8604 if (!hdr)
8605 goto nla_put_failure;
8606
8607 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8608 goto nla_put_failure;
8609
8610 genlmsg_end(msg, hdr);
8611 return genlmsg_reply(msg, info);
8612
8613nla_put_failure:
8614 nlmsg_free(msg);
8615 return -ENOBUFS;
8616}
8617
8618void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8619{
8620 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8621 int i, j;
8622 struct cfg80211_coalesce_rules *rule;
8623
8624 if (!coalesce)
8625 return;
8626
8627 for (i = 0; i < coalesce->n_rules; i++) {
8628 rule = &coalesce->rules[i];
8629 for (j = 0; j < rule->n_patterns; j++)
8630 kfree(rule->patterns[j].mask);
8631 kfree(rule->patterns);
8632 }
8633 kfree(coalesce->rules);
8634 kfree(coalesce);
8635 rdev->coalesce = NULL;
8636}
8637
8638static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8639 struct nlattr *rule,
8640 struct cfg80211_coalesce_rules *new_rule)
8641{
8642 int err, i;
8643 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8644 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8645 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8646 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8647
8648 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8649 nla_len(rule), nl80211_coalesce_policy);
8650 if (err)
8651 return err;
8652
8653 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8654 new_rule->delay =
8655 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8656 if (new_rule->delay > coalesce->max_delay)
8657 return -EINVAL;
8658
8659 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8660 new_rule->condition =
8661 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8662 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8663 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8664 return -EINVAL;
8665
8666 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8667 return -EINVAL;
8668
8669 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8670 rem)
8671 n_patterns++;
8672 if (n_patterns > coalesce->n_patterns)
8673 return -EINVAL;
8674
8675 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8676 GFP_KERNEL);
8677 if (!new_rule->patterns)
8678 return -ENOMEM;
8679
8680 new_rule->n_patterns = n_patterns;
8681 i = 0;
8682
8683 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8684 rem) {
8685 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8686 nla_len(pat), NULL);
8687 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8688 !pat_tb[NL80211_PKTPAT_PATTERN])
8689 return -EINVAL;
8690 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8691 mask_len = DIV_ROUND_UP(pat_len, 8);
8692 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8693 return -EINVAL;
8694 if (pat_len > coalesce->pattern_max_len ||
8695 pat_len < coalesce->pattern_min_len)
8696 return -EINVAL;
8697
8698 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8699 pkt_offset = 0;
8700 else
8701 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8702 if (pkt_offset > coalesce->max_pkt_offset)
8703 return -EINVAL;
8704 new_rule->patterns[i].pkt_offset = pkt_offset;
8705
8706 new_rule->patterns[i].mask =
8707 kmalloc(mask_len + pat_len, GFP_KERNEL);
8708 if (!new_rule->patterns[i].mask)
8709 return -ENOMEM;
8710 new_rule->patterns[i].pattern =
8711 new_rule->patterns[i].mask + mask_len;
8712 memcpy(new_rule->patterns[i].mask,
8713 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8714 new_rule->patterns[i].pattern_len = pat_len;
8715 memcpy(new_rule->patterns[i].pattern,
8716 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8717 i++;
8718 }
8719
8720 return 0;
8721}
8722
8723static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8724{
8725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8726 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8727 struct cfg80211_coalesce new_coalesce = {};
8728 struct cfg80211_coalesce *n_coalesce;
8729 int err, rem_rule, n_rules = 0, i, j;
8730 struct nlattr *rule;
8731 struct cfg80211_coalesce_rules *tmp_rule;
8732
8733 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8734 return -EOPNOTSUPP;
8735
8736 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8737 cfg80211_rdev_free_coalesce(rdev);
8738 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8739 return 0;
8740 }
8741
8742 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8743 rem_rule)
8744 n_rules++;
8745 if (n_rules > coalesce->n_rules)
8746 return -EINVAL;
8747
8748 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8749 GFP_KERNEL);
8750 if (!new_coalesce.rules)
8751 return -ENOMEM;
8752
8753 new_coalesce.n_rules = n_rules;
8754 i = 0;
8755
8756 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8757 rem_rule) {
8758 err = nl80211_parse_coalesce_rule(rdev, rule,
8759 &new_coalesce.rules[i]);
8760 if (err)
8761 goto error;
8762
8763 i++;
8764 }
8765
8766 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8767 if (err)
8768 goto error;
8769
8770 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8771 if (!n_coalesce) {
8772 err = -ENOMEM;
8773 goto error;
8774 }
8775 cfg80211_rdev_free_coalesce(rdev);
8776 rdev->coalesce = n_coalesce;
8777
8778 return 0;
8779error:
8780 for (i = 0; i < new_coalesce.n_rules; i++) {
8781 tmp_rule = &new_coalesce.rules[i];
8782 for (j = 0; j < tmp_rule->n_patterns; j++)
8783 kfree(tmp_rule->patterns[j].mask);
8784 kfree(tmp_rule->patterns);
8785 }
8786 kfree(new_coalesce.rules);
8787
8788 return err;
8789}
8790
Johannes Berge5497d72011-07-05 16:35:40 +02008791static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8792{
8793 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8794 struct net_device *dev = info->user_ptr[1];
8795 struct wireless_dev *wdev = dev->ieee80211_ptr;
8796 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8797 struct cfg80211_gtk_rekey_data rekey_data;
8798 int err;
8799
8800 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8801 return -EINVAL;
8802
8803 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8804 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8805 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8806 nl80211_rekey_policy);
8807 if (err)
8808 return err;
8809
8810 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8811 return -ERANGE;
8812 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8813 return -ERANGE;
8814 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8815 return -ERANGE;
8816
8817 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8818 NL80211_KEK_LEN);
8819 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8820 NL80211_KCK_LEN);
8821 memcpy(rekey_data.replay_ctr,
8822 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8823 NL80211_REPLAY_CTR_LEN);
8824
8825 wdev_lock(wdev);
8826 if (!wdev->current_bss) {
8827 err = -ENOTCONN;
8828 goto out;
8829 }
8830
8831 if (!rdev->ops->set_rekey_data) {
8832 err = -EOPNOTSUPP;
8833 goto out;
8834 }
8835
Hila Gonene35e4d22012-06-27 17:19:42 +03008836 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008837 out:
8838 wdev_unlock(wdev);
8839 return err;
8840}
8841
Johannes Berg28946da2011-11-04 11:18:12 +01008842static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8843 struct genl_info *info)
8844{
8845 struct net_device *dev = info->user_ptr[1];
8846 struct wireless_dev *wdev = dev->ieee80211_ptr;
8847
8848 if (wdev->iftype != NL80211_IFTYPE_AP &&
8849 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8850 return -EINVAL;
8851
Eric W. Biederman15e47302012-09-07 20:12:54 +00008852 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008853 return -EBUSY;
8854
Eric W. Biederman15e47302012-09-07 20:12:54 +00008855 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008856 return 0;
8857}
8858
Johannes Berg7f6cf312011-11-04 11:18:15 +01008859static int nl80211_probe_client(struct sk_buff *skb,
8860 struct genl_info *info)
8861{
8862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8863 struct net_device *dev = info->user_ptr[1];
8864 struct wireless_dev *wdev = dev->ieee80211_ptr;
8865 struct sk_buff *msg;
8866 void *hdr;
8867 const u8 *addr;
8868 u64 cookie;
8869 int err;
8870
8871 if (wdev->iftype != NL80211_IFTYPE_AP &&
8872 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8873 return -EOPNOTSUPP;
8874
8875 if (!info->attrs[NL80211_ATTR_MAC])
8876 return -EINVAL;
8877
8878 if (!rdev->ops->probe_client)
8879 return -EOPNOTSUPP;
8880
8881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8882 if (!msg)
8883 return -ENOMEM;
8884
Eric W. Biederman15e47302012-09-07 20:12:54 +00008885 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008886 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008887 if (!hdr) {
8888 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008889 goto free_msg;
8890 }
8891
8892 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8893
Hila Gonene35e4d22012-06-27 17:19:42 +03008894 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008895 if (err)
8896 goto free_msg;
8897
David S. Miller9360ffd2012-03-29 04:41:26 -04008898 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8899 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008900
8901 genlmsg_end(msg, hdr);
8902
8903 return genlmsg_reply(msg, info);
8904
8905 nla_put_failure:
8906 err = -ENOBUFS;
8907 free_msg:
8908 nlmsg_free(msg);
8909 return err;
8910}
8911
Johannes Berg5e7602302011-11-04 11:18:17 +01008912static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8913{
8914 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008915 struct cfg80211_beacon_registration *reg, *nreg;
8916 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008917
8918 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8919 return -EOPNOTSUPP;
8920
Ben Greear37c73b52012-10-26 14:49:25 -07008921 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8922 if (!nreg)
8923 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008924
Ben Greear37c73b52012-10-26 14:49:25 -07008925 /* First, check if already registered. */
8926 spin_lock_bh(&rdev->beacon_registrations_lock);
8927 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8928 if (reg->nlportid == info->snd_portid) {
8929 rv = -EALREADY;
8930 goto out_err;
8931 }
8932 }
8933 /* Add it to the list */
8934 nreg->nlportid = info->snd_portid;
8935 list_add(&nreg->list, &rdev->beacon_registrations);
8936
8937 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008938
8939 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008940out_err:
8941 spin_unlock_bh(&rdev->beacon_registrations_lock);
8942 kfree(nreg);
8943 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008944}
8945
Johannes Berg98104fde2012-06-16 00:19:54 +02008946static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8947{
8948 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8949 struct wireless_dev *wdev = info->user_ptr[1];
8950 int err;
8951
8952 if (!rdev->ops->start_p2p_device)
8953 return -EOPNOTSUPP;
8954
8955 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8956 return -EOPNOTSUPP;
8957
8958 if (wdev->p2p_started)
8959 return 0;
8960
Johannes Berg98104fde2012-06-16 00:19:54 +02008961 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008962 if (err)
8963 return err;
8964
Johannes Bergeeb126e2012-10-23 15:16:50 +02008965 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008966 if (err)
8967 return err;
8968
8969 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008970 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008971
8972 return 0;
8973}
8974
8975static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8976{
8977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8978 struct wireless_dev *wdev = info->user_ptr[1];
8979
8980 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8981 return -EOPNOTSUPP;
8982
8983 if (!rdev->ops->stop_p2p_device)
8984 return -EOPNOTSUPP;
8985
Johannes Bergf9f47522013-03-19 15:04:07 +01008986 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008987
8988 return 0;
8989}
8990
Johannes Berg3713b4e2013-02-14 16:19:38 +01008991static int nl80211_get_protocol_features(struct sk_buff *skb,
8992 struct genl_info *info)
8993{
8994 void *hdr;
8995 struct sk_buff *msg;
8996
8997 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8998 if (!msg)
8999 return -ENOMEM;
9000
9001 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9002 NL80211_CMD_GET_PROTOCOL_FEATURES);
9003 if (!hdr)
9004 goto nla_put_failure;
9005
9006 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9007 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9008 goto nla_put_failure;
9009
9010 genlmsg_end(msg, hdr);
9011 return genlmsg_reply(msg, info);
9012
9013 nla_put_failure:
9014 kfree_skb(msg);
9015 return -ENOBUFS;
9016}
9017
Jouni Malinen355199e2013-02-27 17:14:27 +02009018static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9019{
9020 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9021 struct cfg80211_update_ft_ies_params ft_params;
9022 struct net_device *dev = info->user_ptr[1];
9023
9024 if (!rdev->ops->update_ft_ies)
9025 return -EOPNOTSUPP;
9026
9027 if (!info->attrs[NL80211_ATTR_MDID] ||
9028 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9029 return -EINVAL;
9030
9031 memset(&ft_params, 0, sizeof(ft_params));
9032 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9033 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9034 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9035
9036 return rdev_update_ft_ies(rdev, dev, &ft_params);
9037}
9038
Arend van Spriel5de17982013-04-18 15:49:00 +02009039static int nl80211_crit_protocol_start(struct sk_buff *skb,
9040 struct genl_info *info)
9041{
9042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9043 struct wireless_dev *wdev = info->user_ptr[1];
9044 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9045 u16 duration;
9046 int ret;
9047
9048 if (!rdev->ops->crit_proto_start)
9049 return -EOPNOTSUPP;
9050
9051 if (WARN_ON(!rdev->ops->crit_proto_stop))
9052 return -EINVAL;
9053
9054 if (rdev->crit_proto_nlportid)
9055 return -EBUSY;
9056
9057 /* determine protocol if provided */
9058 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9059 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9060
9061 if (proto >= NUM_NL80211_CRIT_PROTO)
9062 return -EINVAL;
9063
9064 /* timeout must be provided */
9065 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9066 return -EINVAL;
9067
9068 duration =
9069 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9070
9071 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9072 return -ERANGE;
9073
9074 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9075 if (!ret)
9076 rdev->crit_proto_nlportid = info->snd_portid;
9077
9078 return ret;
9079}
9080
9081static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9082 struct genl_info *info)
9083{
9084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9085 struct wireless_dev *wdev = info->user_ptr[1];
9086
9087 if (!rdev->ops->crit_proto_stop)
9088 return -EOPNOTSUPP;
9089
9090 if (rdev->crit_proto_nlportid) {
9091 rdev->crit_proto_nlportid = 0;
9092 rdev_crit_proto_stop(rdev, wdev);
9093 }
9094 return 0;
9095}
9096
Johannes Bergad7e7182013-11-13 13:37:47 +01009097static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9098{
9099 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9100 struct wireless_dev *wdev =
9101 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9102 int i, err;
9103 u32 vid, subcmd;
9104
9105 if (!rdev->wiphy.vendor_commands)
9106 return -EOPNOTSUPP;
9107
9108 if (IS_ERR(wdev)) {
9109 err = PTR_ERR(wdev);
9110 if (err != -EINVAL)
9111 return err;
9112 wdev = NULL;
9113 } else if (wdev->wiphy != &rdev->wiphy) {
9114 return -EINVAL;
9115 }
9116
9117 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9118 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9119 return -EINVAL;
9120
9121 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9122 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9123 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9124 const struct wiphy_vendor_command *vcmd;
9125 void *data = NULL;
9126 int len = 0;
9127
9128 vcmd = &rdev->wiphy.vendor_commands[i];
9129
9130 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9131 continue;
9132
9133 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9134 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9135 if (!wdev)
9136 return -EINVAL;
9137 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9138 !wdev->netdev)
9139 return -EINVAL;
9140
9141 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9142 if (wdev->netdev &&
9143 !netif_running(wdev->netdev))
9144 return -ENETDOWN;
9145 if (!wdev->netdev && !wdev->p2p_started)
9146 return -ENETDOWN;
9147 }
9148 } else {
9149 wdev = NULL;
9150 }
9151
9152 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9153 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9154 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9155 }
9156
9157 rdev->cur_cmd_info = info;
9158 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9159 data, len);
9160 rdev->cur_cmd_info = NULL;
9161 return err;
9162 }
9163
9164 return -EOPNOTSUPP;
9165}
9166
9167struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9168 enum nl80211_commands cmd,
9169 enum nl80211_attrs attr,
9170 int approxlen)
9171{
9172 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9173
9174 if (WARN_ON(!rdev->cur_cmd_info))
9175 return NULL;
9176
9177 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9178 rdev->cur_cmd_info->snd_portid,
9179 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009180 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009181}
9182EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9183
9184int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9185{
9186 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9187 void *hdr = ((void **)skb->cb)[1];
9188 struct nlattr *data = ((void **)skb->cb)[2];
9189
9190 if (WARN_ON(!rdev->cur_cmd_info)) {
9191 kfree_skb(skb);
9192 return -EINVAL;
9193 }
9194
9195 nla_nest_end(skb, data);
9196 genlmsg_end(skb, hdr);
9197 return genlmsg_reply(skb, rdev->cur_cmd_info);
9198}
9199EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9200
9201
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009202static int nl80211_set_qos_map(struct sk_buff *skb,
9203 struct genl_info *info)
9204{
9205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9206 struct cfg80211_qos_map *qos_map = NULL;
9207 struct net_device *dev = info->user_ptr[1];
9208 u8 *pos, len, num_des, des_len, des;
9209 int ret;
9210
9211 if (!rdev->ops->set_qos_map)
9212 return -EOPNOTSUPP;
9213
9214 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9215 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9216 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9217
9218 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9219 len > IEEE80211_QOS_MAP_LEN_MAX)
9220 return -EINVAL;
9221
9222 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9223 if (!qos_map)
9224 return -ENOMEM;
9225
9226 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9227 if (num_des) {
9228 des_len = num_des *
9229 sizeof(struct cfg80211_dscp_exception);
9230 memcpy(qos_map->dscp_exception, pos, des_len);
9231 qos_map->num_des = num_des;
9232 for (des = 0; des < num_des; des++) {
9233 if (qos_map->dscp_exception[des].up > 7) {
9234 kfree(qos_map);
9235 return -EINVAL;
9236 }
9237 }
9238 pos += des_len;
9239 }
9240 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9241 }
9242
9243 wdev_lock(dev->ieee80211_ptr);
9244 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9245 if (!ret)
9246 ret = rdev_set_qos_map(rdev, dev, qos_map);
9247 wdev_unlock(dev->ieee80211_ptr);
9248
9249 kfree(qos_map);
9250 return ret;
9251}
9252
Johannes Berg4c476992010-10-04 21:36:35 +02009253#define NL80211_FLAG_NEED_WIPHY 0x01
9254#define NL80211_FLAG_NEED_NETDEV 0x02
9255#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009256#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9257#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9258 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009259#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009260/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009261#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9262 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009263
Johannes Bergf84f7712013-11-14 17:14:45 +01009264static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009265 struct genl_info *info)
9266{
9267 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009268 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009269 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009270 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9271
9272 if (rtnl)
9273 rtnl_lock();
9274
9275 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009276 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009277 if (IS_ERR(rdev)) {
9278 if (rtnl)
9279 rtnl_unlock();
9280 return PTR_ERR(rdev);
9281 }
9282 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009283 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9284 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009285 ASSERT_RTNL();
9286
Johannes Berg89a54e42012-06-15 14:33:17 +02009287 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9288 info->attrs);
9289 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009290 if (rtnl)
9291 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009292 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009293 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009294
Johannes Berg89a54e42012-06-15 14:33:17 +02009295 dev = wdev->netdev;
9296 rdev = wiphy_to_dev(wdev->wiphy);
9297
Johannes Berg1bf614e2012-06-15 15:23:36 +02009298 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9299 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009300 if (rtnl)
9301 rtnl_unlock();
9302 return -EINVAL;
9303 }
9304
9305 info->user_ptr[1] = dev;
9306 } else {
9307 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009308 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009309
Johannes Berg1bf614e2012-06-15 15:23:36 +02009310 if (dev) {
9311 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9312 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009313 if (rtnl)
9314 rtnl_unlock();
9315 return -ENETDOWN;
9316 }
9317
9318 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009319 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9320 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009321 if (rtnl)
9322 rtnl_unlock();
9323 return -ENETDOWN;
9324 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009325 }
9326
Johannes Berg4c476992010-10-04 21:36:35 +02009327 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009328 }
9329
9330 return 0;
9331}
9332
Johannes Bergf84f7712013-11-14 17:14:45 +01009333static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009334 struct genl_info *info)
9335{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009336 if (info->user_ptr[1]) {
9337 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9338 struct wireless_dev *wdev = info->user_ptr[1];
9339
9340 if (wdev->netdev)
9341 dev_put(wdev->netdev);
9342 } else {
9343 dev_put(info->user_ptr[1]);
9344 }
9345 }
Johannes Berg4c476992010-10-04 21:36:35 +02009346 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9347 rtnl_unlock();
9348}
9349
Johannes Berg4534de82013-11-14 17:14:46 +01009350static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009351 {
9352 .cmd = NL80211_CMD_GET_WIPHY,
9353 .doit = nl80211_get_wiphy,
9354 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009355 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009356 .policy = nl80211_policy,
9357 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009358 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9359 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009360 },
9361 {
9362 .cmd = NL80211_CMD_SET_WIPHY,
9363 .doit = nl80211_set_wiphy,
9364 .policy = nl80211_policy,
9365 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009366 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009367 },
9368 {
9369 .cmd = NL80211_CMD_GET_INTERFACE,
9370 .doit = nl80211_get_interface,
9371 .dumpit = nl80211_dump_interface,
9372 .policy = nl80211_policy,
9373 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009374 .internal_flags = NL80211_FLAG_NEED_WDEV |
9375 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009376 },
9377 {
9378 .cmd = NL80211_CMD_SET_INTERFACE,
9379 .doit = nl80211_set_interface,
9380 .policy = nl80211_policy,
9381 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009382 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9383 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009384 },
9385 {
9386 .cmd = NL80211_CMD_NEW_INTERFACE,
9387 .doit = nl80211_new_interface,
9388 .policy = nl80211_policy,
9389 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009390 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9391 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009392 },
9393 {
9394 .cmd = NL80211_CMD_DEL_INTERFACE,
9395 .doit = nl80211_del_interface,
9396 .policy = nl80211_policy,
9397 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009398 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009399 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009400 },
Johannes Berg41ade002007-12-19 02:03:29 +01009401 {
9402 .cmd = NL80211_CMD_GET_KEY,
9403 .doit = nl80211_get_key,
9404 .policy = nl80211_policy,
9405 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009406 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009407 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009408 },
9409 {
9410 .cmd = NL80211_CMD_SET_KEY,
9411 .doit = nl80211_set_key,
9412 .policy = nl80211_policy,
9413 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009414 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009415 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009416 },
9417 {
9418 .cmd = NL80211_CMD_NEW_KEY,
9419 .doit = nl80211_new_key,
9420 .policy = nl80211_policy,
9421 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009422 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009423 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009424 },
9425 {
9426 .cmd = NL80211_CMD_DEL_KEY,
9427 .doit = nl80211_del_key,
9428 .policy = nl80211_policy,
9429 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009430 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009431 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009432 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009433 {
9434 .cmd = NL80211_CMD_SET_BEACON,
9435 .policy = nl80211_policy,
9436 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009437 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009438 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009439 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009440 },
9441 {
Johannes Berg88600202012-02-13 15:17:18 +01009442 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009443 .policy = nl80211_policy,
9444 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009445 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009446 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009447 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009448 },
9449 {
Johannes Berg88600202012-02-13 15:17:18 +01009450 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009451 .policy = nl80211_policy,
9452 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009453 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009454 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009455 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009456 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009457 {
9458 .cmd = NL80211_CMD_GET_STATION,
9459 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009460 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009461 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009462 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9463 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009464 },
9465 {
9466 .cmd = NL80211_CMD_SET_STATION,
9467 .doit = nl80211_set_station,
9468 .policy = nl80211_policy,
9469 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009470 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009471 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009472 },
9473 {
9474 .cmd = NL80211_CMD_NEW_STATION,
9475 .doit = nl80211_new_station,
9476 .policy = nl80211_policy,
9477 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009479 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009480 },
9481 {
9482 .cmd = NL80211_CMD_DEL_STATION,
9483 .doit = nl80211_del_station,
9484 .policy = nl80211_policy,
9485 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009487 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009488 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009489 {
9490 .cmd = NL80211_CMD_GET_MPATH,
9491 .doit = nl80211_get_mpath,
9492 .dumpit = nl80211_dump_mpath,
9493 .policy = nl80211_policy,
9494 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009495 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009496 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009497 },
9498 {
9499 .cmd = NL80211_CMD_SET_MPATH,
9500 .doit = nl80211_set_mpath,
9501 .policy = nl80211_policy,
9502 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009503 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009504 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009505 },
9506 {
9507 .cmd = NL80211_CMD_NEW_MPATH,
9508 .doit = nl80211_new_mpath,
9509 .policy = nl80211_policy,
9510 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009511 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009512 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009513 },
9514 {
9515 .cmd = NL80211_CMD_DEL_MPATH,
9516 .doit = nl80211_del_mpath,
9517 .policy = nl80211_policy,
9518 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009519 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009520 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009521 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009522 {
9523 .cmd = NL80211_CMD_SET_BSS,
9524 .doit = nl80211_set_bss,
9525 .policy = nl80211_policy,
9526 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009527 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009528 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009529 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009530 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009531 .cmd = NL80211_CMD_GET_REG,
9532 .doit = nl80211_get_reg,
9533 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009534 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009535 /* can be retrieved by unprivileged users */
9536 },
9537 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009538 .cmd = NL80211_CMD_SET_REG,
9539 .doit = nl80211_set_reg,
9540 .policy = nl80211_policy,
9541 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009542 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009543 },
9544 {
9545 .cmd = NL80211_CMD_REQ_SET_REG,
9546 .doit = nl80211_req_set_reg,
9547 .policy = nl80211_policy,
9548 .flags = GENL_ADMIN_PERM,
9549 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009550 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009551 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9552 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009553 .policy = nl80211_policy,
9554 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009555 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009556 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009557 },
9558 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009559 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9560 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009561 .policy = nl80211_policy,
9562 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009563 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009564 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009565 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009566 {
Johannes Berg2a519312009-02-10 21:25:55 +01009567 .cmd = NL80211_CMD_TRIGGER_SCAN,
9568 .doit = nl80211_trigger_scan,
9569 .policy = nl80211_policy,
9570 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009571 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009572 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009573 },
9574 {
9575 .cmd = NL80211_CMD_GET_SCAN,
9576 .policy = nl80211_policy,
9577 .dumpit = nl80211_dump_scan,
9578 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009579 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009580 .cmd = NL80211_CMD_START_SCHED_SCAN,
9581 .doit = nl80211_start_sched_scan,
9582 .policy = nl80211_policy,
9583 .flags = GENL_ADMIN_PERM,
9584 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9585 NL80211_FLAG_NEED_RTNL,
9586 },
9587 {
9588 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9589 .doit = nl80211_stop_sched_scan,
9590 .policy = nl80211_policy,
9591 .flags = GENL_ADMIN_PERM,
9592 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9593 NL80211_FLAG_NEED_RTNL,
9594 },
9595 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009596 .cmd = NL80211_CMD_AUTHENTICATE,
9597 .doit = nl80211_authenticate,
9598 .policy = nl80211_policy,
9599 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009600 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009601 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009602 },
9603 {
9604 .cmd = NL80211_CMD_ASSOCIATE,
9605 .doit = nl80211_associate,
9606 .policy = nl80211_policy,
9607 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009608 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009609 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009610 },
9611 {
9612 .cmd = NL80211_CMD_DEAUTHENTICATE,
9613 .doit = nl80211_deauthenticate,
9614 .policy = nl80211_policy,
9615 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009616 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009617 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009618 },
9619 {
9620 .cmd = NL80211_CMD_DISASSOCIATE,
9621 .doit = nl80211_disassociate,
9622 .policy = nl80211_policy,
9623 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009624 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009625 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009626 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009627 {
9628 .cmd = NL80211_CMD_JOIN_IBSS,
9629 .doit = nl80211_join_ibss,
9630 .policy = nl80211_policy,
9631 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009632 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009633 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009634 },
9635 {
9636 .cmd = NL80211_CMD_LEAVE_IBSS,
9637 .doit = nl80211_leave_ibss,
9638 .policy = nl80211_policy,
9639 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009640 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009641 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009642 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009643#ifdef CONFIG_NL80211_TESTMODE
9644 {
9645 .cmd = NL80211_CMD_TESTMODE,
9646 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009647 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009648 .policy = nl80211_policy,
9649 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009650 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9651 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009652 },
9653#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009654 {
9655 .cmd = NL80211_CMD_CONNECT,
9656 .doit = nl80211_connect,
9657 .policy = nl80211_policy,
9658 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009659 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009660 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009661 },
9662 {
9663 .cmd = NL80211_CMD_DISCONNECT,
9664 .doit = nl80211_disconnect,
9665 .policy = nl80211_policy,
9666 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009667 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009668 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009669 },
Johannes Berg463d0182009-07-14 00:33:35 +02009670 {
9671 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9672 .doit = nl80211_wiphy_netns,
9673 .policy = nl80211_policy,
9674 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009675 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9676 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009677 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009678 {
9679 .cmd = NL80211_CMD_GET_SURVEY,
9680 .policy = nl80211_policy,
9681 .dumpit = nl80211_dump_survey,
9682 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009683 {
9684 .cmd = NL80211_CMD_SET_PMKSA,
9685 .doit = nl80211_setdel_pmksa,
9686 .policy = nl80211_policy,
9687 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009688 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009689 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009690 },
9691 {
9692 .cmd = NL80211_CMD_DEL_PMKSA,
9693 .doit = nl80211_setdel_pmksa,
9694 .policy = nl80211_policy,
9695 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009696 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009697 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009698 },
9699 {
9700 .cmd = NL80211_CMD_FLUSH_PMKSA,
9701 .doit = nl80211_flush_pmksa,
9702 .policy = nl80211_policy,
9703 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009704 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009705 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009706 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009707 {
9708 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9709 .doit = nl80211_remain_on_channel,
9710 .policy = nl80211_policy,
9711 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009712 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009713 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009714 },
9715 {
9716 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9717 .doit = nl80211_cancel_remain_on_channel,
9718 .policy = nl80211_policy,
9719 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009720 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009721 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009722 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009723 {
9724 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9725 .doit = nl80211_set_tx_bitrate_mask,
9726 .policy = nl80211_policy,
9727 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009728 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9729 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009730 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009731 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009732 .cmd = NL80211_CMD_REGISTER_FRAME,
9733 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009734 .policy = nl80211_policy,
9735 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009736 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009737 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009738 },
9739 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009740 .cmd = NL80211_CMD_FRAME,
9741 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009742 .policy = nl80211_policy,
9743 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009744 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009745 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009746 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009747 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009748 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9749 .doit = nl80211_tx_mgmt_cancel_wait,
9750 .policy = nl80211_policy,
9751 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009752 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009753 NL80211_FLAG_NEED_RTNL,
9754 },
9755 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009756 .cmd = NL80211_CMD_SET_POWER_SAVE,
9757 .doit = nl80211_set_power_save,
9758 .policy = nl80211_policy,
9759 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009760 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9761 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009762 },
9763 {
9764 .cmd = NL80211_CMD_GET_POWER_SAVE,
9765 .doit = nl80211_get_power_save,
9766 .policy = nl80211_policy,
9767 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009768 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9769 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009770 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009771 {
9772 .cmd = NL80211_CMD_SET_CQM,
9773 .doit = nl80211_set_cqm,
9774 .policy = nl80211_policy,
9775 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009776 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9777 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009778 },
Johannes Bergf444de02010-05-05 15:25:02 +02009779 {
9780 .cmd = NL80211_CMD_SET_CHANNEL,
9781 .doit = nl80211_set_channel,
9782 .policy = nl80211_policy,
9783 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009784 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9785 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009786 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009787 {
9788 .cmd = NL80211_CMD_SET_WDS_PEER,
9789 .doit = nl80211_set_wds_peer,
9790 .policy = nl80211_policy,
9791 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009792 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9793 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009794 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009795 {
9796 .cmd = NL80211_CMD_JOIN_MESH,
9797 .doit = nl80211_join_mesh,
9798 .policy = nl80211_policy,
9799 .flags = GENL_ADMIN_PERM,
9800 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9801 NL80211_FLAG_NEED_RTNL,
9802 },
9803 {
9804 .cmd = NL80211_CMD_LEAVE_MESH,
9805 .doit = nl80211_leave_mesh,
9806 .policy = nl80211_policy,
9807 .flags = GENL_ADMIN_PERM,
9808 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9809 NL80211_FLAG_NEED_RTNL,
9810 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009811#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009812 {
9813 .cmd = NL80211_CMD_GET_WOWLAN,
9814 .doit = nl80211_get_wowlan,
9815 .policy = nl80211_policy,
9816 /* can be retrieved by unprivileged users */
9817 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9818 NL80211_FLAG_NEED_RTNL,
9819 },
9820 {
9821 .cmd = NL80211_CMD_SET_WOWLAN,
9822 .doit = nl80211_set_wowlan,
9823 .policy = nl80211_policy,
9824 .flags = GENL_ADMIN_PERM,
9825 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9826 NL80211_FLAG_NEED_RTNL,
9827 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009828#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009829 {
9830 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9831 .doit = nl80211_set_rekey_data,
9832 .policy = nl80211_policy,
9833 .flags = GENL_ADMIN_PERM,
9834 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9835 NL80211_FLAG_NEED_RTNL,
9836 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009837 {
9838 .cmd = NL80211_CMD_TDLS_MGMT,
9839 .doit = nl80211_tdls_mgmt,
9840 .policy = nl80211_policy,
9841 .flags = GENL_ADMIN_PERM,
9842 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9843 NL80211_FLAG_NEED_RTNL,
9844 },
9845 {
9846 .cmd = NL80211_CMD_TDLS_OPER,
9847 .doit = nl80211_tdls_oper,
9848 .policy = nl80211_policy,
9849 .flags = GENL_ADMIN_PERM,
9850 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9851 NL80211_FLAG_NEED_RTNL,
9852 },
Johannes Berg28946da2011-11-04 11:18:12 +01009853 {
9854 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9855 .doit = nl80211_register_unexpected_frame,
9856 .policy = nl80211_policy,
9857 .flags = GENL_ADMIN_PERM,
9858 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9859 NL80211_FLAG_NEED_RTNL,
9860 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009861 {
9862 .cmd = NL80211_CMD_PROBE_CLIENT,
9863 .doit = nl80211_probe_client,
9864 .policy = nl80211_policy,
9865 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009866 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009867 NL80211_FLAG_NEED_RTNL,
9868 },
Johannes Berg5e7602302011-11-04 11:18:17 +01009869 {
9870 .cmd = NL80211_CMD_REGISTER_BEACONS,
9871 .doit = nl80211_register_beacons,
9872 .policy = nl80211_policy,
9873 .flags = GENL_ADMIN_PERM,
9874 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9875 NL80211_FLAG_NEED_RTNL,
9876 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009877 {
9878 .cmd = NL80211_CMD_SET_NOACK_MAP,
9879 .doit = nl80211_set_noack_map,
9880 .policy = nl80211_policy,
9881 .flags = GENL_ADMIN_PERM,
9882 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9883 NL80211_FLAG_NEED_RTNL,
9884 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009885 {
9886 .cmd = NL80211_CMD_START_P2P_DEVICE,
9887 .doit = nl80211_start_p2p_device,
9888 .policy = nl80211_policy,
9889 .flags = GENL_ADMIN_PERM,
9890 .internal_flags = NL80211_FLAG_NEED_WDEV |
9891 NL80211_FLAG_NEED_RTNL,
9892 },
9893 {
9894 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9895 .doit = nl80211_stop_p2p_device,
9896 .policy = nl80211_policy,
9897 .flags = GENL_ADMIN_PERM,
9898 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9899 NL80211_FLAG_NEED_RTNL,
9900 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009901 {
9902 .cmd = NL80211_CMD_SET_MCAST_RATE,
9903 .doit = nl80211_set_mcast_rate,
9904 .policy = nl80211_policy,
9905 .flags = GENL_ADMIN_PERM,
9906 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9907 NL80211_FLAG_NEED_RTNL,
9908 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309909 {
9910 .cmd = NL80211_CMD_SET_MAC_ACL,
9911 .doit = nl80211_set_mac_acl,
9912 .policy = nl80211_policy,
9913 .flags = GENL_ADMIN_PERM,
9914 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9915 NL80211_FLAG_NEED_RTNL,
9916 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009917 {
9918 .cmd = NL80211_CMD_RADAR_DETECT,
9919 .doit = nl80211_start_radar_detection,
9920 .policy = nl80211_policy,
9921 .flags = GENL_ADMIN_PERM,
9922 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9923 NL80211_FLAG_NEED_RTNL,
9924 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009925 {
9926 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9927 .doit = nl80211_get_protocol_features,
9928 .policy = nl80211_policy,
9929 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009930 {
9931 .cmd = NL80211_CMD_UPDATE_FT_IES,
9932 .doit = nl80211_update_ft_ies,
9933 .policy = nl80211_policy,
9934 .flags = GENL_ADMIN_PERM,
9935 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9936 NL80211_FLAG_NEED_RTNL,
9937 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009938 {
9939 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9940 .doit = nl80211_crit_protocol_start,
9941 .policy = nl80211_policy,
9942 .flags = GENL_ADMIN_PERM,
9943 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9944 NL80211_FLAG_NEED_RTNL,
9945 },
9946 {
9947 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9948 .doit = nl80211_crit_protocol_stop,
9949 .policy = nl80211_policy,
9950 .flags = GENL_ADMIN_PERM,
9951 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9952 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009953 },
9954 {
9955 .cmd = NL80211_CMD_GET_COALESCE,
9956 .doit = nl80211_get_coalesce,
9957 .policy = nl80211_policy,
9958 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9959 NL80211_FLAG_NEED_RTNL,
9960 },
9961 {
9962 .cmd = NL80211_CMD_SET_COALESCE,
9963 .doit = nl80211_set_coalesce,
9964 .policy = nl80211_policy,
9965 .flags = GENL_ADMIN_PERM,
9966 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9967 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009968 },
9969 {
9970 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9971 .doit = nl80211_channel_switch,
9972 .policy = nl80211_policy,
9973 .flags = GENL_ADMIN_PERM,
9974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9975 NL80211_FLAG_NEED_RTNL,
9976 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009977 {
9978 .cmd = NL80211_CMD_VENDOR,
9979 .doit = nl80211_vendor_cmd,
9980 .policy = nl80211_policy,
9981 .flags = GENL_ADMIN_PERM,
9982 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9983 NL80211_FLAG_NEED_RTNL,
9984 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009985 {
9986 .cmd = NL80211_CMD_SET_QOS_MAP,
9987 .doit = nl80211_set_qos_map,
9988 .policy = nl80211_policy,
9989 .flags = GENL_ADMIN_PERM,
9990 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9991 NL80211_FLAG_NEED_RTNL,
9992 },
Johannes Berg55682962007-09-20 13:09:35 -04009993};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009994
Johannes Berg55682962007-09-20 13:09:35 -04009995/* notification functions */
9996
9997void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9998{
9999 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010000 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010001
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010003 if (!msg)
10004 return;
10005
Johannes Berg86e8cf92013-06-19 10:57:22 +020010006 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010007 nlmsg_free(msg);
10008 return;
10009 }
10010
Johannes Berg68eb5502013-11-19 15:19:38 +010010011 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010012 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010013}
10014
Johannes Berg362a4152009-05-24 16:43:15 +020010015static int nl80211_add_scan_req(struct sk_buff *msg,
10016 struct cfg80211_registered_device *rdev)
10017{
10018 struct cfg80211_scan_request *req = rdev->scan_req;
10019 struct nlattr *nest;
10020 int i;
10021
10022 if (WARN_ON(!req))
10023 return 0;
10024
10025 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10026 if (!nest)
10027 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010028 for (i = 0; i < req->n_ssids; i++) {
10029 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10030 goto nla_put_failure;
10031 }
Johannes Berg362a4152009-05-24 16:43:15 +020010032 nla_nest_end(msg, nest);
10033
10034 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10035 if (!nest)
10036 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010037 for (i = 0; i < req->n_channels; i++) {
10038 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10039 goto nla_put_failure;
10040 }
Johannes Berg362a4152009-05-24 16:43:15 +020010041 nla_nest_end(msg, nest);
10042
David S. Miller9360ffd2012-03-29 04:41:26 -040010043 if (req->ie &&
10044 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10045 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010046
Johannes Bergae917c92013-10-25 11:05:22 +020010047 if (req->flags &&
10048 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10049 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010050
Johannes Berg362a4152009-05-24 16:43:15 +020010051 return 0;
10052 nla_put_failure:
10053 return -ENOBUFS;
10054}
10055
Johannes Berga538e2d2009-06-16 19:56:42 +020010056static int nl80211_send_scan_msg(struct sk_buff *msg,
10057 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010058 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010059 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010060 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010061{
10062 void *hdr;
10063
Eric W. Biederman15e47302012-09-07 20:12:54 +000010064 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010065 if (!hdr)
10066 return -1;
10067
David S. Miller9360ffd2012-03-29 04:41:26 -040010068 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010069 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10070 wdev->netdev->ifindex)) ||
10071 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010072 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010073
Johannes Berg362a4152009-05-24 16:43:15 +020010074 /* ignore errors and send incomplete event anyway */
10075 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010076
10077 return genlmsg_end(msg, hdr);
10078
10079 nla_put_failure:
10080 genlmsg_cancel(msg, hdr);
10081 return -EMSGSIZE;
10082}
10083
Luciano Coelho807f8a82011-05-11 17:09:35 +030010084static int
10085nl80211_send_sched_scan_msg(struct sk_buff *msg,
10086 struct cfg80211_registered_device *rdev,
10087 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010088 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010089{
10090 void *hdr;
10091
Eric W. Biederman15e47302012-09-07 20:12:54 +000010092 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010093 if (!hdr)
10094 return -1;
10095
David S. Miller9360ffd2012-03-29 04:41:26 -040010096 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10097 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10098 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010099
10100 return genlmsg_end(msg, hdr);
10101
10102 nla_put_failure:
10103 genlmsg_cancel(msg, hdr);
10104 return -EMSGSIZE;
10105}
10106
Johannes Berga538e2d2009-06-16 19:56:42 +020010107void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010108 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010109{
10110 struct sk_buff *msg;
10111
Thomas Graf58050fc2012-06-28 03:57:45 +000010112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010113 if (!msg)
10114 return;
10115
Johannes Bergfd014282012-06-18 19:17:03 +020010116 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010117 NL80211_CMD_TRIGGER_SCAN) < 0) {
10118 nlmsg_free(msg);
10119 return;
10120 }
10121
Johannes Berg68eb5502013-11-19 15:19:38 +010010122 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010123 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010124}
10125
Johannes Bergf9d15d12014-01-22 11:14:19 +020010126struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10127 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010128{
10129 struct sk_buff *msg;
10130
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010131 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010132 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010133 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010134
Johannes Bergfd014282012-06-18 19:17:03 +020010135 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010136 aborted ? NL80211_CMD_SCAN_ABORTED :
10137 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010138 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010139 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010140 }
10141
Johannes Bergf9d15d12014-01-22 11:14:19 +020010142 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010143}
10144
Johannes Bergf9d15d12014-01-22 11:14:19 +020010145void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10146 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010147{
Johannes Berg2a519312009-02-10 21:25:55 +010010148 if (!msg)
10149 return;
10150
Johannes Berg68eb5502013-11-19 15:19:38 +010010151 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010152 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010153}
10154
Luciano Coelho807f8a82011-05-11 17:09:35 +030010155void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10156 struct net_device *netdev)
10157{
10158 struct sk_buff *msg;
10159
10160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10161 if (!msg)
10162 return;
10163
10164 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10165 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10166 nlmsg_free(msg);
10167 return;
10168 }
10169
Johannes Berg68eb5502013-11-19 15:19:38 +010010170 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010171 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010172}
10173
10174void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10175 struct net_device *netdev, u32 cmd)
10176{
10177 struct sk_buff *msg;
10178
Thomas Graf58050fc2012-06-28 03:57:45 +000010179 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010180 if (!msg)
10181 return;
10182
10183 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10184 nlmsg_free(msg);
10185 return;
10186 }
10187
Johannes Berg68eb5502013-11-19 15:19:38 +010010188 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010189 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010190}
10191
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010192/*
10193 * This can happen on global regulatory changes or device specific settings
10194 * based on custom world regulatory domains.
10195 */
10196void nl80211_send_reg_change_event(struct regulatory_request *request)
10197{
10198 struct sk_buff *msg;
10199 void *hdr;
10200
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010201 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010202 if (!msg)
10203 return;
10204
10205 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10206 if (!hdr) {
10207 nlmsg_free(msg);
10208 return;
10209 }
10210
10211 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010212 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10213 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010214
David S. Miller9360ffd2012-03-29 04:41:26 -040010215 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10216 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10217 NL80211_REGDOM_TYPE_WORLD))
10218 goto nla_put_failure;
10219 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10220 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10221 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10222 goto nla_put_failure;
10223 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10224 request->intersect) {
10225 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10226 NL80211_REGDOM_TYPE_INTERSECTION))
10227 goto nla_put_failure;
10228 } else {
10229 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10230 NL80211_REGDOM_TYPE_COUNTRY) ||
10231 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10232 request->alpha2))
10233 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010234 }
10235
Johannes Bergf4173762012-12-03 18:23:37 +010010236 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010237 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10238 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010239
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010240 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010241
Johannes Bergbc43b282009-07-25 10:54:13 +020010242 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010243 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010244 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010245 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010246
10247 return;
10248
10249nla_put_failure:
10250 genlmsg_cancel(msg, hdr);
10251 nlmsg_free(msg);
10252}
10253
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010254static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10255 struct net_device *netdev,
10256 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010257 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010258{
10259 struct sk_buff *msg;
10260 void *hdr;
10261
Johannes Berge6d6e342009-07-01 21:26:47 +020010262 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010263 if (!msg)
10264 return;
10265
10266 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10267 if (!hdr) {
10268 nlmsg_free(msg);
10269 return;
10270 }
10271
David S. Miller9360ffd2012-03-29 04:41:26 -040010272 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10273 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10274 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10275 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010276
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010277 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010278
Johannes Berg68eb5502013-11-19 15:19:38 +010010279 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010280 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010281 return;
10282
10283 nla_put_failure:
10284 genlmsg_cancel(msg, hdr);
10285 nlmsg_free(msg);
10286}
10287
10288void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010289 struct net_device *netdev, const u8 *buf,
10290 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010291{
10292 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010293 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010294}
10295
10296void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10297 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010298 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010299{
Johannes Berge6d6e342009-07-01 21:26:47 +020010300 nl80211_send_mlme_event(rdev, netdev, buf, len,
10301 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010302}
10303
Jouni Malinen53b46b82009-03-27 20:53:56 +020010304void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010305 struct net_device *netdev, const u8 *buf,
10306 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010307{
10308 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010309 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010310}
10311
Jouni Malinen53b46b82009-03-27 20:53:56 +020010312void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10313 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010314 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010315{
10316 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010317 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010318}
10319
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010320void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10321 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010322{
Johannes Berg947add32013-02-22 22:05:20 +010010323 struct wireless_dev *wdev = dev->ieee80211_ptr;
10324 struct wiphy *wiphy = wdev->wiphy;
10325 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010326 const struct ieee80211_mgmt *mgmt = (void *)buf;
10327 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010328
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010329 if (WARN_ON(len < 2))
10330 return;
10331
10332 if (ieee80211_is_deauth(mgmt->frame_control))
10333 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10334 else
10335 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10336
10337 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10338 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010339}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010340EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010341
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010342static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10343 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010344 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010345{
10346 struct sk_buff *msg;
10347 void *hdr;
10348
Johannes Berge6d6e342009-07-01 21:26:47 +020010349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010350 if (!msg)
10351 return;
10352
10353 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10354 if (!hdr) {
10355 nlmsg_free(msg);
10356 return;
10357 }
10358
David S. Miller9360ffd2012-03-29 04:41:26 -040010359 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10360 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10361 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10362 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10363 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010364
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010365 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010366
Johannes Berg68eb5502013-11-19 15:19:38 +010010367 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010368 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010369 return;
10370
10371 nla_put_failure:
10372 genlmsg_cancel(msg, hdr);
10373 nlmsg_free(msg);
10374}
10375
10376void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010377 struct net_device *netdev, const u8 *addr,
10378 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010379{
10380 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010381 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010382}
10383
10384void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010385 struct net_device *netdev, const u8 *addr,
10386 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010387{
Johannes Berge6d6e342009-07-01 21:26:47 +020010388 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10389 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010390}
10391
Samuel Ortizb23aa672009-07-01 21:26:54 +020010392void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10393 struct net_device *netdev, const u8 *bssid,
10394 const u8 *req_ie, size_t req_ie_len,
10395 const u8 *resp_ie, size_t resp_ie_len,
10396 u16 status, gfp_t gfp)
10397{
10398 struct sk_buff *msg;
10399 void *hdr;
10400
Thomas Graf58050fc2012-06-28 03:57:45 +000010401 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010402 if (!msg)
10403 return;
10404
10405 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10406 if (!hdr) {
10407 nlmsg_free(msg);
10408 return;
10409 }
10410
David S. Miller9360ffd2012-03-29 04:41:26 -040010411 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10412 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10413 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10414 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10415 (req_ie &&
10416 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10417 (resp_ie &&
10418 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10419 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010420
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010421 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010422
Johannes Berg68eb5502013-11-19 15:19:38 +010010423 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010424 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010425 return;
10426
10427 nla_put_failure:
10428 genlmsg_cancel(msg, hdr);
10429 nlmsg_free(msg);
10430
10431}
10432
10433void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10434 struct net_device *netdev, const u8 *bssid,
10435 const u8 *req_ie, size_t req_ie_len,
10436 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10437{
10438 struct sk_buff *msg;
10439 void *hdr;
10440
Thomas Graf58050fc2012-06-28 03:57:45 +000010441 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010442 if (!msg)
10443 return;
10444
10445 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10446 if (!hdr) {
10447 nlmsg_free(msg);
10448 return;
10449 }
10450
David S. Miller9360ffd2012-03-29 04:41:26 -040010451 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10452 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10453 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10454 (req_ie &&
10455 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10456 (resp_ie &&
10457 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10458 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010459
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010460 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010461
Johannes Berg68eb5502013-11-19 15:19:38 +010010462 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010463 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010464 return;
10465
10466 nla_put_failure:
10467 genlmsg_cancel(msg, hdr);
10468 nlmsg_free(msg);
10469
10470}
10471
10472void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10473 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010474 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010475{
10476 struct sk_buff *msg;
10477 void *hdr;
10478
Thomas Graf58050fc2012-06-28 03:57:45 +000010479 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010480 if (!msg)
10481 return;
10482
10483 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10484 if (!hdr) {
10485 nlmsg_free(msg);
10486 return;
10487 }
10488
David S. Miller9360ffd2012-03-29 04:41:26 -040010489 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10490 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10491 (from_ap && reason &&
10492 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10493 (from_ap &&
10494 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10495 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10496 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010497
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010498 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010499
Johannes Berg68eb5502013-11-19 15:19:38 +010010500 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010501 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010502 return;
10503
10504 nla_put_failure:
10505 genlmsg_cancel(msg, hdr);
10506 nlmsg_free(msg);
10507
10508}
10509
Johannes Berg04a773a2009-04-19 21:24:32 +020010510void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10511 struct net_device *netdev, const u8 *bssid,
10512 gfp_t gfp)
10513{
10514 struct sk_buff *msg;
10515 void *hdr;
10516
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010518 if (!msg)
10519 return;
10520
10521 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10522 if (!hdr) {
10523 nlmsg_free(msg);
10524 return;
10525 }
10526
David S. Miller9360ffd2012-03-29 04:41:26 -040010527 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10528 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10529 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10530 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010531
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010532 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010533
Johannes Berg68eb5502013-11-19 15:19:38 +010010534 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010535 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010536 return;
10537
10538 nla_put_failure:
10539 genlmsg_cancel(msg, hdr);
10540 nlmsg_free(msg);
10541}
10542
Johannes Berg947add32013-02-22 22:05:20 +010010543void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10544 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010545{
Johannes Berg947add32013-02-22 22:05:20 +010010546 struct wireless_dev *wdev = dev->ieee80211_ptr;
10547 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010548 struct sk_buff *msg;
10549 void *hdr;
10550
Johannes Berg947add32013-02-22 22:05:20 +010010551 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10552 return;
10553
10554 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10555
Javier Cardonac93b5e72011-04-07 15:08:34 -070010556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10557 if (!msg)
10558 return;
10559
10560 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10561 if (!hdr) {
10562 nlmsg_free(msg);
10563 return;
10564 }
10565
David S. Miller9360ffd2012-03-29 04:41:26 -040010566 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010567 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10568 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010569 (ie_len && ie &&
10570 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10571 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010572
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010573 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010574
Johannes Berg68eb5502013-11-19 15:19:38 +010010575 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010576 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010577 return;
10578
10579 nla_put_failure:
10580 genlmsg_cancel(msg, hdr);
10581 nlmsg_free(msg);
10582}
Johannes Berg947add32013-02-22 22:05:20 +010010583EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010584
Jouni Malinena3b8b052009-03-27 21:59:49 +020010585void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10586 struct net_device *netdev, const u8 *addr,
10587 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010588 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010589{
10590 struct sk_buff *msg;
10591 void *hdr;
10592
Johannes Berge6d6e342009-07-01 21:26:47 +020010593 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010594 if (!msg)
10595 return;
10596
10597 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10598 if (!hdr) {
10599 nlmsg_free(msg);
10600 return;
10601 }
10602
David S. Miller9360ffd2012-03-29 04:41:26 -040010603 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10604 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10605 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10606 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10607 (key_id != -1 &&
10608 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10609 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10610 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010611
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010612 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010613
Johannes Berg68eb5502013-11-19 15:19:38 +010010614 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010615 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010616 return;
10617
10618 nla_put_failure:
10619 genlmsg_cancel(msg, hdr);
10620 nlmsg_free(msg);
10621}
10622
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010623void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10624 struct ieee80211_channel *channel_before,
10625 struct ieee80211_channel *channel_after)
10626{
10627 struct sk_buff *msg;
10628 void *hdr;
10629 struct nlattr *nl_freq;
10630
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010631 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010632 if (!msg)
10633 return;
10634
10635 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10636 if (!hdr) {
10637 nlmsg_free(msg);
10638 return;
10639 }
10640
10641 /*
10642 * Since we are applying the beacon hint to a wiphy we know its
10643 * wiphy_idx is valid
10644 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010645 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10646 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010647
10648 /* Before */
10649 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10650 if (!nl_freq)
10651 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010652 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010653 goto nla_put_failure;
10654 nla_nest_end(msg, nl_freq);
10655
10656 /* After */
10657 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10658 if (!nl_freq)
10659 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010660 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010661 goto nla_put_failure;
10662 nla_nest_end(msg, nl_freq);
10663
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010664 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010665
Johannes Berg463d0182009-07-14 00:33:35 +020010666 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010667 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010668 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010669 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010670
10671 return;
10672
10673nla_put_failure:
10674 genlmsg_cancel(msg, hdr);
10675 nlmsg_free(msg);
10676}
10677
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010678static void nl80211_send_remain_on_chan_event(
10679 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010680 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010681 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010682 unsigned int duration, gfp_t gfp)
10683{
10684 struct sk_buff *msg;
10685 void *hdr;
10686
10687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10688 if (!msg)
10689 return;
10690
10691 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10692 if (!hdr) {
10693 nlmsg_free(msg);
10694 return;
10695 }
10696
David S. Miller9360ffd2012-03-29 04:41:26 -040010697 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010698 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10699 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010700 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010701 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010702 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10703 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010704 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10705 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010706
David S. Miller9360ffd2012-03-29 04:41:26 -040010707 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10708 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10709 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010710
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010711 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010712
Johannes Berg68eb5502013-11-19 15:19:38 +010010713 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010714 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010715 return;
10716
10717 nla_put_failure:
10718 genlmsg_cancel(msg, hdr);
10719 nlmsg_free(msg);
10720}
10721
Johannes Berg947add32013-02-22 22:05:20 +010010722void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10723 struct ieee80211_channel *chan,
10724 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010725{
Johannes Berg947add32013-02-22 22:05:20 +010010726 struct wiphy *wiphy = wdev->wiphy;
10727 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10728
10729 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010730 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010731 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010732 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010733}
Johannes Berg947add32013-02-22 22:05:20 +010010734EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010735
Johannes Berg947add32013-02-22 22:05:20 +010010736void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10737 struct ieee80211_channel *chan,
10738 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010739{
Johannes Berg947add32013-02-22 22:05:20 +010010740 struct wiphy *wiphy = wdev->wiphy;
10741 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10742
10743 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010744 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010745 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010746}
Johannes Berg947add32013-02-22 22:05:20 +010010747EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010748
Johannes Berg947add32013-02-22 22:05:20 +010010749void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10750 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010751{
Johannes Berg947add32013-02-22 22:05:20 +010010752 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10753 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010754 struct sk_buff *msg;
10755
Johannes Berg947add32013-02-22 22:05:20 +010010756 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10757
Thomas Graf58050fc2012-06-28 03:57:45 +000010758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010759 if (!msg)
10760 return;
10761
John W. Linville66266b32012-03-15 13:25:41 -040010762 if (nl80211_send_station(msg, 0, 0, 0,
10763 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010764 nlmsg_free(msg);
10765 return;
10766 }
10767
Johannes Berg68eb5502013-11-19 15:19:38 +010010768 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010769 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010770}
Johannes Berg947add32013-02-22 22:05:20 +010010771EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010772
Johannes Berg947add32013-02-22 22:05:20 +010010773void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010774{
Johannes Berg947add32013-02-22 22:05:20 +010010775 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10776 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010777 struct sk_buff *msg;
10778 void *hdr;
10779
Johannes Berg947add32013-02-22 22:05:20 +010010780 trace_cfg80211_del_sta(dev, mac_addr);
10781
Thomas Graf58050fc2012-06-28 03:57:45 +000010782 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010783 if (!msg)
10784 return;
10785
10786 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10787 if (!hdr) {
10788 nlmsg_free(msg);
10789 return;
10790 }
10791
David S. Miller9360ffd2012-03-29 04:41:26 -040010792 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10793 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10794 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010795
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010796 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010797
Johannes Berg68eb5502013-11-19 15:19:38 +010010798 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010799 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010800 return;
10801
10802 nla_put_failure:
10803 genlmsg_cancel(msg, hdr);
10804 nlmsg_free(msg);
10805}
Johannes Berg947add32013-02-22 22:05:20 +010010806EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010807
Johannes Berg947add32013-02-22 22:05:20 +010010808void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10809 enum nl80211_connect_failed_reason reason,
10810 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010811{
Johannes Berg947add32013-02-22 22:05:20 +010010812 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10813 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010814 struct sk_buff *msg;
10815 void *hdr;
10816
10817 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10818 if (!msg)
10819 return;
10820
10821 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10822 if (!hdr) {
10823 nlmsg_free(msg);
10824 return;
10825 }
10826
10827 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10828 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10829 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10830 goto nla_put_failure;
10831
10832 genlmsg_end(msg, hdr);
10833
Johannes Berg68eb5502013-11-19 15:19:38 +010010834 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010835 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010836 return;
10837
10838 nla_put_failure:
10839 genlmsg_cancel(msg, hdr);
10840 nlmsg_free(msg);
10841}
Johannes Berg947add32013-02-22 22:05:20 +010010842EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010843
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010844static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10845 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010846{
10847 struct wireless_dev *wdev = dev->ieee80211_ptr;
10848 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10849 struct sk_buff *msg;
10850 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010851 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010852
Eric W. Biederman15e47302012-09-07 20:12:54 +000010853 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010854 return false;
10855
10856 msg = nlmsg_new(100, gfp);
10857 if (!msg)
10858 return true;
10859
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010860 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010861 if (!hdr) {
10862 nlmsg_free(msg);
10863 return true;
10864 }
10865
David S. Miller9360ffd2012-03-29 04:41:26 -040010866 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10867 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10868 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10869 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010870
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010871 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010872 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010873 return true;
10874
10875 nla_put_failure:
10876 genlmsg_cancel(msg, hdr);
10877 nlmsg_free(msg);
10878 return true;
10879}
10880
Johannes Berg947add32013-02-22 22:05:20 +010010881bool cfg80211_rx_spurious_frame(struct net_device *dev,
10882 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010883{
Johannes Berg947add32013-02-22 22:05:20 +010010884 struct wireless_dev *wdev = dev->ieee80211_ptr;
10885 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010886
Johannes Berg947add32013-02-22 22:05:20 +010010887 trace_cfg80211_rx_spurious_frame(dev, addr);
10888
10889 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10890 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10891 trace_cfg80211_return_bool(false);
10892 return false;
10893 }
10894 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10895 addr, gfp);
10896 trace_cfg80211_return_bool(ret);
10897 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010898}
Johannes Berg947add32013-02-22 22:05:20 +010010899EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10900
10901bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10902 const u8 *addr, gfp_t gfp)
10903{
10904 struct wireless_dev *wdev = dev->ieee80211_ptr;
10905 bool ret;
10906
10907 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10908
10909 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10910 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10911 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10912 trace_cfg80211_return_bool(false);
10913 return false;
10914 }
10915 ret = __nl80211_unexpected_frame(dev,
10916 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10917 addr, gfp);
10918 trace_cfg80211_return_bool(ret);
10919 return ret;
10920}
10921EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010922
Johannes Berg2e161f72010-08-12 15:38:38 +020010923int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010924 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010925 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010926 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010927{
Johannes Berg71bbc992012-06-15 15:30:18 +020010928 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010929 struct sk_buff *msg;
10930 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010931
10932 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10933 if (!msg)
10934 return -ENOMEM;
10935
Johannes Berg2e161f72010-08-12 15:38:38 +020010936 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010937 if (!hdr) {
10938 nlmsg_free(msg);
10939 return -ENOMEM;
10940 }
10941
David S. Miller9360ffd2012-03-29 04:41:26 -040010942 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010943 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10944 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010945 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010946 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10947 (sig_dbm &&
10948 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010949 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10950 (flags &&
10951 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010952 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010953
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010954 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010955
Eric W. Biederman15e47302012-09-07 20:12:54 +000010956 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010957
10958 nla_put_failure:
10959 genlmsg_cancel(msg, hdr);
10960 nlmsg_free(msg);
10961 return -ENOBUFS;
10962}
10963
Johannes Berg947add32013-02-22 22:05:20 +010010964void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10965 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010966{
Johannes Berg947add32013-02-22 22:05:20 +010010967 struct wiphy *wiphy = wdev->wiphy;
10968 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010969 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010970 struct sk_buff *msg;
10971 void *hdr;
10972
Johannes Berg947add32013-02-22 22:05:20 +010010973 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10974
Jouni Malinen026331c2010-02-15 12:53:10 +020010975 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10976 if (!msg)
10977 return;
10978
Johannes Berg2e161f72010-08-12 15:38:38 +020010979 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010980 if (!hdr) {
10981 nlmsg_free(msg);
10982 return;
10983 }
10984
David S. Miller9360ffd2012-03-29 04:41:26 -040010985 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010986 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10987 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010988 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010989 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10990 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10991 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10992 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010993
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010994 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010995
Johannes Berg68eb5502013-11-19 15:19:38 +010010996 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010997 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010998 return;
10999
11000 nla_put_failure:
11001 genlmsg_cancel(msg, hdr);
11002 nlmsg_free(msg);
11003}
Johannes Berg947add32013-02-22 22:05:20 +010011004EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011005
Johannes Berg947add32013-02-22 22:05:20 +010011006void cfg80211_cqm_rssi_notify(struct net_device *dev,
11007 enum nl80211_cqm_rssi_threshold_event rssi_event,
11008 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011009{
Johannes Berg947add32013-02-22 22:05:20 +010011010 struct wireless_dev *wdev = dev->ieee80211_ptr;
11011 struct wiphy *wiphy = wdev->wiphy;
11012 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011013 struct sk_buff *msg;
11014 struct nlattr *pinfoattr;
11015 void *hdr;
11016
Johannes Berg947add32013-02-22 22:05:20 +010011017 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11018
Thomas Graf58050fc2012-06-28 03:57:45 +000011019 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011020 if (!msg)
11021 return;
11022
11023 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11024 if (!hdr) {
11025 nlmsg_free(msg);
11026 return;
11027 }
11028
David S. Miller9360ffd2012-03-29 04:41:26 -040011029 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011030 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011031 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011032
11033 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11034 if (!pinfoattr)
11035 goto nla_put_failure;
11036
David S. Miller9360ffd2012-03-29 04:41:26 -040011037 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11038 rssi_event))
11039 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011040
11041 nla_nest_end(msg, pinfoattr);
11042
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011043 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011044
Johannes Berg68eb5502013-11-19 15:19:38 +010011045 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011046 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011047 return;
11048
11049 nla_put_failure:
11050 genlmsg_cancel(msg, hdr);
11051 nlmsg_free(msg);
11052}
Johannes Berg947add32013-02-22 22:05:20 +010011053EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011054
Johannes Berg947add32013-02-22 22:05:20 +010011055static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11056 struct net_device *netdev, const u8 *bssid,
11057 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011058{
11059 struct sk_buff *msg;
11060 struct nlattr *rekey_attr;
11061 void *hdr;
11062
Thomas Graf58050fc2012-06-28 03:57:45 +000011063 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011064 if (!msg)
11065 return;
11066
11067 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11068 if (!hdr) {
11069 nlmsg_free(msg);
11070 return;
11071 }
11072
David S. Miller9360ffd2012-03-29 04:41:26 -040011073 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11074 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11075 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11076 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011077
11078 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11079 if (!rekey_attr)
11080 goto nla_put_failure;
11081
David S. Miller9360ffd2012-03-29 04:41:26 -040011082 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11083 NL80211_REPLAY_CTR_LEN, replay_ctr))
11084 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011085
11086 nla_nest_end(msg, rekey_attr);
11087
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011088 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011089
Johannes Berg68eb5502013-11-19 15:19:38 +010011090 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011091 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011092 return;
11093
11094 nla_put_failure:
11095 genlmsg_cancel(msg, hdr);
11096 nlmsg_free(msg);
11097}
11098
Johannes Berg947add32013-02-22 22:05:20 +010011099void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11100 const u8 *replay_ctr, gfp_t gfp)
11101{
11102 struct wireless_dev *wdev = dev->ieee80211_ptr;
11103 struct wiphy *wiphy = wdev->wiphy;
11104 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11105
11106 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11107 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11108}
11109EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11110
11111static void
11112nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11113 struct net_device *netdev, int index,
11114 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011115{
11116 struct sk_buff *msg;
11117 struct nlattr *attr;
11118 void *hdr;
11119
Thomas Graf58050fc2012-06-28 03:57:45 +000011120 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011121 if (!msg)
11122 return;
11123
11124 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11125 if (!hdr) {
11126 nlmsg_free(msg);
11127 return;
11128 }
11129
David S. Miller9360ffd2012-03-29 04:41:26 -040011130 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11131 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11132 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011133
11134 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11135 if (!attr)
11136 goto nla_put_failure;
11137
David S. Miller9360ffd2012-03-29 04:41:26 -040011138 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11139 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11140 (preauth &&
11141 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11142 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011143
11144 nla_nest_end(msg, attr);
11145
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011146 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011147
Johannes Berg68eb5502013-11-19 15:19:38 +010011148 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011149 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011150 return;
11151
11152 nla_put_failure:
11153 genlmsg_cancel(msg, hdr);
11154 nlmsg_free(msg);
11155}
11156
Johannes Berg947add32013-02-22 22:05:20 +010011157void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11158 const u8 *bssid, bool preauth, gfp_t gfp)
11159{
11160 struct wireless_dev *wdev = dev->ieee80211_ptr;
11161 struct wiphy *wiphy = wdev->wiphy;
11162 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11163
11164 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11165 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11166}
11167EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11168
11169static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11170 struct net_device *netdev,
11171 struct cfg80211_chan_def *chandef,
11172 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011173{
11174 struct sk_buff *msg;
11175 void *hdr;
11176
Thomas Graf58050fc2012-06-28 03:57:45 +000011177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011178 if (!msg)
11179 return;
11180
11181 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11182 if (!hdr) {
11183 nlmsg_free(msg);
11184 return;
11185 }
11186
Johannes Berg683b6d32012-11-08 21:25:48 +010011187 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11188 goto nla_put_failure;
11189
11190 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011191 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011192
11193 genlmsg_end(msg, hdr);
11194
Johannes Berg68eb5502013-11-19 15:19:38 +010011195 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011196 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011197 return;
11198
11199 nla_put_failure:
11200 genlmsg_cancel(msg, hdr);
11201 nlmsg_free(msg);
11202}
11203
Johannes Berg947add32013-02-22 22:05:20 +010011204void cfg80211_ch_switch_notify(struct net_device *dev,
11205 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011206{
Johannes Berg947add32013-02-22 22:05:20 +010011207 struct wireless_dev *wdev = dev->ieee80211_ptr;
11208 struct wiphy *wiphy = wdev->wiphy;
11209 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11210
Simon Wunderliche487eae2013-11-21 18:19:51 +010011211 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011212
Simon Wunderliche487eae2013-11-21 18:19:51 +010011213 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011214
11215 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011216 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011217 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11218 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011219 return;
Johannes Berg947add32013-02-22 22:05:20 +010011220
Michal Kazior9e0e2962014-01-29 14:22:27 +010011221 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011222 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011223 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011224}
11225EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11226
11227void cfg80211_cqm_txe_notify(struct net_device *dev,
11228 const u8 *peer, u32 num_packets,
11229 u32 rate, u32 intvl, gfp_t gfp)
11230{
11231 struct wireless_dev *wdev = dev->ieee80211_ptr;
11232 struct wiphy *wiphy = wdev->wiphy;
11233 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011234 struct sk_buff *msg;
11235 struct nlattr *pinfoattr;
11236 void *hdr;
11237
11238 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11239 if (!msg)
11240 return;
11241
11242 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11243 if (!hdr) {
11244 nlmsg_free(msg);
11245 return;
11246 }
11247
11248 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011249 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011250 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11251 goto nla_put_failure;
11252
11253 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11254 if (!pinfoattr)
11255 goto nla_put_failure;
11256
11257 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11258 goto nla_put_failure;
11259
11260 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11261 goto nla_put_failure;
11262
11263 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11264 goto nla_put_failure;
11265
11266 nla_nest_end(msg, pinfoattr);
11267
11268 genlmsg_end(msg, hdr);
11269
Johannes Berg68eb5502013-11-19 15:19:38 +010011270 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011271 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011272 return;
11273
11274 nla_put_failure:
11275 genlmsg_cancel(msg, hdr);
11276 nlmsg_free(msg);
11277}
Johannes Berg947add32013-02-22 22:05:20 +010011278EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011279
11280void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011281nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011282 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011283 enum nl80211_radar_event event,
11284 struct net_device *netdev, gfp_t gfp)
11285{
11286 struct sk_buff *msg;
11287 void *hdr;
11288
11289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11290 if (!msg)
11291 return;
11292
11293 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11294 if (!hdr) {
11295 nlmsg_free(msg);
11296 return;
11297 }
11298
11299 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11300 goto nla_put_failure;
11301
11302 /* NOP and radar events don't need a netdev parameter */
11303 if (netdev) {
11304 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11305
11306 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11307 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11308 goto nla_put_failure;
11309 }
11310
11311 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11312 goto nla_put_failure;
11313
11314 if (nl80211_send_chandef(msg, chandef))
11315 goto nla_put_failure;
11316
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011317 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011318
Johannes Berg68eb5502013-11-19 15:19:38 +010011319 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011320 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011321 return;
11322
11323 nla_put_failure:
11324 genlmsg_cancel(msg, hdr);
11325 nlmsg_free(msg);
11326}
11327
Johannes Berg947add32013-02-22 22:05:20 +010011328void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11329 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011330{
Johannes Berg947add32013-02-22 22:05:20 +010011331 struct wireless_dev *wdev = dev->ieee80211_ptr;
11332 struct wiphy *wiphy = wdev->wiphy;
11333 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011334 struct sk_buff *msg;
11335 struct nlattr *pinfoattr;
11336 void *hdr;
11337
Johannes Berg947add32013-02-22 22:05:20 +010011338 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11339
Thomas Graf58050fc2012-06-28 03:57:45 +000011340 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011341 if (!msg)
11342 return;
11343
11344 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11345 if (!hdr) {
11346 nlmsg_free(msg);
11347 return;
11348 }
11349
David S. Miller9360ffd2012-03-29 04:41:26 -040011350 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011351 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011352 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11353 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011354
11355 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11356 if (!pinfoattr)
11357 goto nla_put_failure;
11358
David S. Miller9360ffd2012-03-29 04:41:26 -040011359 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11360 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011361
11362 nla_nest_end(msg, pinfoattr);
11363
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011364 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011365
Johannes Berg68eb5502013-11-19 15:19:38 +010011366 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011367 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011368 return;
11369
11370 nla_put_failure:
11371 genlmsg_cancel(msg, hdr);
11372 nlmsg_free(msg);
11373}
Johannes Berg947add32013-02-22 22:05:20 +010011374EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011375
Johannes Berg7f6cf312011-11-04 11:18:15 +010011376void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11377 u64 cookie, bool acked, gfp_t gfp)
11378{
11379 struct wireless_dev *wdev = dev->ieee80211_ptr;
11380 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11381 struct sk_buff *msg;
11382 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011383
Beni Lev4ee3e062012-08-27 12:49:39 +030011384 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11385
Thomas Graf58050fc2012-06-28 03:57:45 +000011386 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011387
Johannes Berg7f6cf312011-11-04 11:18:15 +010011388 if (!msg)
11389 return;
11390
11391 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11392 if (!hdr) {
11393 nlmsg_free(msg);
11394 return;
11395 }
11396
David S. Miller9360ffd2012-03-29 04:41:26 -040011397 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11398 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11399 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11400 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11401 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11402 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011403
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011404 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011405
Johannes Berg68eb5502013-11-19 15:19:38 +010011406 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011407 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011408 return;
11409
11410 nla_put_failure:
11411 genlmsg_cancel(msg, hdr);
11412 nlmsg_free(msg);
11413}
11414EXPORT_SYMBOL(cfg80211_probe_status);
11415
Johannes Berg5e7602302011-11-04 11:18:17 +010011416void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11417 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011418 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010011419{
11420 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11421 struct sk_buff *msg;
11422 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011423 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010011424
Beni Lev4ee3e062012-08-27 12:49:39 +030011425 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11426
Ben Greear37c73b52012-10-26 14:49:25 -070011427 spin_lock_bh(&rdev->beacon_registrations_lock);
11428 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11429 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11430 if (!msg) {
11431 spin_unlock_bh(&rdev->beacon_registrations_lock);
11432 return;
11433 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011434
Ben Greear37c73b52012-10-26 14:49:25 -070011435 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11436 if (!hdr)
11437 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010011438
Ben Greear37c73b52012-10-26 14:49:25 -070011439 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11440 (freq &&
11441 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11442 (sig_dbm &&
11443 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11444 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11445 goto nla_put_failure;
11446
11447 genlmsg_end(msg, hdr);
11448
11449 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010011450 }
Ben Greear37c73b52012-10-26 14:49:25 -070011451 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011452 return;
11453
11454 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011455 spin_unlock_bh(&rdev->beacon_registrations_lock);
11456 if (hdr)
11457 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010011458 nlmsg_free(msg);
11459}
11460EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11461
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011462#ifdef CONFIG_PM
11463void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11464 struct cfg80211_wowlan_wakeup *wakeup,
11465 gfp_t gfp)
11466{
11467 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11468 struct sk_buff *msg;
11469 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011470 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011471
11472 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11473
11474 if (wakeup)
11475 size += wakeup->packet_present_len;
11476
11477 msg = nlmsg_new(size, gfp);
11478 if (!msg)
11479 return;
11480
11481 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11482 if (!hdr)
11483 goto free_msg;
11484
11485 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11486 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11487 goto free_msg;
11488
11489 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11490 wdev->netdev->ifindex))
11491 goto free_msg;
11492
11493 if (wakeup) {
11494 struct nlattr *reasons;
11495
11496 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011497 if (!reasons)
11498 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011499
11500 if (wakeup->disconnect &&
11501 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11502 goto free_msg;
11503 if (wakeup->magic_pkt &&
11504 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11505 goto free_msg;
11506 if (wakeup->gtk_rekey_failure &&
11507 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11508 goto free_msg;
11509 if (wakeup->eap_identity_req &&
11510 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11511 goto free_msg;
11512 if (wakeup->four_way_handshake &&
11513 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11514 goto free_msg;
11515 if (wakeup->rfkill_release &&
11516 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11517 goto free_msg;
11518
11519 if (wakeup->pattern_idx >= 0 &&
11520 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11521 wakeup->pattern_idx))
11522 goto free_msg;
11523
Johannes Bergae917c92013-10-25 11:05:22 +020011524 if (wakeup->tcp_match &&
11525 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11526 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011527
Johannes Bergae917c92013-10-25 11:05:22 +020011528 if (wakeup->tcp_connlost &&
11529 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11530 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011531
Johannes Bergae917c92013-10-25 11:05:22 +020011532 if (wakeup->tcp_nomoretokens &&
11533 nla_put_flag(msg,
11534 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11535 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011536
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011537 if (wakeup->packet) {
11538 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11539 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11540
11541 if (!wakeup->packet_80211) {
11542 pkt_attr =
11543 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11544 len_attr =
11545 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11546 }
11547
11548 if (wakeup->packet_len &&
11549 nla_put_u32(msg, len_attr, wakeup->packet_len))
11550 goto free_msg;
11551
11552 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11553 wakeup->packet))
11554 goto free_msg;
11555 }
11556
11557 nla_nest_end(msg, reasons);
11558 }
11559
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011560 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011561
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);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011564 return;
11565
11566 free_msg:
11567 nlmsg_free(msg);
11568}
11569EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11570#endif
11571
Jouni Malinen3475b092012-11-16 22:49:57 +020011572void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11573 enum nl80211_tdls_operation oper,
11574 u16 reason_code, gfp_t gfp)
11575{
11576 struct wireless_dev *wdev = dev->ieee80211_ptr;
11577 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11578 struct sk_buff *msg;
11579 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011580
11581 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11582 reason_code);
11583
11584 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11585 if (!msg)
11586 return;
11587
11588 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11589 if (!hdr) {
11590 nlmsg_free(msg);
11591 return;
11592 }
11593
11594 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11595 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11596 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11597 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11598 (reason_code > 0 &&
11599 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11600 goto nla_put_failure;
11601
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011602 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011603
Johannes Berg68eb5502013-11-19 15:19:38 +010011604 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011605 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011606 return;
11607
11608 nla_put_failure:
11609 genlmsg_cancel(msg, hdr);
11610 nlmsg_free(msg);
11611}
11612EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11613
Jouni Malinen026331c2010-02-15 12:53:10 +020011614static int nl80211_netlink_notify(struct notifier_block * nb,
11615 unsigned long state,
11616 void *_notify)
11617{
11618 struct netlink_notify *notify = _notify;
11619 struct cfg80211_registered_device *rdev;
11620 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011621 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011622
11623 if (state != NETLINK_URELEASE)
11624 return NOTIFY_DONE;
11625
11626 rcu_read_lock();
11627
Johannes Berg5e7602302011-11-04 11:18:17 +010011628 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011629 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011630 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011631
11632 spin_lock_bh(&rdev->beacon_registrations_lock);
11633 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11634 list) {
11635 if (reg->nlportid == notify->portid) {
11636 list_del(&reg->list);
11637 kfree(reg);
11638 break;
11639 }
11640 }
11641 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011642 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011643
11644 rcu_read_unlock();
11645
11646 return NOTIFY_DONE;
11647}
11648
11649static struct notifier_block nl80211_netlink_notifier = {
11650 .notifier_call = nl80211_netlink_notify,
11651};
11652
Jouni Malinen355199e2013-02-27 17:14:27 +020011653void cfg80211_ft_event(struct net_device *netdev,
11654 struct cfg80211_ft_event_params *ft_event)
11655{
11656 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11657 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11658 struct sk_buff *msg;
11659 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011660
11661 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11662
11663 if (!ft_event->target_ap)
11664 return;
11665
11666 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11667 if (!msg)
11668 return;
11669
11670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011671 if (!hdr)
11672 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011673
Johannes Bergae917c92013-10-25 11:05:22 +020011674 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11675 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11676 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11677 goto out;
11678
11679 if (ft_event->ies &&
11680 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11681 goto out;
11682 if (ft_event->ric_ies &&
11683 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11684 ft_event->ric_ies))
11685 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011686
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011687 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011688
Johannes Berg68eb5502013-11-19 15:19:38 +010011689 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011690 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011691 return;
11692 out:
11693 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011694}
11695EXPORT_SYMBOL(cfg80211_ft_event);
11696
Arend van Spriel5de17982013-04-18 15:49:00 +020011697void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11698{
11699 struct cfg80211_registered_device *rdev;
11700 struct sk_buff *msg;
11701 void *hdr;
11702 u32 nlportid;
11703
11704 rdev = wiphy_to_dev(wdev->wiphy);
11705 if (!rdev->crit_proto_nlportid)
11706 return;
11707
11708 nlportid = rdev->crit_proto_nlportid;
11709 rdev->crit_proto_nlportid = 0;
11710
11711 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11712 if (!msg)
11713 return;
11714
11715 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11716 if (!hdr)
11717 goto nla_put_failure;
11718
11719 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11720 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11721 goto nla_put_failure;
11722
11723 genlmsg_end(msg, hdr);
11724
11725 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11726 return;
11727
11728 nla_put_failure:
11729 if (hdr)
11730 genlmsg_cancel(msg, hdr);
11731 nlmsg_free(msg);
11732
11733}
11734EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11735
Johannes Berg348baf02014-01-24 14:06:29 +010011736void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11737{
11738 struct wiphy *wiphy = wdev->wiphy;
11739 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11740 struct sk_buff *msg;
11741 void *hdr;
11742
11743 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11744 if (!msg)
11745 return;
11746
11747 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11748 if (!hdr)
11749 goto out;
11750
11751 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11752 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11753 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11754 goto out;
11755
11756 genlmsg_end(msg, hdr);
11757
11758 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11759 NL80211_MCGRP_MLME, GFP_KERNEL);
11760 return;
11761 out:
11762 nlmsg_free(msg);
11763}
11764
Johannes Berg55682962007-09-20 13:09:35 -040011765/* initialisation/exit functions */
11766
11767int nl80211_init(void)
11768{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011769 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011770
Johannes Berg2a94fe42013-11-19 15:19:39 +010011771 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11772 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011773 if (err)
11774 return err;
11775
Jouni Malinen026331c2010-02-15 12:53:10 +020011776 err = netlink_register_notifier(&nl80211_netlink_notifier);
11777 if (err)
11778 goto err_out;
11779
Johannes Berg55682962007-09-20 13:09:35 -040011780 return 0;
11781 err_out:
11782 genl_unregister_family(&nl80211_fam);
11783 return err;
11784}
11785
11786void nl80211_exit(void)
11787{
Jouni Malinen026331c2010-02-15 12:53:10 +020011788 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011789 genl_unregister_family(&nl80211_fam);
11790}