blob: 85bc830fd7e379d46af53741b68e7d27af8fd484 [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 Berg78f22b62014-03-24 17:57:27 +0100388 [NL80211_ATTR_IFACE_SOCKET_OWNER] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400389};
390
Johannes Berge31b8212010-10-05 19:39:30 +0200391/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000392static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200393 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200394 [NL80211_KEY_IDX] = { .type = NLA_U8 },
395 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200396 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200397 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
398 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200399 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100400 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
401};
402
403/* policy for the key default flags */
404static const struct nla_policy
405nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
406 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
407 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200408};
409
Johannes Bergff1b6e62011-05-04 15:37:28 +0200410/* policy for WoWLAN attributes */
411static const struct nla_policy
412nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
413 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
416 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200417 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
418 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
420 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100421 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
422};
423
424static const struct nla_policy
425nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
426 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
427 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
428 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
429 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
430 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
433 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
434 },
435 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
436 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
437 },
438 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
439 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
440 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200441};
442
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700443/* policy for coalesce rule attributes */
444static const struct nla_policy
445nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
446 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
447 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
448 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
449};
450
Johannes Berge5497d72011-07-05 16:35:40 +0200451/* policy for GTK rekey offload attributes */
452static const struct nla_policy
453nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
454 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
455 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
456 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
457};
458
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300459static const struct nla_policy
460nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200461 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300462 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700463 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300464};
465
Johannes Berg97990a02013-04-19 01:02:55 +0200466static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
467 struct netlink_callback *cb,
468 struct cfg80211_registered_device **rdev,
469 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100470{
Johannes Berg67748892010-10-04 21:14:06 +0200471 int err;
472
Johannes Berg67748892010-10-04 21:14:06 +0200473 rtnl_lock();
474
Johannes Berg97990a02013-04-19 01:02:55 +0200475 if (!cb->args[0]) {
476 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
477 nl80211_fam.attrbuf, nl80211_fam.maxattr,
478 nl80211_policy);
479 if (err)
480 goto out_unlock;
481
482 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
483 nl80211_fam.attrbuf);
484 if (IS_ERR(*wdev)) {
485 err = PTR_ERR(*wdev);
486 goto out_unlock;
487 }
488 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200489 /* 0 is the first index - add 1 to parse only once */
490 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200491 cb->args[1] = (*wdev)->identifier;
492 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200493 /* subtract the 1 again here */
494 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200495 struct wireless_dev *tmp;
496
497 if (!wiphy) {
498 err = -ENODEV;
499 goto out_unlock;
500 }
501 *rdev = wiphy_to_dev(wiphy);
502 *wdev = NULL;
503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
505 if (tmp->identifier == cb->args[1]) {
506 *wdev = tmp;
507 break;
508 }
509 }
Johannes Berg97990a02013-04-19 01:02:55 +0200510
511 if (!*wdev) {
512 err = -ENODEV;
513 goto out_unlock;
514 }
Johannes Berg67748892010-10-04 21:14:06 +0200515 }
516
Johannes Berg67748892010-10-04 21:14:06 +0200517 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200518 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200519 rtnl_unlock();
520 return err;
521}
522
Johannes Berg97990a02013-04-19 01:02:55 +0200523static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200524{
Johannes Berg67748892010-10-04 21:14:06 +0200525 rtnl_unlock();
526}
527
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100528/* IE validation */
529static bool is_valid_ie_attr(const struct nlattr *attr)
530{
531 const u8 *pos;
532 int len;
533
534 if (!attr)
535 return true;
536
537 pos = nla_data(attr);
538 len = nla_len(attr);
539
540 while (len) {
541 u8 elemlen;
542
543 if (len < 2)
544 return false;
545 len -= 2;
546
547 elemlen = pos[1];
548 if (elemlen > len)
549 return false;
550
551 len -= elemlen;
552 pos += 2 + elemlen;
553 }
554
555 return true;
556}
557
Johannes Berg55682962007-09-20 13:09:35 -0400558/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000559static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400560 int flags, u8 cmd)
561{
562 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000563 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400564}
565
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400566static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100567 struct ieee80211_channel *chan,
568 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400569{
David S. Miller9360ffd2012-03-29 04:41:26 -0400570 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
571 chan->center_freq))
572 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400573
David S. Miller9360ffd2012-03-29 04:41:26 -0400574 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
575 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
576 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200577 if (chan->flags & IEEE80211_CHAN_NO_IR) {
578 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
579 goto nla_put_failure;
580 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
581 goto nla_put_failure;
582 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100583 if (chan->flags & IEEE80211_CHAN_RADAR) {
584 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
585 goto nla_put_failure;
586 if (large) {
587 u32 time;
588
589 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
590
591 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
592 chan->dfs_state))
593 goto nla_put_failure;
594 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
595 time))
596 goto nla_put_failure;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100597 if (nla_put_u32(msg,
598 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
599 chan->dfs_cac_ms))
600 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100601 }
602 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400603
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100604 if (large) {
605 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
606 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
607 goto nla_put_failure;
608 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
609 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
610 goto nla_put_failure;
611 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
612 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
613 goto nla_put_failure;
614 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
615 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
616 goto nla_put_failure;
David Spinadel570dbde2014-02-23 09:12:59 +0200617 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
618 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
619 goto nla_put_failure;
620 if ((chan->flags & IEEE80211_CHAN_GO_CONCURRENT) &&
621 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_GO_CONCURRENT))
622 goto nla_put_failure;
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100623 }
624
David S. Miller9360ffd2012-03-29 04:41:26 -0400625 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
626 DBM_TO_MBM(chan->max_power)))
627 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400628
629 return 0;
630
631 nla_put_failure:
632 return -ENOBUFS;
633}
634
Johannes Berg55682962007-09-20 13:09:35 -0400635/* netlink command implementations */
636
Johannes Bergb9454e82009-07-08 13:29:08 +0200637struct key_parse {
638 struct key_params p;
639 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200640 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200641 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100642 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200643};
644
645static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
646{
647 struct nlattr *tb[NL80211_KEY_MAX + 1];
648 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
649 nl80211_key_policy);
650 if (err)
651 return err;
652
653 k->def = !!tb[NL80211_KEY_DEFAULT];
654 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
655
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100656 if (k->def) {
657 k->def_uni = true;
658 k->def_multi = true;
659 }
660 if (k->defmgmt)
661 k->def_multi = true;
662
Johannes Bergb9454e82009-07-08 13:29:08 +0200663 if (tb[NL80211_KEY_IDX])
664 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
665
666 if (tb[NL80211_KEY_DATA]) {
667 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
668 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
669 }
670
671 if (tb[NL80211_KEY_SEQ]) {
672 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
673 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
674 }
675
676 if (tb[NL80211_KEY_CIPHER])
677 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
678
Johannes Berge31b8212010-10-05 19:39:30 +0200679 if (tb[NL80211_KEY_TYPE]) {
680 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
681 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
682 return -EINVAL;
683 }
684
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100685 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
686 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100687 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
688 tb[NL80211_KEY_DEFAULT_TYPES],
689 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100690 if (err)
691 return err;
692
693 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
694 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
695 }
696
Johannes Bergb9454e82009-07-08 13:29:08 +0200697 return 0;
698}
699
700static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
701{
702 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
703 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
704 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
705 }
706
707 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
708 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
709 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
710 }
711
712 if (info->attrs[NL80211_ATTR_KEY_IDX])
713 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
714
715 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
716 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
717
718 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
719 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
720
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100721 if (k->def) {
722 k->def_uni = true;
723 k->def_multi = true;
724 }
725 if (k->defmgmt)
726 k->def_multi = true;
727
Johannes Berge31b8212010-10-05 19:39:30 +0200728 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
729 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
730 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
731 return -EINVAL;
732 }
733
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
735 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
736 int err = nla_parse_nested(
737 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
738 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
739 nl80211_key_default_policy);
740 if (err)
741 return err;
742
743 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
744 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
745 }
746
Johannes Bergb9454e82009-07-08 13:29:08 +0200747 return 0;
748}
749
750static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
751{
752 int err;
753
754 memset(k, 0, sizeof(*k));
755 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200756 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200757
758 if (info->attrs[NL80211_ATTR_KEY])
759 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
760 else
761 err = nl80211_parse_key_old(info, k);
762
763 if (err)
764 return err;
765
766 if (k->def && k->defmgmt)
767 return -EINVAL;
768
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100769 if (k->defmgmt) {
770 if (k->def_uni || !k->def_multi)
771 return -EINVAL;
772 }
773
Johannes Bergb9454e82009-07-08 13:29:08 +0200774 if (k->idx != -1) {
775 if (k->defmgmt) {
776 if (k->idx < 4 || k->idx > 5)
777 return -EINVAL;
778 } else if (k->def) {
779 if (k->idx < 0 || k->idx > 3)
780 return -EINVAL;
781 } else {
782 if (k->idx < 0 || k->idx > 5)
783 return -EINVAL;
784 }
785 }
786
787 return 0;
788}
789
Johannes Bergfffd0932009-07-08 14:22:54 +0200790static struct cfg80211_cached_keys *
791nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530792 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200793{
794 struct key_parse parse;
795 struct nlattr *key;
796 struct cfg80211_cached_keys *result;
797 int rem, err, def = 0;
798
799 result = kzalloc(sizeof(*result), GFP_KERNEL);
800 if (!result)
801 return ERR_PTR(-ENOMEM);
802
803 result->def = -1;
804 result->defmgmt = -1;
805
806 nla_for_each_nested(key, keys, rem) {
807 memset(&parse, 0, sizeof(parse));
808 parse.idx = -1;
809
810 err = nl80211_parse_key_new(key, &parse);
811 if (err)
812 goto error;
813 err = -EINVAL;
814 if (!parse.p.key)
815 goto error;
816 if (parse.idx < 0 || parse.idx > 4)
817 goto error;
818 if (parse.def) {
819 if (def)
820 goto error;
821 def = 1;
822 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100823 if (!parse.def_uni || !parse.def_multi)
824 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200825 } else if (parse.defmgmt)
826 goto error;
827 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200828 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 if (err)
830 goto error;
831 result->params[parse.idx].cipher = parse.p.cipher;
832 result->params[parse.idx].key_len = parse.p.key_len;
833 result->params[parse.idx].key = result->data[parse.idx];
834 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530835
836 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
837 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
838 if (no_ht)
839 *no_ht = true;
840 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200841 }
842
843 return result;
844 error:
845 kfree(result);
846 return ERR_PTR(err);
847}
848
849static int nl80211_key_allowed(struct wireless_dev *wdev)
850{
851 ASSERT_WDEV_LOCK(wdev);
852
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 switch (wdev->iftype) {
854 case NL80211_IFTYPE_AP:
855 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200856 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700857 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200858 break;
859 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200860 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200861 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200862 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200863 return -ENOLINK;
864 break;
865 default:
866 return -EINVAL;
867 }
868
869 return 0;
870}
871
Jouni Malinen664834d2014-01-15 00:01:44 +0200872static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
873 struct nlattr *tb)
874{
875 struct ieee80211_channel *chan;
876
877 if (tb == NULL)
878 return NULL;
879 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
880 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
881 return NULL;
882 return chan;
883}
884
Johannes Berg7527a782011-05-13 10:58:57 +0200885static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
886{
887 struct nlattr *nl_modes = nla_nest_start(msg, attr);
888 int i;
889
890 if (!nl_modes)
891 goto nla_put_failure;
892
893 i = 0;
894 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400895 if ((ifmodes & 1) && nla_put_flag(msg, i))
896 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200897 ifmodes >>= 1;
898 i++;
899 }
900
901 nla_nest_end(msg, nl_modes);
902 return 0;
903
904nla_put_failure:
905 return -ENOBUFS;
906}
907
908static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100909 struct sk_buff *msg,
910 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200911{
912 struct nlattr *nl_combis;
913 int i, j;
914
915 nl_combis = nla_nest_start(msg,
916 NL80211_ATTR_INTERFACE_COMBINATIONS);
917 if (!nl_combis)
918 goto nla_put_failure;
919
920 for (i = 0; i < wiphy->n_iface_combinations; i++) {
921 const struct ieee80211_iface_combination *c;
922 struct nlattr *nl_combi, *nl_limits;
923
924 c = &wiphy->iface_combinations[i];
925
926 nl_combi = nla_nest_start(msg, i + 1);
927 if (!nl_combi)
928 goto nla_put_failure;
929
930 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
931 if (!nl_limits)
932 goto nla_put_failure;
933
934 for (j = 0; j < c->n_limits; j++) {
935 struct nlattr *nl_limit;
936
937 nl_limit = nla_nest_start(msg, j + 1);
938 if (!nl_limit)
939 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
941 c->limits[j].max))
942 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200943 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
944 c->limits[j].types))
945 goto nla_put_failure;
946 nla_nest_end(msg, nl_limit);
947 }
948
949 nla_nest_end(msg, nl_limits);
950
David S. Miller9360ffd2012-03-29 04:41:26 -0400951 if (c->beacon_int_infra_match &&
952 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
953 goto nla_put_failure;
954 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
955 c->num_different_channels) ||
956 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
957 c->max_interfaces))
958 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100959 if (large &&
960 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
961 c->radar_detect_widths))
962 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200963
964 nla_nest_end(msg, nl_combi);
965 }
966
967 nla_nest_end(msg, nl_combis);
968
969 return 0;
970nla_put_failure:
971 return -ENOBUFS;
972}
973
Johannes Berg3713b4e2013-02-14 16:19:38 +0100974#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100975static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
976 struct sk_buff *msg)
977{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200978 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100979 struct nlattr *nl_tcp;
980
981 if (!tcp)
982 return 0;
983
984 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
985 if (!nl_tcp)
986 return -ENOBUFS;
987
988 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
989 tcp->data_payload_max))
990 return -ENOBUFS;
991
992 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
993 tcp->data_payload_max))
994 return -ENOBUFS;
995
996 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
997 return -ENOBUFS;
998
999 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1000 sizeof(*tcp->tok), tcp->tok))
1001 return -ENOBUFS;
1002
1003 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1004 tcp->data_interval_max))
1005 return -ENOBUFS;
1006
1007 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1008 tcp->wake_payload_max))
1009 return -ENOBUFS;
1010
1011 nla_nest_end(msg, nl_tcp);
1012 return 0;
1013}
1014
Johannes Berg3713b4e2013-02-14 16:19:38 +01001015static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001016 struct cfg80211_registered_device *dev,
1017 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001018{
1019 struct nlattr *nl_wowlan;
1020
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 return 0;
1023
1024 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1025 if (!nl_wowlan)
1026 return -ENOBUFS;
1027
Johannes Berg964dc9e2013-06-03 17:25:34 +02001028 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001029 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001030 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001031 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001032 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001033 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001034 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001035 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001036 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001037 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001038 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001039 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001040 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001042 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001043 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1044 return -ENOBUFS;
1045
Johannes Berg964dc9e2013-06-03 17:25:34 +02001046 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001047 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001048 .max_patterns = dev->wiphy.wowlan->n_patterns,
1049 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1050 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1051 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001052 };
1053
1054 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1055 sizeof(pat), &pat))
1056 return -ENOBUFS;
1057 }
1058
Johannes Bergb56cf722013-02-20 01:02:38 +01001059 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1060 return -ENOBUFS;
1061
Johannes Berg3713b4e2013-02-14 16:19:38 +01001062 nla_nest_end(msg, nl_wowlan);
1063
1064 return 0;
1065}
1066#endif
1067
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001068static int nl80211_send_coalesce(struct sk_buff *msg,
1069 struct cfg80211_registered_device *dev)
1070{
1071 struct nl80211_coalesce_rule_support rule;
1072
1073 if (!dev->wiphy.coalesce)
1074 return 0;
1075
1076 rule.max_rules = dev->wiphy.coalesce->n_rules;
1077 rule.max_delay = dev->wiphy.coalesce->max_delay;
1078 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1079 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1080 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1081 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1082
1083 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1084 return -ENOBUFS;
1085
1086 return 0;
1087}
1088
Johannes Berg3713b4e2013-02-14 16:19:38 +01001089static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1090 struct ieee80211_supported_band *sband)
1091{
1092 struct nlattr *nl_rates, *nl_rate;
1093 struct ieee80211_rate *rate;
1094 int i;
1095
1096 /* add HT info */
1097 if (sband->ht_cap.ht_supported &&
1098 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1099 sizeof(sband->ht_cap.mcs),
1100 &sband->ht_cap.mcs) ||
1101 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1102 sband->ht_cap.cap) ||
1103 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1104 sband->ht_cap.ampdu_factor) ||
1105 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1106 sband->ht_cap.ampdu_density)))
1107 return -ENOBUFS;
1108
1109 /* add VHT info */
1110 if (sband->vht_cap.vht_supported &&
1111 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1112 sizeof(sband->vht_cap.vht_mcs),
1113 &sband->vht_cap.vht_mcs) ||
1114 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1115 sband->vht_cap.cap)))
1116 return -ENOBUFS;
1117
1118 /* add bitrates */
1119 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1120 if (!nl_rates)
1121 return -ENOBUFS;
1122
1123 for (i = 0; i < sband->n_bitrates; i++) {
1124 nl_rate = nla_nest_start(msg, i);
1125 if (!nl_rate)
1126 return -ENOBUFS;
1127
1128 rate = &sband->bitrates[i];
1129 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1130 rate->bitrate))
1131 return -ENOBUFS;
1132 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1133 nla_put_flag(msg,
1134 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1135 return -ENOBUFS;
1136
1137 nla_nest_end(msg, nl_rate);
1138 }
1139
1140 nla_nest_end(msg, nl_rates);
1141
1142 return 0;
1143}
1144
1145static int
1146nl80211_send_mgmt_stypes(struct sk_buff *msg,
1147 const struct ieee80211_txrx_stypes *mgmt_stypes)
1148{
1149 u16 stypes;
1150 struct nlattr *nl_ftypes, *nl_ifs;
1151 enum nl80211_iftype ift;
1152 int i;
1153
1154 if (!mgmt_stypes)
1155 return 0;
1156
1157 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1158 if (!nl_ifs)
1159 return -ENOBUFS;
1160
1161 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1162 nl_ftypes = nla_nest_start(msg, ift);
1163 if (!nl_ftypes)
1164 return -ENOBUFS;
1165 i = 0;
1166 stypes = mgmt_stypes[ift].tx;
1167 while (stypes) {
1168 if ((stypes & 1) &&
1169 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1170 (i << 4) | IEEE80211_FTYPE_MGMT))
1171 return -ENOBUFS;
1172 stypes >>= 1;
1173 i++;
1174 }
1175 nla_nest_end(msg, nl_ftypes);
1176 }
1177
1178 nla_nest_end(msg, nl_ifs);
1179
1180 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1181 if (!nl_ifs)
1182 return -ENOBUFS;
1183
1184 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1185 nl_ftypes = nla_nest_start(msg, ift);
1186 if (!nl_ftypes)
1187 return -ENOBUFS;
1188 i = 0;
1189 stypes = mgmt_stypes[ift].rx;
1190 while (stypes) {
1191 if ((stypes & 1) &&
1192 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1193 (i << 4) | IEEE80211_FTYPE_MGMT))
1194 return -ENOBUFS;
1195 stypes >>= 1;
1196 i++;
1197 }
1198 nla_nest_end(msg, nl_ftypes);
1199 }
1200 nla_nest_end(msg, nl_ifs);
1201
1202 return 0;
1203}
1204
Johannes Berg86e8cf92013-06-19 10:57:22 +02001205struct nl80211_dump_wiphy_state {
1206 s64 filter_wiphy;
1207 long start;
1208 long split_start, band_start, chan_start;
1209 bool split;
1210};
1211
Johannes Berg3713b4e2013-02-14 16:19:38 +01001212static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1213 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001214 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001215{
1216 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001217 struct nlattr *nl_bands, *nl_band;
1218 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001219 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001220 enum ieee80211_band band;
1221 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001222 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001223 const struct ieee80211_txrx_stypes *mgmt_stypes =
1224 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001225 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001226
Eric W. Biederman15e47302012-09-07 20:12:54 +00001227 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001228 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001229 return -ENOBUFS;
1230
Johannes Berg86e8cf92013-06-19 10:57:22 +02001231 if (WARN_ON(!state))
1232 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001233
David S. Miller9360ffd2012-03-29 04:41:26 -04001234 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001235 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1236 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001238 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001239 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001240
Johannes Berg86e8cf92013-06-19 10:57:22 +02001241 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001242 case 0:
1243 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1244 dev->wiphy.retry_short) ||
1245 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1246 dev->wiphy.retry_long) ||
1247 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1248 dev->wiphy.frag_threshold) ||
1249 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1250 dev->wiphy.rts_threshold) ||
1251 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1252 dev->wiphy.coverage_class) ||
1253 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1254 dev->wiphy.max_scan_ssids) ||
1255 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1256 dev->wiphy.max_sched_scan_ssids) ||
1257 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1258 dev->wiphy.max_scan_ie_len) ||
1259 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1260 dev->wiphy.max_sched_scan_ie_len) ||
1261 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1262 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001263 goto nla_put_failure;
1264
Johannes Berg3713b4e2013-02-14 16:19:38 +01001265 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1266 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1267 goto nla_put_failure;
1268 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1269 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1270 goto nla_put_failure;
1271 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1272 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1273 goto nla_put_failure;
1274 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1275 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1276 goto nla_put_failure;
1277 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1278 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1279 goto nla_put_failure;
1280 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1281 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001282 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001283 state->split_start++;
1284 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 break;
1286 case 1:
1287 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1288 sizeof(u32) * dev->wiphy.n_cipher_suites,
1289 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001290 goto nla_put_failure;
1291
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1293 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001294 goto nla_put_failure;
1295
Johannes Berg3713b4e2013-02-14 16:19:38 +01001296 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1297 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1298 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001299
Johannes Berg3713b4e2013-02-14 16:19:38 +01001300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1301 dev->wiphy.available_antennas_tx) ||
1302 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1303 dev->wiphy.available_antennas_rx))
1304 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001305
Johannes Berg3713b4e2013-02-14 16:19:38 +01001306 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1307 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1308 dev->wiphy.probe_resp_offload))
1309 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001310
Johannes Berg3713b4e2013-02-14 16:19:38 +01001311 if ((dev->wiphy.available_antennas_tx ||
1312 dev->wiphy.available_antennas_rx) &&
1313 dev->ops->get_antenna) {
1314 u32 tx_ant = 0, rx_ant = 0;
1315 int res;
1316 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1317 if (!res) {
1318 if (nla_put_u32(msg,
1319 NL80211_ATTR_WIPHY_ANTENNA_TX,
1320 tx_ant) ||
1321 nla_put_u32(msg,
1322 NL80211_ATTR_WIPHY_ANTENNA_RX,
1323 rx_ant))
1324 goto nla_put_failure;
1325 }
Johannes Bergee688b002008-01-24 19:38:39 +01001326 }
1327
Johannes Berg86e8cf92013-06-19 10:57:22 +02001328 state->split_start++;
1329 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330 break;
1331 case 2:
1332 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1333 dev->wiphy.interface_modes))
1334 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001335 state->split_start++;
1336 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001337 break;
1338 case 3:
1339 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1340 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001341 goto nla_put_failure;
1342
Johannes Berg86e8cf92013-06-19 10:57:22 +02001343 for (band = state->band_start;
1344 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001345 struct ieee80211_supported_band *sband;
1346
1347 sband = dev->wiphy.bands[band];
1348
1349 if (!sband)
1350 continue;
1351
1352 nl_band = nla_nest_start(msg, band);
1353 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001354 goto nla_put_failure;
1355
Johannes Berg86e8cf92013-06-19 10:57:22 +02001356 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001357 case 0:
1358 if (nl80211_send_band_rateinfo(msg, sband))
1359 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001360 state->chan_start++;
1361 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001362 break;
1363 default:
1364 /* add frequencies */
1365 nl_freqs = nla_nest_start(
1366 msg, NL80211_BAND_ATTR_FREQS);
1367 if (!nl_freqs)
1368 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001369
Johannes Berg86e8cf92013-06-19 10:57:22 +02001370 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 i < sband->n_channels;
1372 i++) {
1373 nl_freq = nla_nest_start(msg, i);
1374 if (!nl_freq)
1375 goto nla_put_failure;
1376
1377 chan = &sband->channels[i];
1378
Johannes Berg86e8cf92013-06-19 10:57:22 +02001379 if (nl80211_msg_put_channel(
1380 msg, chan,
1381 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001382 goto nla_put_failure;
1383
1384 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001385 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001386 break;
1387 }
1388 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001389 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001390 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001391 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001392 nla_nest_end(msg, nl_freqs);
1393 }
1394
1395 nla_nest_end(msg, nl_band);
1396
Johannes Berg86e8cf92013-06-19 10:57:22 +02001397 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001398 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001399 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001400 band--;
1401 break;
1402 }
Johannes Bergee688b002008-01-24 19:38:39 +01001403 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001405
Johannes Berg3713b4e2013-02-14 16:19:38 +01001406 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001407 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001408 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001409 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001410
Johannes Berg3713b4e2013-02-14 16:19:38 +01001411 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001412 if (state->band_start == 0 && state->chan_start == 0)
1413 state->split_start++;
1414 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001415 break;
1416 case 4:
1417 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1418 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001419 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001420
1421 i = 0;
1422#define CMD(op, n) \
1423 do { \
1424 if (dev->ops->op) { \
1425 i++; \
1426 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1427 goto nla_put_failure; \
1428 } \
1429 } while (0)
1430
1431 CMD(add_virtual_intf, NEW_INTERFACE);
1432 CMD(change_virtual_intf, SET_INTERFACE);
1433 CMD(add_key, NEW_KEY);
1434 CMD(start_ap, START_AP);
1435 CMD(add_station, NEW_STATION);
1436 CMD(add_mpath, NEW_MPATH);
1437 CMD(update_mesh_config, SET_MESH_CONFIG);
1438 CMD(change_bss, SET_BSS);
1439 CMD(auth, AUTHENTICATE);
1440 CMD(assoc, ASSOCIATE);
1441 CMD(deauth, DEAUTHENTICATE);
1442 CMD(disassoc, DISASSOCIATE);
1443 CMD(join_ibss, JOIN_IBSS);
1444 CMD(join_mesh, JOIN_MESH);
1445 CMD(set_pmksa, SET_PMKSA);
1446 CMD(del_pmksa, DEL_PMKSA);
1447 CMD(flush_pmksa, FLUSH_PMKSA);
1448 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1449 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1450 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1451 CMD(mgmt_tx, FRAME);
1452 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1453 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1454 i++;
1455 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1456 goto nla_put_failure;
1457 }
1458 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1459 dev->ops->join_mesh) {
1460 i++;
1461 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1462 goto nla_put_failure;
1463 }
1464 CMD(set_wds_peer, SET_WDS_PEER);
1465 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1466 CMD(tdls_mgmt, TDLS_MGMT);
1467 CMD(tdls_oper, TDLS_OPER);
1468 }
1469 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1470 CMD(sched_scan_start, START_SCHED_SCAN);
1471 CMD(probe_client, PROBE_CLIENT);
1472 CMD(set_noack_map, SET_NOACK_MAP);
1473 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1474 i++;
1475 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1476 goto nla_put_failure;
1477 }
1478 CMD(start_p2p_device, START_P2P_DEVICE);
1479 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001480 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001481 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1482 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001483 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1484 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001485 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001486 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001487
Kalle Valo4745fc02011-11-17 19:06:10 +02001488#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001490#endif
1491
Johannes Berg8fdc6212009-03-14 09:34:01 +01001492#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001493
Johannes Berg3713b4e2013-02-14 16:19:38 +01001494 if (dev->ops->connect || dev->ops->auth) {
1495 i++;
1496 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001497 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001498 }
1499
Johannes Berg3713b4e2013-02-14 16:19:38 +01001500 if (dev->ops->disconnect || dev->ops->deauth) {
1501 i++;
1502 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1503 goto nla_put_failure;
1504 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001505
Johannes Berg3713b4e2013-02-14 16:19:38 +01001506 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001507 state->split_start++;
1508 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001509 break;
1510 case 5:
1511 if (dev->ops->remain_on_channel &&
1512 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1513 nla_put_u32(msg,
1514 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1515 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001516 goto nla_put_failure;
1517
Johannes Berg3713b4e2013-02-14 16:19:38 +01001518 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1519 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1520 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001521
Johannes Berg3713b4e2013-02-14 16:19:38 +01001522 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1523 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
1525 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001526 break;
1527 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001528#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001529 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001530 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001531 state->split_start++;
1532 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 break;
1534#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001536#endif
1537 case 7:
1538 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1539 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001540 goto nla_put_failure;
1541
Johannes Berg86e8cf92013-06-19 10:57:22 +02001542 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1543 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001544 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001545
Johannes Berg86e8cf92013-06-19 10:57:22 +02001546 state->split_start++;
1547 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001548 break;
1549 case 8:
1550 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1551 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1552 dev->wiphy.ap_sme_capa))
1553 goto nla_put_failure;
1554
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001555 features = dev->wiphy.features;
1556 /*
1557 * We can only add the per-channel limit information if the
1558 * dump is split, otherwise it makes it too big. Therefore
1559 * only advertise it in that case.
1560 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001561 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001562 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1563 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001564 goto nla_put_failure;
1565
1566 if (dev->wiphy.ht_capa_mod_mask &&
1567 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1568 sizeof(*dev->wiphy.ht_capa_mod_mask),
1569 dev->wiphy.ht_capa_mod_mask))
1570 goto nla_put_failure;
1571
1572 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1573 dev->wiphy.max_acl_mac_addrs &&
1574 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1575 dev->wiphy.max_acl_mac_addrs))
1576 goto nla_put_failure;
1577
1578 /*
1579 * Any information below this point is only available to
1580 * applications that can deal with it being split. This
1581 * helps ensure that newly added capabilities don't break
1582 * older tools by overrunning their buffers.
1583 *
1584 * We still increment split_start so that in the split
1585 * case we'll continue with more data in the next round,
1586 * but break unconditionally so unsplit data stops here.
1587 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001588 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001589 break;
1590 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001591 if (dev->wiphy.extended_capabilities &&
1592 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1593 dev->wiphy.extended_capabilities_len,
1594 dev->wiphy.extended_capabilities) ||
1595 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1596 dev->wiphy.extended_capabilities_len,
1597 dev->wiphy.extended_capabilities_mask)))
1598 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001599
Johannes Bergee2aca32013-02-21 17:36:01 +01001600 if (dev->wiphy.vht_capa_mod_mask &&
1601 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1602 sizeof(*dev->wiphy.vht_capa_mod_mask),
1603 dev->wiphy.vht_capa_mod_mask))
1604 goto nla_put_failure;
1605
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001606 state->split_start++;
1607 break;
1608 case 10:
1609 if (nl80211_send_coalesce(msg, dev))
1610 goto nla_put_failure;
1611
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001612 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1613 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1614 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1615 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001616
1617 if (dev->wiphy.max_ap_assoc_sta &&
1618 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1619 dev->wiphy.max_ap_assoc_sta))
1620 goto nla_put_failure;
1621
Johannes Bergad7e7182013-11-13 13:37:47 +01001622 state->split_start++;
1623 break;
1624 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001625 if (dev->wiphy.n_vendor_commands) {
1626 const struct nl80211_vendor_cmd_info *info;
1627 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001628
Johannes Berg567ffc32013-12-18 14:43:31 +01001629 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1630 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001631 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001632
1633 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1634 info = &dev->wiphy.vendor_commands[i].info;
1635 if (nla_put(msg, i + 1, sizeof(*info), info))
1636 goto nla_put_failure;
1637 }
1638 nla_nest_end(msg, nested);
1639 }
1640
1641 if (dev->wiphy.n_vendor_events) {
1642 const struct nl80211_vendor_cmd_info *info;
1643 struct nlattr *nested;
1644
1645 nested = nla_nest_start(msg,
1646 NL80211_ATTR_VENDOR_EVENTS);
1647 if (!nested)
1648 goto nla_put_failure;
1649
1650 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1651 info = &dev->wiphy.vendor_events[i];
1652 if (nla_put(msg, i + 1, sizeof(*info), info))
1653 goto nla_put_failure;
1654 }
1655 nla_nest_end(msg, nested);
1656 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001657
Johannes Berg3713b4e2013-02-14 16:19:38 +01001658 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001659 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001660 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001661 }
Johannes Berg55682962007-09-20 13:09:35 -04001662 return genlmsg_end(msg, hdr);
1663
1664 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001665 genlmsg_cancel(msg, hdr);
1666 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001667}
1668
Johannes Berg86e8cf92013-06-19 10:57:22 +02001669static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1670 struct netlink_callback *cb,
1671 struct nl80211_dump_wiphy_state *state)
1672{
1673 struct nlattr **tb = nl80211_fam.attrbuf;
1674 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1675 tb, nl80211_fam.maxattr, nl80211_policy);
1676 /* ignore parse errors for backward compatibility */
1677 if (ret)
1678 return 0;
1679
1680 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1681 if (tb[NL80211_ATTR_WIPHY])
1682 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1683 if (tb[NL80211_ATTR_WDEV])
1684 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1685 if (tb[NL80211_ATTR_IFINDEX]) {
1686 struct net_device *netdev;
1687 struct cfg80211_registered_device *rdev;
1688 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1689
Ying Xue7f2b8562014-01-15 10:23:45 +08001690 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001691 if (!netdev)
1692 return -ENODEV;
1693 if (netdev->ieee80211_ptr) {
1694 rdev = wiphy_to_dev(
1695 netdev->ieee80211_ptr->wiphy);
1696 state->filter_wiphy = rdev->wiphy_idx;
1697 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001698 }
1699
1700 return 0;
1701}
1702
Johannes Berg55682962007-09-20 13:09:35 -04001703static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1704{
Johannes Berg645e77d2013-03-01 14:03:49 +01001705 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001706 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001707 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001708
Johannes Berg5fe231e2013-05-08 21:45:15 +02001709 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001710 if (!state) {
1711 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001712 if (!state) {
1713 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001714 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716 state->filter_wiphy = -1;
1717 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1718 if (ret) {
1719 kfree(state);
1720 rtnl_unlock();
1721 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001722 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001723 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001724 }
1725
Johannes Berg79c97e92009-07-07 03:56:12 +02001726 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001727 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1728 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001729 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001730 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001731 if (state->filter_wiphy != -1 &&
1732 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001733 continue;
1734 /* attempt to fit multiple wiphy data chunks into the skb */
1735 do {
1736 ret = nl80211_send_wiphy(dev, skb,
1737 NETLINK_CB(cb->skb).portid,
1738 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001739 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001740 if (ret < 0) {
1741 /*
1742 * If sending the wiphy data didn't fit (ENOBUFS
1743 * or EMSGSIZE returned), this SKB is still
1744 * empty (so it's not too big because another
1745 * wiphy dataset is already in the skb) and
1746 * we've not tried to adjust the dump allocation
1747 * yet ... then adjust the alloc size to be
1748 * bigger, and return 1 but with the empty skb.
1749 * This results in an empty message being RX'ed
1750 * in userspace, but that is ignored.
1751 *
1752 * We can then retry with the larger buffer.
1753 */
1754 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001755 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001756 cb->min_dump_alloc < 4096) {
1757 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001758 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001759 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001760 return 1;
1761 }
1762 idx--;
1763 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001764 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001765 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001766 break;
Johannes Berg55682962007-09-20 13:09:35 -04001767 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001768 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001769
Johannes Berg86e8cf92013-06-19 10:57:22 +02001770 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001771
1772 return skb->len;
1773}
1774
Johannes Berg86e8cf92013-06-19 10:57:22 +02001775static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1776{
1777 kfree((void *)cb->args[0]);
1778 return 0;
1779}
1780
Johannes Berg55682962007-09-20 13:09:35 -04001781static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1782{
1783 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001784 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001785 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001786
Johannes Berg645e77d2013-03-01 14:03:49 +01001787 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001788 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001789 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001790
Johannes Berg3713b4e2013-02-14 16:19:38 +01001791 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001792 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001793 nlmsg_free(msg);
1794 return -ENOBUFS;
1795 }
Johannes Berg55682962007-09-20 13:09:35 -04001796
Johannes Berg134e6372009-07-10 09:51:34 +00001797 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001798}
1799
Jouni Malinen31888482008-10-30 16:59:24 +02001800static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1801 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1802 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1803 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1804 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1805 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1806};
1807
1808static int parse_txq_params(struct nlattr *tb[],
1809 struct ieee80211_txq_params *txq_params)
1810{
Johannes Berga3304b02012-03-28 11:04:24 +02001811 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001812 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1813 !tb[NL80211_TXQ_ATTR_AIFS])
1814 return -EINVAL;
1815
Johannes Berga3304b02012-03-28 11:04:24 +02001816 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001817 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1818 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1819 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1820 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1821
Johannes Berga3304b02012-03-28 11:04:24 +02001822 if (txq_params->ac >= NL80211_NUM_ACS)
1823 return -EINVAL;
1824
Jouni Malinen31888482008-10-30 16:59:24 +02001825 return 0;
1826}
1827
Johannes Bergf444de02010-05-05 15:25:02 +02001828static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1829{
1830 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001831 * You can only set the channel explicitly for WDS interfaces,
1832 * all others have their channel managed via their respective
1833 * "establish a connection" command (connect, join, ...)
1834 *
1835 * For AP/GO and mesh mode, the channel can be set with the
1836 * channel userspace API, but is only stored and passed to the
1837 * low-level driver when the AP starts or the mesh is joined.
1838 * This is for backward compatibility, userspace can also give
1839 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001840 *
1841 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001842 * whatever else is going on, so they have their own special
1843 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001844 */
1845 return !wdev ||
1846 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001847 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001848 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1849 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001850}
1851
Johannes Berg683b6d32012-11-08 21:25:48 +01001852static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1853 struct genl_info *info,
1854 struct cfg80211_chan_def *chandef)
1855{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301856 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001857
1858 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1859 return -EINVAL;
1860
1861 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1862
1863 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001864 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1865 chandef->center_freq1 = control_freq;
1866 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001867
1868 /* Primary channel not allowed */
1869 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1870 return -EINVAL;
1871
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001872 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1873 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001874
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001875 chantype = nla_get_u32(
1876 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1877
1878 switch (chantype) {
1879 case NL80211_CHAN_NO_HT:
1880 case NL80211_CHAN_HT20:
1881 case NL80211_CHAN_HT40PLUS:
1882 case NL80211_CHAN_HT40MINUS:
1883 cfg80211_chandef_create(chandef, chandef->chan,
1884 chantype);
1885 break;
1886 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001887 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001888 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001889 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1890 chandef->width =
1891 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1892 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1893 chandef->center_freq1 =
1894 nla_get_u32(
1895 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1896 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1897 chandef->center_freq2 =
1898 nla_get_u32(
1899 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1900 }
1901
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001902 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001903 return -EINVAL;
1904
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001905 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1906 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001907 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001908
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001909 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1910 chandef->width == NL80211_CHAN_WIDTH_10) &&
1911 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1912 return -EINVAL;
1913
Johannes Berg683b6d32012-11-08 21:25:48 +01001914 return 0;
1915}
1916
Johannes Bergf444de02010-05-05 15:25:02 +02001917static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1918 struct wireless_dev *wdev,
1919 struct genl_info *info)
1920{
Johannes Berg683b6d32012-11-08 21:25:48 +01001921 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001922 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001923 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1924
1925 if (wdev)
1926 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001927
Johannes Bergf444de02010-05-05 15:25:02 +02001928 if (!nl80211_can_set_dev_channel(wdev))
1929 return -EOPNOTSUPP;
1930
Johannes Berg683b6d32012-11-08 21:25:48 +01001931 result = nl80211_parse_chandef(rdev, info, &chandef);
1932 if (result)
1933 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001934
Johannes Berge8c9bd52012-06-06 08:18:22 +02001935 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 case NL80211_IFTYPE_AP:
1937 case NL80211_IFTYPE_P2P_GO:
1938 if (wdev->beacon_interval) {
1939 result = -EBUSY;
1940 break;
1941 }
Ilan Peer174e0cd2014-02-23 09:13:01 +02001942 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, iftype)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001943 result = -EINVAL;
1944 break;
1945 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001946 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001947 result = 0;
1948 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001949 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001950 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001951 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001952 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001953 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001954 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001955 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001956 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001957 }
Johannes Bergf444de02010-05-05 15:25:02 +02001958
1959 return result;
1960}
1961
1962static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1963{
Johannes Berg4c476992010-10-04 21:36:35 +02001964 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1965 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001966
Johannes Berg4c476992010-10-04 21:36:35 +02001967 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001968}
1969
Bill Jordane8347eb2010-10-01 13:54:28 -04001970static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1971{
Johannes Berg43b19952010-10-07 13:10:30 +02001972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1973 struct net_device *dev = info->user_ptr[1];
1974 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001975 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001976
1977 if (!info->attrs[NL80211_ATTR_MAC])
1978 return -EINVAL;
1979
Johannes Berg43b19952010-10-07 13:10:30 +02001980 if (netif_running(dev))
1981 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001982
Johannes Berg43b19952010-10-07 13:10:30 +02001983 if (!rdev->ops->set_wds_peer)
1984 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001985
Johannes Berg43b19952010-10-07 13:10:30 +02001986 if (wdev->iftype != NL80211_IFTYPE_WDS)
1987 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001988
1989 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001990 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001991}
1992
1993
Johannes Berg55682962007-09-20 13:09:35 -04001994static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1995{
1996 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001997 struct net_device *netdev = NULL;
1998 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001999 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02002000 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002001 u32 changed;
2002 u8 retry_short = 0, retry_long = 0;
2003 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01002004 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04002005
Johannes Berg5fe231e2013-05-08 21:45:15 +02002006 ASSERT_RTNL();
2007
Johannes Bergf444de02010-05-05 15:25:02 +02002008 /*
2009 * Try to find the wiphy and netdev. Normally this
2010 * function shouldn't need the netdev, but this is
2011 * done for backward compatibility -- previously
2012 * setting the channel was done per wiphy, but now
2013 * it is per netdev. Previous userland like hostapd
2014 * also passed a netdev to set_wiphy, so that it is
2015 * possible to let that go to the right netdev!
2016 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002017
Johannes Bergf444de02010-05-05 15:25:02 +02002018 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2019 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2020
Ying Xue7f2b8562014-01-15 10:23:45 +08002021 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002022 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002023 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002024 else
Johannes Bergf444de02010-05-05 15:25:02 +02002025 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002026 }
2027
Johannes Bergf444de02010-05-05 15:25:02 +02002028 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002029 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2030 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002031 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002032 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002033 wdev = NULL;
2034 netdev = NULL;
2035 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002036 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002037 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002038
2039 /*
2040 * end workaround code, by now the rdev is available
2041 * and locked, and wdev may or may not be NULL.
2042 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002043
2044 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002045 result = cfg80211_dev_rename(
2046 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002047
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002048 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002049 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002050
Jouni Malinen31888482008-10-30 16:59:24 +02002051 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2052 struct ieee80211_txq_params txq_params;
2053 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2054
Ying Xue7f2b8562014-01-15 10:23:45 +08002055 if (!rdev->ops->set_txq_params)
2056 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002057
Ying Xue7f2b8562014-01-15 10:23:45 +08002058 if (!netdev)
2059 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002060
Johannes Berg133a3ff2011-11-03 14:50:13 +01002061 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002062 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2063 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002064
Ying Xue7f2b8562014-01-15 10:23:45 +08002065 if (!netif_running(netdev))
2066 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002067
Jouni Malinen31888482008-10-30 16:59:24 +02002068 nla_for_each_nested(nl_txq_params,
2069 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2070 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002071 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2072 nla_data(nl_txq_params),
2073 nla_len(nl_txq_params),
2074 txq_params_policy);
2075 if (result)
2076 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002077 result = parse_txq_params(tb, &txq_params);
2078 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002079 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002080
Hila Gonene35e4d22012-06-27 17:19:42 +03002081 result = rdev_set_txq_params(rdev, netdev,
2082 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002083 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002084 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002085 }
2086 }
2087
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002088 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002089 result = __nl80211_set_channel(rdev,
2090 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2091 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002092 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002094 }
2095
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002096 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002097 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002098 enum nl80211_tx_power_setting type;
2099 int idx, mbm = 0;
2100
Johannes Bergc8442112012-10-24 10:17:18 +02002101 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2102 txp_wdev = NULL;
2103
Ying Xue7f2b8562014-01-15 10:23:45 +08002104 if (!rdev->ops->set_tx_power)
2105 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002106
2107 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2108 type = nla_get_u32(info->attrs[idx]);
2109
2110 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002111 (type != NL80211_TX_POWER_AUTOMATIC))
2112 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002113
2114 if (type != NL80211_TX_POWER_AUTOMATIC) {
2115 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2116 mbm = nla_get_u32(info->attrs[idx]);
2117 }
2118
Johannes Bergc8442112012-10-24 10:17:18 +02002119 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002120 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002121 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002122 }
2123
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002124 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2125 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2126 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002127 if ((!rdev->wiphy.available_antennas_tx &&
2128 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002129 !rdev->ops->set_antenna)
2130 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002131
2132 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2133 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2134
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002135 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002136 * available antenna masks, except for the "all" mask */
2137 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002138 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2139 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002140
Bruno Randolf7f531e02010-12-16 11:30:22 +09002141 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2142 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002143
Hila Gonene35e4d22012-06-27 17:19:42 +03002144 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002145 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002146 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002147 }
2148
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002149 changed = 0;
2150
2151 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2152 retry_short = nla_get_u8(
2153 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002154 if (retry_short == 0)
2155 return -EINVAL;
2156
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002157 changed |= WIPHY_PARAM_RETRY_SHORT;
2158 }
2159
2160 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2161 retry_long = nla_get_u8(
2162 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002163 if (retry_long == 0)
2164 return -EINVAL;
2165
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002166 changed |= WIPHY_PARAM_RETRY_LONG;
2167 }
2168
2169 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2170 frag_threshold = nla_get_u32(
2171 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002172 if (frag_threshold < 256)
2173 return -EINVAL;
2174
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002175 if (frag_threshold != (u32) -1) {
2176 /*
2177 * Fragments (apart from the last one) are required to
2178 * have even length. Make the fragmentation code
2179 * simpler by stripping LSB should someone try to use
2180 * odd threshold value.
2181 */
2182 frag_threshold &= ~0x1;
2183 }
2184 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2185 }
2186
2187 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2188 rts_threshold = nla_get_u32(
2189 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2190 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2191 }
2192
Lukáš Turek81077e82009-12-21 22:50:47 +01002193 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2194 coverage_class = nla_get_u8(
2195 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2196 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2197 }
2198
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002199 if (changed) {
2200 u8 old_retry_short, old_retry_long;
2201 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002202 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002203
Ying Xue7f2b8562014-01-15 10:23:45 +08002204 if (!rdev->ops->set_wiphy_params)
2205 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002206
2207 old_retry_short = rdev->wiphy.retry_short;
2208 old_retry_long = rdev->wiphy.retry_long;
2209 old_frag_threshold = rdev->wiphy.frag_threshold;
2210 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002211 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002212
2213 if (changed & WIPHY_PARAM_RETRY_SHORT)
2214 rdev->wiphy.retry_short = retry_short;
2215 if (changed & WIPHY_PARAM_RETRY_LONG)
2216 rdev->wiphy.retry_long = retry_long;
2217 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2218 rdev->wiphy.frag_threshold = frag_threshold;
2219 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2220 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002221 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2222 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002223
Hila Gonene35e4d22012-06-27 17:19:42 +03002224 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002225 if (result) {
2226 rdev->wiphy.retry_short = old_retry_short;
2227 rdev->wiphy.retry_long = old_retry_long;
2228 rdev->wiphy.frag_threshold = old_frag_threshold;
2229 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002230 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002231 }
2232 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002233 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002234}
2235
Johannes Berg71bbc992012-06-15 15:30:18 +02002236static inline u64 wdev_id(struct wireless_dev *wdev)
2237{
2238 return (u64)wdev->identifier |
2239 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2240}
Johannes Berg55682962007-09-20 13:09:35 -04002241
Johannes Berg683b6d32012-11-08 21:25:48 +01002242static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002243 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002244{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002245 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002246
Johannes Berg683b6d32012-11-08 21:25:48 +01002247 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2248 chandef->chan->center_freq))
2249 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002250 switch (chandef->width) {
2251 case NL80211_CHAN_WIDTH_20_NOHT:
2252 case NL80211_CHAN_WIDTH_20:
2253 case NL80211_CHAN_WIDTH_40:
2254 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2255 cfg80211_get_chandef_type(chandef)))
2256 return -ENOBUFS;
2257 break;
2258 default:
2259 break;
2260 }
2261 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2262 return -ENOBUFS;
2263 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2264 return -ENOBUFS;
2265 if (chandef->center_freq2 &&
2266 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002267 return -ENOBUFS;
2268 return 0;
2269}
2270
Eric W. Biederman15e47302012-09-07 20:12:54 +00002271static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002272 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002273 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002274{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002275 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002276 void *hdr;
2277
Eric W. Biederman15e47302012-09-07 20:12:54 +00002278 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002279 if (!hdr)
2280 return -1;
2281
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002282 if (dev &&
2283 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002284 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002285 goto nla_put_failure;
2286
2287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2288 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002289 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002290 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002291 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2292 rdev->devlist_generation ^
2293 (cfg80211_rdev_list_generation << 2)))
2294 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002295
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002296 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002297 int ret;
2298 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002299
Johannes Berg683b6d32012-11-08 21:25:48 +01002300 ret = rdev_get_channel(rdev, wdev, &chandef);
2301 if (ret == 0) {
2302 if (nl80211_send_chandef(msg, &chandef))
2303 goto nla_put_failure;
2304 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002305 }
2306
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002307 if (wdev->ssid_len) {
2308 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2309 goto nla_put_failure;
2310 }
2311
Johannes Berg55682962007-09-20 13:09:35 -04002312 return genlmsg_end(msg, hdr);
2313
2314 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002315 genlmsg_cancel(msg, hdr);
2316 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002317}
2318
2319static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2320{
2321 int wp_idx = 0;
2322 int if_idx = 0;
2323 int wp_start = cb->args[0];
2324 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002325 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002326 struct wireless_dev *wdev;
2327
Johannes Berg5fe231e2013-05-08 21:45:15 +02002328 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002329 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2330 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002331 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002332 if (wp_idx < wp_start) {
2333 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002334 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 }
Johannes Berg55682962007-09-20 13:09:35 -04002336 if_idx = 0;
2337
Johannes Berg89a54e42012-06-15 14:33:17 +02002338 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339 if (if_idx < if_start) {
2340 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002341 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002343 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002344 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002345 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002346 goto out;
2347 }
2348 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002349 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002350
2351 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002352 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002353 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002354 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002355
2356 cb->args[0] = wp_idx;
2357 cb->args[1] = if_idx;
2358
2359 return skb->len;
2360}
2361
2362static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2363{
2364 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002365 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002366 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002367
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002369 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002370 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002371
Eric W. Biederman15e47302012-09-07 20:12:54 +00002372 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002373 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002374 nlmsg_free(msg);
2375 return -ENOBUFS;
2376 }
Johannes Berg55682962007-09-20 13:09:35 -04002377
Johannes Berg134e6372009-07-10 09:51:34 +00002378 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002379}
2380
Michael Wu66f7ac52008-01-31 19:48:22 +01002381static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2382 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2383 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2384 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2385 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2386 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002387 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002388};
2389
2390static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2391{
2392 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2393 int flag;
2394
2395 *mntrflags = 0;
2396
2397 if (!nla)
2398 return -EINVAL;
2399
2400 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2401 nla, mntr_flags_policy))
2402 return -EINVAL;
2403
2404 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2405 if (flags[flag])
2406 *mntrflags |= (1<<flag);
2407
2408 return 0;
2409}
2410
Johannes Berg9bc383d2009-11-19 11:55:19 +01002411static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002412 struct net_device *netdev, u8 use_4addr,
2413 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002414{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002415 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002416 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002417 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002418 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002419 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002420
2421 switch (iftype) {
2422 case NL80211_IFTYPE_AP_VLAN:
2423 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2424 return 0;
2425 break;
2426 case NL80211_IFTYPE_STATION:
2427 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2428 return 0;
2429 break;
2430 default:
2431 break;
2432 }
2433
2434 return -EOPNOTSUPP;
2435}
2436
Johannes Berg55682962007-09-20 13:09:35 -04002437static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2438{
Johannes Berg4c476992010-10-04 21:36:35 +02002439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002440 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002441 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002443 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002444 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002445 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002446
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002447 memset(&params, 0, sizeof(params));
2448
Johannes Berg04a773a2009-04-19 21:24:32 +02002449 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002450
Johannes Berg723b0382008-09-16 20:22:09 +02002451 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002452 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002453 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002454 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002455 if (ntype > NL80211_IFTYPE_MAX)
2456 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002457 }
2458
Johannes Berg92ffe052008-09-16 20:39:36 +02002459 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002460 struct wireless_dev *wdev = dev->ieee80211_ptr;
2461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 if (ntype != NL80211_IFTYPE_MESH_POINT)
2463 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002464 if (netif_running(dev))
2465 return -EBUSY;
2466
2467 wdev_lock(wdev);
2468 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2469 IEEE80211_MAX_MESH_ID_LEN);
2470 wdev->mesh_id_up_len =
2471 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2472 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2473 wdev->mesh_id_up_len);
2474 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 }
2476
Felix Fietkau8b787642009-11-10 18:53:10 +01002477 if (info->attrs[NL80211_ATTR_4ADDR]) {
2478 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2479 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002480 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002483 } else {
2484 params.use_4addr = -1;
2485 }
2486
Johannes Berg92ffe052008-09-16 20:39:36 +02002487 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002488 if (ntype != NL80211_IFTYPE_MONITOR)
2489 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002490 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2491 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002492 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002493 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002494
2495 flags = &_flags;
2496 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002497 }
Johannes Berg3b858752009-03-12 09:55:09 +01002498
Luciano Coelho18003292013-08-29 13:26:57 +03002499 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002500 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2501 return -EOPNOTSUPP;
2502
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002503 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002504 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002505 else
2506 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002507
Johannes Berg9bc383d2009-11-19 11:55:19 +01002508 if (!err && params.use_4addr != -1)
2509 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2510
Johannes Berg55682962007-09-20 13:09:35 -04002511 return err;
2512}
2513
2514static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2515{
Johannes Berg4c476992010-10-04 21:36:35 +02002516 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002517 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002518 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002519 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002520 int err;
2521 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002522 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002523
Johannes Berg78f22b62014-03-24 17:57:27 +01002524 /* to avoid failing a new interface creation due to pending removal */
2525 cfg80211_destroy_ifaces(rdev);
2526
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002527 memset(&params, 0, sizeof(params));
2528
Johannes Berg55682962007-09-20 13:09:35 -04002529 if (!info->attrs[NL80211_ATTR_IFNAME])
2530 return -EINVAL;
2531
2532 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2533 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2534 if (type > NL80211_IFTYPE_MAX)
2535 return -EINVAL;
2536 }
2537
Johannes Berg79c97e92009-07-07 03:56:12 +02002538 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002539 !(rdev->wiphy.interface_modes & (1 << type)))
2540 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002541
Arend van Spriel1c18f142013-01-08 10:17:27 +01002542 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2543 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2544 ETH_ALEN);
2545 if (!is_valid_ether_addr(params.macaddr))
2546 return -EADDRNOTAVAIL;
2547 }
2548
Johannes Berg9bc383d2009-11-19 11:55:19 +01002549 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002550 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002551 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002552 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002553 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002554 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002555
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2557 if (!msg)
2558 return -ENOMEM;
2559
Michael Wu66f7ac52008-01-31 19:48:22 +01002560 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2561 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2562 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002563
Luciano Coelho18003292013-08-29 13:26:57 +03002564 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002565 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2566 return -EOPNOTSUPP;
2567
Hila Gonene35e4d22012-06-27 17:19:42 +03002568 wdev = rdev_add_virtual_intf(rdev,
2569 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2570 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002571 if (IS_ERR(wdev)) {
2572 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002573 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002574 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002575
Johannes Berg78f22b62014-03-24 17:57:27 +01002576 if (info->attrs[NL80211_ATTR_IFACE_SOCKET_OWNER])
2577 wdev->owner_nlportid = info->snd_portid;
2578
Johannes Berg98104fde2012-06-16 00:19:54 +02002579 switch (type) {
2580 case NL80211_IFTYPE_MESH_POINT:
2581 if (!info->attrs[NL80211_ATTR_MESH_ID])
2582 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002583 wdev_lock(wdev);
2584 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2585 IEEE80211_MAX_MESH_ID_LEN);
2586 wdev->mesh_id_up_len =
2587 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2588 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2589 wdev->mesh_id_up_len);
2590 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002591 break;
2592 case NL80211_IFTYPE_P2P_DEVICE:
2593 /*
2594 * P2P Device doesn't have a netdev, so doesn't go
2595 * through the netdev notifier and must be added here
2596 */
2597 mutex_init(&wdev->mtx);
2598 INIT_LIST_HEAD(&wdev->event_list);
2599 spin_lock_init(&wdev->event_lock);
2600 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2601 spin_lock_init(&wdev->mgmt_registrations_lock);
2602
Johannes Berg98104fde2012-06-16 00:19:54 +02002603 wdev->identifier = ++rdev->wdev_id;
2604 list_add_rcu(&wdev->list, &rdev->wdev_list);
2605 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002606 break;
2607 default:
2608 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002609 }
2610
Eric W. Biederman15e47302012-09-07 20:12:54 +00002611 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002612 rdev, wdev) < 0) {
2613 nlmsg_free(msg);
2614 return -ENOBUFS;
2615 }
2616
2617 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002618}
2619
2620static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2621{
Johannes Berg4c476992010-10-04 21:36:35 +02002622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002623 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002624
Johannes Berg4c476992010-10-04 21:36:35 +02002625 if (!rdev->ops->del_virtual_intf)
2626 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002627
Johannes Berg84efbb82012-06-16 00:00:26 +02002628 /*
2629 * If we remove a wireless device without a netdev then clear
2630 * user_ptr[1] so that nl80211_post_doit won't dereference it
2631 * to check if it needs to do dev_put(). Otherwise it crashes
2632 * since the wdev has been freed, unlike with a netdev where
2633 * we need the dev_put() for the netdev to really be freed.
2634 */
2635 if (!wdev->netdev)
2636 info->user_ptr[1] = NULL;
2637
Hila Gonene35e4d22012-06-27 17:19:42 +03002638 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002639}
2640
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002641static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2642{
2643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2644 struct net_device *dev = info->user_ptr[1];
2645 u16 noack_map;
2646
2647 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2648 return -EINVAL;
2649
2650 if (!rdev->ops->set_noack_map)
2651 return -EOPNOTSUPP;
2652
2653 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2654
Hila Gonene35e4d22012-06-27 17:19:42 +03002655 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002656}
2657
Johannes Berg41ade002007-12-19 02:03:29 +01002658struct get_key_cookie {
2659 struct sk_buff *msg;
2660 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002661 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002662};
2663
2664static void get_key_callback(void *c, struct key_params *params)
2665{
Johannes Bergb9454e82009-07-08 13:29:08 +02002666 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002667 struct get_key_cookie *cookie = c;
2668
David S. Miller9360ffd2012-03-29 04:41:26 -04002669 if ((params->key &&
2670 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2671 params->key_len, params->key)) ||
2672 (params->seq &&
2673 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2674 params->seq_len, params->seq)) ||
2675 (params->cipher &&
2676 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2677 params->cipher)))
2678 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002679
Johannes Bergb9454e82009-07-08 13:29:08 +02002680 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2681 if (!key)
2682 goto nla_put_failure;
2683
David S. Miller9360ffd2012-03-29 04:41:26 -04002684 if ((params->key &&
2685 nla_put(cookie->msg, NL80211_KEY_DATA,
2686 params->key_len, params->key)) ||
2687 (params->seq &&
2688 nla_put(cookie->msg, NL80211_KEY_SEQ,
2689 params->seq_len, params->seq)) ||
2690 (params->cipher &&
2691 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2692 params->cipher)))
2693 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002694
David S. Miller9360ffd2012-03-29 04:41:26 -04002695 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2696 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002697
2698 nla_nest_end(cookie->msg, key);
2699
Johannes Berg41ade002007-12-19 02:03:29 +01002700 return;
2701 nla_put_failure:
2702 cookie->error = 1;
2703}
2704
2705static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2706{
Johannes Berg4c476992010-10-04 21:36:35 +02002707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002708 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002709 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002710 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002711 const u8 *mac_addr = NULL;
2712 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002713 struct get_key_cookie cookie = {
2714 .error = 0,
2715 };
2716 void *hdr;
2717 struct sk_buff *msg;
2718
2719 if (info->attrs[NL80211_ATTR_KEY_IDX])
2720 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2721
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002722 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002723 return -EINVAL;
2724
2725 if (info->attrs[NL80211_ATTR_MAC])
2726 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2727
Johannes Berge31b8212010-10-05 19:39:30 +02002728 pairwise = !!mac_addr;
2729 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2730 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2731 if (kt >= NUM_NL80211_KEYTYPES)
2732 return -EINVAL;
2733 if (kt != NL80211_KEYTYPE_GROUP &&
2734 kt != NL80211_KEYTYPE_PAIRWISE)
2735 return -EINVAL;
2736 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2737 }
2738
Johannes Berg4c476992010-10-04 21:36:35 +02002739 if (!rdev->ops->get_key)
2740 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002743 if (!msg)
2744 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002745
Eric W. Biederman15e47302012-09-07 20:12:54 +00002746 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002747 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002748 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002749 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002752 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002753
David S. Miller9360ffd2012-03-29 04:41:26 -04002754 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2755 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2756 goto nla_put_failure;
2757 if (mac_addr &&
2758 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2759 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002760
Johannes Berge31b8212010-10-05 19:39:30 +02002761 if (pairwise && mac_addr &&
2762 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2763 return -ENOENT;
2764
Hila Gonene35e4d22012-06-27 17:19:42 +03002765 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2766 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002767
2768 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002769 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002770
2771 if (cookie.error)
2772 goto nla_put_failure;
2773
2774 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002775 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002776
2777 nla_put_failure:
2778 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002779 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002780 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002781 return err;
2782}
2783
2784static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2785{
Johannes Berg4c476992010-10-04 21:36:35 +02002786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002787 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002788 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002789 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Bergb9454e82009-07-08 13:29:08 +02002791 err = nl80211_parse_key(info, &key);
2792 if (err)
2793 return err;
2794
2795 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002796 return -EINVAL;
2797
Johannes Bergb9454e82009-07-08 13:29:08 +02002798 /* only support setting default key */
2799 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002800 return -EINVAL;
2801
Johannes Bergfffd0932009-07-08 14:22:54 +02002802 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002803
2804 if (key.def) {
2805 if (!rdev->ops->set_default_key) {
2806 err = -EOPNOTSUPP;
2807 goto out;
2808 }
2809
2810 err = nl80211_key_allowed(dev->ieee80211_ptr);
2811 if (err)
2812 goto out;
2813
Hila Gonene35e4d22012-06-27 17:19:42 +03002814 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002815 key.def_uni, key.def_multi);
2816
2817 if (err)
2818 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002819
Johannes Berg3d23e342009-09-29 23:27:28 +02002820#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002821 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002822#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002823 } else {
2824 if (key.def_uni || !key.def_multi) {
2825 err = -EINVAL;
2826 goto out;
2827 }
2828
2829 if (!rdev->ops->set_default_mgmt_key) {
2830 err = -EOPNOTSUPP;
2831 goto out;
2832 }
2833
2834 err = nl80211_key_allowed(dev->ieee80211_ptr);
2835 if (err)
2836 goto out;
2837
Hila Gonene35e4d22012-06-27 17:19:42 +03002838 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002839 if (err)
2840 goto out;
2841
2842#ifdef CONFIG_CFG80211_WEXT
2843 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2844#endif
2845 }
2846
2847 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002848 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002849
Johannes Berg41ade002007-12-19 02:03:29 +01002850 return err;
2851}
2852
2853static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2854{
Johannes Berg4c476992010-10-04 21:36:35 +02002855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002856 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002857 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002858 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002859 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002860
Johannes Bergb9454e82009-07-08 13:29:08 +02002861 err = nl80211_parse_key(info, &key);
2862 if (err)
2863 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002864
Johannes Bergb9454e82009-07-08 13:29:08 +02002865 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002866 return -EINVAL;
2867
Johannes Berg41ade002007-12-19 02:03:29 +01002868 if (info->attrs[NL80211_ATTR_MAC])
2869 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2870
Johannes Berge31b8212010-10-05 19:39:30 +02002871 if (key.type == -1) {
2872 if (mac_addr)
2873 key.type = NL80211_KEYTYPE_PAIRWISE;
2874 else
2875 key.type = NL80211_KEYTYPE_GROUP;
2876 }
2877
2878 /* for now */
2879 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2880 key.type != NL80211_KEYTYPE_GROUP)
2881 return -EINVAL;
2882
Johannes Berg4c476992010-10-04 21:36:35 +02002883 if (!rdev->ops->add_key)
2884 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002885
Johannes Berge31b8212010-10-05 19:39:30 +02002886 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2887 key.type == NL80211_KEYTYPE_PAIRWISE,
2888 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002889 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002890
2891 wdev_lock(dev->ieee80211_ptr);
2892 err = nl80211_key_allowed(dev->ieee80211_ptr);
2893 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002894 err = rdev_add_key(rdev, dev, key.idx,
2895 key.type == NL80211_KEYTYPE_PAIRWISE,
2896 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002897 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002898
Johannes Berg41ade002007-12-19 02:03:29 +01002899 return err;
2900}
2901
2902static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2903{
Johannes Berg4c476992010-10-04 21:36:35 +02002904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002905 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002906 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002907 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002908 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002909
Johannes Bergb9454e82009-07-08 13:29:08 +02002910 err = nl80211_parse_key(info, &key);
2911 if (err)
2912 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002913
2914 if (info->attrs[NL80211_ATTR_MAC])
2915 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2916
Johannes Berge31b8212010-10-05 19:39:30 +02002917 if (key.type == -1) {
2918 if (mac_addr)
2919 key.type = NL80211_KEYTYPE_PAIRWISE;
2920 else
2921 key.type = NL80211_KEYTYPE_GROUP;
2922 }
2923
2924 /* for now */
2925 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2926 key.type != NL80211_KEYTYPE_GROUP)
2927 return -EINVAL;
2928
Johannes Berg4c476992010-10-04 21:36:35 +02002929 if (!rdev->ops->del_key)
2930 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002931
Johannes Bergfffd0932009-07-08 14:22:54 +02002932 wdev_lock(dev->ieee80211_ptr);
2933 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002934
2935 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2936 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2937 err = -ENOENT;
2938
Johannes Bergfffd0932009-07-08 14:22:54 +02002939 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002940 err = rdev_del_key(rdev, dev, key.idx,
2941 key.type == NL80211_KEYTYPE_PAIRWISE,
2942 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002943
Johannes Berg3d23e342009-09-29 23:27:28 +02002944#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002945 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002946 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002947 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002948 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002949 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2950 }
2951#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002952 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002953
Johannes Berg41ade002007-12-19 02:03:29 +01002954 return err;
2955}
2956
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302957/* This function returns an error or the number of nested attributes */
2958static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2959{
2960 struct nlattr *attr;
2961 int n_entries = 0, tmp;
2962
2963 nla_for_each_nested(attr, nl_attr, tmp) {
2964 if (nla_len(attr) != ETH_ALEN)
2965 return -EINVAL;
2966
2967 n_entries++;
2968 }
2969
2970 return n_entries;
2971}
2972
2973/*
2974 * This function parses ACL information and allocates memory for ACL data.
2975 * On successful return, the calling function is responsible to free the
2976 * ACL buffer returned by this function.
2977 */
2978static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2979 struct genl_info *info)
2980{
2981 enum nl80211_acl_policy acl_policy;
2982 struct nlattr *attr;
2983 struct cfg80211_acl_data *acl;
2984 int i = 0, n_entries, tmp;
2985
2986 if (!wiphy->max_acl_mac_addrs)
2987 return ERR_PTR(-EOPNOTSUPP);
2988
2989 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2990 return ERR_PTR(-EINVAL);
2991
2992 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2993 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2994 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2995 return ERR_PTR(-EINVAL);
2996
2997 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2998 return ERR_PTR(-EINVAL);
2999
3000 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3001 if (n_entries < 0)
3002 return ERR_PTR(n_entries);
3003
3004 if (n_entries > wiphy->max_acl_mac_addrs)
3005 return ERR_PTR(-ENOTSUPP);
3006
3007 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3008 GFP_KERNEL);
3009 if (!acl)
3010 return ERR_PTR(-ENOMEM);
3011
3012 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3013 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3014 i++;
3015 }
3016
3017 acl->n_acl_entries = n_entries;
3018 acl->acl_policy = acl_policy;
3019
3020 return acl;
3021}
3022
3023static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3024{
3025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3026 struct net_device *dev = info->user_ptr[1];
3027 struct cfg80211_acl_data *acl;
3028 int err;
3029
3030 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3031 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3032 return -EOPNOTSUPP;
3033
3034 if (!dev->ieee80211_ptr->beacon_interval)
3035 return -EINVAL;
3036
3037 acl = parse_acl_data(&rdev->wiphy, info);
3038 if (IS_ERR(acl))
3039 return PTR_ERR(acl);
3040
3041 err = rdev_set_mac_acl(rdev, dev, acl);
3042
3043 kfree(acl);
3044
3045 return err;
3046}
3047
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003048static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003049 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003050{
Johannes Berg88600202012-02-13 15:17:18 +01003051 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003052
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003053 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3054 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3055 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3056 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003057 return -EINVAL;
3058
Johannes Berg88600202012-02-13 15:17:18 +01003059 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003061 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3062 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3063 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003064 if (!bcn->head_len)
3065 return -EINVAL;
3066 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003067 }
3068
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003069 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3070 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3071 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003072 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003073 }
3074
Johannes Berg4c476992010-10-04 21:36:35 +02003075 if (!haveinfo)
3076 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003077
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003078 if (attrs[NL80211_ATTR_IE]) {
3079 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3080 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003081 }
3082
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003083 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003084 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003085 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003086 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003087 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003088 }
3089
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003090 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003091 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003092 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003093 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003094 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003095 }
3096
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003097 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3098 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3099 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003100 }
3101
Johannes Berg88600202012-02-13 15:17:18 +01003102 return 0;
3103}
3104
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003105static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3106 struct cfg80211_ap_settings *params)
3107{
3108 struct wireless_dev *wdev;
3109 bool ret = false;
3110
Johannes Berg89a54e42012-06-15 14:33:17 +02003111 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003112 if (wdev->iftype != NL80211_IFTYPE_AP &&
3113 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3114 continue;
3115
Johannes Berg683b6d32012-11-08 21:25:48 +01003116 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003117 continue;
3118
Johannes Berg683b6d32012-11-08 21:25:48 +01003119 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003120 ret = true;
3121 break;
3122 }
3123
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003124 return ret;
3125}
3126
Jouni Malinene39e5b52012-09-30 19:29:39 +03003127static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3128 enum nl80211_auth_type auth_type,
3129 enum nl80211_commands cmd)
3130{
3131 if (auth_type > NL80211_AUTHTYPE_MAX)
3132 return false;
3133
3134 switch (cmd) {
3135 case NL80211_CMD_AUTHENTICATE:
3136 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3137 auth_type == NL80211_AUTHTYPE_SAE)
3138 return false;
3139 return true;
3140 case NL80211_CMD_CONNECT:
3141 case NL80211_CMD_START_AP:
3142 /* SAE not supported yet */
3143 if (auth_type == NL80211_AUTHTYPE_SAE)
3144 return false;
3145 return true;
3146 default:
3147 return false;
3148 }
3149}
3150
Johannes Berg88600202012-02-13 15:17:18 +01003151static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3152{
3153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3154 struct net_device *dev = info->user_ptr[1];
3155 struct wireless_dev *wdev = dev->ieee80211_ptr;
3156 struct cfg80211_ap_settings params;
3157 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003158 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003159
3160 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3161 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3162 return -EOPNOTSUPP;
3163
3164 if (!rdev->ops->start_ap)
3165 return -EOPNOTSUPP;
3166
3167 if (wdev->beacon_interval)
3168 return -EALREADY;
3169
3170 memset(&params, 0, sizeof(params));
3171
3172 /* these are required for START_AP */
3173 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3174 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3175 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3176 return -EINVAL;
3177
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003178 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003179 if (err)
3180 return err;
3181
3182 params.beacon_interval =
3183 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3184 params.dtim_period =
3185 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3186
3187 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3188 if (err)
3189 return err;
3190
3191 /*
3192 * In theory, some of these attributes should be required here
3193 * but since they were not used when the command was originally
3194 * added, keep them optional for old user space programs to let
3195 * them continue to work with drivers that do not need the
3196 * additional information -- drivers must check!
3197 */
3198 if (info->attrs[NL80211_ATTR_SSID]) {
3199 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3200 params.ssid_len =
3201 nla_len(info->attrs[NL80211_ATTR_SSID]);
3202 if (params.ssid_len == 0 ||
3203 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3204 return -EINVAL;
3205 }
3206
3207 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3208 params.hidden_ssid = nla_get_u32(
3209 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3210 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3211 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3212 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3213 return -EINVAL;
3214 }
3215
3216 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3217
3218 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3219 params.auth_type = nla_get_u32(
3220 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003221 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3222 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003223 return -EINVAL;
3224 } else
3225 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3226
3227 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3228 NL80211_MAX_NR_CIPHER_SUITES);
3229 if (err)
3230 return err;
3231
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303232 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3233 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3234 return -EOPNOTSUPP;
3235 params.inactivity_timeout = nla_get_u16(
3236 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3237 }
3238
Johannes Berg53cabad2012-11-14 15:17:28 +01003239 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3240 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3241 return -EINVAL;
3242 params.p2p_ctwindow =
3243 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3244 if (params.p2p_ctwindow > 127)
3245 return -EINVAL;
3246 if (params.p2p_ctwindow != 0 &&
3247 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3248 return -EINVAL;
3249 }
3250
3251 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3252 u8 tmp;
3253
3254 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3255 return -EINVAL;
3256 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3257 if (tmp > 1)
3258 return -EINVAL;
3259 params.p2p_opp_ps = tmp;
3260 if (params.p2p_opp_ps != 0 &&
3261 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3262 return -EINVAL;
3263 }
3264
Johannes Bergaa430da2012-05-16 23:50:18 +02003265 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003266 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3267 if (err)
3268 return err;
3269 } else if (wdev->preset_chandef.chan) {
3270 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003271 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003272 return -EINVAL;
3273
Ilan Peer174e0cd2014-02-23 09:13:01 +02003274 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
3275 wdev->iftype))
Johannes Bergaa430da2012-05-16 23:50:18 +02003276 return -EINVAL;
3277
Simon Wunderlich04f39042013-02-08 18:16:19 +01003278 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3279 if (err < 0)
3280 return err;
3281 if (err) {
3282 radar_detect_width = BIT(params.chandef.width);
3283 params.radar_required = true;
3284 }
3285
Simon Wunderlich04f39042013-02-08 18:16:19 +01003286 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3287 params.chandef.chan,
3288 CHAN_MODE_SHARED,
3289 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003290 if (err)
3291 return err;
3292
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303293 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3294 params.acl = parse_acl_data(&rdev->wiphy, info);
3295 if (IS_ERR(params.acl))
3296 return PTR_ERR(params.acl);
3297 }
3298
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003299 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003300 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003301 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003302 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003303 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003304 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003305 wdev->ssid_len = params.ssid_len;
3306 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003307 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003308 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303309
3310 kfree(params.acl);
3311
Johannes Berg56d18932011-05-09 18:41:15 +02003312 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003313}
3314
Johannes Berg88600202012-02-13 15:17:18 +01003315static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3316{
3317 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3318 struct net_device *dev = info->user_ptr[1];
3319 struct wireless_dev *wdev = dev->ieee80211_ptr;
3320 struct cfg80211_beacon_data params;
3321 int err;
3322
3323 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3324 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3325 return -EOPNOTSUPP;
3326
3327 if (!rdev->ops->change_beacon)
3328 return -EOPNOTSUPP;
3329
3330 if (!wdev->beacon_interval)
3331 return -EINVAL;
3332
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003333 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003334 if (err)
3335 return err;
3336
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003337 wdev_lock(wdev);
3338 err = rdev_change_beacon(rdev, dev, &params);
3339 wdev_unlock(wdev);
3340
3341 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003342}
3343
3344static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003345{
Johannes Berg4c476992010-10-04 21:36:35 +02003346 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3347 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003348
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003349 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003350}
3351
Johannes Berg5727ef12007-12-19 02:03:34 +01003352static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3353 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3354 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3355 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003356 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003357 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003358 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003359};
3360
Johannes Bergeccb8e82009-05-11 21:57:56 +03003361static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003362 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003363 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003364{
3365 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003366 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003367 int flag;
3368
Johannes Bergeccb8e82009-05-11 21:57:56 +03003369 /*
3370 * Try parsing the new attribute first so userspace
3371 * can specify both for older kernels.
3372 */
3373 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3374 if (nla) {
3375 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003376
Johannes Bergeccb8e82009-05-11 21:57:56 +03003377 sta_flags = nla_data(nla);
3378 params->sta_flags_mask = sta_flags->mask;
3379 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003380 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003381 if ((params->sta_flags_mask |
3382 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3383 return -EINVAL;
3384 return 0;
3385 }
3386
3387 /* if present, parse the old attribute */
3388
3389 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003390 if (!nla)
3391 return 0;
3392
3393 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3394 nla, sta_flags_policy))
3395 return -EINVAL;
3396
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003397 /*
3398 * Only allow certain flags for interface types so that
3399 * other attributes are silently ignored. Remember that
3400 * this is backward compatibility code with old userspace
3401 * and shouldn't be hit in other cases anyway.
3402 */
3403 switch (iftype) {
3404 case NL80211_IFTYPE_AP:
3405 case NL80211_IFTYPE_AP_VLAN:
3406 case NL80211_IFTYPE_P2P_GO:
3407 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3408 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3409 BIT(NL80211_STA_FLAG_WME) |
3410 BIT(NL80211_STA_FLAG_MFP);
3411 break;
3412 case NL80211_IFTYPE_P2P_CLIENT:
3413 case NL80211_IFTYPE_STATION:
3414 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3415 BIT(NL80211_STA_FLAG_TDLS_PEER);
3416 break;
3417 case NL80211_IFTYPE_MESH_POINT:
3418 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3419 BIT(NL80211_STA_FLAG_MFP) |
3420 BIT(NL80211_STA_FLAG_AUTHORIZED);
3421 default:
3422 return -EINVAL;
3423 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003424
Johannes Berg3383b5a2012-05-10 20:14:43 +02003425 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3426 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003427 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003428
Johannes Berg3383b5a2012-05-10 20:14:43 +02003429 /* no longer support new API additions in old API */
3430 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3431 return -EINVAL;
3432 }
3433 }
3434
Johannes Berg5727ef12007-12-19 02:03:34 +01003435 return 0;
3436}
3437
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003438static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3439 int attr)
3440{
3441 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003442 u32 bitrate;
3443 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003444
3445 rate = nla_nest_start(msg, attr);
3446 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003447 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003448
3449 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3450 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003451 /* report 16-bit bitrate only if we can */
3452 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003453 if (bitrate > 0 &&
3454 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3455 return false;
3456 if (bitrate_compat > 0 &&
3457 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3458 return false;
3459
3460 if (info->flags & RATE_INFO_FLAGS_MCS) {
3461 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3462 return false;
3463 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3464 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3465 return false;
3466 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3467 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3468 return false;
3469 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3470 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3471 return false;
3472 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3473 return false;
3474 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3475 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3476 return false;
3477 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3478 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3479 return false;
3480 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3481 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3482 return false;
3483 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3484 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3485 return false;
3486 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3487 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3488 return false;
3489 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003490
3491 nla_nest_end(msg, rate);
3492 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003493}
3494
Felix Fietkau119363c2013-04-22 16:29:30 +02003495static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3496 int id)
3497{
3498 void *attr;
3499 int i = 0;
3500
3501 if (!mask)
3502 return true;
3503
3504 attr = nla_nest_start(msg, id);
3505 if (!attr)
3506 return false;
3507
3508 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3509 if (!(mask & BIT(i)))
3510 continue;
3511
3512 if (nla_put_u8(msg, i, signal[i]))
3513 return false;
3514 }
3515
3516 nla_nest_end(msg, attr);
3517
3518 return true;
3519}
3520
Eric W. Biederman15e47302012-09-07 20:12:54 +00003521static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003522 int flags,
3523 struct cfg80211_registered_device *rdev,
3524 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003525 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003526{
3527 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003528 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003529
Eric W. Biederman15e47302012-09-07 20:12:54 +00003530 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003531 if (!hdr)
3532 return -1;
3533
David S. Miller9360ffd2012-03-29 04:41:26 -04003534 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3535 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3536 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3537 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003538
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3540 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003541 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003542 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3543 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3544 sinfo->connected_time))
3545 goto nla_put_failure;
3546 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3547 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3548 sinfo->inactive_time))
3549 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003550 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3551 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003552 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003553 (u32)sinfo->rx_bytes))
3554 goto nla_put_failure;
3555 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003556 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003557 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3558 (u32)sinfo->tx_bytes))
3559 goto nla_put_failure;
3560 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3561 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003562 sinfo->rx_bytes))
3563 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003564 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3565 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003566 sinfo->tx_bytes))
3567 goto nla_put_failure;
3568 if ((sinfo->filled & STATION_INFO_LLID) &&
3569 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3570 goto nla_put_failure;
3571 if ((sinfo->filled & STATION_INFO_PLID) &&
3572 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3573 goto nla_put_failure;
3574 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3575 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3576 sinfo->plink_state))
3577 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003578 switch (rdev->wiphy.signal_type) {
3579 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003580 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3581 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3582 sinfo->signal))
3583 goto nla_put_failure;
3584 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3585 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3586 sinfo->signal_avg))
3587 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003588 break;
3589 default:
3590 break;
3591 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003592 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3593 if (!nl80211_put_signal(msg, sinfo->chains,
3594 sinfo->chain_signal,
3595 NL80211_STA_INFO_CHAIN_SIGNAL))
3596 goto nla_put_failure;
3597 }
3598 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3599 if (!nl80211_put_signal(msg, sinfo->chains,
3600 sinfo->chain_signal_avg,
3601 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3602 goto nla_put_failure;
3603 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003604 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003605 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3606 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003607 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003608 }
3609 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3610 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3611 NL80211_STA_INFO_RX_BITRATE))
3612 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003613 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003614 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3615 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3616 sinfo->rx_packets))
3617 goto nla_put_failure;
3618 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3619 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3620 sinfo->tx_packets))
3621 goto nla_put_failure;
3622 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3623 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3624 sinfo->tx_retries))
3625 goto nla_put_failure;
3626 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3627 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3628 sinfo->tx_failed))
3629 goto nla_put_failure;
3630 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3631 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3632 sinfo->beacon_loss_count))
3633 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003634 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3635 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3636 sinfo->local_pm))
3637 goto nla_put_failure;
3638 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3639 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3640 sinfo->peer_pm))
3641 goto nla_put_failure;
3642 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3643 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3644 sinfo->nonpeer_pm))
3645 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003646 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3647 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3648 if (!bss_param)
3649 goto nla_put_failure;
3650
David S. Miller9360ffd2012-03-29 04:41:26 -04003651 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3652 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3653 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3654 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3655 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3656 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3657 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3658 sinfo->bss_param.dtim_period) ||
3659 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3660 sinfo->bss_param.beacon_interval))
3661 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003662
3663 nla_nest_end(msg, bss_param);
3664 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003665 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3666 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3667 sizeof(struct nl80211_sta_flag_update),
3668 &sinfo->sta_flags))
3669 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003670 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3671 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3672 sinfo->t_offset))
3673 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003674 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003675
David S. Miller9360ffd2012-03-29 04:41:26 -04003676 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3677 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3678 sinfo->assoc_req_ies))
3679 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003680
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003681 return genlmsg_end(msg, hdr);
3682
3683 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003684 genlmsg_cancel(msg, hdr);
3685 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003686}
3687
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003688static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003689 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003690{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003691 struct station_info sinfo;
3692 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003693 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003694 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003695 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003696 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003697
Johannes Berg97990a02013-04-19 01:02:55 +02003698 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003699 if (err)
3700 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003701
Johannes Berg97990a02013-04-19 01:02:55 +02003702 if (!wdev->netdev) {
3703 err = -EINVAL;
3704 goto out_err;
3705 }
3706
Johannes Bergbba95fe2008-07-29 13:22:51 +02003707 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003708 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003709 goto out_err;
3710 }
3711
Johannes Bergbba95fe2008-07-29 13:22:51 +02003712 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003713 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003714 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003715 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003716 if (err == -ENOENT)
3717 break;
3718 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003719 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003720
3721 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003722 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003723 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003724 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003725 &sinfo) < 0)
3726 goto out;
3727
3728 sta_idx++;
3729 }
3730
3731
3732 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003733 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003734 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003735 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003736 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003737
3738 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003739}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003740
Johannes Berg5727ef12007-12-19 02:03:34 +01003741static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3742{
Johannes Berg4c476992010-10-04 21:36:35 +02003743 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3744 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003745 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003746 struct sk_buff *msg;
3747 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003748 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003749
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003750 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003751
3752 if (!info->attrs[NL80211_ATTR_MAC])
3753 return -EINVAL;
3754
3755 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3756
Johannes Berg4c476992010-10-04 21:36:35 +02003757 if (!rdev->ops->get_station)
3758 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003759
Hila Gonene35e4d22012-06-27 17:19:42 +03003760 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003761 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003762 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003763
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003764 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003765 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003766 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003767
Eric W. Biederman15e47302012-09-07 20:12:54 +00003768 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003769 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003770 nlmsg_free(msg);
3771 return -ENOBUFS;
3772 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003773
Johannes Berg4c476992010-10-04 21:36:35 +02003774 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003775}
3776
Johannes Berg77ee7c82013-02-15 00:48:33 +01003777int cfg80211_check_station_change(struct wiphy *wiphy,
3778 struct station_parameters *params,
3779 enum cfg80211_station_type statype)
3780{
3781 if (params->listen_interval != -1)
3782 return -EINVAL;
3783 if (params->aid)
3784 return -EINVAL;
3785
3786 /* When you run into this, adjust the code below for the new flag */
3787 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3788
3789 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003790 case CFG80211_STA_MESH_PEER_KERNEL:
3791 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003792 /*
3793 * No ignoring the TDLS flag here -- the userspace mesh
3794 * code doesn't have the bug of including TDLS in the
3795 * mask everywhere.
3796 */
3797 if (params->sta_flags_mask &
3798 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3799 BIT(NL80211_STA_FLAG_MFP) |
3800 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3801 return -EINVAL;
3802 break;
3803 case CFG80211_STA_TDLS_PEER_SETUP:
3804 case CFG80211_STA_TDLS_PEER_ACTIVE:
3805 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3806 return -EINVAL;
3807 /* ignore since it can't change */
3808 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3809 break;
3810 default:
3811 /* disallow mesh-specific things */
3812 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3813 return -EINVAL;
3814 if (params->local_pm)
3815 return -EINVAL;
3816 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3817 return -EINVAL;
3818 }
3819
3820 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3821 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3822 /* TDLS can't be set, ... */
3823 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3824 return -EINVAL;
3825 /*
3826 * ... but don't bother the driver with it. This works around
3827 * a hostapd/wpa_supplicant issue -- it always includes the
3828 * TLDS_PEER flag in the mask even for AP mode.
3829 */
3830 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3831 }
3832
3833 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3834 /* reject other things that can't change */
3835 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3836 return -EINVAL;
3837 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3838 return -EINVAL;
3839 if (params->supported_rates)
3840 return -EINVAL;
3841 if (params->ext_capab || params->ht_capa || params->vht_capa)
3842 return -EINVAL;
3843 }
3844
3845 if (statype != CFG80211_STA_AP_CLIENT) {
3846 if (params->vlan)
3847 return -EINVAL;
3848 }
3849
3850 switch (statype) {
3851 case CFG80211_STA_AP_MLME_CLIENT:
3852 /* Use this only for authorizing/unauthorizing a station */
3853 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3854 return -EOPNOTSUPP;
3855 break;
3856 case CFG80211_STA_AP_CLIENT:
3857 /* accept only the listed bits */
3858 if (params->sta_flags_mask &
3859 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3860 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3861 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3862 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3863 BIT(NL80211_STA_FLAG_WME) |
3864 BIT(NL80211_STA_FLAG_MFP)))
3865 return -EINVAL;
3866
3867 /* but authenticated/associated only if driver handles it */
3868 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3869 params->sta_flags_mask &
3870 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3871 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3872 return -EINVAL;
3873 break;
3874 case CFG80211_STA_IBSS:
3875 case CFG80211_STA_AP_STA:
3876 /* reject any changes other than AUTHORIZED */
3877 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3878 return -EINVAL;
3879 break;
3880 case CFG80211_STA_TDLS_PEER_SETUP:
3881 /* reject any changes other than AUTHORIZED or WME */
3882 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3883 BIT(NL80211_STA_FLAG_WME)))
3884 return -EINVAL;
3885 /* force (at least) rates when authorizing */
3886 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3887 !params->supported_rates)
3888 return -EINVAL;
3889 break;
3890 case CFG80211_STA_TDLS_PEER_ACTIVE:
3891 /* reject any changes */
3892 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003893 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003894 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3895 return -EINVAL;
3896 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003897 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003898 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3899 return -EINVAL;
3900 break;
3901 }
3902
3903 return 0;
3904}
3905EXPORT_SYMBOL(cfg80211_check_station_change);
3906
Johannes Berg5727ef12007-12-19 02:03:34 +01003907/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003908 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003909 */
Johannes Berg80b99892011-11-18 16:23:01 +01003910static struct net_device *get_vlan(struct genl_info *info,
3911 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003912{
Johannes Berg463d0182009-07-14 00:33:35 +02003913 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003914 struct net_device *v;
3915 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003916
Johannes Berg80b99892011-11-18 16:23:01 +01003917 if (!vlanattr)
3918 return NULL;
3919
3920 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3921 if (!v)
3922 return ERR_PTR(-ENODEV);
3923
3924 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3925 ret = -EINVAL;
3926 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003927 }
Johannes Berg80b99892011-11-18 16:23:01 +01003928
Johannes Berg77ee7c82013-02-15 00:48:33 +01003929 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3930 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3931 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3932 ret = -EINVAL;
3933 goto error;
3934 }
3935
Johannes Berg80b99892011-11-18 16:23:01 +01003936 if (!netif_running(v)) {
3937 ret = -ENETDOWN;
3938 goto error;
3939 }
3940
3941 return v;
3942 error:
3943 dev_put(v);
3944 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003945}
3946
Johannes Berg94e860f2014-01-20 23:58:15 +01003947static const struct nla_policy
3948nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003949 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3950 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3951};
3952
Johannes Bergff276692013-02-15 00:09:01 +01003953static int nl80211_parse_sta_wme(struct genl_info *info,
3954 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003955{
Jouni Malinendf881292013-02-14 21:10:54 +02003956 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3957 struct nlattr *nla;
3958 int err;
3959
Jouni Malinendf881292013-02-14 21:10:54 +02003960 /* parse WME attributes if present */
3961 if (!info->attrs[NL80211_ATTR_STA_WME])
3962 return 0;
3963
3964 nla = info->attrs[NL80211_ATTR_STA_WME];
3965 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3966 nl80211_sta_wme_policy);
3967 if (err)
3968 return err;
3969
3970 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3971 params->uapsd_queues = nla_get_u8(
3972 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3973 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3974 return -EINVAL;
3975
3976 if (tb[NL80211_STA_WME_MAX_SP])
3977 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3978
3979 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3980 return -EINVAL;
3981
3982 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3983
3984 return 0;
3985}
3986
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303987static int nl80211_parse_sta_channel_info(struct genl_info *info,
3988 struct station_parameters *params)
3989{
3990 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3991 params->supported_channels =
3992 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3993 params->supported_channels_len =
3994 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3995 /*
3996 * Need to include at least one (first channel, number of
3997 * channels) tuple for each subband, and must have proper
3998 * tuples for the rest of the data as well.
3999 */
4000 if (params->supported_channels_len < 2)
4001 return -EINVAL;
4002 if (params->supported_channels_len % 2)
4003 return -EINVAL;
4004 }
4005
4006 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4007 params->supported_oper_classes =
4008 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4009 params->supported_oper_classes_len =
4010 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4011 /*
4012 * The value of the Length field of the Supported Operating
4013 * Classes element is between 2 and 253.
4014 */
4015 if (params->supported_oper_classes_len < 2 ||
4016 params->supported_oper_classes_len > 253)
4017 return -EINVAL;
4018 }
4019 return 0;
4020}
4021
Johannes Bergff276692013-02-15 00:09:01 +01004022static int nl80211_set_station_tdls(struct genl_info *info,
4023 struct station_parameters *params)
4024{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304025 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004026 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004027 if (info->attrs[NL80211_ATTR_PEER_AID])
4028 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004029 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4030 params->ht_capa =
4031 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4032 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4033 params->vht_capa =
4034 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4035
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304036 err = nl80211_parse_sta_channel_info(info, params);
4037 if (err)
4038 return err;
4039
Johannes Bergff276692013-02-15 00:09:01 +01004040 return nl80211_parse_sta_wme(info, params);
4041}
4042
Johannes Berg5727ef12007-12-19 02:03:34 +01004043static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4044{
Johannes Berg4c476992010-10-04 21:36:35 +02004045 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004046 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004047 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004048 u8 *mac_addr;
4049 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004050
4051 memset(&params, 0, sizeof(params));
4052
4053 params.listen_interval = -1;
4054
Johannes Berg77ee7c82013-02-15 00:48:33 +01004055 if (!rdev->ops->change_station)
4056 return -EOPNOTSUPP;
4057
Johannes Berg5727ef12007-12-19 02:03:34 +01004058 if (info->attrs[NL80211_ATTR_STA_AID])
4059 return -EINVAL;
4060
4061 if (!info->attrs[NL80211_ATTR_MAC])
4062 return -EINVAL;
4063
4064 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4065
4066 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4067 params.supported_rates =
4068 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4069 params.supported_rates_len =
4070 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4071 }
4072
Jouni Malinen9d62a982013-02-14 21:10:13 +02004073 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4074 params.capability =
4075 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4076 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4077 }
4078
4079 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4080 params.ext_capab =
4081 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4082 params.ext_capab_len =
4083 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4084 }
4085
Jouni Malinendf881292013-02-14 21:10:54 +02004086 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004087 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004088
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004089 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004090 return -EINVAL;
4091
Johannes Bergf8bacc22013-02-14 23:27:01 +01004092 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004093 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004094 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4095 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4096 return -EINVAL;
4097 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004098
Johannes Bergf8bacc22013-02-14 23:27:01 +01004099 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004100 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004101 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4102 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4103 return -EINVAL;
4104 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4105 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004106
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004107 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4108 enum nl80211_mesh_power_mode pm = nla_get_u32(
4109 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4110
4111 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4112 pm > NL80211_MESH_POWER_MAX)
4113 return -EINVAL;
4114
4115 params.local_pm = pm;
4116 }
4117
Johannes Berg77ee7c82013-02-15 00:48:33 +01004118 /* Include parameters for TDLS peer (will check later) */
4119 err = nl80211_set_station_tdls(info, &params);
4120 if (err)
4121 return err;
4122
4123 params.vlan = get_vlan(info, rdev);
4124 if (IS_ERR(params.vlan))
4125 return PTR_ERR(params.vlan);
4126
Johannes Berga97f4422009-06-18 17:23:43 +02004127 switch (dev->ieee80211_ptr->iftype) {
4128 case NL80211_IFTYPE_AP:
4129 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004130 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004131 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004132 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004133 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004134 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004135 break;
4136 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004137 err = -EOPNOTSUPP;
4138 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004139 }
4140
Johannes Berg77ee7c82013-02-15 00:48:33 +01004141 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004142 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004143
Johannes Berg77ee7c82013-02-15 00:48:33 +01004144 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004145 if (params.vlan)
4146 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004147
Johannes Berg5727ef12007-12-19 02:03:34 +01004148 return err;
4149}
4150
4151static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4152{
Johannes Berg4c476992010-10-04 21:36:35 +02004153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004154 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004155 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004156 struct station_parameters params;
4157 u8 *mac_addr = NULL;
4158
4159 memset(&params, 0, sizeof(params));
4160
Johannes Berg984c3112013-02-14 23:43:25 +01004161 if (!rdev->ops->add_station)
4162 return -EOPNOTSUPP;
4163
Johannes Berg5727ef12007-12-19 02:03:34 +01004164 if (!info->attrs[NL80211_ATTR_MAC])
4165 return -EINVAL;
4166
Johannes Berg5727ef12007-12-19 02:03:34 +01004167 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4168 return -EINVAL;
4169
4170 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4171 return -EINVAL;
4172
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004173 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4174 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004175 return -EINVAL;
4176
Johannes Berg5727ef12007-12-19 02:03:34 +01004177 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4178 params.supported_rates =
4179 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4180 params.supported_rates_len =
4181 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4182 params.listen_interval =
4183 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004184
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004185 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004186 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004187 else
4188 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004189 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4190 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004191
Jouni Malinen9d62a982013-02-14 21:10:13 +02004192 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4193 params.capability =
4194 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4195 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4196 }
4197
4198 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4199 params.ext_capab =
4200 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4201 params.ext_capab_len =
4202 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4203 }
4204
Jouni Malinen36aedc902008-08-25 11:58:58 +03004205 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4206 params.ht_capa =
4207 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004208
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004209 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4210 params.vht_capa =
4211 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4212
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004213 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4214 params.opmode_notif_used = true;
4215 params.opmode_notif =
4216 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4217 }
4218
Johannes Bergf8bacc22013-02-14 23:27:01 +01004219 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004220 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004221 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4222 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4223 return -EINVAL;
4224 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004225
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304226 err = nl80211_parse_sta_channel_info(info, &params);
4227 if (err)
4228 return err;
4229
Johannes Bergff276692013-02-15 00:09:01 +01004230 err = nl80211_parse_sta_wme(info, &params);
4231 if (err)
4232 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004233
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004234 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004235 return -EINVAL;
4236
Johannes Berg77ee7c82013-02-15 00:48:33 +01004237 /* When you run into this, adjust the code below for the new flag */
4238 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4239
Johannes Bergbdd90d52011-12-14 12:20:27 +01004240 switch (dev->ieee80211_ptr->iftype) {
4241 case NL80211_IFTYPE_AP:
4242 case NL80211_IFTYPE_AP_VLAN:
4243 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004244 /* ignore WME attributes if iface/sta is not capable */
4245 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4246 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4247 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004248
Johannes Bergbdd90d52011-12-14 12:20:27 +01004249 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004250 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4251 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004252 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004253 /* but don't bother the driver with it */
4254 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004255
Johannes Bergd582cff2012-10-26 17:53:44 +02004256 /* allow authenticated/associated only if driver handles it */
4257 if (!(rdev->wiphy.features &
4258 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4259 params.sta_flags_mask &
4260 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4261 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4262 return -EINVAL;
4263
Johannes Bergbdd90d52011-12-14 12:20:27 +01004264 /* must be last in here for error handling */
4265 params.vlan = get_vlan(info, rdev);
4266 if (IS_ERR(params.vlan))
4267 return PTR_ERR(params.vlan);
4268 break;
4269 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004270 /* ignore uAPSD data */
4271 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4272
Johannes Bergd582cff2012-10-26 17:53:44 +02004273 /* associated is disallowed */
4274 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4275 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004276 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004277 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4278 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004279 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004280 break;
4281 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004282 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004283 /* ignore uAPSD data */
4284 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4285
Johannes Berg77ee7c82013-02-15 00:48:33 +01004286 /* these are disallowed */
4287 if (params.sta_flags_mask &
4288 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4289 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004290 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004291 /* Only TDLS peers can be added */
4292 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4293 return -EINVAL;
4294 /* Can only add if TDLS ... */
4295 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4296 return -EOPNOTSUPP;
4297 /* ... with external setup is supported */
4298 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4299 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004300 /*
4301 * Older wpa_supplicant versions always mark the TDLS peer
4302 * as authorized, but it shouldn't yet be.
4303 */
4304 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004305 break;
4306 default:
4307 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004308 }
4309
Johannes Bergbdd90d52011-12-14 12:20:27 +01004310 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004311
Hila Gonene35e4d22012-06-27 17:19:42 +03004312 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004313
Johannes Berg5727ef12007-12-19 02:03:34 +01004314 if (params.vlan)
4315 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004316 return err;
4317}
4318
4319static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4320{
Johannes Berg4c476992010-10-04 21:36:35 +02004321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4322 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004323 u8 *mac_addr = NULL;
4324
4325 if (info->attrs[NL80211_ATTR_MAC])
4326 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4327
Johannes Berge80cf852009-05-11 14:43:13 +02004328 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004329 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004330 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004331 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4332 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004333
Johannes Berg4c476992010-10-04 21:36:35 +02004334 if (!rdev->ops->del_station)
4335 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004336
Hila Gonene35e4d22012-06-27 17:19:42 +03004337 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004338}
4339
Eric W. Biederman15e47302012-09-07 20:12:54 +00004340static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004341 int flags, struct net_device *dev,
4342 u8 *dst, u8 *next_hop,
4343 struct mpath_info *pinfo)
4344{
4345 void *hdr;
4346 struct nlattr *pinfoattr;
4347
Eric W. Biederman15e47302012-09-07 20:12:54 +00004348 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004349 if (!hdr)
4350 return -1;
4351
David S. Miller9360ffd2012-03-29 04:41:26 -04004352 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4353 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4354 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4355 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4356 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004357
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004358 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4359 if (!pinfoattr)
4360 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004361 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4362 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4363 pinfo->frame_qlen))
4364 goto nla_put_failure;
4365 if (((pinfo->filled & MPATH_INFO_SN) &&
4366 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4367 ((pinfo->filled & MPATH_INFO_METRIC) &&
4368 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4369 pinfo->metric)) ||
4370 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4371 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4372 pinfo->exptime)) ||
4373 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4374 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4375 pinfo->flags)) ||
4376 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4377 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4378 pinfo->discovery_timeout)) ||
4379 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4380 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4381 pinfo->discovery_retries)))
4382 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004383
4384 nla_nest_end(msg, pinfoattr);
4385
4386 return genlmsg_end(msg, hdr);
4387
4388 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004389 genlmsg_cancel(msg, hdr);
4390 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004391}
4392
4393static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004394 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004395{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004396 struct mpath_info pinfo;
4397 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004398 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004399 u8 dst[ETH_ALEN];
4400 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004401 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004402 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004403
Johannes Berg97990a02013-04-19 01:02:55 +02004404 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004405 if (err)
4406 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004407
4408 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004409 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004410 goto out_err;
4411 }
4412
Johannes Berg97990a02013-04-19 01:02:55 +02004413 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004414 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004415 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004416 }
4417
Johannes Bergbba95fe2008-07-29 13:22:51 +02004418 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004419 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4420 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004421 if (err == -ENOENT)
4422 break;
4423 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004424 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004425
Eric W. Biederman15e47302012-09-07 20:12:54 +00004426 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004427 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004428 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004429 &pinfo) < 0)
4430 goto out;
4431
4432 path_idx++;
4433 }
4434
4435
4436 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004437 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004438 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004439 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004440 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004441 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004442}
4443
4444static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4445{
Johannes Berg4c476992010-10-04 21:36:35 +02004446 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004447 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004448 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004449 struct mpath_info pinfo;
4450 struct sk_buff *msg;
4451 u8 *dst = NULL;
4452 u8 next_hop[ETH_ALEN];
4453
4454 memset(&pinfo, 0, sizeof(pinfo));
4455
4456 if (!info->attrs[NL80211_ATTR_MAC])
4457 return -EINVAL;
4458
4459 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4460
Johannes Berg4c476992010-10-04 21:36:35 +02004461 if (!rdev->ops->get_mpath)
4462 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004463
Johannes Berg4c476992010-10-04 21:36:35 +02004464 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4465 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004466
Hila Gonene35e4d22012-06-27 17:19:42 +03004467 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004468 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004469 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004472 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004473 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004474
Eric W. Biederman15e47302012-09-07 20:12:54 +00004475 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004476 dev, dst, next_hop, &pinfo) < 0) {
4477 nlmsg_free(msg);
4478 return -ENOBUFS;
4479 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004480
Johannes Berg4c476992010-10-04 21:36:35 +02004481 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004482}
4483
4484static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4485{
Johannes Berg4c476992010-10-04 21:36:35 +02004486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4487 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004488 u8 *dst = NULL;
4489 u8 *next_hop = NULL;
4490
4491 if (!info->attrs[NL80211_ATTR_MAC])
4492 return -EINVAL;
4493
4494 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4495 return -EINVAL;
4496
4497 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4498 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4499
Johannes Berg4c476992010-10-04 21:36:35 +02004500 if (!rdev->ops->change_mpath)
4501 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004502
Johannes Berg4c476992010-10-04 21:36:35 +02004503 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4504 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004505
Hila Gonene35e4d22012-06-27 17:19:42 +03004506 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004507}
Johannes Berg4c476992010-10-04 21:36:35 +02004508
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004509static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4510{
Johannes Berg4c476992010-10-04 21:36:35 +02004511 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4512 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004513 u8 *dst = NULL;
4514 u8 *next_hop = NULL;
4515
4516 if (!info->attrs[NL80211_ATTR_MAC])
4517 return -EINVAL;
4518
4519 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4520 return -EINVAL;
4521
4522 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4523 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4524
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (!rdev->ops->add_mpath)
4526 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004527
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4529 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004530
Hila Gonene35e4d22012-06-27 17:19:42 +03004531 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004532}
4533
4534static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4535{
Johannes Berg4c476992010-10-04 21:36:35 +02004536 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4537 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004538 u8 *dst = NULL;
4539
4540 if (info->attrs[NL80211_ATTR_MAC])
4541 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4542
Johannes Berg4c476992010-10-04 21:36:35 +02004543 if (!rdev->ops->del_mpath)
4544 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004545
Hila Gonene35e4d22012-06-27 17:19:42 +03004546 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004547}
4548
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004549static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4550{
Johannes Berg4c476992010-10-04 21:36:35 +02004551 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4552 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004553 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004554 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004555 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004556
4557 memset(&params, 0, sizeof(params));
4558 /* default to not changing parameters */
4559 params.use_cts_prot = -1;
4560 params.use_short_preamble = -1;
4561 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004562 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004563 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004564 params.p2p_ctwindow = -1;
4565 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004566
4567 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4568 params.use_cts_prot =
4569 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4570 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4571 params.use_short_preamble =
4572 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4573 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4574 params.use_short_slot_time =
4575 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004576 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4577 params.basic_rates =
4578 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4579 params.basic_rates_len =
4580 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4581 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004582 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4583 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004584 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4585 params.ht_opmode =
4586 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004587
Johannes Berg53cabad2012-11-14 15:17:28 +01004588 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4589 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4590 return -EINVAL;
4591 params.p2p_ctwindow =
4592 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4593 if (params.p2p_ctwindow < 0)
4594 return -EINVAL;
4595 if (params.p2p_ctwindow != 0 &&
4596 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4597 return -EINVAL;
4598 }
4599
4600 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4601 u8 tmp;
4602
4603 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4604 return -EINVAL;
4605 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4606 if (tmp > 1)
4607 return -EINVAL;
4608 params.p2p_opp_ps = tmp;
4609 if (params.p2p_opp_ps &&
4610 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4611 return -EINVAL;
4612 }
4613
Johannes Berg4c476992010-10-04 21:36:35 +02004614 if (!rdev->ops->change_bss)
4615 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004616
Johannes Berg074ac8d2010-09-16 14:58:22 +02004617 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004618 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4619 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004620
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004621 wdev_lock(wdev);
4622 err = rdev_change_bss(rdev, dev, &params);
4623 wdev_unlock(wdev);
4624
4625 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004626}
4627
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004628static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004629 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4630 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4631 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4632 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4633 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4634 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004635 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004636};
4637
4638static int parse_reg_rule(struct nlattr *tb[],
4639 struct ieee80211_reg_rule *reg_rule)
4640{
4641 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4642 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4643
4644 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4645 return -EINVAL;
4646 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4647 return -EINVAL;
4648 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4649 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004650 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4651 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004652 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4653 return -EINVAL;
4654
4655 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4656
4657 freq_range->start_freq_khz =
4658 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4659 freq_range->end_freq_khz =
4660 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004661 freq_range->max_bandwidth_khz =
4662 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004663
4664 power_rule->max_eirp =
4665 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4666
4667 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4668 power_rule->max_antenna_gain =
4669 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4670
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004671 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4672 reg_rule->dfs_cac_ms =
4673 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4674
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004675 return 0;
4676}
4677
4678static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4679{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004680 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004681 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004682
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004683 /*
4684 * You should only get this when cfg80211 hasn't yet initialized
4685 * completely when built-in to the kernel right between the time
4686 * window between nl80211_init() and regulatory_init(), if that is
4687 * even possible.
4688 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004689 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004690 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004691
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004692 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4693 user_reg_hint_type =
4694 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4695 else
4696 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4697
4698 switch (user_reg_hint_type) {
4699 case NL80211_USER_REG_HINT_USER:
4700 case NL80211_USER_REG_HINT_CELL_BASE:
Ilan Peer52616f22014-02-25 16:26:00 +02004701 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4702 return -EINVAL;
4703
4704 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4705 return regulatory_hint_user(data, user_reg_hint_type);
4706 case NL80211_USER_REG_HINT_INDOOR:
4707 return regulatory_hint_indoor_user();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004708 default:
4709 return -EINVAL;
4710 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004711}
4712
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004713static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004714 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004715{
Johannes Berg4c476992010-10-04 21:36:35 +02004716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004717 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004718 struct wireless_dev *wdev = dev->ieee80211_ptr;
4719 struct mesh_config cur_params;
4720 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004721 void *hdr;
4722 struct nlattr *pinfoattr;
4723 struct sk_buff *msg;
4724
Johannes Berg29cbe682010-12-03 09:20:44 +01004725 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4726 return -EOPNOTSUPP;
4727
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004728 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004729 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004730
Johannes Berg29cbe682010-12-03 09:20:44 +01004731 wdev_lock(wdev);
4732 /* If not connected, get default parameters */
4733 if (!wdev->mesh_id_len)
4734 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4735 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004736 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004737 wdev_unlock(wdev);
4738
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004739 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004740 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004741
4742 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004743 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004744 if (!msg)
4745 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004746 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004747 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004748 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004749 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004750 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004751 if (!pinfoattr)
4752 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004753 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4754 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4755 cur_params.dot11MeshRetryTimeout) ||
4756 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4757 cur_params.dot11MeshConfirmTimeout) ||
4758 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4759 cur_params.dot11MeshHoldingTimeout) ||
4760 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4761 cur_params.dot11MeshMaxPeerLinks) ||
4762 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4763 cur_params.dot11MeshMaxRetries) ||
4764 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4765 cur_params.dot11MeshTTL) ||
4766 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4767 cur_params.element_ttl) ||
4768 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4769 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004770 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4771 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004772 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4773 cur_params.dot11MeshHWMPmaxPREQretries) ||
4774 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4775 cur_params.path_refresh_time) ||
4776 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4777 cur_params.min_discovery_timeout) ||
4778 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4779 cur_params.dot11MeshHWMPactivePathTimeout) ||
4780 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4781 cur_params.dot11MeshHWMPpreqMinInterval) ||
4782 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4783 cur_params.dot11MeshHWMPperrMinInterval) ||
4784 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4785 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4786 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4787 cur_params.dot11MeshHWMPRootMode) ||
4788 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4789 cur_params.dot11MeshHWMPRannInterval) ||
4790 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4791 cur_params.dot11MeshGateAnnouncementProtocol) ||
4792 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4793 cur_params.dot11MeshForwarding) ||
4794 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004795 cur_params.rssi_threshold) ||
4796 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004797 cur_params.ht_opmode) ||
4798 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4799 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4800 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004801 cur_params.dot11MeshHWMProotInterval) ||
4802 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004803 cur_params.dot11MeshHWMPconfirmationInterval) ||
4804 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4805 cur_params.power_mode) ||
4806 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004807 cur_params.dot11MeshAwakeWindowDuration) ||
4808 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4809 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004810 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004811 nla_nest_end(msg, pinfoattr);
4812 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004813 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004814
Johannes Berg3b858752009-03-12 09:55:09 +01004815 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004816 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004817 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004818 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004819 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004820}
4821
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004822static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004823 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4824 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4825 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4826 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4827 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4828 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004829 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004830 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004831 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004832 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4833 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4834 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4835 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4836 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004837 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004838 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004839 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004840 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004841 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004842 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004843 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4844 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004845 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4846 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004847 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004848 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4849 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004850 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004851};
4852
Javier Cardonac80d5452010-12-16 17:37:49 -08004853static const struct nla_policy
4854 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004855 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004856 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4857 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004858 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004859 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004860 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004861 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004862 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004863 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004864};
4865
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004866static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004867 struct mesh_config *cfg,
4868 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004869{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004870 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004871 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004872
Marco Porschea54fba2013-01-07 16:04:48 +01004873#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4874do { \
4875 if (tb[attr]) { \
4876 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4877 return -EINVAL; \
4878 cfg->param = fn(tb[attr]); \
4879 mask |= (1 << (attr - 1)); \
4880 } \
4881} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004882
4883
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004884 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004885 return -EINVAL;
4886 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004887 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004888 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004889 return -EINVAL;
4890
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004891 /* This makes sure that there aren't more than 32 mesh config
4892 * parameters (otherwise our bitfield scheme would not work.) */
4893 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4894
4895 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004896 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004897 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4898 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004899 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004900 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4901 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004902 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004903 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4904 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004905 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004906 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4907 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004908 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004909 mask, NL80211_MESHCONF_MAX_RETRIES,
4910 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004911 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004912 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004914 mask, NL80211_MESHCONF_ELEMENT_TTL,
4915 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004917 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4918 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4920 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004921 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4922 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004924 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4925 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004927 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4928 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004930 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4931 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4933 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004934 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4935 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004936 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004937 1, 65535, mask,
4938 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004939 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004941 1, 65535, mask,
4942 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004943 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004944 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004945 dot11MeshHWMPnetDiameterTraversalTime,
4946 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004947 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4948 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004949 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4950 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4951 nla_get_u8);
4952 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4953 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004954 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004955 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004956 dot11MeshGateAnnouncementProtocol, 0, 1,
4957 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004958 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004959 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004960 mask, NL80211_MESHCONF_FORWARDING,
4961 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004962 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004963 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004964 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004965 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004966 mask, NL80211_MESHCONF_HT_OPMODE,
4967 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004968 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004969 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004970 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4971 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004972 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004973 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4974 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004975 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004976 dot11MeshHWMPconfirmationInterval,
4977 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004978 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4979 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004980 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4981 NL80211_MESH_POWER_ACTIVE,
4982 NL80211_MESH_POWER_MAX,
4983 mask, NL80211_MESHCONF_POWER_MODE,
4984 nla_get_u32);
4985 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4986 0, 65535, mask,
4987 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004988 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4989 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4990 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004991 if (mask_out)
4992 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004993
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004994 return 0;
4995
4996#undef FILL_IN_MESH_PARAM_IF_SET
4997}
4998
Javier Cardonac80d5452010-12-16 17:37:49 -08004999static int nl80211_parse_mesh_setup(struct genl_info *info,
5000 struct mesh_setup *setup)
5001{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005002 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08005003 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5004
5005 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5006 return -EINVAL;
5007 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5008 info->attrs[NL80211_ATTR_MESH_SETUP],
5009 nl80211_mesh_setup_params_policy))
5010 return -EINVAL;
5011
Javier Cardonad299a1f2012-03-31 11:31:33 -07005012 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5013 setup->sync_method =
5014 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5015 IEEE80211_SYNC_METHOD_VENDOR :
5016 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5017
Javier Cardonac80d5452010-12-16 17:37:49 -08005018 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5019 setup->path_sel_proto =
5020 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5021 IEEE80211_PATH_PROTOCOL_VENDOR :
5022 IEEE80211_PATH_PROTOCOL_HWMP;
5023
5024 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5025 setup->path_metric =
5026 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5027 IEEE80211_PATH_METRIC_VENDOR :
5028 IEEE80211_PATH_METRIC_AIRTIME;
5029
Javier Cardona581a8b02011-04-07 15:08:27 -07005030
5031 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005032 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005033 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005034 if (!is_valid_ie_attr(ieattr))
5035 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005036 setup->ie = nla_data(ieattr);
5037 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005038 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005039 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5040 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5041 return -EINVAL;
5042 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005043 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5044 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005045 if (setup->is_secure)
5046 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005047
Colleen Twitty6e16d902013-05-08 11:45:59 -07005048 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5049 if (!setup->user_mpm)
5050 return -EINVAL;
5051 setup->auth_id =
5052 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5053 }
5054
Javier Cardonac80d5452010-12-16 17:37:49 -08005055 return 0;
5056}
5057
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005058static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005059 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005060{
5061 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5062 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005063 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005064 struct mesh_config cfg;
5065 u32 mask;
5066 int err;
5067
Johannes Berg29cbe682010-12-03 09:20:44 +01005068 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5069 return -EOPNOTSUPP;
5070
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005071 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005072 return -EOPNOTSUPP;
5073
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005074 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005075 if (err)
5076 return err;
5077
Johannes Berg29cbe682010-12-03 09:20:44 +01005078 wdev_lock(wdev);
5079 if (!wdev->mesh_id_len)
5080 err = -ENOLINK;
5081
5082 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005083 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005084
5085 wdev_unlock(wdev);
5086
5087 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005088}
5089
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005090static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5091{
Johannes Berg458f4f92012-12-06 15:47:38 +01005092 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005093 struct sk_buff *msg;
5094 void *hdr = NULL;
5095 struct nlattr *nl_reg_rules;
5096 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005097
5098 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005099 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005100
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005101 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005102 if (!msg)
5103 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005104
Eric W. Biederman15e47302012-09-07 20:12:54 +00005105 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005106 NL80211_CMD_GET_REG);
5107 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005108 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005109
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005110 if (reg_last_request_cell_base() &&
5111 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5112 NL80211_USER_REG_HINT_CELL_BASE))
5113 goto nla_put_failure;
5114
Johannes Berg458f4f92012-12-06 15:47:38 +01005115 rcu_read_lock();
5116 regdom = rcu_dereference(cfg80211_regdomain);
5117
5118 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5119 (regdom->dfs_region &&
5120 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5121 goto nla_put_failure_rcu;
5122
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005123 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5124 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005125 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005126
Johannes Berg458f4f92012-12-06 15:47:38 +01005127 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005128 struct nlattr *nl_reg_rule;
5129 const struct ieee80211_reg_rule *reg_rule;
5130 const struct ieee80211_freq_range *freq_range;
5131 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005132 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005133
Johannes Berg458f4f92012-12-06 15:47:38 +01005134 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005135 freq_range = &reg_rule->freq_range;
5136 power_rule = &reg_rule->power_rule;
5137
5138 nl_reg_rule = nla_nest_start(msg, i);
5139 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005140 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005141
Janusz Dziedzic97524822014-01-30 09:52:20 +01005142 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5143 if (!max_bandwidth_khz)
5144 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5145 reg_rule);
5146
David S. Miller9360ffd2012-03-29 04:41:26 -04005147 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5148 reg_rule->flags) ||
5149 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5150 freq_range->start_freq_khz) ||
5151 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5152 freq_range->end_freq_khz) ||
5153 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005154 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005155 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5156 power_rule->max_antenna_gain) ||
5157 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005158 power_rule->max_eirp) ||
5159 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5160 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005161 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005162
5163 nla_nest_end(msg, nl_reg_rule);
5164 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005165 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005166
5167 nla_nest_end(msg, nl_reg_rules);
5168
5169 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005170 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005171
Johannes Berg458f4f92012-12-06 15:47:38 +01005172nla_put_failure_rcu:
5173 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005174nla_put_failure:
5175 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005176put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005177 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005178 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005179}
5180
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005181static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5182{
5183 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5184 struct nlattr *nl_reg_rule;
5185 char *alpha2 = NULL;
5186 int rem_reg_rules = 0, r = 0;
5187 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005188 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005189 struct ieee80211_regdomain *rd = NULL;
5190
5191 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5192 return -EINVAL;
5193
5194 if (!info->attrs[NL80211_ATTR_REG_RULES])
5195 return -EINVAL;
5196
5197 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5198
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005199 if (info->attrs[NL80211_ATTR_DFS_REGION])
5200 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5201
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005202 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005203 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005204 num_rules++;
5205 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005206 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005207 }
5208
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005209 if (!reg_is_valid_request(alpha2))
5210 return -EINVAL;
5211
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005212 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005213 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005214
5215 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005216 if (!rd)
5217 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005218
5219 rd->n_reg_rules = num_rules;
5220 rd->alpha2[0] = alpha2[0];
5221 rd->alpha2[1] = alpha2[1];
5222
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005223 /*
5224 * Disable DFS master mode if the DFS region was
5225 * not supported or known on this kernel.
5226 */
5227 if (reg_supported_dfs_region(dfs_region))
5228 rd->dfs_region = dfs_region;
5229
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005230 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005231 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005232 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5233 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5234 reg_rule_policy);
5235 if (r)
5236 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005237 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5238 if (r)
5239 goto bad_reg;
5240
5241 rule_idx++;
5242
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005243 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5244 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005245 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005246 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005247 }
5248
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005249 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005250 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005251 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005252
Johannes Bergd2372b32008-10-24 20:32:20 +02005253 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005254 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005255 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005256}
5257
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005258static int validate_scan_freqs(struct nlattr *freqs)
5259{
5260 struct nlattr *attr1, *attr2;
5261 int n_channels = 0, tmp1, tmp2;
5262
5263 nla_for_each_nested(attr1, freqs, tmp1) {
5264 n_channels++;
5265 /*
5266 * Some hardware has a limited channel list for
5267 * scanning, and it is pretty much nonsensical
5268 * to scan for a channel twice, so disallow that
5269 * and don't require drivers to check that the
5270 * channel list they get isn't longer than what
5271 * they can scan, as long as they can scan all
5272 * the channels they registered at once.
5273 */
5274 nla_for_each_nested(attr2, freqs, tmp2)
5275 if (attr1 != attr2 &&
5276 nla_get_u32(attr1) == nla_get_u32(attr2))
5277 return 0;
5278 }
5279
5280 return n_channels;
5281}
5282
Johannes Berg2a519312009-02-10 21:25:55 +01005283static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5284{
Johannes Berg4c476992010-10-04 21:36:35 +02005285 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005286 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005287 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005288 struct nlattr *attr;
5289 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005290 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005291 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005292
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005293 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5294 return -EINVAL;
5295
Johannes Berg79c97e92009-07-07 03:56:12 +02005296 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005297
Johannes Berg4c476992010-10-04 21:36:35 +02005298 if (!rdev->ops->scan)
5299 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005300
Johannes Bergf9d15d12014-01-22 11:14:19 +02005301 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005302 err = -EBUSY;
5303 goto unlock;
5304 }
Johannes Berg2a519312009-02-10 21:25:55 +01005305
5306 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005307 n_channels = validate_scan_freqs(
5308 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005309 if (!n_channels) {
5310 err = -EINVAL;
5311 goto unlock;
5312 }
Johannes Berg2a519312009-02-10 21:25:55 +01005313 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005314 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005315 }
5316
5317 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5318 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5319 n_ssids++;
5320
Johannes Bergf9f47522013-03-19 15:04:07 +01005321 if (n_ssids > wiphy->max_scan_ssids) {
5322 err = -EINVAL;
5323 goto unlock;
5324 }
Johannes Berg2a519312009-02-10 21:25:55 +01005325
Jouni Malinen70692ad2009-02-16 19:39:13 +02005326 if (info->attrs[NL80211_ATTR_IE])
5327 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5328 else
5329 ie_len = 0;
5330
Johannes Bergf9f47522013-03-19 15:04:07 +01005331 if (ie_len > wiphy->max_scan_ie_len) {
5332 err = -EINVAL;
5333 goto unlock;
5334 }
Johannes Berg18a83652009-03-31 12:12:05 +02005335
Johannes Berg2a519312009-02-10 21:25:55 +01005336 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005337 + sizeof(*request->ssids) * n_ssids
5338 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005339 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005340 if (!request) {
5341 err = -ENOMEM;
5342 goto unlock;
5343 }
Johannes Berg2a519312009-02-10 21:25:55 +01005344
Johannes Berg2a519312009-02-10 21:25:55 +01005345 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005346 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005347 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005348 if (ie_len) {
5349 if (request->ssids)
5350 request->ie = (void *)(request->ssids + n_ssids);
5351 else
5352 request->ie = (void *)(request->channels + n_channels);
5353 }
Johannes Berg2a519312009-02-10 21:25:55 +01005354
Johannes Berg584991d2009-11-02 13:32:03 +01005355 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005356 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5357 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005358 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005359 struct ieee80211_channel *chan;
5360
5361 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5362
5363 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005364 err = -EINVAL;
5365 goto out_free;
5366 }
Johannes Berg584991d2009-11-02 13:32:03 +01005367
5368 /* ignore disabled channels */
5369 if (chan->flags & IEEE80211_CHAN_DISABLED)
5370 continue;
5371
5372 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005373 i++;
5374 }
5375 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005376 enum ieee80211_band band;
5377
Johannes Berg2a519312009-02-10 21:25:55 +01005378 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005379 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5380 int j;
5381 if (!wiphy->bands[band])
5382 continue;
5383 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005384 struct ieee80211_channel *chan;
5385
5386 chan = &wiphy->bands[band]->channels[j];
5387
5388 if (chan->flags & IEEE80211_CHAN_DISABLED)
5389 continue;
5390
5391 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005392 i++;
5393 }
5394 }
5395 }
5396
Johannes Berg584991d2009-11-02 13:32:03 +01005397 if (!i) {
5398 err = -EINVAL;
5399 goto out_free;
5400 }
5401
5402 request->n_channels = i;
5403
Johannes Berg2a519312009-02-10 21:25:55 +01005404 i = 0;
5405 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5406 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005407 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005408 err = -EINVAL;
5409 goto out_free;
5410 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005411 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005412 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005413 i++;
5414 }
5415 }
5416
Jouni Malinen70692ad2009-02-16 19:39:13 +02005417 if (info->attrs[NL80211_ATTR_IE]) {
5418 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005419 memcpy((void *)request->ie,
5420 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005421 request->ie_len);
5422 }
5423
Johannes Berg34850ab2011-07-18 18:08:35 +02005424 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005425 if (wiphy->bands[i])
5426 request->rates[i] =
5427 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005428
5429 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5430 nla_for_each_nested(attr,
5431 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5432 tmp) {
5433 enum ieee80211_band band = nla_type(attr);
5434
Dan Carpenter84404622011-07-29 11:52:18 +03005435 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005436 err = -EINVAL;
5437 goto out_free;
5438 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005439
5440 if (!wiphy->bands[band])
5441 continue;
5442
Johannes Berg34850ab2011-07-18 18:08:35 +02005443 err = ieee80211_get_ratemask(wiphy->bands[band],
5444 nla_data(attr),
5445 nla_len(attr),
5446 &request->rates[band]);
5447 if (err)
5448 goto out_free;
5449 }
5450 }
5451
Sam Leffler46856bb2012-10-11 21:03:32 -07005452 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005453 request->flags = nla_get_u32(
5454 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005455 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5456 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005457 err = -EOPNOTSUPP;
5458 goto out_free;
5459 }
5460 }
Sam Lefflered4737712012-10-11 21:03:31 -07005461
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305462 request->no_cck =
5463 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5464
Johannes Bergfd014282012-06-18 19:17:03 +02005465 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005466 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005467 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005468
Johannes Berg79c97e92009-07-07 03:56:12 +02005469 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005470 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005471
Johannes Berg463d0182009-07-14 00:33:35 +02005472 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005473 nl80211_send_scan_start(rdev, wdev);
5474 if (wdev->netdev)
5475 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005476 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005477 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005478 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005479 kfree(request);
5480 }
Johannes Berg3b858752009-03-12 09:55:09 +01005481
Johannes Bergf9f47522013-03-19 15:04:07 +01005482 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005483 return err;
5484}
5485
Luciano Coelho807f8a82011-05-11 17:09:35 +03005486static int nl80211_start_sched_scan(struct sk_buff *skb,
5487 struct genl_info *info)
5488{
5489 struct cfg80211_sched_scan_request *request;
5490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5491 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005492 struct nlattr *attr;
5493 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005494 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005495 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005496 enum ieee80211_band band;
5497 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005498 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005499 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005500
5501 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5502 !rdev->ops->sched_scan_start)
5503 return -EOPNOTSUPP;
5504
5505 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5506 return -EINVAL;
5507
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005508 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5509 return -EINVAL;
5510
5511 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5512 if (interval == 0)
5513 return -EINVAL;
5514
Luciano Coelho807f8a82011-05-11 17:09:35 +03005515 wiphy = &rdev->wiphy;
5516
5517 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5518 n_channels = validate_scan_freqs(
5519 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5520 if (!n_channels)
5521 return -EINVAL;
5522 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005523 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005524 }
5525
5526 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5527 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5528 tmp)
5529 n_ssids++;
5530
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005531 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005532 return -EINVAL;
5533
Johannes Bergea73cbc2014-01-24 10:53:53 +01005534 /*
5535 * First, count the number of 'real' matchsets. Due to an issue with
5536 * the old implementation, matchsets containing only the RSSI attribute
5537 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5538 * RSSI for all matchsets, rather than their own matchset for reporting
5539 * all APs with a strong RSSI. This is needed to be compatible with
5540 * older userspace that treated a matchset with only the RSSI as the
5541 * global RSSI for all other matchsets - if there are other matchsets.
5542 */
5543 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005544 nla_for_each_nested(attr,
5545 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005546 tmp) {
5547 struct nlattr *rssi;
5548
5549 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5550 nla_data(attr), nla_len(attr),
5551 nl80211_match_policy);
5552 if (err)
5553 return err;
5554 /* add other standalone attributes here */
5555 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5556 n_match_sets++;
5557 continue;
5558 }
5559 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5560 if (rssi)
5561 default_match_rssi = nla_get_s32(rssi);
5562 }
5563 }
5564
5565 /* However, if there's no other matchset, add the RSSI one */
5566 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5567 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005568
5569 if (n_match_sets > wiphy->max_match_sets)
5570 return -EINVAL;
5571
Luciano Coelho807f8a82011-05-11 17:09:35 +03005572 if (info->attrs[NL80211_ATTR_IE])
5573 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5574 else
5575 ie_len = 0;
5576
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005577 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005578 return -EINVAL;
5579
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005580 if (rdev->sched_scan_req) {
5581 err = -EINPROGRESS;
5582 goto out;
5583 }
5584
Luciano Coelho807f8a82011-05-11 17:09:35 +03005585 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005586 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005587 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005588 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005589 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005590 if (!request) {
5591 err = -ENOMEM;
5592 goto out;
5593 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005594
5595 if (n_ssids)
5596 request->ssids = (void *)&request->channels[n_channels];
5597 request->n_ssids = n_ssids;
5598 if (ie_len) {
5599 if (request->ssids)
5600 request->ie = (void *)(request->ssids + n_ssids);
5601 else
5602 request->ie = (void *)(request->channels + n_channels);
5603 }
5604
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005605 if (n_match_sets) {
5606 if (request->ie)
5607 request->match_sets = (void *)(request->ie + ie_len);
5608 else if (request->ssids)
5609 request->match_sets =
5610 (void *)(request->ssids + n_ssids);
5611 else
5612 request->match_sets =
5613 (void *)(request->channels + n_channels);
5614 }
5615 request->n_match_sets = n_match_sets;
5616
Luciano Coelho807f8a82011-05-11 17:09:35 +03005617 i = 0;
5618 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5619 /* user specified, bail out if channel not found */
5620 nla_for_each_nested(attr,
5621 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5622 tmp) {
5623 struct ieee80211_channel *chan;
5624
5625 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5626
5627 if (!chan) {
5628 err = -EINVAL;
5629 goto out_free;
5630 }
5631
5632 /* ignore disabled channels */
5633 if (chan->flags & IEEE80211_CHAN_DISABLED)
5634 continue;
5635
5636 request->channels[i] = chan;
5637 i++;
5638 }
5639 } else {
5640 /* all channels */
5641 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5642 int j;
5643 if (!wiphy->bands[band])
5644 continue;
5645 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5646 struct ieee80211_channel *chan;
5647
5648 chan = &wiphy->bands[band]->channels[j];
5649
5650 if (chan->flags & IEEE80211_CHAN_DISABLED)
5651 continue;
5652
5653 request->channels[i] = chan;
5654 i++;
5655 }
5656 }
5657 }
5658
5659 if (!i) {
5660 err = -EINVAL;
5661 goto out_free;
5662 }
5663
5664 request->n_channels = i;
5665
5666 i = 0;
5667 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5668 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5669 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005670 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005671 err = -EINVAL;
5672 goto out_free;
5673 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005674 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005675 memcpy(request->ssids[i].ssid, nla_data(attr),
5676 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005677 i++;
5678 }
5679 }
5680
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005681 i = 0;
5682 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5683 nla_for_each_nested(attr,
5684 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5685 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005686 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005687
Johannes Bergae811e22014-01-24 10:17:47 +01005688 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5689 nla_data(attr), nla_len(attr),
5690 nl80211_match_policy);
5691 if (err)
5692 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005693 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005694 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005695 if (WARN_ON(i >= n_match_sets)) {
5696 /* this indicates a programming error,
5697 * the loop above should have verified
5698 * things properly
5699 */
5700 err = -EINVAL;
5701 goto out_free;
5702 }
5703
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005704 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5705 err = -EINVAL;
5706 goto out_free;
5707 }
5708 memcpy(request->match_sets[i].ssid.ssid,
5709 nla_data(ssid), nla_len(ssid));
5710 request->match_sets[i].ssid.ssid_len =
5711 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005712 /* special attribute - old implemenation w/a */
5713 request->match_sets[i].rssi_thold =
5714 default_match_rssi;
5715 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5716 if (rssi)
5717 request->match_sets[i].rssi_thold =
5718 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005719 }
5720 i++;
5721 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005722
5723 /* there was no other matchset, so the RSSI one is alone */
5724 if (i == 0)
5725 request->match_sets[0].rssi_thold = default_match_rssi;
5726
5727 request->min_rssi_thold = INT_MAX;
5728 for (i = 0; i < n_match_sets; i++)
5729 request->min_rssi_thold =
5730 min(request->match_sets[i].rssi_thold,
5731 request->min_rssi_thold);
5732 } else {
5733 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005734 }
5735
Johannes Berg9900e482014-02-04 21:01:25 +01005736 if (ie_len) {
5737 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005738 memcpy((void *)request->ie,
5739 nla_data(info->attrs[NL80211_ATTR_IE]),
5740 request->ie_len);
5741 }
5742
Sam Leffler46856bb2012-10-11 21:03:32 -07005743 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005744 request->flags = nla_get_u32(
5745 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005746 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5747 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005748 err = -EOPNOTSUPP;
5749 goto out_free;
5750 }
5751 }
Sam Lefflered4737712012-10-11 21:03:31 -07005752
Luciano Coelho807f8a82011-05-11 17:09:35 +03005753 request->dev = dev;
5754 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005755 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005756 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005757
Hila Gonene35e4d22012-06-27 17:19:42 +03005758 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005759 if (!err) {
5760 rdev->sched_scan_req = request;
5761 nl80211_send_sched_scan(rdev, dev,
5762 NL80211_CMD_START_SCHED_SCAN);
5763 goto out;
5764 }
5765
5766out_free:
5767 kfree(request);
5768out:
5769 return err;
5770}
5771
5772static int nl80211_stop_sched_scan(struct sk_buff *skb,
5773 struct genl_info *info)
5774{
5775 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5776
5777 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5778 !rdev->ops->sched_scan_stop)
5779 return -EOPNOTSUPP;
5780
Johannes Berg5fe231e2013-05-08 21:45:15 +02005781 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005782}
5783
Simon Wunderlich04f39042013-02-08 18:16:19 +01005784static int nl80211_start_radar_detection(struct sk_buff *skb,
5785 struct genl_info *info)
5786{
5787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5788 struct net_device *dev = info->user_ptr[1];
5789 struct wireless_dev *wdev = dev->ieee80211_ptr;
5790 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005791 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005792 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005793 int err;
5794
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005795 dfs_region = reg_get_dfs_region(wdev->wiphy);
5796 if (dfs_region == NL80211_DFS_UNSET)
5797 return -EINVAL;
5798
Simon Wunderlich04f39042013-02-08 18:16:19 +01005799 err = nl80211_parse_chandef(rdev, info, &chandef);
5800 if (err)
5801 return err;
5802
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005803 if (netif_carrier_ok(dev))
5804 return -EBUSY;
5805
Simon Wunderlich04f39042013-02-08 18:16:19 +01005806 if (wdev->cac_started)
5807 return -EBUSY;
5808
5809 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5810 if (err < 0)
5811 return err;
5812
5813 if (err == 0)
5814 return -EINVAL;
5815
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005816 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005817 return -EINVAL;
5818
5819 if (!rdev->ops->start_radar_detection)
5820 return -EOPNOTSUPP;
5821
Simon Wunderlich04f39042013-02-08 18:16:19 +01005822 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5823 chandef.chan, CHAN_MODE_SHARED,
5824 BIT(chandef.width));
5825 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005826 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005827
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005828 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
5829 if (WARN_ON(!cac_time_ms))
5830 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
5831
5832 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
5833 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01005834 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005835 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005836 wdev->cac_started = true;
5837 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005838 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005839 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005840 return err;
5841}
5842
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005843static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5844{
5845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5846 struct net_device *dev = info->user_ptr[1];
5847 struct wireless_dev *wdev = dev->ieee80211_ptr;
5848 struct cfg80211_csa_settings params;
5849 /* csa_attrs is defined static to avoid waste of stack size - this
5850 * function is called under RTNL lock, so this should not be a problem.
5851 */
5852 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5853 u8 radar_detect_width = 0;
5854 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005855 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005856
5857 if (!rdev->ops->channel_switch ||
5858 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5859 return -EOPNOTSUPP;
5860
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005861 switch (dev->ieee80211_ptr->iftype) {
5862 case NL80211_IFTYPE_AP:
5863 case NL80211_IFTYPE_P2P_GO:
5864 need_new_beacon = true;
5865
5866 /* useless if AP is not running */
5867 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005868 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005869 break;
5870 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005871 if (!wdev->ssid_len)
5872 return -ENOTCONN;
5873 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005874 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005875 if (!wdev->mesh_id_len)
5876 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005877 break;
5878 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005879 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005880 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005881
5882 memset(&params, 0, sizeof(params));
5883
5884 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5885 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5886 return -EINVAL;
5887
5888 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005889 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005890 return -EINVAL;
5891
5892 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5893
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005894 if (!need_new_beacon)
5895 goto skip_beacons;
5896
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005897 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5898 if (err)
5899 return err;
5900
5901 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5902 info->attrs[NL80211_ATTR_CSA_IES],
5903 nl80211_policy);
5904 if (err)
5905 return err;
5906
5907 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5908 if (err)
5909 return err;
5910
5911 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5912 return -EINVAL;
5913
5914 params.counter_offset_beacon =
5915 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5916 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5917 return -EINVAL;
5918
5919 /* sanity check - counters should be the same */
5920 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5921 params.count)
5922 return -EINVAL;
5923
5924 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5925 params.counter_offset_presp =
5926 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5927 if (params.counter_offset_presp >=
5928 params.beacon_csa.probe_resp_len)
5929 return -EINVAL;
5930
5931 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5932 params.count)
5933 return -EINVAL;
5934 }
5935
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005936skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005937 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5938 if (err)
5939 return err;
5940
Ilan Peer174e0cd2014-02-23 09:13:01 +02005941 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
5942 wdev->iftype))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005943 return -EINVAL;
5944
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005945 switch (dev->ieee80211_ptr->iftype) {
5946 case NL80211_IFTYPE_AP:
5947 case NL80211_IFTYPE_P2P_GO:
5948 case NL80211_IFTYPE_ADHOC:
5949 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005950 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5951 &params.chandef);
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005952 if (err < 0)
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005953 return err;
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005954 if (err) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005955 radar_detect_width = BIT(params.chandef.width);
5956 params.radar_required = true;
5957 }
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005958 break;
5959 default:
5960 break;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005961 }
5962
5963 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5964 params.chandef.chan,
5965 CHAN_MODE_SHARED,
5966 radar_detect_width);
5967 if (err)
5968 return err;
5969
5970 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5971 params.block_tx = true;
5972
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005973 wdev_lock(wdev);
5974 err = rdev_channel_switch(rdev, dev, &params);
5975 wdev_unlock(wdev);
5976
5977 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005978}
5979
Johannes Berg9720bb32011-06-21 09:45:33 +02005980static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5981 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005982 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005983 struct wireless_dev *wdev,
5984 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005985{
Johannes Berg48ab9052009-07-10 18:42:31 +02005986 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005987 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005988 void *hdr;
5989 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005990 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005991
5992 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005993
Eric W. Biederman15e47302012-09-07 20:12:54 +00005994 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005995 NL80211_CMD_NEW_SCAN_RESULTS);
5996 if (!hdr)
5997 return -1;
5998
Johannes Berg9720bb32011-06-21 09:45:33 +02005999 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6000
Johannes Berg97990a02013-04-19 01:02:55 +02006001 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6002 goto nla_put_failure;
6003 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04006004 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6005 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02006006 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6007 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006008
6009 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6010 if (!bss)
6011 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006012 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006013 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006014 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006015
6016 rcu_read_lock();
6017 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006018 if (ies) {
6019 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6020 goto fail_unlock_rcu;
6021 tsf = true;
6022 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6023 ies->len, ies->data))
6024 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006025 }
6026 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006027 if (ies) {
6028 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6029 goto fail_unlock_rcu;
6030 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6031 ies->len, ies->data))
6032 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006033 }
6034 rcu_read_unlock();
6035
David S. Miller9360ffd2012-03-29 04:41:26 -04006036 if (res->beacon_interval &&
6037 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6038 goto nla_put_failure;
6039 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6040 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006041 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006042 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6043 jiffies_to_msecs(jiffies - intbss->ts)))
6044 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006045
Johannes Berg77965c92009-02-18 18:45:06 +01006046 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006047 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006048 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6049 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006050 break;
6051 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006052 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6053 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006054 break;
6055 default:
6056 break;
6057 }
6058
Johannes Berg48ab9052009-07-10 18:42:31 +02006059 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006060 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006061 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006062 if (intbss == wdev->current_bss &&
6063 nla_put_u32(msg, NL80211_BSS_STATUS,
6064 NL80211_BSS_STATUS_ASSOCIATED))
6065 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006066 break;
6067 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006068 if (intbss == wdev->current_bss &&
6069 nla_put_u32(msg, NL80211_BSS_STATUS,
6070 NL80211_BSS_STATUS_IBSS_JOINED))
6071 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006072 break;
6073 default:
6074 break;
6075 }
6076
Johannes Berg2a519312009-02-10 21:25:55 +01006077 nla_nest_end(msg, bss);
6078
6079 return genlmsg_end(msg, hdr);
6080
Johannes Berg8cef2c92013-02-05 16:54:31 +01006081 fail_unlock_rcu:
6082 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006083 nla_put_failure:
6084 genlmsg_cancel(msg, hdr);
6085 return -EMSGSIZE;
6086}
6087
Johannes Berg97990a02013-04-19 01:02:55 +02006088static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006089{
Johannes Berg48ab9052009-07-10 18:42:31 +02006090 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006091 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006092 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006093 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006094 int err;
6095
Johannes Berg97990a02013-04-19 01:02:55 +02006096 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006097 if (err)
6098 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006099
Johannes Berg48ab9052009-07-10 18:42:31 +02006100 wdev_lock(wdev);
6101 spin_lock_bh(&rdev->bss_lock);
6102 cfg80211_bss_expire(rdev);
6103
Johannes Berg9720bb32011-06-21 09:45:33 +02006104 cb->seq = rdev->bss_generation;
6105
Johannes Berg48ab9052009-07-10 18:42:31 +02006106 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006107 if (++idx <= start)
6108 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006109 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006110 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006111 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006112 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006113 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006114 }
6115 }
6116
Johannes Berg48ab9052009-07-10 18:42:31 +02006117 spin_unlock_bh(&rdev->bss_lock);
6118 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006119
Johannes Berg97990a02013-04-19 01:02:55 +02006120 cb->args[2] = idx;
6121 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006122
Johannes Berg67748892010-10-04 21:14:06 +02006123 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006124}
6125
Eric W. Biederman15e47302012-09-07 20:12:54 +00006126static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006127 int flags, struct net_device *dev,
6128 struct survey_info *survey)
6129{
6130 void *hdr;
6131 struct nlattr *infoattr;
6132
Eric W. Biederman15e47302012-09-07 20:12:54 +00006133 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006134 NL80211_CMD_NEW_SURVEY_RESULTS);
6135 if (!hdr)
6136 return -ENOMEM;
6137
David S. Miller9360ffd2012-03-29 04:41:26 -04006138 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6139 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006140
6141 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6142 if (!infoattr)
6143 goto nla_put_failure;
6144
David S. Miller9360ffd2012-03-29 04:41:26 -04006145 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6146 survey->channel->center_freq))
6147 goto nla_put_failure;
6148
6149 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6150 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6151 goto nla_put_failure;
6152 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6153 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6154 goto nla_put_failure;
6155 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6156 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6157 survey->channel_time))
6158 goto nla_put_failure;
6159 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6160 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6161 survey->channel_time_busy))
6162 goto nla_put_failure;
6163 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6164 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6165 survey->channel_time_ext_busy))
6166 goto nla_put_failure;
6167 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6168 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6169 survey->channel_time_rx))
6170 goto nla_put_failure;
6171 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6172 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6173 survey->channel_time_tx))
6174 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006175
6176 nla_nest_end(msg, infoattr);
6177
6178 return genlmsg_end(msg, hdr);
6179
6180 nla_put_failure:
6181 genlmsg_cancel(msg, hdr);
6182 return -EMSGSIZE;
6183}
6184
6185static int nl80211_dump_survey(struct sk_buff *skb,
6186 struct netlink_callback *cb)
6187{
6188 struct survey_info survey;
6189 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006190 struct wireless_dev *wdev;
6191 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006192 int res;
6193
Johannes Berg97990a02013-04-19 01:02:55 +02006194 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006195 if (res)
6196 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006197
Johannes Berg97990a02013-04-19 01:02:55 +02006198 if (!wdev->netdev) {
6199 res = -EINVAL;
6200 goto out_err;
6201 }
6202
Holger Schurig61fa7132009-11-11 12:25:40 +01006203 if (!dev->ops->dump_survey) {
6204 res = -EOPNOTSUPP;
6205 goto out_err;
6206 }
6207
6208 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006209 struct ieee80211_channel *chan;
6210
Johannes Berg97990a02013-04-19 01:02:55 +02006211 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006212 if (res == -ENOENT)
6213 break;
6214 if (res)
6215 goto out_err;
6216
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006217 /* Survey without a channel doesn't make sense */
6218 if (!survey.channel) {
6219 res = -EINVAL;
6220 goto out;
6221 }
6222
6223 chan = ieee80211_get_channel(&dev->wiphy,
6224 survey.channel->center_freq);
6225 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6226 survey_idx++;
6227 continue;
6228 }
6229
Holger Schurig61fa7132009-11-11 12:25:40 +01006230 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006231 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006232 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006233 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006234 goto out;
6235 survey_idx++;
6236 }
6237
6238 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006239 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006240 res = skb->len;
6241 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006242 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006243 return res;
6244}
6245
Samuel Ortizb23aa672009-07-01 21:26:54 +02006246static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6247{
6248 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6249 NL80211_WPA_VERSION_2));
6250}
6251
Jouni Malinen636a5d32009-03-19 13:39:22 +02006252static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6253{
Johannes Berg4c476992010-10-04 21:36:35 +02006254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6255 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006256 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006257 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6258 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006259 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006260 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006261 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006262
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006263 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6264 return -EINVAL;
6265
6266 if (!info->attrs[NL80211_ATTR_MAC])
6267 return -EINVAL;
6268
Jouni Malinen17780922009-03-27 20:52:47 +02006269 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6270 return -EINVAL;
6271
Johannes Berg19957bb2009-07-02 17:20:43 +02006272 if (!info->attrs[NL80211_ATTR_SSID])
6273 return -EINVAL;
6274
6275 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6276 return -EINVAL;
6277
Johannes Bergfffd0932009-07-08 14:22:54 +02006278 err = nl80211_parse_key(info, &key);
6279 if (err)
6280 return err;
6281
6282 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006283 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6284 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006285 if (!key.p.key || !key.p.key_len)
6286 return -EINVAL;
6287 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6288 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6289 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6290 key.p.key_len != WLAN_KEY_LEN_WEP104))
6291 return -EINVAL;
6292 if (key.idx > 4)
6293 return -EINVAL;
6294 } else {
6295 key.p.key_len = 0;
6296 key.p.key = NULL;
6297 }
6298
Johannes Bergafea0b72010-08-10 09:46:42 +02006299 if (key.idx >= 0) {
6300 int i;
6301 bool ok = false;
6302 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6303 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6304 ok = true;
6305 break;
6306 }
6307 }
Johannes Berg4c476992010-10-04 21:36:35 +02006308 if (!ok)
6309 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006310 }
6311
Johannes Berg4c476992010-10-04 21:36:35 +02006312 if (!rdev->ops->auth)
6313 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006314
Johannes Berg074ac8d2010-09-16 14:58:22 +02006315 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006316 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6317 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006318
Johannes Berg19957bb2009-07-02 17:20:43 +02006319 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006320 chan = nl80211_get_valid_chan(&rdev->wiphy,
6321 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6322 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006323 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006324
Johannes Berg19957bb2009-07-02 17:20:43 +02006325 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6326 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6327
6328 if (info->attrs[NL80211_ATTR_IE]) {
6329 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6330 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6331 }
6332
6333 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006334 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006335 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006336
Jouni Malinene39e5b52012-09-30 19:29:39 +03006337 if (auth_type == NL80211_AUTHTYPE_SAE &&
6338 !info->attrs[NL80211_ATTR_SAE_DATA])
6339 return -EINVAL;
6340
6341 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6342 if (auth_type != NL80211_AUTHTYPE_SAE)
6343 return -EINVAL;
6344 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6345 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6346 /* need to include at least Auth Transaction and Status Code */
6347 if (sae_data_len < 4)
6348 return -EINVAL;
6349 }
6350
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006351 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6352
Johannes Berg95de8172012-01-20 13:55:25 +01006353 /*
6354 * Since we no longer track auth state, ignore
6355 * requests to only change local state.
6356 */
6357 if (local_state_change)
6358 return 0;
6359
Johannes Berg91bf9b22013-05-15 17:44:01 +02006360 wdev_lock(dev->ieee80211_ptr);
6361 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6362 ssid, ssid_len, ie, ie_len,
6363 key.p.key, key.p.key_len, key.idx,
6364 sae_data, sae_data_len);
6365 wdev_unlock(dev->ieee80211_ptr);
6366 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006367}
6368
Johannes Bergc0692b82010-08-27 14:26:53 +03006369static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6370 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006371 struct cfg80211_crypto_settings *settings,
6372 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006373{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006374 memset(settings, 0, sizeof(*settings));
6375
Samuel Ortizb23aa672009-07-01 21:26:54 +02006376 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6377
Johannes Bergc0692b82010-08-27 14:26:53 +03006378 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6379 u16 proto;
6380 proto = nla_get_u16(
6381 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6382 settings->control_port_ethertype = cpu_to_be16(proto);
6383 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6384 proto != ETH_P_PAE)
6385 return -EINVAL;
6386 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6387 settings->control_port_no_encrypt = true;
6388 } else
6389 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6390
Samuel Ortizb23aa672009-07-01 21:26:54 +02006391 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6392 void *data;
6393 int len, i;
6394
6395 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6396 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6397 settings->n_ciphers_pairwise = len / sizeof(u32);
6398
6399 if (len % sizeof(u32))
6400 return -EINVAL;
6401
Johannes Berg3dc27d22009-07-02 21:36:37 +02006402 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006403 return -EINVAL;
6404
6405 memcpy(settings->ciphers_pairwise, data, len);
6406
6407 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006408 if (!cfg80211_supported_cipher_suite(
6409 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006410 settings->ciphers_pairwise[i]))
6411 return -EINVAL;
6412 }
6413
6414 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6415 settings->cipher_group =
6416 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006417 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6418 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006419 return -EINVAL;
6420 }
6421
6422 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6423 settings->wpa_versions =
6424 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6425 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6426 return -EINVAL;
6427 }
6428
6429 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6430 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006431 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006432
6433 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6434 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6435 settings->n_akm_suites = len / sizeof(u32);
6436
6437 if (len % sizeof(u32))
6438 return -EINVAL;
6439
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006440 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6441 return -EINVAL;
6442
Samuel Ortizb23aa672009-07-01 21:26:54 +02006443 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006444 }
6445
6446 return 0;
6447}
6448
Jouni Malinen636a5d32009-03-19 13:39:22 +02006449static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6450{
Johannes Berg4c476992010-10-04 21:36:35 +02006451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6452 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006453 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006454 struct cfg80211_assoc_request req = {};
6455 const u8 *bssid, *ssid;
6456 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006457
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006458 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6459 return -EINVAL;
6460
6461 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006462 !info->attrs[NL80211_ATTR_SSID] ||
6463 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006464 return -EINVAL;
6465
Johannes Berg4c476992010-10-04 21:36:35 +02006466 if (!rdev->ops->assoc)
6467 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006468
Johannes Berg074ac8d2010-09-16 14:58:22 +02006469 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006470 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6471 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006472
Johannes Berg19957bb2009-07-02 17:20:43 +02006473 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006474
Jouni Malinen664834d2014-01-15 00:01:44 +02006475 chan = nl80211_get_valid_chan(&rdev->wiphy,
6476 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6477 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006478 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006479
Johannes Berg19957bb2009-07-02 17:20:43 +02006480 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6481 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006482
6483 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006484 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6485 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006486 }
6487
Jouni Malinendc6382c2009-05-06 22:09:37 +03006488 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006489 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006490 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006491 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006492 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006493 else if (mfp != NL80211_MFP_NO)
6494 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006495 }
6496
Johannes Berg3e5d7642009-07-07 14:37:26 +02006497 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006498 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006499
Ben Greear7e7c8922011-11-18 11:31:59 -08006500 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006501 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006502
6503 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006504 memcpy(&req.ht_capa_mask,
6505 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6506 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006507
6508 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006509 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006510 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006511 memcpy(&req.ht_capa,
6512 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6513 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006514 }
6515
Johannes Bergee2aca32013-02-21 17:36:01 +01006516 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006517 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006518
6519 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006520 memcpy(&req.vht_capa_mask,
6521 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6522 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006523
6524 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006525 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006526 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006527 memcpy(&req.vht_capa,
6528 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6529 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006530 }
6531
Johannes Bergf62fab72013-02-21 20:09:09 +01006532 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006533 if (!err) {
6534 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006535 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6536 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006537 wdev_unlock(dev->ieee80211_ptr);
6538 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006539
Jouni Malinen636a5d32009-03-19 13:39:22 +02006540 return err;
6541}
6542
6543static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6544{
Johannes Berg4c476992010-10-04 21:36:35 +02006545 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6546 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006547 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006548 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006549 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006550 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006551
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006552 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6553 return -EINVAL;
6554
6555 if (!info->attrs[NL80211_ATTR_MAC])
6556 return -EINVAL;
6557
6558 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6559 return -EINVAL;
6560
Johannes Berg4c476992010-10-04 21:36:35 +02006561 if (!rdev->ops->deauth)
6562 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006563
Johannes Berg074ac8d2010-09-16 14:58:22 +02006564 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006565 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6566 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006567
Johannes Berg19957bb2009-07-02 17:20:43 +02006568 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006569
Johannes Berg19957bb2009-07-02 17:20:43 +02006570 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6571 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006572 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006573 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006574 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006575
6576 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006577 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6578 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006579 }
6580
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006581 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6582
Johannes Berg91bf9b22013-05-15 17:44:01 +02006583 wdev_lock(dev->ieee80211_ptr);
6584 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6585 local_state_change);
6586 wdev_unlock(dev->ieee80211_ptr);
6587 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006588}
6589
6590static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6591{
Johannes Berg4c476992010-10-04 21:36:35 +02006592 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6593 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006594 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006595 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006596 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006597 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006598
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006599 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6600 return -EINVAL;
6601
6602 if (!info->attrs[NL80211_ATTR_MAC])
6603 return -EINVAL;
6604
6605 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6606 return -EINVAL;
6607
Johannes Berg4c476992010-10-04 21:36:35 +02006608 if (!rdev->ops->disassoc)
6609 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006610
Johannes Berg074ac8d2010-09-16 14:58:22 +02006611 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006612 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6613 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006614
Johannes Berg19957bb2009-07-02 17:20:43 +02006615 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006616
Johannes Berg19957bb2009-07-02 17:20:43 +02006617 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6618 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006619 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006620 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006621 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006622
6623 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006624 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6625 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006626 }
6627
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006628 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6629
Johannes Berg91bf9b22013-05-15 17:44:01 +02006630 wdev_lock(dev->ieee80211_ptr);
6631 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6632 local_state_change);
6633 wdev_unlock(dev->ieee80211_ptr);
6634 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006635}
6636
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006637static bool
6638nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6639 int mcast_rate[IEEE80211_NUM_BANDS],
6640 int rateval)
6641{
6642 struct wiphy *wiphy = &rdev->wiphy;
6643 bool found = false;
6644 int band, i;
6645
6646 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6647 struct ieee80211_supported_band *sband;
6648
6649 sband = wiphy->bands[band];
6650 if (!sband)
6651 continue;
6652
6653 for (i = 0; i < sband->n_bitrates; i++) {
6654 if (sband->bitrates[i].bitrate == rateval) {
6655 mcast_rate[band] = i + 1;
6656 found = true;
6657 break;
6658 }
6659 }
6660 }
6661
6662 return found;
6663}
6664
Johannes Berg04a773a2009-04-19 21:24:32 +02006665static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6666{
Johannes Berg4c476992010-10-04 21:36:35 +02006667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6668 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006669 struct cfg80211_ibss_params ibss;
6670 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006671 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006672 int err;
6673
Johannes Berg8e30bc52009-04-22 17:45:38 +02006674 memset(&ibss, 0, sizeof(ibss));
6675
Johannes Berg04a773a2009-04-19 21:24:32 +02006676 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6677 return -EINVAL;
6678
Johannes Berg683b6d32012-11-08 21:25:48 +01006679 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006680 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6681 return -EINVAL;
6682
Johannes Berg8e30bc52009-04-22 17:45:38 +02006683 ibss.beacon_interval = 100;
6684
6685 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6686 ibss.beacon_interval =
6687 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6688 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6689 return -EINVAL;
6690 }
6691
Johannes Berg4c476992010-10-04 21:36:35 +02006692 if (!rdev->ops->join_ibss)
6693 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006694
Johannes Berg4c476992010-10-04 21:36:35 +02006695 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6696 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006697
Johannes Berg79c97e92009-07-07 03:56:12 +02006698 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006699
Johannes Berg39193492011-09-16 13:45:25 +02006700 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006701 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006702
6703 if (!is_valid_ether_addr(ibss.bssid))
6704 return -EINVAL;
6705 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006706 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6707 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6708
6709 if (info->attrs[NL80211_ATTR_IE]) {
6710 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6711 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6712 }
6713
Johannes Berg683b6d32012-11-08 21:25:48 +01006714 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6715 if (err)
6716 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006717
Ilan Peer174e0cd2014-02-23 09:13:01 +02006718 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
6719 NL80211_IFTYPE_ADHOC))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006720 return -EINVAL;
6721
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006722 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006723 case NL80211_CHAN_WIDTH_5:
6724 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006725 case NL80211_CHAN_WIDTH_20_NOHT:
6726 break;
6727 case NL80211_CHAN_WIDTH_20:
6728 case NL80211_CHAN_WIDTH_40:
6729 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6730 break;
6731 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006732 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006733 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006734
Johannes Berg04a773a2009-04-19 21:24:32 +02006735 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006736 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006737
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006738 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6739 u8 *rates =
6740 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6741 int n_rates =
6742 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6743 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006744 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006745
Johannes Berg34850ab2011-07-18 18:08:35 +02006746 err = ieee80211_get_ratemask(sband, rates, n_rates,
6747 &ibss.basic_rates);
6748 if (err)
6749 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006750 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006751
Simon Wunderlich803768f2013-06-28 10:39:58 +02006752 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6753 memcpy(&ibss.ht_capa_mask,
6754 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6755 sizeof(ibss.ht_capa_mask));
6756
6757 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6758 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6759 return -EINVAL;
6760 memcpy(&ibss.ht_capa,
6761 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6762 sizeof(ibss.ht_capa));
6763 }
6764
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006765 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6766 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6767 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6768 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006769
Johannes Berg4c476992010-10-04 21:36:35 +02006770 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306771 bool no_ht = false;
6772
Johannes Berg4c476992010-10-04 21:36:35 +02006773 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306774 info->attrs[NL80211_ATTR_KEYS],
6775 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006776 if (IS_ERR(connkeys))
6777 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306778
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006779 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6780 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306781 kfree(connkeys);
6782 return -EINVAL;
6783 }
Johannes Berg4c476992010-10-04 21:36:35 +02006784 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006785
Antonio Quartulli267335d2012-01-31 20:25:47 +01006786 ibss.control_port =
6787 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6788
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006789 ibss.userspace_handles_dfs =
6790 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6791
Johannes Berg4c476992010-10-04 21:36:35 +02006792 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006793 if (err)
6794 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006795 return err;
6796}
6797
6798static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6799{
Johannes Berg4c476992010-10-04 21:36:35 +02006800 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6801 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006802
Johannes Berg4c476992010-10-04 21:36:35 +02006803 if (!rdev->ops->leave_ibss)
6804 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006805
Johannes Berg4c476992010-10-04 21:36:35 +02006806 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6807 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006808
Johannes Berg4c476992010-10-04 21:36:35 +02006809 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006810}
6811
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006812static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6813{
6814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6815 struct net_device *dev = info->user_ptr[1];
6816 int mcast_rate[IEEE80211_NUM_BANDS];
6817 u32 nla_rate;
6818 int err;
6819
6820 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6821 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6822 return -EOPNOTSUPP;
6823
6824 if (!rdev->ops->set_mcast_rate)
6825 return -EOPNOTSUPP;
6826
6827 memset(mcast_rate, 0, sizeof(mcast_rate));
6828
6829 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6830 return -EINVAL;
6831
6832 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6833 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6834 return -EINVAL;
6835
6836 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6837
6838 return err;
6839}
6840
Johannes Bergad7e7182013-11-13 13:37:47 +01006841static struct sk_buff *
6842__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6843 int approxlen, u32 portid, u32 seq,
6844 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006845 enum nl80211_attrs attr,
6846 const struct nl80211_vendor_cmd_info *info,
6847 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006848{
6849 struct sk_buff *skb;
6850 void *hdr;
6851 struct nlattr *data;
6852
6853 skb = nlmsg_new(approxlen + 100, gfp);
6854 if (!skb)
6855 return NULL;
6856
6857 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6858 if (!hdr) {
6859 kfree_skb(skb);
6860 return NULL;
6861 }
6862
6863 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6864 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006865
6866 if (info) {
6867 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6868 info->vendor_id))
6869 goto nla_put_failure;
6870 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6871 info->subcmd))
6872 goto nla_put_failure;
6873 }
6874
Johannes Bergad7e7182013-11-13 13:37:47 +01006875 data = nla_nest_start(skb, attr);
6876
6877 ((void **)skb->cb)[0] = rdev;
6878 ((void **)skb->cb)[1] = hdr;
6879 ((void **)skb->cb)[2] = data;
6880
6881 return skb;
6882
6883 nla_put_failure:
6884 kfree_skb(skb);
6885 return NULL;
6886}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006887
Johannes Berge03ad6e2014-01-01 17:22:30 +01006888struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6889 enum nl80211_commands cmd,
6890 enum nl80211_attrs attr,
6891 int vendor_event_idx,
6892 int approxlen, gfp_t gfp)
6893{
6894 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6895 const struct nl80211_vendor_cmd_info *info;
6896
6897 switch (cmd) {
6898 case NL80211_CMD_TESTMODE:
6899 if (WARN_ON(vendor_event_idx != -1))
6900 return NULL;
6901 info = NULL;
6902 break;
6903 case NL80211_CMD_VENDOR:
6904 if (WARN_ON(vendor_event_idx < 0 ||
6905 vendor_event_idx >= wiphy->n_vendor_events))
6906 return NULL;
6907 info = &wiphy->vendor_events[vendor_event_idx];
6908 break;
6909 default:
6910 WARN_ON(1);
6911 return NULL;
6912 }
6913
6914 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6915 cmd, attr, info, gfp);
6916}
6917EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6918
6919void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6920{
6921 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6922 void *hdr = ((void **)skb->cb)[1];
6923 struct nlattr *data = ((void **)skb->cb)[2];
6924 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6925
6926 nla_nest_end(skb, data);
6927 genlmsg_end(skb, hdr);
6928
6929 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6930 mcgrp = NL80211_MCGRP_VENDOR;
6931
6932 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6933 mcgrp, gfp);
6934}
6935EXPORT_SYMBOL(__cfg80211_send_event_skb);
6936
Johannes Bergaff89a92009-07-01 21:26:51 +02006937#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006938static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6939{
Johannes Berg4c476992010-10-04 21:36:35 +02006940 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006941 struct wireless_dev *wdev =
6942 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006943 int err;
6944
David Spinadelfc73f112013-07-31 18:04:15 +03006945 if (!rdev->ops->testmode_cmd)
6946 return -EOPNOTSUPP;
6947
6948 if (IS_ERR(wdev)) {
6949 err = PTR_ERR(wdev);
6950 if (err != -EINVAL)
6951 return err;
6952 wdev = NULL;
6953 } else if (wdev->wiphy != &rdev->wiphy) {
6954 return -EINVAL;
6955 }
6956
Johannes Bergaff89a92009-07-01 21:26:51 +02006957 if (!info->attrs[NL80211_ATTR_TESTDATA])
6958 return -EINVAL;
6959
Johannes Bergad7e7182013-11-13 13:37:47 +01006960 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006961 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006962 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6963 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006964 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006965
Johannes Bergaff89a92009-07-01 21:26:51 +02006966 return err;
6967}
6968
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006969static int nl80211_testmode_dump(struct sk_buff *skb,
6970 struct netlink_callback *cb)
6971{
Johannes Berg00918d32011-12-13 17:22:05 +01006972 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006973 int err;
6974 long phy_idx;
6975 void *data = NULL;
6976 int data_len = 0;
6977
Johannes Berg5fe231e2013-05-08 21:45:15 +02006978 rtnl_lock();
6979
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006980 if (cb->args[0]) {
6981 /*
6982 * 0 is a valid index, but not valid for args[0],
6983 * so we need to offset by 1.
6984 */
6985 phy_idx = cb->args[0] - 1;
6986 } else {
6987 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6988 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6989 nl80211_policy);
6990 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006991 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006992
Johannes Berg2bd7e352012-06-15 14:23:16 +02006993 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6994 nl80211_fam.attrbuf);
6995 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006996 err = PTR_ERR(rdev);
6997 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006998 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006999 phy_idx = rdev->wiphy_idx;
7000 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02007001
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007002 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7003 cb->args[1] =
7004 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7005 }
7006
7007 if (cb->args[1]) {
7008 data = nla_data((void *)cb->args[1]);
7009 data_len = nla_len((void *)cb->args[1]);
7010 }
7011
Johannes Berg00918d32011-12-13 17:22:05 +01007012 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7013 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007014 err = -ENOENT;
7015 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007016 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007017
Johannes Berg00918d32011-12-13 17:22:05 +01007018 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007019 err = -EOPNOTSUPP;
7020 goto out_err;
7021 }
7022
7023 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007024 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007025 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7026 NL80211_CMD_TESTMODE);
7027 struct nlattr *tmdata;
7028
Dan Carpentercb35fba2013-08-14 14:50:01 +03007029 if (!hdr)
7030 break;
7031
David S. Miller9360ffd2012-03-29 04:41:26 -04007032 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007033 genlmsg_cancel(skb, hdr);
7034 break;
7035 }
7036
7037 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7038 if (!tmdata) {
7039 genlmsg_cancel(skb, hdr);
7040 break;
7041 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007042 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007043 nla_nest_end(skb, tmdata);
7044
7045 if (err == -ENOBUFS || err == -ENOENT) {
7046 genlmsg_cancel(skb, hdr);
7047 break;
7048 } else if (err) {
7049 genlmsg_cancel(skb, hdr);
7050 goto out_err;
7051 }
7052
7053 genlmsg_end(skb, hdr);
7054 }
7055
7056 err = skb->len;
7057 /* see above */
7058 cb->args[0] = phy_idx + 1;
7059 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007060 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007061 return err;
7062}
Johannes Bergaff89a92009-07-01 21:26:51 +02007063#endif
7064
Samuel Ortizb23aa672009-07-01 21:26:54 +02007065static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7066{
Johannes Berg4c476992010-10-04 21:36:35 +02007067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7068 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007069 struct cfg80211_connect_params connect;
7070 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007071 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007072 int err;
7073
7074 memset(&connect, 0, sizeof(connect));
7075
7076 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7077 return -EINVAL;
7078
7079 if (!info->attrs[NL80211_ATTR_SSID] ||
7080 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7081 return -EINVAL;
7082
7083 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7084 connect.auth_type =
7085 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007086 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7087 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007088 return -EINVAL;
7089 } else
7090 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7091
7092 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7093
Johannes Bergc0692b82010-08-27 14:26:53 +03007094 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007095 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007096 if (err)
7097 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007098
Johannes Berg074ac8d2010-09-16 14:58:22 +02007099 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007100 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7101 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007102
Johannes Berg79c97e92009-07-07 03:56:12 +02007103 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007104
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307105 connect.bg_scan_period = -1;
7106 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7107 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7108 connect.bg_scan_period =
7109 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7110 }
7111
Samuel Ortizb23aa672009-07-01 21:26:54 +02007112 if (info->attrs[NL80211_ATTR_MAC])
7113 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007114 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7115 connect.bssid_hint =
7116 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007117 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7118 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7119
7120 if (info->attrs[NL80211_ATTR_IE]) {
7121 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7122 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7123 }
7124
Jouni Malinencee00a92013-01-15 17:15:57 +02007125 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7126 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7127 if (connect.mfp != NL80211_MFP_REQUIRED &&
7128 connect.mfp != NL80211_MFP_NO)
7129 return -EINVAL;
7130 } else {
7131 connect.mfp = NL80211_MFP_NO;
7132 }
7133
Samuel Ortizb23aa672009-07-01 21:26:54 +02007134 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007135 connect.channel = nl80211_get_valid_chan(
7136 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7137 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007138 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007139 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007140 connect.channel_hint = nl80211_get_valid_chan(
7141 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7142 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007143 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007144 }
7145
Johannes Bergfffd0932009-07-08 14:22:54 +02007146 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7147 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307148 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007149 if (IS_ERR(connkeys))
7150 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007151 }
7152
Ben Greear7e7c8922011-11-18 11:31:59 -08007153 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7154 connect.flags |= ASSOC_REQ_DISABLE_HT;
7155
7156 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7157 memcpy(&connect.ht_capa_mask,
7158 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7159 sizeof(connect.ht_capa_mask));
7160
7161 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007162 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7163 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007164 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007165 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007166 memcpy(&connect.ht_capa,
7167 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7168 sizeof(connect.ht_capa));
7169 }
7170
Johannes Bergee2aca32013-02-21 17:36:01 +01007171 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7172 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7173
7174 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7175 memcpy(&connect.vht_capa_mask,
7176 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7177 sizeof(connect.vht_capa_mask));
7178
7179 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7180 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7181 kfree(connkeys);
7182 return -EINVAL;
7183 }
7184 memcpy(&connect.vht_capa,
7185 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7186 sizeof(connect.vht_capa));
7187 }
7188
Johannes Berg83739b02013-05-15 17:44:01 +02007189 wdev_lock(dev->ieee80211_ptr);
7190 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7191 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007192 if (err)
7193 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007194 return err;
7195}
7196
7197static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7198{
Johannes Berg4c476992010-10-04 21:36:35 +02007199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7200 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007201 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007202 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007203
7204 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7205 reason = WLAN_REASON_DEAUTH_LEAVING;
7206 else
7207 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7208
7209 if (reason == 0)
7210 return -EINVAL;
7211
Johannes Berg074ac8d2010-09-16 14:58:22 +02007212 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007213 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7214 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007215
Johannes Berg83739b02013-05-15 17:44:01 +02007216 wdev_lock(dev->ieee80211_ptr);
7217 ret = cfg80211_disconnect(rdev, dev, reason, true);
7218 wdev_unlock(dev->ieee80211_ptr);
7219 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007220}
7221
Johannes Berg463d0182009-07-14 00:33:35 +02007222static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7223{
Johannes Berg4c476992010-10-04 21:36:35 +02007224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007225 struct net *net;
7226 int err;
7227 u32 pid;
7228
7229 if (!info->attrs[NL80211_ATTR_PID])
7230 return -EINVAL;
7231
7232 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7233
Johannes Berg463d0182009-07-14 00:33:35 +02007234 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007235 if (IS_ERR(net))
7236 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007237
7238 err = 0;
7239
7240 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007241 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7242 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007243
Johannes Berg463d0182009-07-14 00:33:35 +02007244 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007245 return err;
7246}
7247
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007248static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7249{
Johannes Berg4c476992010-10-04 21:36:35 +02007250 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007251 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7252 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007253 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007254 struct cfg80211_pmksa pmksa;
7255
7256 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7257
7258 if (!info->attrs[NL80211_ATTR_MAC])
7259 return -EINVAL;
7260
7261 if (!info->attrs[NL80211_ATTR_PMKID])
7262 return -EINVAL;
7263
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007264 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7265 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7266
Johannes Berg074ac8d2010-09-16 14:58:22 +02007267 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007268 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7269 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007270
7271 switch (info->genlhdr->cmd) {
7272 case NL80211_CMD_SET_PMKSA:
7273 rdev_ops = rdev->ops->set_pmksa;
7274 break;
7275 case NL80211_CMD_DEL_PMKSA:
7276 rdev_ops = rdev->ops->del_pmksa;
7277 break;
7278 default:
7279 WARN_ON(1);
7280 break;
7281 }
7282
Johannes Berg4c476992010-10-04 21:36:35 +02007283 if (!rdev_ops)
7284 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007285
Johannes Berg4c476992010-10-04 21:36:35 +02007286 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007287}
7288
7289static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7290{
Johannes Berg4c476992010-10-04 21:36:35 +02007291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7292 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007293
Johannes Berg074ac8d2010-09-16 14:58:22 +02007294 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007295 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7296 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007297
Johannes Berg4c476992010-10-04 21:36:35 +02007298 if (!rdev->ops->flush_pmksa)
7299 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007300
Hila Gonene35e4d22012-06-27 17:19:42 +03007301 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007302}
7303
Arik Nemtsov109086c2011-09-28 14:12:50 +03007304static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7305{
7306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7307 struct net_device *dev = info->user_ptr[1];
7308 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307309 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007310 u16 status_code;
7311 u8 *peer;
7312
7313 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7314 !rdev->ops->tdls_mgmt)
7315 return -EOPNOTSUPP;
7316
7317 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7318 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7319 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7320 !info->attrs[NL80211_ATTR_IE] ||
7321 !info->attrs[NL80211_ATTR_MAC])
7322 return -EINVAL;
7323
7324 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7325 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7326 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7327 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307328 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7329 peer_capability =
7330 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007331
Hila Gonene35e4d22012-06-27 17:19:42 +03007332 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307333 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007334 nla_data(info->attrs[NL80211_ATTR_IE]),
7335 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007336}
7337
7338static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7339{
7340 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7341 struct net_device *dev = info->user_ptr[1];
7342 enum nl80211_tdls_operation operation;
7343 u8 *peer;
7344
7345 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7346 !rdev->ops->tdls_oper)
7347 return -EOPNOTSUPP;
7348
7349 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7350 !info->attrs[NL80211_ATTR_MAC])
7351 return -EINVAL;
7352
7353 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7354 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7355
Hila Gonene35e4d22012-06-27 17:19:42 +03007356 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007357}
7358
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007359static int nl80211_remain_on_channel(struct sk_buff *skb,
7360 struct genl_info *info)
7361{
Johannes Berg4c476992010-10-04 21:36:35 +02007362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007363 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007364 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007365 struct sk_buff *msg;
7366 void *hdr;
7367 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007368 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007369 int err;
7370
7371 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7372 !info->attrs[NL80211_ATTR_DURATION])
7373 return -EINVAL;
7374
7375 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7376
Johannes Berg7c4ef712011-11-18 15:33:48 +01007377 if (!rdev->ops->remain_on_channel ||
7378 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007379 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007380
Johannes Bergebf348f2012-06-01 12:50:54 +02007381 /*
7382 * We should be on that channel for at least a minimum amount of
7383 * time (10ms) but no longer than the driver supports.
7384 */
7385 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7386 duration > rdev->wiphy.max_remain_on_channel_duration)
7387 return -EINVAL;
7388
Johannes Berg683b6d32012-11-08 21:25:48 +01007389 err = nl80211_parse_chandef(rdev, info, &chandef);
7390 if (err)
7391 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007392
7393 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007394 if (!msg)
7395 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007396
Eric W. Biederman15e47302012-09-07 20:12:54 +00007397 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007398 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007399 if (!hdr) {
7400 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007401 goto free_msg;
7402 }
7403
Johannes Berg683b6d32012-11-08 21:25:48 +01007404 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7405 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007406
7407 if (err)
7408 goto free_msg;
7409
David S. Miller9360ffd2012-03-29 04:41:26 -04007410 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7411 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007412
7413 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007414
7415 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007416
7417 nla_put_failure:
7418 err = -ENOBUFS;
7419 free_msg:
7420 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007421 return err;
7422}
7423
7424static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7425 struct genl_info *info)
7426{
Johannes Berg4c476992010-10-04 21:36:35 +02007427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007428 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007429 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007430
7431 if (!info->attrs[NL80211_ATTR_COOKIE])
7432 return -EINVAL;
7433
Johannes Berg4c476992010-10-04 21:36:35 +02007434 if (!rdev->ops->cancel_remain_on_channel)
7435 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007436
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007437 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7438
Hila Gonene35e4d22012-06-27 17:19:42 +03007439 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007440}
7441
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007442static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7443 u8 *rates, u8 rates_len)
7444{
7445 u8 i;
7446 u32 mask = 0;
7447
7448 for (i = 0; i < rates_len; i++) {
7449 int rate = (rates[i] & 0x7f) * 5;
7450 int ridx;
7451 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7452 struct ieee80211_rate *srate =
7453 &sband->bitrates[ridx];
7454 if (rate == srate->bitrate) {
7455 mask |= 1 << ridx;
7456 break;
7457 }
7458 }
7459 if (ridx == sband->n_bitrates)
7460 return 0; /* rate not found */
7461 }
7462
7463 return mask;
7464}
7465
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007466static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7467 u8 *rates, u8 rates_len,
7468 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7469{
7470 u8 i;
7471
7472 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7473
7474 for (i = 0; i < rates_len; i++) {
7475 int ridx, rbit;
7476
7477 ridx = rates[i] / 8;
7478 rbit = BIT(rates[i] % 8);
7479
7480 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007481 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007482 return false;
7483
7484 /* check availability */
7485 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7486 mcs[ridx] |= rbit;
7487 else
7488 return false;
7489 }
7490
7491 return true;
7492}
7493
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007494static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7495{
7496 u16 mcs_mask = 0;
7497
7498 switch (vht_mcs_map) {
7499 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7500 break;
7501 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7502 mcs_mask = 0x00FF;
7503 break;
7504 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7505 mcs_mask = 0x01FF;
7506 break;
7507 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7508 mcs_mask = 0x03FF;
7509 break;
7510 default:
7511 break;
7512 }
7513
7514 return mcs_mask;
7515}
7516
7517static void vht_build_mcs_mask(u16 vht_mcs_map,
7518 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7519{
7520 u8 nss;
7521
7522 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7523 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7524 vht_mcs_map >>= 2;
7525 }
7526}
7527
7528static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7529 struct nl80211_txrate_vht *txrate,
7530 u16 mcs[NL80211_VHT_NSS_MAX])
7531{
7532 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7533 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7534 u8 i;
7535
7536 if (!sband->vht_cap.vht_supported)
7537 return false;
7538
7539 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7540
7541 /* Build vht_mcs_mask from VHT capabilities */
7542 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7543
7544 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7545 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7546 mcs[i] = txrate->mcs[i];
7547 else
7548 return false;
7549 }
7550
7551 return true;
7552}
7553
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007554static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007555 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7556 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007557 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7558 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007559 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007560 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007561};
7562
7563static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7564 struct genl_info *info)
7565{
7566 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007567 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007568 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007569 int rem, i;
7570 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007571 struct nlattr *tx_rates;
7572 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007573 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007574
Johannes Berg4c476992010-10-04 21:36:35 +02007575 if (!rdev->ops->set_bitrate_mask)
7576 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007577
7578 memset(&mask, 0, sizeof(mask));
7579 /* Default to all rates enabled */
7580 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7581 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007582
7583 if (!sband)
7584 continue;
7585
7586 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007587 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007588 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007589 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007590
7591 if (!sband->vht_cap.vht_supported)
7592 continue;
7593
7594 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7595 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007596 }
7597
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007598 /* if no rates are given set it back to the defaults */
7599 if (!info->attrs[NL80211_ATTR_TX_RATES])
7600 goto out;
7601
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007602 /*
7603 * The nested attribute uses enum nl80211_band as the index. This maps
7604 * directly to the enum ieee80211_band values used in cfg80211.
7605 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007606 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007607 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007608 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007609 int err;
7610
Johannes Berg4c476992010-10-04 21:36:35 +02007611 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7612 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007613 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007614 if (sband == NULL)
7615 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007616 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7617 nla_len(tx_rates), nl80211_txattr_policy);
7618 if (err)
7619 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007620 if (tb[NL80211_TXRATE_LEGACY]) {
7621 mask.control[band].legacy = rateset_to_mask(
7622 sband,
7623 nla_data(tb[NL80211_TXRATE_LEGACY]),
7624 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307625 if ((mask.control[band].legacy == 0) &&
7626 nla_len(tb[NL80211_TXRATE_LEGACY]))
7627 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007628 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007629 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007630 if (!ht_rateset_to_mask(
7631 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007632 nla_data(tb[NL80211_TXRATE_HT]),
7633 nla_len(tb[NL80211_TXRATE_HT]),
7634 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007635 return -EINVAL;
7636 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007637 if (tb[NL80211_TXRATE_VHT]) {
7638 if (!vht_set_mcs_mask(
7639 sband,
7640 nla_data(tb[NL80211_TXRATE_VHT]),
7641 mask.control[band].vht_mcs))
7642 return -EINVAL;
7643 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007644 if (tb[NL80211_TXRATE_GI]) {
7645 mask.control[band].gi =
7646 nla_get_u8(tb[NL80211_TXRATE_GI]);
7647 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7648 return -EINVAL;
7649 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007650
7651 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007652 /* don't allow empty legacy rates if HT or VHT
7653 * are not even supported.
7654 */
7655 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7656 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007657 return -EINVAL;
7658
7659 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007660 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007661 goto out;
7662
7663 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7664 if (mask.control[band].vht_mcs[i])
7665 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007666
7667 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007668 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007669 }
7670 }
7671
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007672out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007673 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007674}
7675
Johannes Berg2e161f72010-08-12 15:38:38 +02007676static int nl80211_register_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 Berg2e161f72010-08-12 15:38:38 +02007680 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007681
7682 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7683 return -EINVAL;
7684
Johannes Berg2e161f72010-08-12 15:38:38 +02007685 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7686 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007687
Johannes Berg71bbc992012-06-15 15:30:18 +02007688 switch (wdev->iftype) {
7689 case NL80211_IFTYPE_STATION:
7690 case NL80211_IFTYPE_ADHOC:
7691 case NL80211_IFTYPE_P2P_CLIENT:
7692 case NL80211_IFTYPE_AP:
7693 case NL80211_IFTYPE_AP_VLAN:
7694 case NL80211_IFTYPE_MESH_POINT:
7695 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007696 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007697 break;
7698 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007699 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007700 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007701
7702 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007703 if (!rdev->ops->mgmt_tx)
7704 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007705
Eric W. Biederman15e47302012-09-07 20:12:54 +00007706 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007707 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7708 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007709}
7710
Johannes Berg2e161f72010-08-12 15:38:38 +02007711static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007712{
Johannes Berg4c476992010-10-04 21:36:35 +02007713 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007714 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007715 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007716 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007717 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007718 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007719 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007720 struct cfg80211_mgmt_tx_params params = {
7721 .dont_wait_for_ack =
7722 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7723 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007724
Johannes Berg683b6d32012-11-08 21:25:48 +01007725 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007726 return -EINVAL;
7727
Johannes Berg4c476992010-10-04 21:36:35 +02007728 if (!rdev->ops->mgmt_tx)
7729 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007730
Johannes Berg71bbc992012-06-15 15:30:18 +02007731 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007732 case NL80211_IFTYPE_P2P_DEVICE:
7733 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7734 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007735 case NL80211_IFTYPE_STATION:
7736 case NL80211_IFTYPE_ADHOC:
7737 case NL80211_IFTYPE_P2P_CLIENT:
7738 case NL80211_IFTYPE_AP:
7739 case NL80211_IFTYPE_AP_VLAN:
7740 case NL80211_IFTYPE_MESH_POINT:
7741 case NL80211_IFTYPE_P2P_GO:
7742 break;
7743 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007744 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007745 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007746
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007747 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007748 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007749 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007750 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007751
7752 /*
7753 * We should wait on the channel for at least a minimum amount
7754 * of time (10ms) but no longer than the driver supports.
7755 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007756 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7757 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007758 return -EINVAL;
7759
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007760 }
7761
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007762 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007763
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007764 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007765 return -EINVAL;
7766
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007767 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307768
Antonio Quartulliea141b752013-06-11 14:20:03 +02007769 /* get the channel if any has been specified, otherwise pass NULL to
7770 * the driver. The latter will use the current one
7771 */
7772 chandef.chan = NULL;
7773 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7774 err = nl80211_parse_chandef(rdev, info, &chandef);
7775 if (err)
7776 return err;
7777 }
7778
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007779 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007780 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007781
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007782 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007783 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7784 if (!msg)
7785 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007786
Eric W. Biederman15e47302012-09-07 20:12:54 +00007787 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007788 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007789 if (!hdr) {
7790 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007791 goto free_msg;
7792 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007793 }
Johannes Berge247bd902011-11-04 11:18:21 +01007794
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007795 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7796 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7797 params.chan = chandef.chan;
7798 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007799 if (err)
7800 goto free_msg;
7801
Johannes Berge247bd902011-11-04 11:18:21 +01007802 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007803 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7804 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007805
Johannes Berge247bd902011-11-04 11:18:21 +01007806 genlmsg_end(msg, hdr);
7807 return genlmsg_reply(msg, info);
7808 }
7809
7810 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007811
7812 nla_put_failure:
7813 err = -ENOBUFS;
7814 free_msg:
7815 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007816 return err;
7817}
7818
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007819static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7820{
7821 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007822 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007823 u64 cookie;
7824
7825 if (!info->attrs[NL80211_ATTR_COOKIE])
7826 return -EINVAL;
7827
7828 if (!rdev->ops->mgmt_tx_cancel_wait)
7829 return -EOPNOTSUPP;
7830
Johannes Berg71bbc992012-06-15 15:30:18 +02007831 switch (wdev->iftype) {
7832 case NL80211_IFTYPE_STATION:
7833 case NL80211_IFTYPE_ADHOC:
7834 case NL80211_IFTYPE_P2P_CLIENT:
7835 case NL80211_IFTYPE_AP:
7836 case NL80211_IFTYPE_AP_VLAN:
7837 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007838 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007839 break;
7840 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007841 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007842 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007843
7844 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7845
Hila Gonene35e4d22012-06-27 17:19:42 +03007846 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007847}
7848
Kalle Valoffb9eb32010-02-17 17:58:10 +02007849static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7850{
Johannes Berg4c476992010-10-04 21:36:35 +02007851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007852 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007853 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007854 u8 ps_state;
7855 bool state;
7856 int err;
7857
Johannes Berg4c476992010-10-04 21:36:35 +02007858 if (!info->attrs[NL80211_ATTR_PS_STATE])
7859 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007860
7861 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7862
Johannes Berg4c476992010-10-04 21:36:35 +02007863 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7864 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007865
7866 wdev = dev->ieee80211_ptr;
7867
Johannes Berg4c476992010-10-04 21:36:35 +02007868 if (!rdev->ops->set_power_mgmt)
7869 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007870
7871 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7872
7873 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007874 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007875
Hila Gonene35e4d22012-06-27 17:19:42 +03007876 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007877 if (!err)
7878 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007879 return err;
7880}
7881
7882static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7883{
Johannes Berg4c476992010-10-04 21:36:35 +02007884 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007885 enum nl80211_ps_state ps_state;
7886 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007887 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007888 struct sk_buff *msg;
7889 void *hdr;
7890 int err;
7891
Kalle Valoffb9eb32010-02-17 17:58:10 +02007892 wdev = dev->ieee80211_ptr;
7893
Johannes Berg4c476992010-10-04 21:36:35 +02007894 if (!rdev->ops->set_power_mgmt)
7895 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007896
7897 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007898 if (!msg)
7899 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007900
Eric W. Biederman15e47302012-09-07 20:12:54 +00007901 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007902 NL80211_CMD_GET_POWER_SAVE);
7903 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007904 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007905 goto free_msg;
7906 }
7907
7908 if (wdev->ps)
7909 ps_state = NL80211_PS_ENABLED;
7910 else
7911 ps_state = NL80211_PS_DISABLED;
7912
David S. Miller9360ffd2012-03-29 04:41:26 -04007913 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7914 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007915
7916 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007917 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007918
Johannes Berg4c476992010-10-04 21:36:35 +02007919 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007920 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007921 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007922 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007923 return err;
7924}
7925
Johannes Berg94e860f2014-01-20 23:58:15 +01007926static const struct nla_policy
7927nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007928 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7929 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7930 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007931 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7932 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7933 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007934};
7935
Thomas Pedersen84f10702012-07-12 16:17:33 -07007936static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007937 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007938{
7939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007940 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007941 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007942
Johannes Bergd9d8b012012-11-26 12:51:52 +01007943 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007944 return -EINVAL;
7945
Thomas Pedersen84f10702012-07-12 16:17:33 -07007946 if (!rdev->ops->set_cqm_txe_config)
7947 return -EOPNOTSUPP;
7948
7949 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7950 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7951 return -EOPNOTSUPP;
7952
Hila Gonene35e4d22012-06-27 17:19:42 +03007953 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007954}
7955
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007956static int nl80211_set_cqm_rssi(struct genl_info *info,
7957 s32 threshold, u32 hysteresis)
7958{
Johannes Berg4c476992010-10-04 21:36:35 +02007959 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007960 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007961 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007962
7963 if (threshold > 0)
7964 return -EINVAL;
7965
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007966 /* disabling - hysteresis should also be zero then */
7967 if (threshold == 0)
7968 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007969
Johannes Berg4c476992010-10-04 21:36:35 +02007970 if (!rdev->ops->set_cqm_rssi_config)
7971 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007972
Johannes Berg074ac8d2010-09-16 14:58:22 +02007973 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007974 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7975 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007976
Hila Gonene35e4d22012-06-27 17:19:42 +03007977 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007978}
7979
7980static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7981{
7982 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7983 struct nlattr *cqm;
7984 int err;
7985
7986 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007987 if (!cqm)
7988 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007989
7990 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7991 nl80211_attr_cqm_policy);
7992 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007993 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007994
7995 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7996 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007997 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7998 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007999
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008000 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8001 }
8002
8003 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8004 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8005 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8006 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8007 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8008 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8009
8010 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8011 }
8012
8013 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008014}
8015
Johannes Berg29cbe682010-12-03 09:20:44 +01008016static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8017{
8018 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8019 struct net_device *dev = info->user_ptr[1];
8020 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008021 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008022 int err;
8023
8024 /* start with default */
8025 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008026 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008027
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008028 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008029 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008030 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008031 if (err)
8032 return err;
8033 }
8034
8035 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8036 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8037 return -EINVAL;
8038
Javier Cardonac80d5452010-12-16 17:37:49 -08008039 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8040 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8041
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008042 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8043 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8044 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8045 return -EINVAL;
8046
Marco Porsch9bdbf042013-01-07 16:04:51 +01008047 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8048 setup.beacon_interval =
8049 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8050 if (setup.beacon_interval < 10 ||
8051 setup.beacon_interval > 10000)
8052 return -EINVAL;
8053 }
8054
8055 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8056 setup.dtim_period =
8057 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8058 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8059 return -EINVAL;
8060 }
8061
Javier Cardonac80d5452010-12-16 17:37:49 -08008062 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8063 /* parse additional setup parameters if given */
8064 err = nl80211_parse_mesh_setup(info, &setup);
8065 if (err)
8066 return err;
8067 }
8068
Thomas Pedersend37bb182013-03-04 13:06:13 -08008069 if (setup.user_mpm)
8070 cfg.auto_open_plinks = false;
8071
Johannes Bergcc1d2802012-05-16 23:50:20 +02008072 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008073 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8074 if (err)
8075 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008076 } else {
8077 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008078 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008079 }
8080
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008081 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8082 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8083 int n_rates =
8084 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8085 struct ieee80211_supported_band *sband;
8086
8087 if (!setup.chandef.chan)
8088 return -EINVAL;
8089
8090 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8091
8092 err = ieee80211_get_ratemask(sband, rates, n_rates,
8093 &setup.basic_rates);
8094 if (err)
8095 return err;
8096 }
8097
Javier Cardonac80d5452010-12-16 17:37:49 -08008098 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008099}
8100
8101static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8102{
8103 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8104 struct net_device *dev = info->user_ptr[1];
8105
8106 return cfg80211_leave_mesh(rdev, dev);
8107}
8108
Johannes Bergdfb89c52012-06-27 09:23:48 +02008109#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008110static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8111 struct cfg80211_registered_device *rdev)
8112{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008113 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008114 struct nlattr *nl_pats, *nl_pat;
8115 int i, pat_len;
8116
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008117 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008118 return 0;
8119
8120 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8121 if (!nl_pats)
8122 return -ENOBUFS;
8123
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008124 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008125 nl_pat = nla_nest_start(msg, i + 1);
8126 if (!nl_pat)
8127 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008128 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008129 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008130 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008131 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8132 wowlan->patterns[i].pattern) ||
8133 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008134 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008135 return -ENOBUFS;
8136 nla_nest_end(msg, nl_pat);
8137 }
8138 nla_nest_end(msg, nl_pats);
8139
8140 return 0;
8141}
8142
Johannes Berg2a0e0472013-01-23 22:57:40 +01008143static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8144 struct cfg80211_wowlan_tcp *tcp)
8145{
8146 struct nlattr *nl_tcp;
8147
8148 if (!tcp)
8149 return 0;
8150
8151 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8152 if (!nl_tcp)
8153 return -ENOBUFS;
8154
8155 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8156 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8157 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8158 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8159 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8160 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8161 tcp->payload_len, tcp->payload) ||
8162 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8163 tcp->data_interval) ||
8164 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8165 tcp->wake_len, tcp->wake_data) ||
8166 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8167 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8168 return -ENOBUFS;
8169
8170 if (tcp->payload_seq.len &&
8171 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8172 sizeof(tcp->payload_seq), &tcp->payload_seq))
8173 return -ENOBUFS;
8174
8175 if (tcp->payload_tok.len &&
8176 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8177 sizeof(tcp->payload_tok) + tcp->tokens_size,
8178 &tcp->payload_tok))
8179 return -ENOBUFS;
8180
Johannes Berge248ad32013-05-16 10:24:28 +02008181 nla_nest_end(msg, nl_tcp);
8182
Johannes Berg2a0e0472013-01-23 22:57:40 +01008183 return 0;
8184}
8185
Johannes Bergff1b6e62011-05-04 15:37:28 +02008186static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8187{
8188 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8189 struct sk_buff *msg;
8190 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008191 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008192
Johannes Berg964dc9e2013-06-03 17:25:34 +02008193 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008194 return -EOPNOTSUPP;
8195
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008196 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008197 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008198 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8199 rdev->wiphy.wowlan_config->tcp->payload_len +
8200 rdev->wiphy.wowlan_config->tcp->wake_len +
8201 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008202 }
8203
8204 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008205 if (!msg)
8206 return -ENOMEM;
8207
Eric W. Biederman15e47302012-09-07 20:12:54 +00008208 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008209 NL80211_CMD_GET_WOWLAN);
8210 if (!hdr)
8211 goto nla_put_failure;
8212
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008213 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008214 struct nlattr *nl_wowlan;
8215
8216 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8217 if (!nl_wowlan)
8218 goto nla_put_failure;
8219
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008220 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008222 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008224 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008226 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008227 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008228 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008229 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008230 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008231 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008232 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008233 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8234 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008235
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008236 if (nl80211_send_wowlan_patterns(msg, rdev))
8237 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008238
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008239 if (nl80211_send_wowlan_tcp(msg,
8240 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008241 goto nla_put_failure;
8242
Johannes Bergff1b6e62011-05-04 15:37:28 +02008243 nla_nest_end(msg, nl_wowlan);
8244 }
8245
8246 genlmsg_end(msg, hdr);
8247 return genlmsg_reply(msg, info);
8248
8249nla_put_failure:
8250 nlmsg_free(msg);
8251 return -ENOBUFS;
8252}
8253
Johannes Berg2a0e0472013-01-23 22:57:40 +01008254static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8255 struct nlattr *attr,
8256 struct cfg80211_wowlan *trig)
8257{
8258 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8259 struct cfg80211_wowlan_tcp *cfg;
8260 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8261 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8262 u32 size;
8263 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8264 int err, port;
8265
Johannes Berg964dc9e2013-06-03 17:25:34 +02008266 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008267 return -EINVAL;
8268
8269 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8270 nla_data(attr), nla_len(attr),
8271 nl80211_wowlan_tcp_policy);
8272 if (err)
8273 return err;
8274
8275 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8276 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8277 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8278 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8279 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8280 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8281 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8282 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8283 return -EINVAL;
8284
8285 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008286 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008287 return -EINVAL;
8288
8289 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008290 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008291 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008292 return -EINVAL;
8293
8294 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008295 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008296 return -EINVAL;
8297
8298 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8299 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8300 return -EINVAL;
8301
8302 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8303 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8304
8305 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8306 tokens_size = tokln - sizeof(*tok);
8307
8308 if (!tok->len || tokens_size % tok->len)
8309 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008310 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008311 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008312 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008313 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008314 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008315 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008316 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008317 return -EINVAL;
8318 if (tok->offset + tok->len > data_size)
8319 return -EINVAL;
8320 }
8321
8322 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8323 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008324 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008325 return -EINVAL;
8326 if (seq->len == 0 || seq->len > 4)
8327 return -EINVAL;
8328 if (seq->len + seq->offset > data_size)
8329 return -EINVAL;
8330 }
8331
8332 size = sizeof(*cfg);
8333 size += data_size;
8334 size += wake_size + wake_mask_size;
8335 size += tokens_size;
8336
8337 cfg = kzalloc(size, GFP_KERNEL);
8338 if (!cfg)
8339 return -ENOMEM;
8340 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8341 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8342 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8343 ETH_ALEN);
8344 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8345 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8346 else
8347 port = 0;
8348#ifdef CONFIG_INET
8349 /* allocate a socket and port for it and use it */
8350 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8351 IPPROTO_TCP, &cfg->sock, 1);
8352 if (err) {
8353 kfree(cfg);
8354 return err;
8355 }
8356 if (inet_csk_get_port(cfg->sock->sk, port)) {
8357 sock_release(cfg->sock);
8358 kfree(cfg);
8359 return -EADDRINUSE;
8360 }
8361 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8362#else
8363 if (!port) {
8364 kfree(cfg);
8365 return -EINVAL;
8366 }
8367 cfg->src_port = port;
8368#endif
8369
8370 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8371 cfg->payload_len = data_size;
8372 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8373 memcpy((void *)cfg->payload,
8374 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8375 data_size);
8376 if (seq)
8377 cfg->payload_seq = *seq;
8378 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8379 cfg->wake_len = wake_size;
8380 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8381 memcpy((void *)cfg->wake_data,
8382 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8383 wake_size);
8384 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8385 data_size + wake_size;
8386 memcpy((void *)cfg->wake_mask,
8387 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8388 wake_mask_size);
8389 if (tok) {
8390 cfg->tokens_size = tokens_size;
8391 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8392 }
8393
8394 trig->tcp = cfg;
8395
8396 return 0;
8397}
8398
Johannes Bergff1b6e62011-05-04 15:37:28 +02008399static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8400{
8401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8402 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008403 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008404 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008405 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008406 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008407 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008408
Johannes Berg964dc9e2013-06-03 17:25:34 +02008409 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008410 return -EOPNOTSUPP;
8411
Johannes Bergae33bd82012-07-12 16:25:02 +02008412 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8413 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008414 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008415 goto set_wakeup;
8416 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008417
8418 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8419 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8420 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8421 nl80211_wowlan_policy);
8422 if (err)
8423 return err;
8424
8425 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8426 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8427 return -EINVAL;
8428 new_triggers.any = true;
8429 }
8430
8431 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8432 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8433 return -EINVAL;
8434 new_triggers.disconnect = true;
8435 }
8436
8437 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8438 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8439 return -EINVAL;
8440 new_triggers.magic_pkt = true;
8441 }
8442
Johannes Berg77dbbb12011-07-13 10:48:55 +02008443 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8444 return -EINVAL;
8445
8446 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8447 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8448 return -EINVAL;
8449 new_triggers.gtk_rekey_failure = true;
8450 }
8451
8452 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8453 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8454 return -EINVAL;
8455 new_triggers.eap_identity_req = true;
8456 }
8457
8458 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8459 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8460 return -EINVAL;
8461 new_triggers.four_way_handshake = true;
8462 }
8463
8464 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8465 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8466 return -EINVAL;
8467 new_triggers.rfkill_release = true;
8468 }
8469
Johannes Bergff1b6e62011-05-04 15:37:28 +02008470 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8471 struct nlattr *pat;
8472 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008473 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008474 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008475
8476 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8477 rem)
8478 n_patterns++;
8479 if (n_patterns > wowlan->n_patterns)
8480 return -EINVAL;
8481
8482 new_triggers.patterns = kcalloc(n_patterns,
8483 sizeof(new_triggers.patterns[0]),
8484 GFP_KERNEL);
8485 if (!new_triggers.patterns)
8486 return -ENOMEM;
8487
8488 new_triggers.n_patterns = n_patterns;
8489 i = 0;
8490
8491 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8492 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008493 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8494 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008495 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008496 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8497 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008498 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008499 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008500 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008501 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008502 goto error;
8503 if (pat_len > wowlan->pattern_max_len ||
8504 pat_len < wowlan->pattern_min_len)
8505 goto error;
8506
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008507 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008508 pkt_offset = 0;
8509 else
8510 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008511 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008512 if (pkt_offset > wowlan->max_pkt_offset)
8513 goto error;
8514 new_triggers.patterns[i].pkt_offset = pkt_offset;
8515
Johannes Bergff1b6e62011-05-04 15:37:28 +02008516 new_triggers.patterns[i].mask =
8517 kmalloc(mask_len + pat_len, GFP_KERNEL);
8518 if (!new_triggers.patterns[i].mask) {
8519 err = -ENOMEM;
8520 goto error;
8521 }
8522 new_triggers.patterns[i].pattern =
8523 new_triggers.patterns[i].mask + mask_len;
8524 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008525 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008526 mask_len);
8527 new_triggers.patterns[i].pattern_len = pat_len;
8528 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008529 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008530 pat_len);
8531 i++;
8532 }
8533 }
8534
Johannes Berg2a0e0472013-01-23 22:57:40 +01008535 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8536 err = nl80211_parse_wowlan_tcp(
8537 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8538 &new_triggers);
8539 if (err)
8540 goto error;
8541 }
8542
Johannes Bergae33bd82012-07-12 16:25:02 +02008543 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8544 if (!ntrig) {
8545 err = -ENOMEM;
8546 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008547 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008548 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008549 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008550
Johannes Bergae33bd82012-07-12 16:25:02 +02008551 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008552 if (rdev->ops->set_wakeup &&
8553 prev_enabled != !!rdev->wiphy.wowlan_config)
8554 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008555
Johannes Bergff1b6e62011-05-04 15:37:28 +02008556 return 0;
8557 error:
8558 for (i = 0; i < new_triggers.n_patterns; i++)
8559 kfree(new_triggers.patterns[i].mask);
8560 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008561 if (new_triggers.tcp && new_triggers.tcp->sock)
8562 sock_release(new_triggers.tcp->sock);
8563 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008564 return err;
8565}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008566#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008567
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008568static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8569 struct cfg80211_registered_device *rdev)
8570{
8571 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8572 int i, j, pat_len;
8573 struct cfg80211_coalesce_rules *rule;
8574
8575 if (!rdev->coalesce->n_rules)
8576 return 0;
8577
8578 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8579 if (!nl_rules)
8580 return -ENOBUFS;
8581
8582 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8583 nl_rule = nla_nest_start(msg, i + 1);
8584 if (!nl_rule)
8585 return -ENOBUFS;
8586
8587 rule = &rdev->coalesce->rules[i];
8588 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8589 rule->delay))
8590 return -ENOBUFS;
8591
8592 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8593 rule->condition))
8594 return -ENOBUFS;
8595
8596 nl_pats = nla_nest_start(msg,
8597 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8598 if (!nl_pats)
8599 return -ENOBUFS;
8600
8601 for (j = 0; j < rule->n_patterns; j++) {
8602 nl_pat = nla_nest_start(msg, j + 1);
8603 if (!nl_pat)
8604 return -ENOBUFS;
8605 pat_len = rule->patterns[j].pattern_len;
8606 if (nla_put(msg, NL80211_PKTPAT_MASK,
8607 DIV_ROUND_UP(pat_len, 8),
8608 rule->patterns[j].mask) ||
8609 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8610 rule->patterns[j].pattern) ||
8611 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8612 rule->patterns[j].pkt_offset))
8613 return -ENOBUFS;
8614 nla_nest_end(msg, nl_pat);
8615 }
8616 nla_nest_end(msg, nl_pats);
8617 nla_nest_end(msg, nl_rule);
8618 }
8619 nla_nest_end(msg, nl_rules);
8620
8621 return 0;
8622}
8623
8624static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8625{
8626 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8627 struct sk_buff *msg;
8628 void *hdr;
8629
8630 if (!rdev->wiphy.coalesce)
8631 return -EOPNOTSUPP;
8632
8633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8634 if (!msg)
8635 return -ENOMEM;
8636
8637 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8638 NL80211_CMD_GET_COALESCE);
8639 if (!hdr)
8640 goto nla_put_failure;
8641
8642 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8643 goto nla_put_failure;
8644
8645 genlmsg_end(msg, hdr);
8646 return genlmsg_reply(msg, info);
8647
8648nla_put_failure:
8649 nlmsg_free(msg);
8650 return -ENOBUFS;
8651}
8652
8653void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8654{
8655 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8656 int i, j;
8657 struct cfg80211_coalesce_rules *rule;
8658
8659 if (!coalesce)
8660 return;
8661
8662 for (i = 0; i < coalesce->n_rules; i++) {
8663 rule = &coalesce->rules[i];
8664 for (j = 0; j < rule->n_patterns; j++)
8665 kfree(rule->patterns[j].mask);
8666 kfree(rule->patterns);
8667 }
8668 kfree(coalesce->rules);
8669 kfree(coalesce);
8670 rdev->coalesce = NULL;
8671}
8672
8673static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8674 struct nlattr *rule,
8675 struct cfg80211_coalesce_rules *new_rule)
8676{
8677 int err, i;
8678 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8679 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8680 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8681 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8682
8683 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8684 nla_len(rule), nl80211_coalesce_policy);
8685 if (err)
8686 return err;
8687
8688 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8689 new_rule->delay =
8690 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8691 if (new_rule->delay > coalesce->max_delay)
8692 return -EINVAL;
8693
8694 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8695 new_rule->condition =
8696 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8697 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8698 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8699 return -EINVAL;
8700
8701 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8702 return -EINVAL;
8703
8704 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8705 rem)
8706 n_patterns++;
8707 if (n_patterns > coalesce->n_patterns)
8708 return -EINVAL;
8709
8710 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8711 GFP_KERNEL);
8712 if (!new_rule->patterns)
8713 return -ENOMEM;
8714
8715 new_rule->n_patterns = n_patterns;
8716 i = 0;
8717
8718 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8719 rem) {
8720 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8721 nla_len(pat), NULL);
8722 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8723 !pat_tb[NL80211_PKTPAT_PATTERN])
8724 return -EINVAL;
8725 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8726 mask_len = DIV_ROUND_UP(pat_len, 8);
8727 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8728 return -EINVAL;
8729 if (pat_len > coalesce->pattern_max_len ||
8730 pat_len < coalesce->pattern_min_len)
8731 return -EINVAL;
8732
8733 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8734 pkt_offset = 0;
8735 else
8736 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8737 if (pkt_offset > coalesce->max_pkt_offset)
8738 return -EINVAL;
8739 new_rule->patterns[i].pkt_offset = pkt_offset;
8740
8741 new_rule->patterns[i].mask =
8742 kmalloc(mask_len + pat_len, GFP_KERNEL);
8743 if (!new_rule->patterns[i].mask)
8744 return -ENOMEM;
8745 new_rule->patterns[i].pattern =
8746 new_rule->patterns[i].mask + mask_len;
8747 memcpy(new_rule->patterns[i].mask,
8748 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8749 new_rule->patterns[i].pattern_len = pat_len;
8750 memcpy(new_rule->patterns[i].pattern,
8751 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8752 i++;
8753 }
8754
8755 return 0;
8756}
8757
8758static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8759{
8760 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8761 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8762 struct cfg80211_coalesce new_coalesce = {};
8763 struct cfg80211_coalesce *n_coalesce;
8764 int err, rem_rule, n_rules = 0, i, j;
8765 struct nlattr *rule;
8766 struct cfg80211_coalesce_rules *tmp_rule;
8767
8768 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8769 return -EOPNOTSUPP;
8770
8771 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8772 cfg80211_rdev_free_coalesce(rdev);
8773 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8774 return 0;
8775 }
8776
8777 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8778 rem_rule)
8779 n_rules++;
8780 if (n_rules > coalesce->n_rules)
8781 return -EINVAL;
8782
8783 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8784 GFP_KERNEL);
8785 if (!new_coalesce.rules)
8786 return -ENOMEM;
8787
8788 new_coalesce.n_rules = n_rules;
8789 i = 0;
8790
8791 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8792 rem_rule) {
8793 err = nl80211_parse_coalesce_rule(rdev, rule,
8794 &new_coalesce.rules[i]);
8795 if (err)
8796 goto error;
8797
8798 i++;
8799 }
8800
8801 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8802 if (err)
8803 goto error;
8804
8805 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8806 if (!n_coalesce) {
8807 err = -ENOMEM;
8808 goto error;
8809 }
8810 cfg80211_rdev_free_coalesce(rdev);
8811 rdev->coalesce = n_coalesce;
8812
8813 return 0;
8814error:
8815 for (i = 0; i < new_coalesce.n_rules; i++) {
8816 tmp_rule = &new_coalesce.rules[i];
8817 for (j = 0; j < tmp_rule->n_patterns; j++)
8818 kfree(tmp_rule->patterns[j].mask);
8819 kfree(tmp_rule->patterns);
8820 }
8821 kfree(new_coalesce.rules);
8822
8823 return err;
8824}
8825
Johannes Berge5497d72011-07-05 16:35:40 +02008826static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8827{
8828 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8829 struct net_device *dev = info->user_ptr[1];
8830 struct wireless_dev *wdev = dev->ieee80211_ptr;
8831 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8832 struct cfg80211_gtk_rekey_data rekey_data;
8833 int err;
8834
8835 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8836 return -EINVAL;
8837
8838 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8839 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8840 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8841 nl80211_rekey_policy);
8842 if (err)
8843 return err;
8844
8845 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8846 return -ERANGE;
8847 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8848 return -ERANGE;
8849 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8850 return -ERANGE;
8851
8852 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8853 NL80211_KEK_LEN);
8854 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8855 NL80211_KCK_LEN);
8856 memcpy(rekey_data.replay_ctr,
8857 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8858 NL80211_REPLAY_CTR_LEN);
8859
8860 wdev_lock(wdev);
8861 if (!wdev->current_bss) {
8862 err = -ENOTCONN;
8863 goto out;
8864 }
8865
8866 if (!rdev->ops->set_rekey_data) {
8867 err = -EOPNOTSUPP;
8868 goto out;
8869 }
8870
Hila Gonene35e4d22012-06-27 17:19:42 +03008871 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008872 out:
8873 wdev_unlock(wdev);
8874 return err;
8875}
8876
Johannes Berg28946da2011-11-04 11:18:12 +01008877static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8878 struct genl_info *info)
8879{
8880 struct net_device *dev = info->user_ptr[1];
8881 struct wireless_dev *wdev = dev->ieee80211_ptr;
8882
8883 if (wdev->iftype != NL80211_IFTYPE_AP &&
8884 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8885 return -EINVAL;
8886
Eric W. Biederman15e47302012-09-07 20:12:54 +00008887 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008888 return -EBUSY;
8889
Eric W. Biederman15e47302012-09-07 20:12:54 +00008890 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008891 return 0;
8892}
8893
Johannes Berg7f6cf312011-11-04 11:18:15 +01008894static int nl80211_probe_client(struct sk_buff *skb,
8895 struct genl_info *info)
8896{
8897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8898 struct net_device *dev = info->user_ptr[1];
8899 struct wireless_dev *wdev = dev->ieee80211_ptr;
8900 struct sk_buff *msg;
8901 void *hdr;
8902 const u8 *addr;
8903 u64 cookie;
8904 int err;
8905
8906 if (wdev->iftype != NL80211_IFTYPE_AP &&
8907 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8908 return -EOPNOTSUPP;
8909
8910 if (!info->attrs[NL80211_ATTR_MAC])
8911 return -EINVAL;
8912
8913 if (!rdev->ops->probe_client)
8914 return -EOPNOTSUPP;
8915
8916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8917 if (!msg)
8918 return -ENOMEM;
8919
Eric W. Biederman15e47302012-09-07 20:12:54 +00008920 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008921 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008922 if (!hdr) {
8923 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008924 goto free_msg;
8925 }
8926
8927 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8928
Hila Gonene35e4d22012-06-27 17:19:42 +03008929 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008930 if (err)
8931 goto free_msg;
8932
David S. Miller9360ffd2012-03-29 04:41:26 -04008933 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8934 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008935
8936 genlmsg_end(msg, hdr);
8937
8938 return genlmsg_reply(msg, info);
8939
8940 nla_put_failure:
8941 err = -ENOBUFS;
8942 free_msg:
8943 nlmsg_free(msg);
8944 return err;
8945}
8946
Johannes Berg5e7602302011-11-04 11:18:17 +01008947static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8948{
8949 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008950 struct cfg80211_beacon_registration *reg, *nreg;
8951 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008952
8953 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8954 return -EOPNOTSUPP;
8955
Ben Greear37c73b52012-10-26 14:49:25 -07008956 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8957 if (!nreg)
8958 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008959
Ben Greear37c73b52012-10-26 14:49:25 -07008960 /* First, check if already registered. */
8961 spin_lock_bh(&rdev->beacon_registrations_lock);
8962 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8963 if (reg->nlportid == info->snd_portid) {
8964 rv = -EALREADY;
8965 goto out_err;
8966 }
8967 }
8968 /* Add it to the list */
8969 nreg->nlportid = info->snd_portid;
8970 list_add(&nreg->list, &rdev->beacon_registrations);
8971
8972 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008973
8974 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008975out_err:
8976 spin_unlock_bh(&rdev->beacon_registrations_lock);
8977 kfree(nreg);
8978 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008979}
8980
Johannes Berg98104fde2012-06-16 00:19:54 +02008981static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8982{
8983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8984 struct wireless_dev *wdev = info->user_ptr[1];
8985 int err;
8986
8987 if (!rdev->ops->start_p2p_device)
8988 return -EOPNOTSUPP;
8989
8990 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8991 return -EOPNOTSUPP;
8992
8993 if (wdev->p2p_started)
8994 return 0;
8995
Johannes Berg98104fde2012-06-16 00:19:54 +02008996 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008997 if (err)
8998 return err;
8999
Johannes Bergeeb126e2012-10-23 15:16:50 +02009000 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009001 if (err)
9002 return err;
9003
9004 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02009005 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02009006
9007 return 0;
9008}
9009
9010static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9011{
9012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9013 struct wireless_dev *wdev = info->user_ptr[1];
9014
9015 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9016 return -EOPNOTSUPP;
9017
9018 if (!rdev->ops->stop_p2p_device)
9019 return -EOPNOTSUPP;
9020
Johannes Bergf9f47522013-03-19 15:04:07 +01009021 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009022
9023 return 0;
9024}
9025
Johannes Berg3713b4e2013-02-14 16:19:38 +01009026static int nl80211_get_protocol_features(struct sk_buff *skb,
9027 struct genl_info *info)
9028{
9029 void *hdr;
9030 struct sk_buff *msg;
9031
9032 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9033 if (!msg)
9034 return -ENOMEM;
9035
9036 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9037 NL80211_CMD_GET_PROTOCOL_FEATURES);
9038 if (!hdr)
9039 goto nla_put_failure;
9040
9041 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9042 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9043 goto nla_put_failure;
9044
9045 genlmsg_end(msg, hdr);
9046 return genlmsg_reply(msg, info);
9047
9048 nla_put_failure:
9049 kfree_skb(msg);
9050 return -ENOBUFS;
9051}
9052
Jouni Malinen355199e2013-02-27 17:14:27 +02009053static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9054{
9055 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9056 struct cfg80211_update_ft_ies_params ft_params;
9057 struct net_device *dev = info->user_ptr[1];
9058
9059 if (!rdev->ops->update_ft_ies)
9060 return -EOPNOTSUPP;
9061
9062 if (!info->attrs[NL80211_ATTR_MDID] ||
9063 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9064 return -EINVAL;
9065
9066 memset(&ft_params, 0, sizeof(ft_params));
9067 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9068 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9069 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9070
9071 return rdev_update_ft_ies(rdev, dev, &ft_params);
9072}
9073
Arend van Spriel5de17982013-04-18 15:49:00 +02009074static int nl80211_crit_protocol_start(struct sk_buff *skb,
9075 struct genl_info *info)
9076{
9077 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9078 struct wireless_dev *wdev = info->user_ptr[1];
9079 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9080 u16 duration;
9081 int ret;
9082
9083 if (!rdev->ops->crit_proto_start)
9084 return -EOPNOTSUPP;
9085
9086 if (WARN_ON(!rdev->ops->crit_proto_stop))
9087 return -EINVAL;
9088
9089 if (rdev->crit_proto_nlportid)
9090 return -EBUSY;
9091
9092 /* determine protocol if provided */
9093 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9094 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9095
9096 if (proto >= NUM_NL80211_CRIT_PROTO)
9097 return -EINVAL;
9098
9099 /* timeout must be provided */
9100 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9101 return -EINVAL;
9102
9103 duration =
9104 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9105
9106 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9107 return -ERANGE;
9108
9109 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9110 if (!ret)
9111 rdev->crit_proto_nlportid = info->snd_portid;
9112
9113 return ret;
9114}
9115
9116static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9117 struct genl_info *info)
9118{
9119 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9120 struct wireless_dev *wdev = info->user_ptr[1];
9121
9122 if (!rdev->ops->crit_proto_stop)
9123 return -EOPNOTSUPP;
9124
9125 if (rdev->crit_proto_nlportid) {
9126 rdev->crit_proto_nlportid = 0;
9127 rdev_crit_proto_stop(rdev, wdev);
9128 }
9129 return 0;
9130}
9131
Johannes Bergad7e7182013-11-13 13:37:47 +01009132static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9133{
9134 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9135 struct wireless_dev *wdev =
9136 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9137 int i, err;
9138 u32 vid, subcmd;
9139
9140 if (!rdev->wiphy.vendor_commands)
9141 return -EOPNOTSUPP;
9142
9143 if (IS_ERR(wdev)) {
9144 err = PTR_ERR(wdev);
9145 if (err != -EINVAL)
9146 return err;
9147 wdev = NULL;
9148 } else if (wdev->wiphy != &rdev->wiphy) {
9149 return -EINVAL;
9150 }
9151
9152 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9153 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9154 return -EINVAL;
9155
9156 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9157 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9158 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9159 const struct wiphy_vendor_command *vcmd;
9160 void *data = NULL;
9161 int len = 0;
9162
9163 vcmd = &rdev->wiphy.vendor_commands[i];
9164
9165 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9166 continue;
9167
9168 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9169 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9170 if (!wdev)
9171 return -EINVAL;
9172 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9173 !wdev->netdev)
9174 return -EINVAL;
9175
9176 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9177 if (wdev->netdev &&
9178 !netif_running(wdev->netdev))
9179 return -ENETDOWN;
9180 if (!wdev->netdev && !wdev->p2p_started)
9181 return -ENETDOWN;
9182 }
9183 } else {
9184 wdev = NULL;
9185 }
9186
9187 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9188 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9189 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9190 }
9191
9192 rdev->cur_cmd_info = info;
9193 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9194 data, len);
9195 rdev->cur_cmd_info = NULL;
9196 return err;
9197 }
9198
9199 return -EOPNOTSUPP;
9200}
9201
9202struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9203 enum nl80211_commands cmd,
9204 enum nl80211_attrs attr,
9205 int approxlen)
9206{
9207 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9208
9209 if (WARN_ON(!rdev->cur_cmd_info))
9210 return NULL;
9211
9212 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9213 rdev->cur_cmd_info->snd_portid,
9214 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009215 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009216}
9217EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9218
9219int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9220{
9221 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9222 void *hdr = ((void **)skb->cb)[1];
9223 struct nlattr *data = ((void **)skb->cb)[2];
9224
9225 if (WARN_ON(!rdev->cur_cmd_info)) {
9226 kfree_skb(skb);
9227 return -EINVAL;
9228 }
9229
9230 nla_nest_end(skb, data);
9231 genlmsg_end(skb, hdr);
9232 return genlmsg_reply(skb, rdev->cur_cmd_info);
9233}
9234EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9235
9236
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009237static int nl80211_set_qos_map(struct sk_buff *skb,
9238 struct genl_info *info)
9239{
9240 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9241 struct cfg80211_qos_map *qos_map = NULL;
9242 struct net_device *dev = info->user_ptr[1];
9243 u8 *pos, len, num_des, des_len, des;
9244 int ret;
9245
9246 if (!rdev->ops->set_qos_map)
9247 return -EOPNOTSUPP;
9248
9249 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9250 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9251 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9252
9253 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9254 len > IEEE80211_QOS_MAP_LEN_MAX)
9255 return -EINVAL;
9256
9257 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9258 if (!qos_map)
9259 return -ENOMEM;
9260
9261 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9262 if (num_des) {
9263 des_len = num_des *
9264 sizeof(struct cfg80211_dscp_exception);
9265 memcpy(qos_map->dscp_exception, pos, des_len);
9266 qos_map->num_des = num_des;
9267 for (des = 0; des < num_des; des++) {
9268 if (qos_map->dscp_exception[des].up > 7) {
9269 kfree(qos_map);
9270 return -EINVAL;
9271 }
9272 }
9273 pos += des_len;
9274 }
9275 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9276 }
9277
9278 wdev_lock(dev->ieee80211_ptr);
9279 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9280 if (!ret)
9281 ret = rdev_set_qos_map(rdev, dev, qos_map);
9282 wdev_unlock(dev->ieee80211_ptr);
9283
9284 kfree(qos_map);
9285 return ret;
9286}
9287
Johannes Berg4c476992010-10-04 21:36:35 +02009288#define NL80211_FLAG_NEED_WIPHY 0x01
9289#define NL80211_FLAG_NEED_NETDEV 0x02
9290#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009291#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9292#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9293 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009294#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009295/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009296#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9297 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009298
Johannes Bergf84f7712013-11-14 17:14:45 +01009299static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009300 struct genl_info *info)
9301{
9302 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009303 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009304 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009305 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9306
9307 if (rtnl)
9308 rtnl_lock();
9309
9310 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009311 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009312 if (IS_ERR(rdev)) {
9313 if (rtnl)
9314 rtnl_unlock();
9315 return PTR_ERR(rdev);
9316 }
9317 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009318 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9319 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009320 ASSERT_RTNL();
9321
Johannes Berg89a54e42012-06-15 14:33:17 +02009322 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9323 info->attrs);
9324 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009325 if (rtnl)
9326 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009327 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009328 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009329
Johannes Berg89a54e42012-06-15 14:33:17 +02009330 dev = wdev->netdev;
9331 rdev = wiphy_to_dev(wdev->wiphy);
9332
Johannes Berg1bf614e2012-06-15 15:23:36 +02009333 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9334 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009335 if (rtnl)
9336 rtnl_unlock();
9337 return -EINVAL;
9338 }
9339
9340 info->user_ptr[1] = dev;
9341 } else {
9342 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009343 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009344
Johannes Berg1bf614e2012-06-15 15:23:36 +02009345 if (dev) {
9346 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9347 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009348 if (rtnl)
9349 rtnl_unlock();
9350 return -ENETDOWN;
9351 }
9352
9353 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009354 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9355 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009356 if (rtnl)
9357 rtnl_unlock();
9358 return -ENETDOWN;
9359 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009360 }
9361
Johannes Berg4c476992010-10-04 21:36:35 +02009362 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009363 }
9364
9365 return 0;
9366}
9367
Johannes Bergf84f7712013-11-14 17:14:45 +01009368static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009369 struct genl_info *info)
9370{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009371 if (info->user_ptr[1]) {
9372 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9373 struct wireless_dev *wdev = info->user_ptr[1];
9374
9375 if (wdev->netdev)
9376 dev_put(wdev->netdev);
9377 } else {
9378 dev_put(info->user_ptr[1]);
9379 }
9380 }
Johannes Berg4c476992010-10-04 21:36:35 +02009381 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9382 rtnl_unlock();
9383}
9384
Johannes Berg4534de82013-11-14 17:14:46 +01009385static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009386 {
9387 .cmd = NL80211_CMD_GET_WIPHY,
9388 .doit = nl80211_get_wiphy,
9389 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009390 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009391 .policy = nl80211_policy,
9392 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009393 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9394 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009395 },
9396 {
9397 .cmd = NL80211_CMD_SET_WIPHY,
9398 .doit = nl80211_set_wiphy,
9399 .policy = nl80211_policy,
9400 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009401 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009402 },
9403 {
9404 .cmd = NL80211_CMD_GET_INTERFACE,
9405 .doit = nl80211_get_interface,
9406 .dumpit = nl80211_dump_interface,
9407 .policy = nl80211_policy,
9408 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009409 .internal_flags = NL80211_FLAG_NEED_WDEV |
9410 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009411 },
9412 {
9413 .cmd = NL80211_CMD_SET_INTERFACE,
9414 .doit = nl80211_set_interface,
9415 .policy = nl80211_policy,
9416 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009417 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9418 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009419 },
9420 {
9421 .cmd = NL80211_CMD_NEW_INTERFACE,
9422 .doit = nl80211_new_interface,
9423 .policy = nl80211_policy,
9424 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009425 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9426 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009427 },
9428 {
9429 .cmd = NL80211_CMD_DEL_INTERFACE,
9430 .doit = nl80211_del_interface,
9431 .policy = nl80211_policy,
9432 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009433 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009434 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009435 },
Johannes Berg41ade002007-12-19 02:03:29 +01009436 {
9437 .cmd = NL80211_CMD_GET_KEY,
9438 .doit = nl80211_get_key,
9439 .policy = nl80211_policy,
9440 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009441 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009442 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009443 },
9444 {
9445 .cmd = NL80211_CMD_SET_KEY,
9446 .doit = nl80211_set_key,
9447 .policy = nl80211_policy,
9448 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009449 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009450 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009451 },
9452 {
9453 .cmd = NL80211_CMD_NEW_KEY,
9454 .doit = nl80211_new_key,
9455 .policy = nl80211_policy,
9456 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009457 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009458 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009459 },
9460 {
9461 .cmd = NL80211_CMD_DEL_KEY,
9462 .doit = nl80211_del_key,
9463 .policy = nl80211_policy,
9464 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009465 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009466 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009467 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009468 {
9469 .cmd = NL80211_CMD_SET_BEACON,
9470 .policy = nl80211_policy,
9471 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009472 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009473 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009474 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009475 },
9476 {
Johannes Berg88600202012-02-13 15:17:18 +01009477 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009478 .policy = nl80211_policy,
9479 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009480 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009481 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009482 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009483 },
9484 {
Johannes Berg88600202012-02-13 15:17:18 +01009485 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009486 .policy = nl80211_policy,
9487 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009488 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009489 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009490 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009491 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009492 {
9493 .cmd = NL80211_CMD_GET_STATION,
9494 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009495 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009496 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009497 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9498 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009499 },
9500 {
9501 .cmd = NL80211_CMD_SET_STATION,
9502 .doit = nl80211_set_station,
9503 .policy = nl80211_policy,
9504 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009505 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009506 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009507 },
9508 {
9509 .cmd = NL80211_CMD_NEW_STATION,
9510 .doit = nl80211_new_station,
9511 .policy = nl80211_policy,
9512 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009513 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009514 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009515 },
9516 {
9517 .cmd = NL80211_CMD_DEL_STATION,
9518 .doit = nl80211_del_station,
9519 .policy = nl80211_policy,
9520 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009522 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009523 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009524 {
9525 .cmd = NL80211_CMD_GET_MPATH,
9526 .doit = nl80211_get_mpath,
9527 .dumpit = nl80211_dump_mpath,
9528 .policy = nl80211_policy,
9529 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009530 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009531 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009532 },
9533 {
9534 .cmd = NL80211_CMD_SET_MPATH,
9535 .doit = nl80211_set_mpath,
9536 .policy = nl80211_policy,
9537 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009538 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009539 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009540 },
9541 {
9542 .cmd = NL80211_CMD_NEW_MPATH,
9543 .doit = nl80211_new_mpath,
9544 .policy = nl80211_policy,
9545 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009546 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009547 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009548 },
9549 {
9550 .cmd = NL80211_CMD_DEL_MPATH,
9551 .doit = nl80211_del_mpath,
9552 .policy = nl80211_policy,
9553 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009554 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009555 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009556 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009557 {
9558 .cmd = NL80211_CMD_SET_BSS,
9559 .doit = nl80211_set_bss,
9560 .policy = nl80211_policy,
9561 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009562 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009563 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009564 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009565 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009566 .cmd = NL80211_CMD_GET_REG,
9567 .doit = nl80211_get_reg,
9568 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009569 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009570 /* can be retrieved by unprivileged users */
9571 },
9572 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009573 .cmd = NL80211_CMD_SET_REG,
9574 .doit = nl80211_set_reg,
9575 .policy = nl80211_policy,
9576 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009577 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009578 },
9579 {
9580 .cmd = NL80211_CMD_REQ_SET_REG,
9581 .doit = nl80211_req_set_reg,
9582 .policy = nl80211_policy,
9583 .flags = GENL_ADMIN_PERM,
9584 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009585 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009586 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9587 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009588 .policy = nl80211_policy,
9589 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009590 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009591 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009592 },
9593 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009594 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9595 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009596 .policy = nl80211_policy,
9597 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009599 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009600 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009601 {
Johannes Berg2a519312009-02-10 21:25:55 +01009602 .cmd = NL80211_CMD_TRIGGER_SCAN,
9603 .doit = nl80211_trigger_scan,
9604 .policy = nl80211_policy,
9605 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009606 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009607 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009608 },
9609 {
9610 .cmd = NL80211_CMD_GET_SCAN,
9611 .policy = nl80211_policy,
9612 .dumpit = nl80211_dump_scan,
9613 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009614 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009615 .cmd = NL80211_CMD_START_SCHED_SCAN,
9616 .doit = nl80211_start_sched_scan,
9617 .policy = nl80211_policy,
9618 .flags = GENL_ADMIN_PERM,
9619 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9620 NL80211_FLAG_NEED_RTNL,
9621 },
9622 {
9623 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9624 .doit = nl80211_stop_sched_scan,
9625 .policy = nl80211_policy,
9626 .flags = GENL_ADMIN_PERM,
9627 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9628 NL80211_FLAG_NEED_RTNL,
9629 },
9630 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009631 .cmd = NL80211_CMD_AUTHENTICATE,
9632 .doit = nl80211_authenticate,
9633 .policy = nl80211_policy,
9634 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009635 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009636 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009637 },
9638 {
9639 .cmd = NL80211_CMD_ASSOCIATE,
9640 .doit = nl80211_associate,
9641 .policy = nl80211_policy,
9642 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009643 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009644 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009645 },
9646 {
9647 .cmd = NL80211_CMD_DEAUTHENTICATE,
9648 .doit = nl80211_deauthenticate,
9649 .policy = nl80211_policy,
9650 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009651 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009652 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009653 },
9654 {
9655 .cmd = NL80211_CMD_DISASSOCIATE,
9656 .doit = nl80211_disassociate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009661 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009662 {
9663 .cmd = NL80211_CMD_JOIN_IBSS,
9664 .doit = nl80211_join_ibss,
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,
Johannes Berg04a773a2009-04-19 21:24:32 +02009669 },
9670 {
9671 .cmd = NL80211_CMD_LEAVE_IBSS,
9672 .doit = nl80211_leave_ibss,
9673 .policy = nl80211_policy,
9674 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009675 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009676 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009677 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009678#ifdef CONFIG_NL80211_TESTMODE
9679 {
9680 .cmd = NL80211_CMD_TESTMODE,
9681 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009682 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009683 .policy = nl80211_policy,
9684 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009685 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9686 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009687 },
9688#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009689 {
9690 .cmd = NL80211_CMD_CONNECT,
9691 .doit = nl80211_connect,
9692 .policy = nl80211_policy,
9693 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009695 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009696 },
9697 {
9698 .cmd = NL80211_CMD_DISCONNECT,
9699 .doit = nl80211_disconnect,
9700 .policy = nl80211_policy,
9701 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009703 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009704 },
Johannes Berg463d0182009-07-14 00:33:35 +02009705 {
9706 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9707 .doit = nl80211_wiphy_netns,
9708 .policy = nl80211_policy,
9709 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009710 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9711 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009712 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009713 {
9714 .cmd = NL80211_CMD_GET_SURVEY,
9715 .policy = nl80211_policy,
9716 .dumpit = nl80211_dump_survey,
9717 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009718 {
9719 .cmd = NL80211_CMD_SET_PMKSA,
9720 .doit = nl80211_setdel_pmksa,
9721 .policy = nl80211_policy,
9722 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009723 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009724 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009725 },
9726 {
9727 .cmd = NL80211_CMD_DEL_PMKSA,
9728 .doit = nl80211_setdel_pmksa,
9729 .policy = nl80211_policy,
9730 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009731 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009732 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009733 },
9734 {
9735 .cmd = NL80211_CMD_FLUSH_PMKSA,
9736 .doit = nl80211_flush_pmksa,
9737 .policy = nl80211_policy,
9738 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009739 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009740 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009741 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009742 {
9743 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9744 .doit = nl80211_remain_on_channel,
9745 .policy = nl80211_policy,
9746 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009747 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009748 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009749 },
9750 {
9751 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9752 .doit = nl80211_cancel_remain_on_channel,
9753 .policy = nl80211_policy,
9754 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009755 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009756 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009757 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009758 {
9759 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9760 .doit = nl80211_set_tx_bitrate_mask,
9761 .policy = nl80211_policy,
9762 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009763 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9764 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009765 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009766 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009767 .cmd = NL80211_CMD_REGISTER_FRAME,
9768 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009769 .policy = nl80211_policy,
9770 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009771 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009772 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009773 },
9774 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009775 .cmd = NL80211_CMD_FRAME,
9776 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009777 .policy = nl80211_policy,
9778 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009779 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009780 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009781 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009782 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009783 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9784 .doit = nl80211_tx_mgmt_cancel_wait,
9785 .policy = nl80211_policy,
9786 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009787 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009788 NL80211_FLAG_NEED_RTNL,
9789 },
9790 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009791 .cmd = NL80211_CMD_SET_POWER_SAVE,
9792 .doit = nl80211_set_power_save,
9793 .policy = nl80211_policy,
9794 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009795 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9796 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009797 },
9798 {
9799 .cmd = NL80211_CMD_GET_POWER_SAVE,
9800 .doit = nl80211_get_power_save,
9801 .policy = nl80211_policy,
9802 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009803 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9804 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009805 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009806 {
9807 .cmd = NL80211_CMD_SET_CQM,
9808 .doit = nl80211_set_cqm,
9809 .policy = nl80211_policy,
9810 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009811 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9812 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009813 },
Johannes Bergf444de02010-05-05 15:25:02 +02009814 {
9815 .cmd = NL80211_CMD_SET_CHANNEL,
9816 .doit = nl80211_set_channel,
9817 .policy = nl80211_policy,
9818 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009819 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9820 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009821 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009822 {
9823 .cmd = NL80211_CMD_SET_WDS_PEER,
9824 .doit = nl80211_set_wds_peer,
9825 .policy = nl80211_policy,
9826 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009827 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9828 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009829 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009830 {
9831 .cmd = NL80211_CMD_JOIN_MESH,
9832 .doit = nl80211_join_mesh,
9833 .policy = nl80211_policy,
9834 .flags = GENL_ADMIN_PERM,
9835 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9836 NL80211_FLAG_NEED_RTNL,
9837 },
9838 {
9839 .cmd = NL80211_CMD_LEAVE_MESH,
9840 .doit = nl80211_leave_mesh,
9841 .policy = nl80211_policy,
9842 .flags = GENL_ADMIN_PERM,
9843 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9844 NL80211_FLAG_NEED_RTNL,
9845 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009846#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009847 {
9848 .cmd = NL80211_CMD_GET_WOWLAN,
9849 .doit = nl80211_get_wowlan,
9850 .policy = nl80211_policy,
9851 /* can be retrieved by unprivileged users */
9852 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9853 NL80211_FLAG_NEED_RTNL,
9854 },
9855 {
9856 .cmd = NL80211_CMD_SET_WOWLAN,
9857 .doit = nl80211_set_wowlan,
9858 .policy = nl80211_policy,
9859 .flags = GENL_ADMIN_PERM,
9860 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9861 NL80211_FLAG_NEED_RTNL,
9862 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009863#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009864 {
9865 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9866 .doit = nl80211_set_rekey_data,
9867 .policy = nl80211_policy,
9868 .flags = GENL_ADMIN_PERM,
9869 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9870 NL80211_FLAG_NEED_RTNL,
9871 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009872 {
9873 .cmd = NL80211_CMD_TDLS_MGMT,
9874 .doit = nl80211_tdls_mgmt,
9875 .policy = nl80211_policy,
9876 .flags = GENL_ADMIN_PERM,
9877 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9878 NL80211_FLAG_NEED_RTNL,
9879 },
9880 {
9881 .cmd = NL80211_CMD_TDLS_OPER,
9882 .doit = nl80211_tdls_oper,
9883 .policy = nl80211_policy,
9884 .flags = GENL_ADMIN_PERM,
9885 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9886 NL80211_FLAG_NEED_RTNL,
9887 },
Johannes Berg28946da2011-11-04 11:18:12 +01009888 {
9889 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9890 .doit = nl80211_register_unexpected_frame,
9891 .policy = nl80211_policy,
9892 .flags = GENL_ADMIN_PERM,
9893 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9894 NL80211_FLAG_NEED_RTNL,
9895 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009896 {
9897 .cmd = NL80211_CMD_PROBE_CLIENT,
9898 .doit = nl80211_probe_client,
9899 .policy = nl80211_policy,
9900 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009901 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009902 NL80211_FLAG_NEED_RTNL,
9903 },
Johannes Berg5e7602302011-11-04 11:18:17 +01009904 {
9905 .cmd = NL80211_CMD_REGISTER_BEACONS,
9906 .doit = nl80211_register_beacons,
9907 .policy = nl80211_policy,
9908 .flags = GENL_ADMIN_PERM,
9909 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9910 NL80211_FLAG_NEED_RTNL,
9911 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009912 {
9913 .cmd = NL80211_CMD_SET_NOACK_MAP,
9914 .doit = nl80211_set_noack_map,
9915 .policy = nl80211_policy,
9916 .flags = GENL_ADMIN_PERM,
9917 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9918 NL80211_FLAG_NEED_RTNL,
9919 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009920 {
9921 .cmd = NL80211_CMD_START_P2P_DEVICE,
9922 .doit = nl80211_start_p2p_device,
9923 .policy = nl80211_policy,
9924 .flags = GENL_ADMIN_PERM,
9925 .internal_flags = NL80211_FLAG_NEED_WDEV |
9926 NL80211_FLAG_NEED_RTNL,
9927 },
9928 {
9929 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9930 .doit = nl80211_stop_p2p_device,
9931 .policy = nl80211_policy,
9932 .flags = GENL_ADMIN_PERM,
9933 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9934 NL80211_FLAG_NEED_RTNL,
9935 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009936 {
9937 .cmd = NL80211_CMD_SET_MCAST_RATE,
9938 .doit = nl80211_set_mcast_rate,
9939 .policy = nl80211_policy,
9940 .flags = GENL_ADMIN_PERM,
9941 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9942 NL80211_FLAG_NEED_RTNL,
9943 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309944 {
9945 .cmd = NL80211_CMD_SET_MAC_ACL,
9946 .doit = nl80211_set_mac_acl,
9947 .policy = nl80211_policy,
9948 .flags = GENL_ADMIN_PERM,
9949 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9950 NL80211_FLAG_NEED_RTNL,
9951 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009952 {
9953 .cmd = NL80211_CMD_RADAR_DETECT,
9954 .doit = nl80211_start_radar_detection,
9955 .policy = nl80211_policy,
9956 .flags = GENL_ADMIN_PERM,
9957 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9958 NL80211_FLAG_NEED_RTNL,
9959 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009960 {
9961 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9962 .doit = nl80211_get_protocol_features,
9963 .policy = nl80211_policy,
9964 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009965 {
9966 .cmd = NL80211_CMD_UPDATE_FT_IES,
9967 .doit = nl80211_update_ft_ies,
9968 .policy = nl80211_policy,
9969 .flags = GENL_ADMIN_PERM,
9970 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9971 NL80211_FLAG_NEED_RTNL,
9972 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009973 {
9974 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9975 .doit = nl80211_crit_protocol_start,
9976 .policy = nl80211_policy,
9977 .flags = GENL_ADMIN_PERM,
9978 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9979 NL80211_FLAG_NEED_RTNL,
9980 },
9981 {
9982 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9983 .doit = nl80211_crit_protocol_stop,
9984 .policy = nl80211_policy,
9985 .flags = GENL_ADMIN_PERM,
9986 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9987 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009988 },
9989 {
9990 .cmd = NL80211_CMD_GET_COALESCE,
9991 .doit = nl80211_get_coalesce,
9992 .policy = nl80211_policy,
9993 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9994 NL80211_FLAG_NEED_RTNL,
9995 },
9996 {
9997 .cmd = NL80211_CMD_SET_COALESCE,
9998 .doit = nl80211_set_coalesce,
9999 .policy = nl80211_policy,
10000 .flags = GENL_ADMIN_PERM,
10001 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10002 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020010003 },
10004 {
10005 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10006 .doit = nl80211_channel_switch,
10007 .policy = nl80211_policy,
10008 .flags = GENL_ADMIN_PERM,
10009 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10010 NL80211_FLAG_NEED_RTNL,
10011 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010012 {
10013 .cmd = NL80211_CMD_VENDOR,
10014 .doit = nl80211_vendor_cmd,
10015 .policy = nl80211_policy,
10016 .flags = GENL_ADMIN_PERM,
10017 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10018 NL80211_FLAG_NEED_RTNL,
10019 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010020 {
10021 .cmd = NL80211_CMD_SET_QOS_MAP,
10022 .doit = nl80211_set_qos_map,
10023 .policy = nl80211_policy,
10024 .flags = GENL_ADMIN_PERM,
10025 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10026 NL80211_FLAG_NEED_RTNL,
10027 },
Johannes Berg55682962007-09-20 13:09:35 -040010028};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010029
Johannes Berg55682962007-09-20 13:09:35 -040010030/* notification functions */
10031
10032void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
10033{
10034 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010035 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010036
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010037 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010038 if (!msg)
10039 return;
10040
Johannes Berg86e8cf92013-06-19 10:57:22 +020010041 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010042 nlmsg_free(msg);
10043 return;
10044 }
10045
Johannes Berg68eb5502013-11-19 15:19:38 +010010046 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010047 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010048}
10049
Johannes Berg362a4152009-05-24 16:43:15 +020010050static int nl80211_add_scan_req(struct sk_buff *msg,
10051 struct cfg80211_registered_device *rdev)
10052{
10053 struct cfg80211_scan_request *req = rdev->scan_req;
10054 struct nlattr *nest;
10055 int i;
10056
10057 if (WARN_ON(!req))
10058 return 0;
10059
10060 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10061 if (!nest)
10062 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010063 for (i = 0; i < req->n_ssids; i++) {
10064 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10065 goto nla_put_failure;
10066 }
Johannes Berg362a4152009-05-24 16:43:15 +020010067 nla_nest_end(msg, nest);
10068
10069 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10070 if (!nest)
10071 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010072 for (i = 0; i < req->n_channels; i++) {
10073 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10074 goto nla_put_failure;
10075 }
Johannes Berg362a4152009-05-24 16:43:15 +020010076 nla_nest_end(msg, nest);
10077
David S. Miller9360ffd2012-03-29 04:41:26 -040010078 if (req->ie &&
10079 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10080 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010081
Johannes Bergae917c92013-10-25 11:05:22 +020010082 if (req->flags &&
10083 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10084 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010085
Johannes Berg362a4152009-05-24 16:43:15 +020010086 return 0;
10087 nla_put_failure:
10088 return -ENOBUFS;
10089}
10090
Johannes Berga538e2d2009-06-16 19:56:42 +020010091static int nl80211_send_scan_msg(struct sk_buff *msg,
10092 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010093 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010094 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010095 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010096{
10097 void *hdr;
10098
Eric W. Biederman15e47302012-09-07 20:12:54 +000010099 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010100 if (!hdr)
10101 return -1;
10102
David S. Miller9360ffd2012-03-29 04:41:26 -040010103 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010104 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10105 wdev->netdev->ifindex)) ||
10106 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010107 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010108
Johannes Berg362a4152009-05-24 16:43:15 +020010109 /* ignore errors and send incomplete event anyway */
10110 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010111
10112 return genlmsg_end(msg, hdr);
10113
10114 nla_put_failure:
10115 genlmsg_cancel(msg, hdr);
10116 return -EMSGSIZE;
10117}
10118
Luciano Coelho807f8a82011-05-11 17:09:35 +030010119static int
10120nl80211_send_sched_scan_msg(struct sk_buff *msg,
10121 struct cfg80211_registered_device *rdev,
10122 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010123 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010124{
10125 void *hdr;
10126
Eric W. Biederman15e47302012-09-07 20:12:54 +000010127 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010128 if (!hdr)
10129 return -1;
10130
David S. Miller9360ffd2012-03-29 04:41:26 -040010131 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10132 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10133 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010134
10135 return genlmsg_end(msg, hdr);
10136
10137 nla_put_failure:
10138 genlmsg_cancel(msg, hdr);
10139 return -EMSGSIZE;
10140}
10141
Johannes Berga538e2d2009-06-16 19:56:42 +020010142void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010143 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010144{
10145 struct sk_buff *msg;
10146
Thomas Graf58050fc2012-06-28 03:57:45 +000010147 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010148 if (!msg)
10149 return;
10150
Johannes Bergfd014282012-06-18 19:17:03 +020010151 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010152 NL80211_CMD_TRIGGER_SCAN) < 0) {
10153 nlmsg_free(msg);
10154 return;
10155 }
10156
Johannes Berg68eb5502013-11-19 15:19:38 +010010157 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010158 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010159}
10160
Johannes Bergf9d15d12014-01-22 11:14:19 +020010161struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10162 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010163{
10164 struct sk_buff *msg;
10165
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010167 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010168 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010169
Johannes Bergfd014282012-06-18 19:17:03 +020010170 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010171 aborted ? NL80211_CMD_SCAN_ABORTED :
10172 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010173 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010174 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010175 }
10176
Johannes Bergf9d15d12014-01-22 11:14:19 +020010177 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010178}
10179
Johannes Bergf9d15d12014-01-22 11:14:19 +020010180void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10181 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010182{
Johannes Berg2a519312009-02-10 21:25:55 +010010183 if (!msg)
10184 return;
10185
Johannes Berg68eb5502013-11-19 15:19:38 +010010186 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010187 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010188}
10189
Luciano Coelho807f8a82011-05-11 17:09:35 +030010190void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10191 struct net_device *netdev)
10192{
10193 struct sk_buff *msg;
10194
10195 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10196 if (!msg)
10197 return;
10198
10199 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10200 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10201 nlmsg_free(msg);
10202 return;
10203 }
10204
Johannes Berg68eb5502013-11-19 15:19:38 +010010205 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010206 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010207}
10208
10209void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10210 struct net_device *netdev, u32 cmd)
10211{
10212 struct sk_buff *msg;
10213
Thomas Graf58050fc2012-06-28 03:57:45 +000010214 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010215 if (!msg)
10216 return;
10217
10218 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10219 nlmsg_free(msg);
10220 return;
10221 }
10222
Johannes Berg68eb5502013-11-19 15:19:38 +010010223 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010224 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010225}
10226
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010227/*
10228 * This can happen on global regulatory changes or device specific settings
10229 * based on custom world regulatory domains.
10230 */
10231void nl80211_send_reg_change_event(struct regulatory_request *request)
10232{
10233 struct sk_buff *msg;
10234 void *hdr;
10235
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010237 if (!msg)
10238 return;
10239
10240 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10241 if (!hdr) {
10242 nlmsg_free(msg);
10243 return;
10244 }
10245
10246 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010247 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10248 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010249
David S. Miller9360ffd2012-03-29 04:41:26 -040010250 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10251 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10252 NL80211_REGDOM_TYPE_WORLD))
10253 goto nla_put_failure;
10254 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10255 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10256 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10257 goto nla_put_failure;
10258 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10259 request->intersect) {
10260 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10261 NL80211_REGDOM_TYPE_INTERSECTION))
10262 goto nla_put_failure;
10263 } else {
10264 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10265 NL80211_REGDOM_TYPE_COUNTRY) ||
10266 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10267 request->alpha2))
10268 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010269 }
10270
Johannes Bergf4173762012-12-03 18:23:37 +010010271 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010272 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10273 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010274
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010275 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010276
Johannes Bergbc43b282009-07-25 10:54:13 +020010277 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010278 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010279 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010280 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010281
10282 return;
10283
10284nla_put_failure:
10285 genlmsg_cancel(msg, hdr);
10286 nlmsg_free(msg);
10287}
10288
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010289static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10290 struct net_device *netdev,
10291 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010292 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010293{
10294 struct sk_buff *msg;
10295 void *hdr;
10296
Johannes Berge6d6e342009-07-01 21:26:47 +020010297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010298 if (!msg)
10299 return;
10300
10301 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10302 if (!hdr) {
10303 nlmsg_free(msg);
10304 return;
10305 }
10306
David S. Miller9360ffd2012-03-29 04:41:26 -040010307 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10308 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10309 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10310 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010311
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010312 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010313
Johannes Berg68eb5502013-11-19 15:19:38 +010010314 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010315 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010316 return;
10317
10318 nla_put_failure:
10319 genlmsg_cancel(msg, hdr);
10320 nlmsg_free(msg);
10321}
10322
10323void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010324 struct net_device *netdev, const u8 *buf,
10325 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010326{
10327 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010328 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010329}
10330
10331void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10332 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010333 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010334{
Johannes Berge6d6e342009-07-01 21:26:47 +020010335 nl80211_send_mlme_event(rdev, netdev, buf, len,
10336 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010337}
10338
Jouni Malinen53b46b82009-03-27 20:53:56 +020010339void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010340 struct net_device *netdev, const u8 *buf,
10341 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010342{
10343 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010344 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010345}
10346
Jouni Malinen53b46b82009-03-27 20:53:56 +020010347void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10348 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010349 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010350{
10351 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010352 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010353}
10354
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010355void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10356 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010357{
Johannes Berg947add32013-02-22 22:05:20 +010010358 struct wireless_dev *wdev = dev->ieee80211_ptr;
10359 struct wiphy *wiphy = wdev->wiphy;
10360 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010361 const struct ieee80211_mgmt *mgmt = (void *)buf;
10362 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010363
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010364 if (WARN_ON(len < 2))
10365 return;
10366
10367 if (ieee80211_is_deauth(mgmt->frame_control))
10368 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10369 else
10370 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10371
10372 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10373 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010374}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010375EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010376
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010377static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10378 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010379 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010380{
10381 struct sk_buff *msg;
10382 void *hdr;
10383
Johannes Berge6d6e342009-07-01 21:26:47 +020010384 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010385 if (!msg)
10386 return;
10387
10388 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10389 if (!hdr) {
10390 nlmsg_free(msg);
10391 return;
10392 }
10393
David S. Miller9360ffd2012-03-29 04:41:26 -040010394 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10395 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10396 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10397 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10398 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010399
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010400 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010401
Johannes Berg68eb5502013-11-19 15:19:38 +010010402 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010403 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010404 return;
10405
10406 nla_put_failure:
10407 genlmsg_cancel(msg, hdr);
10408 nlmsg_free(msg);
10409}
10410
10411void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010412 struct net_device *netdev, const u8 *addr,
10413 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010414{
10415 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010416 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010417}
10418
10419void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010420 struct net_device *netdev, const u8 *addr,
10421 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010422{
Johannes Berge6d6e342009-07-01 21:26:47 +020010423 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10424 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010425}
10426
Samuel Ortizb23aa672009-07-01 21:26:54 +020010427void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10428 struct net_device *netdev, const u8 *bssid,
10429 const u8 *req_ie, size_t req_ie_len,
10430 const u8 *resp_ie, size_t resp_ie_len,
10431 u16 status, gfp_t gfp)
10432{
10433 struct sk_buff *msg;
10434 void *hdr;
10435
Thomas Graf58050fc2012-06-28 03:57:45 +000010436 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010437 if (!msg)
10438 return;
10439
10440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10441 if (!hdr) {
10442 nlmsg_free(msg);
10443 return;
10444 }
10445
David S. Miller9360ffd2012-03-29 04:41:26 -040010446 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10447 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10448 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10449 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10450 (req_ie &&
10451 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10452 (resp_ie &&
10453 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10454 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010455
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010456 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010457
Johannes Berg68eb5502013-11-19 15:19:38 +010010458 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010459 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010460 return;
10461
10462 nla_put_failure:
10463 genlmsg_cancel(msg, hdr);
10464 nlmsg_free(msg);
10465
10466}
10467
10468void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10469 struct net_device *netdev, const u8 *bssid,
10470 const u8 *req_ie, size_t req_ie_len,
10471 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10472{
10473 struct sk_buff *msg;
10474 void *hdr;
10475
Thomas Graf58050fc2012-06-28 03:57:45 +000010476 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010477 if (!msg)
10478 return;
10479
10480 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10481 if (!hdr) {
10482 nlmsg_free(msg);
10483 return;
10484 }
10485
David S. Miller9360ffd2012-03-29 04:41:26 -040010486 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10487 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10488 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10489 (req_ie &&
10490 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10491 (resp_ie &&
10492 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10493 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010494
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010495 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010496
Johannes Berg68eb5502013-11-19 15:19:38 +010010497 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010498 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010499 return;
10500
10501 nla_put_failure:
10502 genlmsg_cancel(msg, hdr);
10503 nlmsg_free(msg);
10504
10505}
10506
10507void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10508 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010509 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010510{
10511 struct sk_buff *msg;
10512 void *hdr;
10513
Thomas Graf58050fc2012-06-28 03:57:45 +000010514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010515 if (!msg)
10516 return;
10517
10518 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10519 if (!hdr) {
10520 nlmsg_free(msg);
10521 return;
10522 }
10523
David S. Miller9360ffd2012-03-29 04:41:26 -040010524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10525 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10526 (from_ap && reason &&
10527 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10528 (from_ap &&
10529 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10530 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10531 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010532
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010533 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010534
Johannes Berg68eb5502013-11-19 15:19:38 +010010535 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010536 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010537 return;
10538
10539 nla_put_failure:
10540 genlmsg_cancel(msg, hdr);
10541 nlmsg_free(msg);
10542
10543}
10544
Johannes Berg04a773a2009-04-19 21:24:32 +020010545void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10546 struct net_device *netdev, const u8 *bssid,
10547 gfp_t gfp)
10548{
10549 struct sk_buff *msg;
10550 void *hdr;
10551
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010553 if (!msg)
10554 return;
10555
10556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10557 if (!hdr) {
10558 nlmsg_free(msg);
10559 return;
10560 }
10561
David S. Miller9360ffd2012-03-29 04:41:26 -040010562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10563 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10565 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010566
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010567 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010568
Johannes Berg68eb5502013-11-19 15:19:38 +010010569 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010570 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010571 return;
10572
10573 nla_put_failure:
10574 genlmsg_cancel(msg, hdr);
10575 nlmsg_free(msg);
10576}
10577
Johannes Berg947add32013-02-22 22:05:20 +010010578void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10579 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010580{
Johannes Berg947add32013-02-22 22:05:20 +010010581 struct wireless_dev *wdev = dev->ieee80211_ptr;
10582 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010583 struct sk_buff *msg;
10584 void *hdr;
10585
Johannes Berg947add32013-02-22 22:05:20 +010010586 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10587 return;
10588
10589 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10590
Javier Cardonac93b5e72011-04-07 15:08:34 -070010591 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10592 if (!msg)
10593 return;
10594
10595 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10596 if (!hdr) {
10597 nlmsg_free(msg);
10598 return;
10599 }
10600
David S. Miller9360ffd2012-03-29 04:41:26 -040010601 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010602 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10603 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010604 (ie_len && ie &&
10605 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10606 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010607
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010608 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010609
Johannes Berg68eb5502013-11-19 15:19:38 +010010610 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010611 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010612 return;
10613
10614 nla_put_failure:
10615 genlmsg_cancel(msg, hdr);
10616 nlmsg_free(msg);
10617}
Johannes Berg947add32013-02-22 22:05:20 +010010618EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010619
Jouni Malinena3b8b052009-03-27 21:59:49 +020010620void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10621 struct net_device *netdev, const u8 *addr,
10622 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010623 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010624{
10625 struct sk_buff *msg;
10626 void *hdr;
10627
Johannes Berge6d6e342009-07-01 21:26:47 +020010628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010629 if (!msg)
10630 return;
10631
10632 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10633 if (!hdr) {
10634 nlmsg_free(msg);
10635 return;
10636 }
10637
David S. Miller9360ffd2012-03-29 04:41:26 -040010638 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10639 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10640 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10641 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10642 (key_id != -1 &&
10643 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10644 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10645 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010646
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010647 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010648
Johannes Berg68eb5502013-11-19 15:19:38 +010010649 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010650 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010651 return;
10652
10653 nla_put_failure:
10654 genlmsg_cancel(msg, hdr);
10655 nlmsg_free(msg);
10656}
10657
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010658void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10659 struct ieee80211_channel *channel_before,
10660 struct ieee80211_channel *channel_after)
10661{
10662 struct sk_buff *msg;
10663 void *hdr;
10664 struct nlattr *nl_freq;
10665
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010666 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010667 if (!msg)
10668 return;
10669
10670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10671 if (!hdr) {
10672 nlmsg_free(msg);
10673 return;
10674 }
10675
10676 /*
10677 * Since we are applying the beacon hint to a wiphy we know its
10678 * wiphy_idx is valid
10679 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010680 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10681 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010682
10683 /* Before */
10684 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10685 if (!nl_freq)
10686 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010687 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010688 goto nla_put_failure;
10689 nla_nest_end(msg, nl_freq);
10690
10691 /* After */
10692 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10693 if (!nl_freq)
10694 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010695 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010696 goto nla_put_failure;
10697 nla_nest_end(msg, nl_freq);
10698
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010699 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010700
Johannes Berg463d0182009-07-14 00:33:35 +020010701 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010702 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010703 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010704 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010705
10706 return;
10707
10708nla_put_failure:
10709 genlmsg_cancel(msg, hdr);
10710 nlmsg_free(msg);
10711}
10712
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010713static void nl80211_send_remain_on_chan_event(
10714 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010715 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010716 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010717 unsigned int duration, gfp_t gfp)
10718{
10719 struct sk_buff *msg;
10720 void *hdr;
10721
10722 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10723 if (!msg)
10724 return;
10725
10726 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10727 if (!hdr) {
10728 nlmsg_free(msg);
10729 return;
10730 }
10731
David S. Miller9360ffd2012-03-29 04:41:26 -040010732 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010733 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10734 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010735 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010736 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010737 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10738 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010739 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10740 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010741
David S. Miller9360ffd2012-03-29 04:41:26 -040010742 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10743 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10744 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010745
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010746 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010747
Johannes Berg68eb5502013-11-19 15:19:38 +010010748 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010749 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010750 return;
10751
10752 nla_put_failure:
10753 genlmsg_cancel(msg, hdr);
10754 nlmsg_free(msg);
10755}
10756
Johannes Berg947add32013-02-22 22:05:20 +010010757void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10758 struct ieee80211_channel *chan,
10759 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010760{
Johannes Berg947add32013-02-22 22:05:20 +010010761 struct wiphy *wiphy = wdev->wiphy;
10762 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10763
10764 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010765 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010766 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010767 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010768}
Johannes Berg947add32013-02-22 22:05:20 +010010769EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010770
Johannes Berg947add32013-02-22 22:05:20 +010010771void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10772 struct ieee80211_channel *chan,
10773 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010774{
Johannes Berg947add32013-02-22 22:05:20 +010010775 struct wiphy *wiphy = wdev->wiphy;
10776 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10777
10778 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010779 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010780 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010781}
Johannes Berg947add32013-02-22 22:05:20 +010010782EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010783
Johannes Berg947add32013-02-22 22:05:20 +010010784void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10785 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010786{
Johannes Berg947add32013-02-22 22:05:20 +010010787 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10788 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010789 struct sk_buff *msg;
10790
Johannes Berg947add32013-02-22 22:05:20 +010010791 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10792
Thomas Graf58050fc2012-06-28 03:57:45 +000010793 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010794 if (!msg)
10795 return;
10796
John W. Linville66266b32012-03-15 13:25:41 -040010797 if (nl80211_send_station(msg, 0, 0, 0,
10798 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010799 nlmsg_free(msg);
10800 return;
10801 }
10802
Johannes Berg68eb5502013-11-19 15:19:38 +010010803 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010804 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010805}
Johannes Berg947add32013-02-22 22:05:20 +010010806EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010807
Johannes Berg947add32013-02-22 22:05:20 +010010808void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010809{
Johannes Berg947add32013-02-22 22:05:20 +010010810 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10811 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010812 struct sk_buff *msg;
10813 void *hdr;
10814
Johannes Berg947add32013-02-22 22:05:20 +010010815 trace_cfg80211_del_sta(dev, mac_addr);
10816
Thomas Graf58050fc2012-06-28 03:57:45 +000010817 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010818 if (!msg)
10819 return;
10820
10821 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10822 if (!hdr) {
10823 nlmsg_free(msg);
10824 return;
10825 }
10826
David S. Miller9360ffd2012-03-29 04:41:26 -040010827 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10828 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10829 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010830
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010831 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010832
Johannes Berg68eb5502013-11-19 15:19:38 +010010833 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010834 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010835 return;
10836
10837 nla_put_failure:
10838 genlmsg_cancel(msg, hdr);
10839 nlmsg_free(msg);
10840}
Johannes Berg947add32013-02-22 22:05:20 +010010841EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010842
Johannes Berg947add32013-02-22 22:05:20 +010010843void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10844 enum nl80211_connect_failed_reason reason,
10845 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010846{
Johannes Berg947add32013-02-22 22:05:20 +010010847 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10848 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010849 struct sk_buff *msg;
10850 void *hdr;
10851
10852 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10853 if (!msg)
10854 return;
10855
10856 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10857 if (!hdr) {
10858 nlmsg_free(msg);
10859 return;
10860 }
10861
10862 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10863 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10864 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10865 goto nla_put_failure;
10866
10867 genlmsg_end(msg, hdr);
10868
Johannes Berg68eb5502013-11-19 15:19:38 +010010869 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010870 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010871 return;
10872
10873 nla_put_failure:
10874 genlmsg_cancel(msg, hdr);
10875 nlmsg_free(msg);
10876}
Johannes Berg947add32013-02-22 22:05:20 +010010877EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010878
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010879static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10880 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010881{
10882 struct wireless_dev *wdev = dev->ieee80211_ptr;
10883 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10884 struct sk_buff *msg;
10885 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010886 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010887
Eric W. Biederman15e47302012-09-07 20:12:54 +000010888 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010889 return false;
10890
10891 msg = nlmsg_new(100, gfp);
10892 if (!msg)
10893 return true;
10894
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010895 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010896 if (!hdr) {
10897 nlmsg_free(msg);
10898 return true;
10899 }
10900
David S. Miller9360ffd2012-03-29 04:41:26 -040010901 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10902 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10903 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10904 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010905
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010906 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010907 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010908 return true;
10909
10910 nla_put_failure:
10911 genlmsg_cancel(msg, hdr);
10912 nlmsg_free(msg);
10913 return true;
10914}
10915
Johannes Berg947add32013-02-22 22:05:20 +010010916bool cfg80211_rx_spurious_frame(struct net_device *dev,
10917 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010918{
Johannes Berg947add32013-02-22 22:05:20 +010010919 struct wireless_dev *wdev = dev->ieee80211_ptr;
10920 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010921
Johannes Berg947add32013-02-22 22:05:20 +010010922 trace_cfg80211_rx_spurious_frame(dev, addr);
10923
10924 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10925 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10926 trace_cfg80211_return_bool(false);
10927 return false;
10928 }
10929 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10930 addr, gfp);
10931 trace_cfg80211_return_bool(ret);
10932 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010933}
Johannes Berg947add32013-02-22 22:05:20 +010010934EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10935
10936bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10937 const u8 *addr, gfp_t gfp)
10938{
10939 struct wireless_dev *wdev = dev->ieee80211_ptr;
10940 bool ret;
10941
10942 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10943
10944 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10945 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10946 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10947 trace_cfg80211_return_bool(false);
10948 return false;
10949 }
10950 ret = __nl80211_unexpected_frame(dev,
10951 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10952 addr, gfp);
10953 trace_cfg80211_return_bool(ret);
10954 return ret;
10955}
10956EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010957
Johannes Berg2e161f72010-08-12 15:38:38 +020010958int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010959 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010960 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010961 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010962{
Johannes Berg71bbc992012-06-15 15:30:18 +020010963 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010964 struct sk_buff *msg;
10965 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010966
10967 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10968 if (!msg)
10969 return -ENOMEM;
10970
Johannes Berg2e161f72010-08-12 15:38:38 +020010971 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010972 if (!hdr) {
10973 nlmsg_free(msg);
10974 return -ENOMEM;
10975 }
10976
David S. Miller9360ffd2012-03-29 04:41:26 -040010977 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010978 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10979 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010980 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010981 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10982 (sig_dbm &&
10983 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010984 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10985 (flags &&
10986 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010987 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010988
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010989 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010990
Eric W. Biederman15e47302012-09-07 20:12:54 +000010991 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010992
10993 nla_put_failure:
10994 genlmsg_cancel(msg, hdr);
10995 nlmsg_free(msg);
10996 return -ENOBUFS;
10997}
10998
Johannes Berg947add32013-02-22 22:05:20 +010010999void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
11000 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011001{
Johannes Berg947add32013-02-22 22:05:20 +010011002 struct wiphy *wiphy = wdev->wiphy;
11003 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020011004 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011005 struct sk_buff *msg;
11006 void *hdr;
11007
Johannes Berg947add32013-02-22 22:05:20 +010011008 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11009
Jouni Malinen026331c2010-02-15 12:53:10 +020011010 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11011 if (!msg)
11012 return;
11013
Johannes Berg2e161f72010-08-12 15:38:38 +020011014 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011015 if (!hdr) {
11016 nlmsg_free(msg);
11017 return;
11018 }
11019
David S. Miller9360ffd2012-03-29 04:41:26 -040011020 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011021 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11022 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011023 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011024 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11025 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11026 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11027 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011028
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011029 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011030
Johannes Berg68eb5502013-11-19 15:19:38 +010011031 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011032 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011033 return;
11034
11035 nla_put_failure:
11036 genlmsg_cancel(msg, hdr);
11037 nlmsg_free(msg);
11038}
Johannes Berg947add32013-02-22 22:05:20 +010011039EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011040
Johannes Berg947add32013-02-22 22:05:20 +010011041void cfg80211_cqm_rssi_notify(struct net_device *dev,
11042 enum nl80211_cqm_rssi_threshold_event rssi_event,
11043 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011044{
Johannes Berg947add32013-02-22 22:05:20 +010011045 struct wireless_dev *wdev = dev->ieee80211_ptr;
11046 struct wiphy *wiphy = wdev->wiphy;
11047 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011048 struct sk_buff *msg;
11049 struct nlattr *pinfoattr;
11050 void *hdr;
11051
Johannes Berg947add32013-02-22 22:05:20 +010011052 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11053
Thomas Graf58050fc2012-06-28 03:57:45 +000011054 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011055 if (!msg)
11056 return;
11057
11058 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11059 if (!hdr) {
11060 nlmsg_free(msg);
11061 return;
11062 }
11063
David S. Miller9360ffd2012-03-29 04:41:26 -040011064 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011065 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011066 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011067
11068 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11069 if (!pinfoattr)
11070 goto nla_put_failure;
11071
David S. Miller9360ffd2012-03-29 04:41:26 -040011072 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11073 rssi_event))
11074 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011075
11076 nla_nest_end(msg, pinfoattr);
11077
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011078 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011079
Johannes Berg68eb5502013-11-19 15:19:38 +010011080 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011081 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011082 return;
11083
11084 nla_put_failure:
11085 genlmsg_cancel(msg, hdr);
11086 nlmsg_free(msg);
11087}
Johannes Berg947add32013-02-22 22:05:20 +010011088EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011089
Johannes Berg947add32013-02-22 22:05:20 +010011090static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11091 struct net_device *netdev, const u8 *bssid,
11092 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011093{
11094 struct sk_buff *msg;
11095 struct nlattr *rekey_attr;
11096 void *hdr;
11097
Thomas Graf58050fc2012-06-28 03:57:45 +000011098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011099 if (!msg)
11100 return;
11101
11102 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11103 if (!hdr) {
11104 nlmsg_free(msg);
11105 return;
11106 }
11107
David S. Miller9360ffd2012-03-29 04:41:26 -040011108 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11109 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11110 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11111 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011112
11113 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11114 if (!rekey_attr)
11115 goto nla_put_failure;
11116
David S. Miller9360ffd2012-03-29 04:41:26 -040011117 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11118 NL80211_REPLAY_CTR_LEN, replay_ctr))
11119 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011120
11121 nla_nest_end(msg, rekey_attr);
11122
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011123 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011124
Johannes Berg68eb5502013-11-19 15:19:38 +010011125 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011126 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011127 return;
11128
11129 nla_put_failure:
11130 genlmsg_cancel(msg, hdr);
11131 nlmsg_free(msg);
11132}
11133
Johannes Berg947add32013-02-22 22:05:20 +010011134void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11135 const u8 *replay_ctr, gfp_t gfp)
11136{
11137 struct wireless_dev *wdev = dev->ieee80211_ptr;
11138 struct wiphy *wiphy = wdev->wiphy;
11139 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11140
11141 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11142 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11143}
11144EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11145
11146static void
11147nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11148 struct net_device *netdev, int index,
11149 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011150{
11151 struct sk_buff *msg;
11152 struct nlattr *attr;
11153 void *hdr;
11154
Thomas Graf58050fc2012-06-28 03:57:45 +000011155 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011156 if (!msg)
11157 return;
11158
11159 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11160 if (!hdr) {
11161 nlmsg_free(msg);
11162 return;
11163 }
11164
David S. Miller9360ffd2012-03-29 04:41:26 -040011165 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11166 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11167 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011168
11169 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11170 if (!attr)
11171 goto nla_put_failure;
11172
David S. Miller9360ffd2012-03-29 04:41:26 -040011173 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11174 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11175 (preauth &&
11176 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11177 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011178
11179 nla_nest_end(msg, attr);
11180
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011181 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011182
Johannes Berg68eb5502013-11-19 15:19:38 +010011183 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011184 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011185 return;
11186
11187 nla_put_failure:
11188 genlmsg_cancel(msg, hdr);
11189 nlmsg_free(msg);
11190}
11191
Johannes Berg947add32013-02-22 22:05:20 +010011192void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11193 const u8 *bssid, bool preauth, gfp_t gfp)
11194{
11195 struct wireless_dev *wdev = dev->ieee80211_ptr;
11196 struct wiphy *wiphy = wdev->wiphy;
11197 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11198
11199 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11200 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11201}
11202EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11203
11204static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11205 struct net_device *netdev,
11206 struct cfg80211_chan_def *chandef,
11207 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011208{
11209 struct sk_buff *msg;
11210 void *hdr;
11211
Thomas Graf58050fc2012-06-28 03:57:45 +000011212 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011213 if (!msg)
11214 return;
11215
11216 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11217 if (!hdr) {
11218 nlmsg_free(msg);
11219 return;
11220 }
11221
Johannes Berg683b6d32012-11-08 21:25:48 +010011222 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11223 goto nla_put_failure;
11224
11225 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011226 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011227
11228 genlmsg_end(msg, hdr);
11229
Johannes Berg68eb5502013-11-19 15:19:38 +010011230 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011231 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011232 return;
11233
11234 nla_put_failure:
11235 genlmsg_cancel(msg, hdr);
11236 nlmsg_free(msg);
11237}
11238
Johannes Berg947add32013-02-22 22:05:20 +010011239void cfg80211_ch_switch_notify(struct net_device *dev,
11240 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011241{
Johannes Berg947add32013-02-22 22:05:20 +010011242 struct wireless_dev *wdev = dev->ieee80211_ptr;
11243 struct wiphy *wiphy = wdev->wiphy;
11244 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11245
Simon Wunderliche487eae2013-11-21 18:19:51 +010011246 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011247
Simon Wunderliche487eae2013-11-21 18:19:51 +010011248 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011249
11250 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011251 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011252 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11253 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011254 return;
Johannes Berg947add32013-02-22 22:05:20 +010011255
Michal Kazior9e0e2962014-01-29 14:22:27 +010011256 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011257 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011258 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011259}
11260EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11261
11262void cfg80211_cqm_txe_notify(struct net_device *dev,
11263 const u8 *peer, u32 num_packets,
11264 u32 rate, u32 intvl, gfp_t gfp)
11265{
11266 struct wireless_dev *wdev = dev->ieee80211_ptr;
11267 struct wiphy *wiphy = wdev->wiphy;
11268 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011269 struct sk_buff *msg;
11270 struct nlattr *pinfoattr;
11271 void *hdr;
11272
11273 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11274 if (!msg)
11275 return;
11276
11277 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11278 if (!hdr) {
11279 nlmsg_free(msg);
11280 return;
11281 }
11282
11283 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011284 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011285 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11286 goto nla_put_failure;
11287
11288 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11289 if (!pinfoattr)
11290 goto nla_put_failure;
11291
11292 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11293 goto nla_put_failure;
11294
11295 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11296 goto nla_put_failure;
11297
11298 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11299 goto nla_put_failure;
11300
11301 nla_nest_end(msg, pinfoattr);
11302
11303 genlmsg_end(msg, hdr);
11304
Johannes Berg68eb5502013-11-19 15:19:38 +010011305 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011306 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011307 return;
11308
11309 nla_put_failure:
11310 genlmsg_cancel(msg, hdr);
11311 nlmsg_free(msg);
11312}
Johannes Berg947add32013-02-22 22:05:20 +010011313EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011314
11315void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011316nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011317 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011318 enum nl80211_radar_event event,
11319 struct net_device *netdev, gfp_t gfp)
11320{
11321 struct sk_buff *msg;
11322 void *hdr;
11323
11324 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11325 if (!msg)
11326 return;
11327
11328 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11329 if (!hdr) {
11330 nlmsg_free(msg);
11331 return;
11332 }
11333
11334 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11335 goto nla_put_failure;
11336
11337 /* NOP and radar events don't need a netdev parameter */
11338 if (netdev) {
11339 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11340
11341 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11342 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11343 goto nla_put_failure;
11344 }
11345
11346 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11347 goto nla_put_failure;
11348
11349 if (nl80211_send_chandef(msg, chandef))
11350 goto nla_put_failure;
11351
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011352 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011353
Johannes Berg68eb5502013-11-19 15:19:38 +010011354 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011355 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011356 return;
11357
11358 nla_put_failure:
11359 genlmsg_cancel(msg, hdr);
11360 nlmsg_free(msg);
11361}
11362
Johannes Berg947add32013-02-22 22:05:20 +010011363void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11364 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011365{
Johannes Berg947add32013-02-22 22:05:20 +010011366 struct wireless_dev *wdev = dev->ieee80211_ptr;
11367 struct wiphy *wiphy = wdev->wiphy;
11368 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011369 struct sk_buff *msg;
11370 struct nlattr *pinfoattr;
11371 void *hdr;
11372
Johannes Berg947add32013-02-22 22:05:20 +010011373 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11374
Thomas Graf58050fc2012-06-28 03:57:45 +000011375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011376 if (!msg)
11377 return;
11378
11379 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11380 if (!hdr) {
11381 nlmsg_free(msg);
11382 return;
11383 }
11384
David S. Miller9360ffd2012-03-29 04:41:26 -040011385 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011386 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011387 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11388 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011389
11390 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11391 if (!pinfoattr)
11392 goto nla_put_failure;
11393
David S. Miller9360ffd2012-03-29 04:41:26 -040011394 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11395 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011396
11397 nla_nest_end(msg, pinfoattr);
11398
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011399 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011400
Johannes Berg68eb5502013-11-19 15:19:38 +010011401 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011402 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011403 return;
11404
11405 nla_put_failure:
11406 genlmsg_cancel(msg, hdr);
11407 nlmsg_free(msg);
11408}
Johannes Berg947add32013-02-22 22:05:20 +010011409EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011410
Johannes Berg7f6cf312011-11-04 11:18:15 +010011411void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11412 u64 cookie, bool acked, gfp_t gfp)
11413{
11414 struct wireless_dev *wdev = dev->ieee80211_ptr;
11415 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11416 struct sk_buff *msg;
11417 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011418
Beni Lev4ee3e062012-08-27 12:49:39 +030011419 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11420
Thomas Graf58050fc2012-06-28 03:57:45 +000011421 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011422
Johannes Berg7f6cf312011-11-04 11:18:15 +010011423 if (!msg)
11424 return;
11425
11426 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11427 if (!hdr) {
11428 nlmsg_free(msg);
11429 return;
11430 }
11431
David S. Miller9360ffd2012-03-29 04:41:26 -040011432 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11433 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11434 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11435 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11436 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11437 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011438
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011439 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011440
Johannes Berg68eb5502013-11-19 15:19:38 +010011441 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011442 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011443 return;
11444
11445 nla_put_failure:
11446 genlmsg_cancel(msg, hdr);
11447 nlmsg_free(msg);
11448}
11449EXPORT_SYMBOL(cfg80211_probe_status);
11450
Johannes Berg5e7602302011-11-04 11:18:17 +010011451void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11452 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011453 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010011454{
11455 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11456 struct sk_buff *msg;
11457 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011458 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010011459
Beni Lev4ee3e062012-08-27 12:49:39 +030011460 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11461
Ben Greear37c73b52012-10-26 14:49:25 -070011462 spin_lock_bh(&rdev->beacon_registrations_lock);
11463 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11464 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11465 if (!msg) {
11466 spin_unlock_bh(&rdev->beacon_registrations_lock);
11467 return;
11468 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011469
Ben Greear37c73b52012-10-26 14:49:25 -070011470 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11471 if (!hdr)
11472 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010011473
Ben Greear37c73b52012-10-26 14:49:25 -070011474 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11475 (freq &&
11476 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11477 (sig_dbm &&
11478 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11479 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11480 goto nla_put_failure;
11481
11482 genlmsg_end(msg, hdr);
11483
11484 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010011485 }
Ben Greear37c73b52012-10-26 14:49:25 -070011486 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011487 return;
11488
11489 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011490 spin_unlock_bh(&rdev->beacon_registrations_lock);
11491 if (hdr)
11492 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010011493 nlmsg_free(msg);
11494}
11495EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11496
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011497#ifdef CONFIG_PM
11498void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11499 struct cfg80211_wowlan_wakeup *wakeup,
11500 gfp_t gfp)
11501{
11502 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11503 struct sk_buff *msg;
11504 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011505 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011506
11507 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11508
11509 if (wakeup)
11510 size += wakeup->packet_present_len;
11511
11512 msg = nlmsg_new(size, gfp);
11513 if (!msg)
11514 return;
11515
11516 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11517 if (!hdr)
11518 goto free_msg;
11519
11520 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11521 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11522 goto free_msg;
11523
11524 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11525 wdev->netdev->ifindex))
11526 goto free_msg;
11527
11528 if (wakeup) {
11529 struct nlattr *reasons;
11530
11531 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011532 if (!reasons)
11533 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011534
11535 if (wakeup->disconnect &&
11536 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11537 goto free_msg;
11538 if (wakeup->magic_pkt &&
11539 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11540 goto free_msg;
11541 if (wakeup->gtk_rekey_failure &&
11542 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11543 goto free_msg;
11544 if (wakeup->eap_identity_req &&
11545 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11546 goto free_msg;
11547 if (wakeup->four_way_handshake &&
11548 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11549 goto free_msg;
11550 if (wakeup->rfkill_release &&
11551 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11552 goto free_msg;
11553
11554 if (wakeup->pattern_idx >= 0 &&
11555 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11556 wakeup->pattern_idx))
11557 goto free_msg;
11558
Johannes Bergae917c92013-10-25 11:05:22 +020011559 if (wakeup->tcp_match &&
11560 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11561 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011562
Johannes Bergae917c92013-10-25 11:05:22 +020011563 if (wakeup->tcp_connlost &&
11564 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11565 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011566
Johannes Bergae917c92013-10-25 11:05:22 +020011567 if (wakeup->tcp_nomoretokens &&
11568 nla_put_flag(msg,
11569 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11570 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011571
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011572 if (wakeup->packet) {
11573 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11574 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11575
11576 if (!wakeup->packet_80211) {
11577 pkt_attr =
11578 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11579 len_attr =
11580 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11581 }
11582
11583 if (wakeup->packet_len &&
11584 nla_put_u32(msg, len_attr, wakeup->packet_len))
11585 goto free_msg;
11586
11587 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11588 wakeup->packet))
11589 goto free_msg;
11590 }
11591
11592 nla_nest_end(msg, reasons);
11593 }
11594
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011595 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011596
Johannes Berg68eb5502013-11-19 15:19:38 +010011597 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011598 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011599 return;
11600
11601 free_msg:
11602 nlmsg_free(msg);
11603}
11604EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11605#endif
11606
Jouni Malinen3475b092012-11-16 22:49:57 +020011607void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11608 enum nl80211_tdls_operation oper,
11609 u16 reason_code, gfp_t gfp)
11610{
11611 struct wireless_dev *wdev = dev->ieee80211_ptr;
11612 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11613 struct sk_buff *msg;
11614 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011615
11616 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11617 reason_code);
11618
11619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11620 if (!msg)
11621 return;
11622
11623 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11624 if (!hdr) {
11625 nlmsg_free(msg);
11626 return;
11627 }
11628
11629 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11630 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11631 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11632 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11633 (reason_code > 0 &&
11634 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11635 goto nla_put_failure;
11636
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011637 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011638
Johannes Berg68eb5502013-11-19 15:19:38 +010011639 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011640 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011641 return;
11642
11643 nla_put_failure:
11644 genlmsg_cancel(msg, hdr);
11645 nlmsg_free(msg);
11646}
11647EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11648
Jouni Malinen026331c2010-02-15 12:53:10 +020011649static int nl80211_netlink_notify(struct notifier_block * nb,
11650 unsigned long state,
11651 void *_notify)
11652{
11653 struct netlink_notify *notify = _notify;
11654 struct cfg80211_registered_device *rdev;
11655 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011656 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011657
11658 if (state != NETLINK_URELEASE)
11659 return NOTIFY_DONE;
11660
11661 rcu_read_lock();
11662
Johannes Berg5e7602302011-11-04 11:18:17 +010011663 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010011664 bool schedule_destroy_work = false;
11665
11666 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000011667 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011668
Johannes Berg78f22b62014-03-24 17:57:27 +010011669 if (wdev->owner_nlportid == notify->portid)
11670 schedule_destroy_work = true;
11671 }
11672
Ben Greear37c73b52012-10-26 14:49:25 -070011673 spin_lock_bh(&rdev->beacon_registrations_lock);
11674 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11675 list) {
11676 if (reg->nlportid == notify->portid) {
11677 list_del(&reg->list);
11678 kfree(reg);
11679 break;
11680 }
11681 }
11682 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010011683
11684 if (schedule_destroy_work) {
11685 struct cfg80211_iface_destroy *destroy;
11686
11687 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
11688 if (destroy) {
11689 destroy->nlportid = notify->portid;
11690 spin_lock(&rdev->destroy_list_lock);
11691 list_add(&destroy->list, &rdev->destroy_list);
11692 spin_unlock(&rdev->destroy_list_lock);
11693 schedule_work(&rdev->destroy_work);
11694 }
11695 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011696 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011697
11698 rcu_read_unlock();
11699
11700 return NOTIFY_DONE;
11701}
11702
11703static struct notifier_block nl80211_netlink_notifier = {
11704 .notifier_call = nl80211_netlink_notify,
11705};
11706
Jouni Malinen355199e2013-02-27 17:14:27 +020011707void cfg80211_ft_event(struct net_device *netdev,
11708 struct cfg80211_ft_event_params *ft_event)
11709{
11710 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11711 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11712 struct sk_buff *msg;
11713 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011714
11715 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11716
11717 if (!ft_event->target_ap)
11718 return;
11719
11720 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11721 if (!msg)
11722 return;
11723
11724 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011725 if (!hdr)
11726 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011727
Johannes Bergae917c92013-10-25 11:05:22 +020011728 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11729 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11730 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11731 goto out;
11732
11733 if (ft_event->ies &&
11734 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11735 goto out;
11736 if (ft_event->ric_ies &&
11737 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11738 ft_event->ric_ies))
11739 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011740
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011741 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011742
Johannes Berg68eb5502013-11-19 15:19:38 +010011743 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011744 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011745 return;
11746 out:
11747 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011748}
11749EXPORT_SYMBOL(cfg80211_ft_event);
11750
Arend van Spriel5de17982013-04-18 15:49:00 +020011751void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11752{
11753 struct cfg80211_registered_device *rdev;
11754 struct sk_buff *msg;
11755 void *hdr;
11756 u32 nlportid;
11757
11758 rdev = wiphy_to_dev(wdev->wiphy);
11759 if (!rdev->crit_proto_nlportid)
11760 return;
11761
11762 nlportid = rdev->crit_proto_nlportid;
11763 rdev->crit_proto_nlportid = 0;
11764
11765 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11766 if (!msg)
11767 return;
11768
11769 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11770 if (!hdr)
11771 goto nla_put_failure;
11772
11773 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11774 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11775 goto nla_put_failure;
11776
11777 genlmsg_end(msg, hdr);
11778
11779 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11780 return;
11781
11782 nla_put_failure:
11783 if (hdr)
11784 genlmsg_cancel(msg, hdr);
11785 nlmsg_free(msg);
11786
11787}
11788EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11789
Johannes Berg348baf02014-01-24 14:06:29 +010011790void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11791{
11792 struct wiphy *wiphy = wdev->wiphy;
11793 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11794 struct sk_buff *msg;
11795 void *hdr;
11796
11797 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11798 if (!msg)
11799 return;
11800
11801 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11802 if (!hdr)
11803 goto out;
11804
11805 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11806 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11807 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11808 goto out;
11809
11810 genlmsg_end(msg, hdr);
11811
11812 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11813 NL80211_MCGRP_MLME, GFP_KERNEL);
11814 return;
11815 out:
11816 nlmsg_free(msg);
11817}
11818
Johannes Berg55682962007-09-20 13:09:35 -040011819/* initialisation/exit functions */
11820
11821int nl80211_init(void)
11822{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011823 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011824
Johannes Berg2a94fe42013-11-19 15:19:39 +010011825 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11826 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011827 if (err)
11828 return err;
11829
Jouni Malinen026331c2010-02-15 12:53:10 +020011830 err = netlink_register_notifier(&nl80211_netlink_notifier);
11831 if (err)
11832 goto err_out;
11833
Johannes Berg55682962007-09-20 13:09:35 -040011834 return 0;
11835 err_out:
11836 genl_unregister_family(&nl80211_fam);
11837 return err;
11838}
11839
11840void nl80211_exit(void)
11841{
Jouni Malinen026331c2010-02-15 12:53:10 +020011842 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011843 genl_unregister_family(&nl80211_fam);
11844}