blob: c5ead18ad3abe0b4bf6dde49a7d2668494eb6480 [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 Malinen36aedc92008-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 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001942 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
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
Johannes Berg683b6d32012-11-08 21:25:48 +01003274 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003275 return -EINVAL;
3276
Simon Wunderlich04f39042013-02-08 18:16:19 +01003277 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3278 if (err < 0)
3279 return err;
3280 if (err) {
3281 radar_detect_width = BIT(params.chandef.width);
3282 params.radar_required = true;
3283 }
3284
Simon Wunderlich04f39042013-02-08 18:16:19 +01003285 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3286 params.chandef.chan,
3287 CHAN_MODE_SHARED,
3288 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003289 if (err)
3290 return err;
3291
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303292 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3293 params.acl = parse_acl_data(&rdev->wiphy, info);
3294 if (IS_ERR(params.acl))
3295 return PTR_ERR(params.acl);
3296 }
3297
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003298 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003299 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003300 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003301 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003302 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003303 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003304 wdev->ssid_len = params.ssid_len;
3305 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003306 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003307 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303308
3309 kfree(params.acl);
3310
Johannes Berg56d18932011-05-09 18:41:15 +02003311 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003312}
3313
Johannes Berg88600202012-02-13 15:17:18 +01003314static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3315{
3316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3317 struct net_device *dev = info->user_ptr[1];
3318 struct wireless_dev *wdev = dev->ieee80211_ptr;
3319 struct cfg80211_beacon_data params;
3320 int err;
3321
3322 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3323 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3324 return -EOPNOTSUPP;
3325
3326 if (!rdev->ops->change_beacon)
3327 return -EOPNOTSUPP;
3328
3329 if (!wdev->beacon_interval)
3330 return -EINVAL;
3331
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003332 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003333 if (err)
3334 return err;
3335
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003336 wdev_lock(wdev);
3337 err = rdev_change_beacon(rdev, dev, &params);
3338 wdev_unlock(wdev);
3339
3340 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003341}
3342
3343static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003344{
Johannes Berg4c476992010-10-04 21:36:35 +02003345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3346 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003347
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003348 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003349}
3350
Johannes Berg5727ef12007-12-19 02:03:34 +01003351static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3352 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3353 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3354 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003355 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003356 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003357 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003358};
3359
Johannes Bergeccb8e82009-05-11 21:57:56 +03003360static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003361 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003362 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003363{
3364 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003365 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003366 int flag;
3367
Johannes Bergeccb8e82009-05-11 21:57:56 +03003368 /*
3369 * Try parsing the new attribute first so userspace
3370 * can specify both for older kernels.
3371 */
3372 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3373 if (nla) {
3374 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003375
Johannes Bergeccb8e82009-05-11 21:57:56 +03003376 sta_flags = nla_data(nla);
3377 params->sta_flags_mask = sta_flags->mask;
3378 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003379 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003380 if ((params->sta_flags_mask |
3381 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3382 return -EINVAL;
3383 return 0;
3384 }
3385
3386 /* if present, parse the old attribute */
3387
3388 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003389 if (!nla)
3390 return 0;
3391
3392 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3393 nla, sta_flags_policy))
3394 return -EINVAL;
3395
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003396 /*
3397 * Only allow certain flags for interface types so that
3398 * other attributes are silently ignored. Remember that
3399 * this is backward compatibility code with old userspace
3400 * and shouldn't be hit in other cases anyway.
3401 */
3402 switch (iftype) {
3403 case NL80211_IFTYPE_AP:
3404 case NL80211_IFTYPE_AP_VLAN:
3405 case NL80211_IFTYPE_P2P_GO:
3406 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3407 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3408 BIT(NL80211_STA_FLAG_WME) |
3409 BIT(NL80211_STA_FLAG_MFP);
3410 break;
3411 case NL80211_IFTYPE_P2P_CLIENT:
3412 case NL80211_IFTYPE_STATION:
3413 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3414 BIT(NL80211_STA_FLAG_TDLS_PEER);
3415 break;
3416 case NL80211_IFTYPE_MESH_POINT:
3417 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3418 BIT(NL80211_STA_FLAG_MFP) |
3419 BIT(NL80211_STA_FLAG_AUTHORIZED);
3420 default:
3421 return -EINVAL;
3422 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003423
Johannes Berg3383b5a2012-05-10 20:14:43 +02003424 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3425 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003426 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003427
Johannes Berg3383b5a2012-05-10 20:14:43 +02003428 /* no longer support new API additions in old API */
3429 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3430 return -EINVAL;
3431 }
3432 }
3433
Johannes Berg5727ef12007-12-19 02:03:34 +01003434 return 0;
3435}
3436
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003437static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3438 int attr)
3439{
3440 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003441 u32 bitrate;
3442 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003443
3444 rate = nla_nest_start(msg, attr);
3445 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003446 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003447
3448 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3449 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003450 /* report 16-bit bitrate only if we can */
3451 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003452 if (bitrate > 0 &&
3453 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3454 return false;
3455 if (bitrate_compat > 0 &&
3456 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3457 return false;
3458
3459 if (info->flags & RATE_INFO_FLAGS_MCS) {
3460 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3461 return false;
3462 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3463 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3464 return false;
3465 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3466 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3467 return false;
3468 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3469 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3470 return false;
3471 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3472 return false;
3473 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3474 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3475 return false;
3476 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3477 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3478 return false;
3479 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3480 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3481 return false;
3482 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3483 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3484 return false;
3485 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3486 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3487 return false;
3488 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003489
3490 nla_nest_end(msg, rate);
3491 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003492}
3493
Felix Fietkau119363c2013-04-22 16:29:30 +02003494static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3495 int id)
3496{
3497 void *attr;
3498 int i = 0;
3499
3500 if (!mask)
3501 return true;
3502
3503 attr = nla_nest_start(msg, id);
3504 if (!attr)
3505 return false;
3506
3507 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3508 if (!(mask & BIT(i)))
3509 continue;
3510
3511 if (nla_put_u8(msg, i, signal[i]))
3512 return false;
3513 }
3514
3515 nla_nest_end(msg, attr);
3516
3517 return true;
3518}
3519
Eric W. Biederman15e47302012-09-07 20:12:54 +00003520static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003521 int flags,
3522 struct cfg80211_registered_device *rdev,
3523 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003524 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003525{
3526 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003527 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003528
Eric W. Biederman15e47302012-09-07 20:12:54 +00003529 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003530 if (!hdr)
3531 return -1;
3532
David S. Miller9360ffd2012-03-29 04:41:26 -04003533 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3534 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3535 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3536 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003537
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003538 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3539 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003540 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003541 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3542 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3543 sinfo->connected_time))
3544 goto nla_put_failure;
3545 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3546 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3547 sinfo->inactive_time))
3548 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003549 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3550 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003551 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003552 (u32)sinfo->rx_bytes))
3553 goto nla_put_failure;
3554 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003555 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003556 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3557 (u32)sinfo->tx_bytes))
3558 goto nla_put_failure;
3559 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3560 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003561 sinfo->rx_bytes))
3562 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003563 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3564 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003565 sinfo->tx_bytes))
3566 goto nla_put_failure;
3567 if ((sinfo->filled & STATION_INFO_LLID) &&
3568 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3569 goto nla_put_failure;
3570 if ((sinfo->filled & STATION_INFO_PLID) &&
3571 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3572 goto nla_put_failure;
3573 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3574 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3575 sinfo->plink_state))
3576 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003577 switch (rdev->wiphy.signal_type) {
3578 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003579 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3580 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3581 sinfo->signal))
3582 goto nla_put_failure;
3583 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3584 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3585 sinfo->signal_avg))
3586 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003587 break;
3588 default:
3589 break;
3590 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003591 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3592 if (!nl80211_put_signal(msg, sinfo->chains,
3593 sinfo->chain_signal,
3594 NL80211_STA_INFO_CHAIN_SIGNAL))
3595 goto nla_put_failure;
3596 }
3597 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3598 if (!nl80211_put_signal(msg, sinfo->chains,
3599 sinfo->chain_signal_avg,
3600 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3601 goto nla_put_failure;
3602 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003603 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003604 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3605 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003606 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003607 }
3608 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3609 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3610 NL80211_STA_INFO_RX_BITRATE))
3611 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003612 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003613 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3614 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3615 sinfo->rx_packets))
3616 goto nla_put_failure;
3617 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3618 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3619 sinfo->tx_packets))
3620 goto nla_put_failure;
3621 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3622 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3623 sinfo->tx_retries))
3624 goto nla_put_failure;
3625 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3626 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3627 sinfo->tx_failed))
3628 goto nla_put_failure;
3629 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3630 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3631 sinfo->beacon_loss_count))
3632 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003633 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3634 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3635 sinfo->local_pm))
3636 goto nla_put_failure;
3637 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3638 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3639 sinfo->peer_pm))
3640 goto nla_put_failure;
3641 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3642 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3643 sinfo->nonpeer_pm))
3644 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003645 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3646 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3647 if (!bss_param)
3648 goto nla_put_failure;
3649
David S. Miller9360ffd2012-03-29 04:41:26 -04003650 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3651 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3652 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3653 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3654 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3655 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3656 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3657 sinfo->bss_param.dtim_period) ||
3658 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3659 sinfo->bss_param.beacon_interval))
3660 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003661
3662 nla_nest_end(msg, bss_param);
3663 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003664 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3665 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3666 sizeof(struct nl80211_sta_flag_update),
3667 &sinfo->sta_flags))
3668 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003669 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3670 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3671 sinfo->t_offset))
3672 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003673 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003674
David S. Miller9360ffd2012-03-29 04:41:26 -04003675 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3676 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3677 sinfo->assoc_req_ies))
3678 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003679
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003680 return genlmsg_end(msg, hdr);
3681
3682 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003683 genlmsg_cancel(msg, hdr);
3684 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003685}
3686
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003687static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003688 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003689{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003690 struct station_info sinfo;
3691 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003692 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003693 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003694 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003695 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003696
Johannes Berg97990a02013-04-19 01:02:55 +02003697 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003698 if (err)
3699 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003700
Johannes Berg97990a02013-04-19 01:02:55 +02003701 if (!wdev->netdev) {
3702 err = -EINVAL;
3703 goto out_err;
3704 }
3705
Johannes Bergbba95fe2008-07-29 13:22:51 +02003706 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003707 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003708 goto out_err;
3709 }
3710
Johannes Bergbba95fe2008-07-29 13:22:51 +02003711 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003712 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003713 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003714 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003715 if (err == -ENOENT)
3716 break;
3717 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003718 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003719
3720 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003721 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003722 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003723 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003724 &sinfo) < 0)
3725 goto out;
3726
3727 sta_idx++;
3728 }
3729
3730
3731 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003732 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003733 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003734 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003735 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003736
3737 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003738}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003739
Johannes Berg5727ef12007-12-19 02:03:34 +01003740static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3741{
Johannes Berg4c476992010-10-04 21:36:35 +02003742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3743 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003744 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003745 struct sk_buff *msg;
3746 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003747 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003748
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003749 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003750
3751 if (!info->attrs[NL80211_ATTR_MAC])
3752 return -EINVAL;
3753
3754 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3755
Johannes Berg4c476992010-10-04 21:36:35 +02003756 if (!rdev->ops->get_station)
3757 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003758
Hila Gonene35e4d22012-06-27 17:19:42 +03003759 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003760 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003761 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003762
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003764 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003765 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003766
Eric W. Biederman15e47302012-09-07 20:12:54 +00003767 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003768 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003769 nlmsg_free(msg);
3770 return -ENOBUFS;
3771 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003772
Johannes Berg4c476992010-10-04 21:36:35 +02003773 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003774}
3775
Johannes Berg77ee7c82013-02-15 00:48:33 +01003776int cfg80211_check_station_change(struct wiphy *wiphy,
3777 struct station_parameters *params,
3778 enum cfg80211_station_type statype)
3779{
3780 if (params->listen_interval != -1)
3781 return -EINVAL;
3782 if (params->aid)
3783 return -EINVAL;
3784
3785 /* When you run into this, adjust the code below for the new flag */
3786 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3787
3788 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003789 case CFG80211_STA_MESH_PEER_KERNEL:
3790 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003791 /*
3792 * No ignoring the TDLS flag here -- the userspace mesh
3793 * code doesn't have the bug of including TDLS in the
3794 * mask everywhere.
3795 */
3796 if (params->sta_flags_mask &
3797 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3798 BIT(NL80211_STA_FLAG_MFP) |
3799 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3800 return -EINVAL;
3801 break;
3802 case CFG80211_STA_TDLS_PEER_SETUP:
3803 case CFG80211_STA_TDLS_PEER_ACTIVE:
3804 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3805 return -EINVAL;
3806 /* ignore since it can't change */
3807 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3808 break;
3809 default:
3810 /* disallow mesh-specific things */
3811 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3812 return -EINVAL;
3813 if (params->local_pm)
3814 return -EINVAL;
3815 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3816 return -EINVAL;
3817 }
3818
3819 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3820 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3821 /* TDLS can't be set, ... */
3822 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3823 return -EINVAL;
3824 /*
3825 * ... but don't bother the driver with it. This works around
3826 * a hostapd/wpa_supplicant issue -- it always includes the
3827 * TLDS_PEER flag in the mask even for AP mode.
3828 */
3829 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3830 }
3831
3832 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3833 /* reject other things that can't change */
3834 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3835 return -EINVAL;
3836 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3837 return -EINVAL;
3838 if (params->supported_rates)
3839 return -EINVAL;
3840 if (params->ext_capab || params->ht_capa || params->vht_capa)
3841 return -EINVAL;
3842 }
3843
3844 if (statype != CFG80211_STA_AP_CLIENT) {
3845 if (params->vlan)
3846 return -EINVAL;
3847 }
3848
3849 switch (statype) {
3850 case CFG80211_STA_AP_MLME_CLIENT:
3851 /* Use this only for authorizing/unauthorizing a station */
3852 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3853 return -EOPNOTSUPP;
3854 break;
3855 case CFG80211_STA_AP_CLIENT:
3856 /* accept only the listed bits */
3857 if (params->sta_flags_mask &
3858 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3859 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3860 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3861 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3862 BIT(NL80211_STA_FLAG_WME) |
3863 BIT(NL80211_STA_FLAG_MFP)))
3864 return -EINVAL;
3865
3866 /* but authenticated/associated only if driver handles it */
3867 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3868 params->sta_flags_mask &
3869 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3870 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3871 return -EINVAL;
3872 break;
3873 case CFG80211_STA_IBSS:
3874 case CFG80211_STA_AP_STA:
3875 /* reject any changes other than AUTHORIZED */
3876 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3877 return -EINVAL;
3878 break;
3879 case CFG80211_STA_TDLS_PEER_SETUP:
3880 /* reject any changes other than AUTHORIZED or WME */
3881 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3882 BIT(NL80211_STA_FLAG_WME)))
3883 return -EINVAL;
3884 /* force (at least) rates when authorizing */
3885 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3886 !params->supported_rates)
3887 return -EINVAL;
3888 break;
3889 case CFG80211_STA_TDLS_PEER_ACTIVE:
3890 /* reject any changes */
3891 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003892 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003893 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3894 return -EINVAL;
3895 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003896 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003897 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3898 return -EINVAL;
3899 break;
3900 }
3901
3902 return 0;
3903}
3904EXPORT_SYMBOL(cfg80211_check_station_change);
3905
Johannes Berg5727ef12007-12-19 02:03:34 +01003906/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003907 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003908 */
Johannes Berg80b99892011-11-18 16:23:01 +01003909static struct net_device *get_vlan(struct genl_info *info,
3910 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003911{
Johannes Berg463d0182009-07-14 00:33:35 +02003912 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003913 struct net_device *v;
3914 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003915
Johannes Berg80b99892011-11-18 16:23:01 +01003916 if (!vlanattr)
3917 return NULL;
3918
3919 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3920 if (!v)
3921 return ERR_PTR(-ENODEV);
3922
3923 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3924 ret = -EINVAL;
3925 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003926 }
Johannes Berg80b99892011-11-18 16:23:01 +01003927
Johannes Berg77ee7c82013-02-15 00:48:33 +01003928 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3929 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3930 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3931 ret = -EINVAL;
3932 goto error;
3933 }
3934
Johannes Berg80b99892011-11-18 16:23:01 +01003935 if (!netif_running(v)) {
3936 ret = -ENETDOWN;
3937 goto error;
3938 }
3939
3940 return v;
3941 error:
3942 dev_put(v);
3943 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003944}
3945
Johannes Berg94e860f2014-01-20 23:58:15 +01003946static const struct nla_policy
3947nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003948 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3949 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3950};
3951
Johannes Bergff276692013-02-15 00:09:01 +01003952static int nl80211_parse_sta_wme(struct genl_info *info,
3953 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003954{
Jouni Malinendf881292013-02-14 21:10:54 +02003955 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3956 struct nlattr *nla;
3957 int err;
3958
Jouni Malinendf881292013-02-14 21:10:54 +02003959 /* parse WME attributes if present */
3960 if (!info->attrs[NL80211_ATTR_STA_WME])
3961 return 0;
3962
3963 nla = info->attrs[NL80211_ATTR_STA_WME];
3964 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3965 nl80211_sta_wme_policy);
3966 if (err)
3967 return err;
3968
3969 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3970 params->uapsd_queues = nla_get_u8(
3971 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3972 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3973 return -EINVAL;
3974
3975 if (tb[NL80211_STA_WME_MAX_SP])
3976 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3977
3978 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3979 return -EINVAL;
3980
3981 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3982
3983 return 0;
3984}
3985
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303986static int nl80211_parse_sta_channel_info(struct genl_info *info,
3987 struct station_parameters *params)
3988{
3989 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3990 params->supported_channels =
3991 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3992 params->supported_channels_len =
3993 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3994 /*
3995 * Need to include at least one (first channel, number of
3996 * channels) tuple for each subband, and must have proper
3997 * tuples for the rest of the data as well.
3998 */
3999 if (params->supported_channels_len < 2)
4000 return -EINVAL;
4001 if (params->supported_channels_len % 2)
4002 return -EINVAL;
4003 }
4004
4005 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4006 params->supported_oper_classes =
4007 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4008 params->supported_oper_classes_len =
4009 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4010 /*
4011 * The value of the Length field of the Supported Operating
4012 * Classes element is between 2 and 253.
4013 */
4014 if (params->supported_oper_classes_len < 2 ||
4015 params->supported_oper_classes_len > 253)
4016 return -EINVAL;
4017 }
4018 return 0;
4019}
4020
Johannes Bergff276692013-02-15 00:09:01 +01004021static int nl80211_set_station_tdls(struct genl_info *info,
4022 struct station_parameters *params)
4023{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304024 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004025 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004026 if (info->attrs[NL80211_ATTR_PEER_AID])
4027 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004028 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4029 params->ht_capa =
4030 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4031 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4032 params->vht_capa =
4033 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4034
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304035 err = nl80211_parse_sta_channel_info(info, params);
4036 if (err)
4037 return err;
4038
Johannes Bergff276692013-02-15 00:09:01 +01004039 return nl80211_parse_sta_wme(info, params);
4040}
4041
Johannes Berg5727ef12007-12-19 02:03:34 +01004042static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4043{
Johannes Berg4c476992010-10-04 21:36:35 +02004044 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004045 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004046 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004047 u8 *mac_addr;
4048 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004049
4050 memset(&params, 0, sizeof(params));
4051
4052 params.listen_interval = -1;
4053
Johannes Berg77ee7c82013-02-15 00:48:33 +01004054 if (!rdev->ops->change_station)
4055 return -EOPNOTSUPP;
4056
Johannes Berg5727ef12007-12-19 02:03:34 +01004057 if (info->attrs[NL80211_ATTR_STA_AID])
4058 return -EINVAL;
4059
4060 if (!info->attrs[NL80211_ATTR_MAC])
4061 return -EINVAL;
4062
4063 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4064
4065 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4066 params.supported_rates =
4067 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4068 params.supported_rates_len =
4069 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4070 }
4071
Jouni Malinen9d62a982013-02-14 21:10:13 +02004072 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4073 params.capability =
4074 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4075 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4076 }
4077
4078 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4079 params.ext_capab =
4080 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4081 params.ext_capab_len =
4082 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4083 }
4084
Jouni Malinendf881292013-02-14 21:10:54 +02004085 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004086 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03004087
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004088 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004089 return -EINVAL;
4090
Johannes Bergf8bacc22013-02-14 23:27:01 +01004091 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004092 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004093 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4094 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4095 return -EINVAL;
4096 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004097
Johannes Bergf8bacc22013-02-14 23:27:01 +01004098 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004099 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004100 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4101 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4102 return -EINVAL;
4103 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4104 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004105
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004106 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4107 enum nl80211_mesh_power_mode pm = nla_get_u32(
4108 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4109
4110 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4111 pm > NL80211_MESH_POWER_MAX)
4112 return -EINVAL;
4113
4114 params.local_pm = pm;
4115 }
4116
Johannes Berg77ee7c82013-02-15 00:48:33 +01004117 /* Include parameters for TDLS peer (will check later) */
4118 err = nl80211_set_station_tdls(info, &params);
4119 if (err)
4120 return err;
4121
4122 params.vlan = get_vlan(info, rdev);
4123 if (IS_ERR(params.vlan))
4124 return PTR_ERR(params.vlan);
4125
Johannes Berga97f4422009-06-18 17:23:43 +02004126 switch (dev->ieee80211_ptr->iftype) {
4127 case NL80211_IFTYPE_AP:
4128 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004129 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004130 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004131 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004132 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004133 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004134 break;
4135 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004136 err = -EOPNOTSUPP;
4137 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004138 }
4139
Johannes Berg77ee7c82013-02-15 00:48:33 +01004140 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004141 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004142
Johannes Berg77ee7c82013-02-15 00:48:33 +01004143 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004144 if (params.vlan)
4145 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004146
Johannes Berg5727ef12007-12-19 02:03:34 +01004147 return err;
4148}
4149
4150static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4151{
Johannes Berg4c476992010-10-04 21:36:35 +02004152 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004153 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004154 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004155 struct station_parameters params;
4156 u8 *mac_addr = NULL;
4157
4158 memset(&params, 0, sizeof(params));
4159
Johannes Berg984c3112013-02-14 23:43:25 +01004160 if (!rdev->ops->add_station)
4161 return -EOPNOTSUPP;
4162
Johannes Berg5727ef12007-12-19 02:03:34 +01004163 if (!info->attrs[NL80211_ATTR_MAC])
4164 return -EINVAL;
4165
Johannes Berg5727ef12007-12-19 02:03:34 +01004166 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4167 return -EINVAL;
4168
4169 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4170 return -EINVAL;
4171
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004172 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4173 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004174 return -EINVAL;
4175
Johannes Berg5727ef12007-12-19 02:03:34 +01004176 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4177 params.supported_rates =
4178 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4179 params.supported_rates_len =
4180 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4181 params.listen_interval =
4182 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004183
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004184 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004185 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004186 else
4187 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004188 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4189 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004190
Jouni Malinen9d62a982013-02-14 21:10:13 +02004191 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4192 params.capability =
4193 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4194 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4195 }
4196
4197 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4198 params.ext_capab =
4199 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4200 params.ext_capab_len =
4201 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4202 }
4203
Jouni Malinen36aedc92008-08-25 11:58:58 +03004204 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4205 params.ht_capa =
4206 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004207
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004208 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4209 params.vht_capa =
4210 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4211
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004212 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4213 params.opmode_notif_used = true;
4214 params.opmode_notif =
4215 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4216 }
4217
Johannes Bergf8bacc22013-02-14 23:27:01 +01004218 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004219 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004220 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4221 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4222 return -EINVAL;
4223 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004224
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304225 err = nl80211_parse_sta_channel_info(info, &params);
4226 if (err)
4227 return err;
4228
Johannes Bergff276692013-02-15 00:09:01 +01004229 err = nl80211_parse_sta_wme(info, &params);
4230 if (err)
4231 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004232
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004233 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004234 return -EINVAL;
4235
Johannes Berg77ee7c82013-02-15 00:48:33 +01004236 /* When you run into this, adjust the code below for the new flag */
4237 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4238
Johannes Bergbdd90d52011-12-14 12:20:27 +01004239 switch (dev->ieee80211_ptr->iftype) {
4240 case NL80211_IFTYPE_AP:
4241 case NL80211_IFTYPE_AP_VLAN:
4242 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004243 /* ignore WME attributes if iface/sta is not capable */
4244 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4245 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4246 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004247
Johannes Bergbdd90d52011-12-14 12:20:27 +01004248 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004249 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4250 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004251 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004252 /* but don't bother the driver with it */
4253 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004254
Johannes Bergd582cff2012-10-26 17:53:44 +02004255 /* allow authenticated/associated only if driver handles it */
4256 if (!(rdev->wiphy.features &
4257 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4258 params.sta_flags_mask &
4259 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4260 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4261 return -EINVAL;
4262
Johannes Bergbdd90d52011-12-14 12:20:27 +01004263 /* must be last in here for error handling */
4264 params.vlan = get_vlan(info, rdev);
4265 if (IS_ERR(params.vlan))
4266 return PTR_ERR(params.vlan);
4267 break;
4268 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004269 /* ignore uAPSD data */
4270 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4271
Johannes Bergd582cff2012-10-26 17:53:44 +02004272 /* associated is disallowed */
4273 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4274 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004275 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004276 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4277 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004278 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004279 break;
4280 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004281 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004282 /* ignore uAPSD data */
4283 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4284
Johannes Berg77ee7c82013-02-15 00:48:33 +01004285 /* these are disallowed */
4286 if (params.sta_flags_mask &
4287 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4288 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004289 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004290 /* Only TDLS peers can be added */
4291 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4292 return -EINVAL;
4293 /* Can only add if TDLS ... */
4294 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4295 return -EOPNOTSUPP;
4296 /* ... with external setup is supported */
4297 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4298 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004299 /*
4300 * Older wpa_supplicant versions always mark the TDLS peer
4301 * as authorized, but it shouldn't yet be.
4302 */
4303 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004304 break;
4305 default:
4306 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004307 }
4308
Johannes Bergbdd90d52011-12-14 12:20:27 +01004309 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004310
Hila Gonene35e4d22012-06-27 17:19:42 +03004311 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004312
Johannes Berg5727ef12007-12-19 02:03:34 +01004313 if (params.vlan)
4314 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004315 return err;
4316}
4317
4318static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4319{
Johannes Berg4c476992010-10-04 21:36:35 +02004320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4321 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004322 u8 *mac_addr = NULL;
4323
4324 if (info->attrs[NL80211_ATTR_MAC])
4325 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4326
Johannes Berge80cf852009-05-11 14:43:13 +02004327 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004328 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004329 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004330 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4331 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004332
Johannes Berg4c476992010-10-04 21:36:35 +02004333 if (!rdev->ops->del_station)
4334 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004335
Hila Gonene35e4d22012-06-27 17:19:42 +03004336 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004337}
4338
Eric W. Biederman15e47302012-09-07 20:12:54 +00004339static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004340 int flags, struct net_device *dev,
4341 u8 *dst, u8 *next_hop,
4342 struct mpath_info *pinfo)
4343{
4344 void *hdr;
4345 struct nlattr *pinfoattr;
4346
Eric W. Biederman15e47302012-09-07 20:12:54 +00004347 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004348 if (!hdr)
4349 return -1;
4350
David S. Miller9360ffd2012-03-29 04:41:26 -04004351 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4352 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4353 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4354 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4355 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004356
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004357 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4358 if (!pinfoattr)
4359 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004360 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4361 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4362 pinfo->frame_qlen))
4363 goto nla_put_failure;
4364 if (((pinfo->filled & MPATH_INFO_SN) &&
4365 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4366 ((pinfo->filled & MPATH_INFO_METRIC) &&
4367 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4368 pinfo->metric)) ||
4369 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4370 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4371 pinfo->exptime)) ||
4372 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4373 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4374 pinfo->flags)) ||
4375 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4376 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4377 pinfo->discovery_timeout)) ||
4378 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4379 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4380 pinfo->discovery_retries)))
4381 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004382
4383 nla_nest_end(msg, pinfoattr);
4384
4385 return genlmsg_end(msg, hdr);
4386
4387 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004388 genlmsg_cancel(msg, hdr);
4389 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004390}
4391
4392static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004393 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004394{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004395 struct mpath_info pinfo;
4396 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004397 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004398 u8 dst[ETH_ALEN];
4399 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004400 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004401 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004402
Johannes Berg97990a02013-04-19 01:02:55 +02004403 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004404 if (err)
4405 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004406
4407 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004408 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004409 goto out_err;
4410 }
4411
Johannes Berg97990a02013-04-19 01:02:55 +02004412 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004413 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004414 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004415 }
4416
Johannes Bergbba95fe2008-07-29 13:22:51 +02004417 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004418 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4419 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004420 if (err == -ENOENT)
4421 break;
4422 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004423 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004424
Eric W. Biederman15e47302012-09-07 20:12:54 +00004425 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004426 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004427 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004428 &pinfo) < 0)
4429 goto out;
4430
4431 path_idx++;
4432 }
4433
4434
4435 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004436 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004437 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004438 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004439 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004440 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004441}
4442
4443static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4444{
Johannes Berg4c476992010-10-04 21:36:35 +02004445 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004446 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004447 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004448 struct mpath_info pinfo;
4449 struct sk_buff *msg;
4450 u8 *dst = NULL;
4451 u8 next_hop[ETH_ALEN];
4452
4453 memset(&pinfo, 0, sizeof(pinfo));
4454
4455 if (!info->attrs[NL80211_ATTR_MAC])
4456 return -EINVAL;
4457
4458 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4459
Johannes Berg4c476992010-10-04 21:36:35 +02004460 if (!rdev->ops->get_mpath)
4461 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4464 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004465
Hila Gonene35e4d22012-06-27 17:19:42 +03004466 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004467 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004468 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004469
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004470 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004471 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004472 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004473
Eric W. Biederman15e47302012-09-07 20:12:54 +00004474 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004475 dev, dst, next_hop, &pinfo) < 0) {
4476 nlmsg_free(msg);
4477 return -ENOBUFS;
4478 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004479
Johannes Berg4c476992010-10-04 21:36:35 +02004480 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004481}
4482
4483static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4484{
Johannes Berg4c476992010-10-04 21:36:35 +02004485 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4486 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487 u8 *dst = NULL;
4488 u8 *next_hop = NULL;
4489
4490 if (!info->attrs[NL80211_ATTR_MAC])
4491 return -EINVAL;
4492
4493 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4494 return -EINVAL;
4495
4496 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4497 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4498
Johannes Berg4c476992010-10-04 21:36:35 +02004499 if (!rdev->ops->change_mpath)
4500 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004501
Johannes Berg4c476992010-10-04 21:36:35 +02004502 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4503 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004504
Hila Gonene35e4d22012-06-27 17:19:42 +03004505 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004506}
Johannes Berg4c476992010-10-04 21:36:35 +02004507
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004508static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4509{
Johannes Berg4c476992010-10-04 21:36:35 +02004510 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4511 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004512 u8 *dst = NULL;
4513 u8 *next_hop = NULL;
4514
4515 if (!info->attrs[NL80211_ATTR_MAC])
4516 return -EINVAL;
4517
4518 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4519 return -EINVAL;
4520
4521 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4522 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4523
Johannes Berg4c476992010-10-04 21:36:35 +02004524 if (!rdev->ops->add_mpath)
4525 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004526
Johannes Berg4c476992010-10-04 21:36:35 +02004527 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4528 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004529
Hila Gonene35e4d22012-06-27 17:19:42 +03004530 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004531}
4532
4533static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4534{
Johannes Berg4c476992010-10-04 21:36:35 +02004535 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4536 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004537 u8 *dst = NULL;
4538
4539 if (info->attrs[NL80211_ATTR_MAC])
4540 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4541
Johannes Berg4c476992010-10-04 21:36:35 +02004542 if (!rdev->ops->del_mpath)
4543 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004544
Hila Gonene35e4d22012-06-27 17:19:42 +03004545 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004546}
4547
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004548static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4549{
Johannes Berg4c476992010-10-04 21:36:35 +02004550 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4551 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004552 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004553 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004554 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004555
4556 memset(&params, 0, sizeof(params));
4557 /* default to not changing parameters */
4558 params.use_cts_prot = -1;
4559 params.use_short_preamble = -1;
4560 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004561 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004562 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004563 params.p2p_ctwindow = -1;
4564 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004565
4566 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4567 params.use_cts_prot =
4568 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4569 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4570 params.use_short_preamble =
4571 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4572 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4573 params.use_short_slot_time =
4574 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004575 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4576 params.basic_rates =
4577 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4578 params.basic_rates_len =
4579 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4580 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004581 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4582 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004583 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4584 params.ht_opmode =
4585 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004586
Johannes Berg53cabad2012-11-14 15:17:28 +01004587 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4588 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4589 return -EINVAL;
4590 params.p2p_ctwindow =
4591 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4592 if (params.p2p_ctwindow < 0)
4593 return -EINVAL;
4594 if (params.p2p_ctwindow != 0 &&
4595 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4596 return -EINVAL;
4597 }
4598
4599 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4600 u8 tmp;
4601
4602 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4603 return -EINVAL;
4604 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4605 if (tmp > 1)
4606 return -EINVAL;
4607 params.p2p_opp_ps = tmp;
4608 if (params.p2p_opp_ps &&
4609 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4610 return -EINVAL;
4611 }
4612
Johannes Berg4c476992010-10-04 21:36:35 +02004613 if (!rdev->ops->change_bss)
4614 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004615
Johannes Berg074ac8d2010-09-16 14:58:22 +02004616 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004617 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4618 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004619
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004620 wdev_lock(wdev);
4621 err = rdev_change_bss(rdev, dev, &params);
4622 wdev_unlock(wdev);
4623
4624 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004625}
4626
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004627static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004628 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4629 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4630 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4631 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4632 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4633 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004634 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004635};
4636
4637static int parse_reg_rule(struct nlattr *tb[],
4638 struct ieee80211_reg_rule *reg_rule)
4639{
4640 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4641 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4642
4643 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4644 return -EINVAL;
4645 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4646 return -EINVAL;
4647 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4648 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004649 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4650 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004651 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4652 return -EINVAL;
4653
4654 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4655
4656 freq_range->start_freq_khz =
4657 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4658 freq_range->end_freq_khz =
4659 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004660 freq_range->max_bandwidth_khz =
4661 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004662
4663 power_rule->max_eirp =
4664 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4665
4666 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4667 power_rule->max_antenna_gain =
4668 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4669
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004670 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4671 reg_rule->dfs_cac_ms =
4672 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4673
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004674 return 0;
4675}
4676
4677static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4678{
4679 int r;
4680 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. Rodriguezfe33eb32009-02-21 00:04:30 -05004692 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4693 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004694
4695 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4696
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004697 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4698 user_reg_hint_type =
4699 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4700 else
4701 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4702
4703 switch (user_reg_hint_type) {
4704 case NL80211_USER_REG_HINT_USER:
4705 case NL80211_USER_REG_HINT_CELL_BASE:
4706 break;
4707 default:
4708 return -EINVAL;
4709 }
4710
4711 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004712
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004713 return r;
4714}
4715
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004716static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004717 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004718{
Johannes Berg4c476992010-10-04 21:36:35 +02004719 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004720 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004721 struct wireless_dev *wdev = dev->ieee80211_ptr;
4722 struct mesh_config cur_params;
4723 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004724 void *hdr;
4725 struct nlattr *pinfoattr;
4726 struct sk_buff *msg;
4727
Johannes Berg29cbe682010-12-03 09:20:44 +01004728 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4729 return -EOPNOTSUPP;
4730
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004731 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004732 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004733
Johannes Berg29cbe682010-12-03 09:20:44 +01004734 wdev_lock(wdev);
4735 /* If not connected, get default parameters */
4736 if (!wdev->mesh_id_len)
4737 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4738 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004739 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004740 wdev_unlock(wdev);
4741
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004742 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004743 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004744
4745 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004747 if (!msg)
4748 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004749 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004750 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004751 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004752 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004753 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004754 if (!pinfoattr)
4755 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004756 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4757 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4758 cur_params.dot11MeshRetryTimeout) ||
4759 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4760 cur_params.dot11MeshConfirmTimeout) ||
4761 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4762 cur_params.dot11MeshHoldingTimeout) ||
4763 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4764 cur_params.dot11MeshMaxPeerLinks) ||
4765 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4766 cur_params.dot11MeshMaxRetries) ||
4767 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4768 cur_params.dot11MeshTTL) ||
4769 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4770 cur_params.element_ttl) ||
4771 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4772 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004773 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4774 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004775 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4776 cur_params.dot11MeshHWMPmaxPREQretries) ||
4777 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4778 cur_params.path_refresh_time) ||
4779 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4780 cur_params.min_discovery_timeout) ||
4781 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4782 cur_params.dot11MeshHWMPactivePathTimeout) ||
4783 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4784 cur_params.dot11MeshHWMPpreqMinInterval) ||
4785 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4786 cur_params.dot11MeshHWMPperrMinInterval) ||
4787 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4788 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4789 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4790 cur_params.dot11MeshHWMPRootMode) ||
4791 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4792 cur_params.dot11MeshHWMPRannInterval) ||
4793 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4794 cur_params.dot11MeshGateAnnouncementProtocol) ||
4795 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4796 cur_params.dot11MeshForwarding) ||
4797 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004798 cur_params.rssi_threshold) ||
4799 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004800 cur_params.ht_opmode) ||
4801 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4802 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4803 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004804 cur_params.dot11MeshHWMProotInterval) ||
4805 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004806 cur_params.dot11MeshHWMPconfirmationInterval) ||
4807 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4808 cur_params.power_mode) ||
4809 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004810 cur_params.dot11MeshAwakeWindowDuration) ||
4811 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4812 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004813 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004814 nla_nest_end(msg, pinfoattr);
4815 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004816 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004817
Johannes Berg3b858752009-03-12 09:55:09 +01004818 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004819 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004820 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004821 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004822 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004823}
4824
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004825static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004826 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4827 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4828 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4829 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4830 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4831 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004832 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004833 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004834 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004835 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4836 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4837 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4838 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4839 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004840 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004841 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004842 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004843 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004844 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004845 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004846 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4847 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004848 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4849 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004850 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004851 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4852 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004853 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004854};
4855
Javier Cardonac80d5452010-12-16 17:37:49 -08004856static const struct nla_policy
4857 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004858 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004859 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4860 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004861 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004862 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004863 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004864 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004865 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004866 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004867};
4868
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004869static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004870 struct mesh_config *cfg,
4871 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004872{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004873 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004874 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004875
Marco Porschea54fba2013-01-07 16:04:48 +01004876#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4877do { \
4878 if (tb[attr]) { \
4879 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4880 return -EINVAL; \
4881 cfg->param = fn(tb[attr]); \
4882 mask |= (1 << (attr - 1)); \
4883 } \
4884} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004885
4886
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004887 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004888 return -EINVAL;
4889 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004890 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004891 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004892 return -EINVAL;
4893
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004894 /* This makes sure that there aren't more than 32 mesh config
4895 * parameters (otherwise our bitfield scheme would not work.) */
4896 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4897
4898 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004899 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004900 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4901 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004902 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004903 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4904 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004905 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004906 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4907 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004908 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004909 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4910 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004911 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004912 mask, NL80211_MESHCONF_MAX_RETRIES,
4913 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004914 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004915 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004917 mask, NL80211_MESHCONF_ELEMENT_TTL,
4918 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004920 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4921 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4923 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004924 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4925 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004927 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4928 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004930 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4931 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004933 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4934 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004935 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4936 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004937 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4938 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004939 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004940 1, 65535, mask,
4941 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004942 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004944 1, 65535, mask,
4945 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004946 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004947 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004948 dot11MeshHWMPnetDiameterTraversalTime,
4949 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004950 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4951 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004952 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4953 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4954 nla_get_u8);
4955 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4956 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004957 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004958 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004959 dot11MeshGateAnnouncementProtocol, 0, 1,
4960 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004961 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004962 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004963 mask, NL80211_MESHCONF_FORWARDING,
4964 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004965 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004966 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004967 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004968 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004969 mask, NL80211_MESHCONF_HT_OPMODE,
4970 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004971 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004972 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004973 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4974 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004975 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004976 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4977 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004978 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004979 dot11MeshHWMPconfirmationInterval,
4980 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004981 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4982 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004983 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4984 NL80211_MESH_POWER_ACTIVE,
4985 NL80211_MESH_POWER_MAX,
4986 mask, NL80211_MESHCONF_POWER_MODE,
4987 nla_get_u32);
4988 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4989 0, 65535, mask,
4990 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004991 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4992 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4993 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004994 if (mask_out)
4995 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004996
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004997 return 0;
4998
4999#undef FILL_IN_MESH_PARAM_IF_SET
5000}
5001
Javier Cardonac80d5452010-12-16 17:37:49 -08005002static int nl80211_parse_mesh_setup(struct genl_info *info,
5003 struct mesh_setup *setup)
5004{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08005006 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5007
5008 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5009 return -EINVAL;
5010 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5011 info->attrs[NL80211_ATTR_MESH_SETUP],
5012 nl80211_mesh_setup_params_policy))
5013 return -EINVAL;
5014
Javier Cardonad299a1f2012-03-31 11:31:33 -07005015 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5016 setup->sync_method =
5017 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5018 IEEE80211_SYNC_METHOD_VENDOR :
5019 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5020
Javier Cardonac80d5452010-12-16 17:37:49 -08005021 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5022 setup->path_sel_proto =
5023 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5024 IEEE80211_PATH_PROTOCOL_VENDOR :
5025 IEEE80211_PATH_PROTOCOL_HWMP;
5026
5027 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5028 setup->path_metric =
5029 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5030 IEEE80211_PATH_METRIC_VENDOR :
5031 IEEE80211_PATH_METRIC_AIRTIME;
5032
Javier Cardona581a8b02011-04-07 15:08:27 -07005033
5034 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005035 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005036 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005037 if (!is_valid_ie_attr(ieattr))
5038 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005039 setup->ie = nla_data(ieattr);
5040 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005041 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005042 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5043 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5044 return -EINVAL;
5045 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005046 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5047 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005048 if (setup->is_secure)
5049 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005050
Colleen Twitty6e16d902013-05-08 11:45:59 -07005051 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5052 if (!setup->user_mpm)
5053 return -EINVAL;
5054 setup->auth_id =
5055 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5056 }
5057
Javier Cardonac80d5452010-12-16 17:37:49 -08005058 return 0;
5059}
5060
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005061static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005062 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005063{
5064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5065 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005066 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005067 struct mesh_config cfg;
5068 u32 mask;
5069 int err;
5070
Johannes Berg29cbe682010-12-03 09:20:44 +01005071 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5072 return -EOPNOTSUPP;
5073
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005074 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005075 return -EOPNOTSUPP;
5076
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005077 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005078 if (err)
5079 return err;
5080
Johannes Berg29cbe682010-12-03 09:20:44 +01005081 wdev_lock(wdev);
5082 if (!wdev->mesh_id_len)
5083 err = -ENOLINK;
5084
5085 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005086 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005087
5088 wdev_unlock(wdev);
5089
5090 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005091}
5092
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005093static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5094{
Johannes Berg458f4f92012-12-06 15:47:38 +01005095 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005096 struct sk_buff *msg;
5097 void *hdr = NULL;
5098 struct nlattr *nl_reg_rules;
5099 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005100
5101 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005102 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005103
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005104 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005105 if (!msg)
5106 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005107
Eric W. Biederman15e47302012-09-07 20:12:54 +00005108 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005109 NL80211_CMD_GET_REG);
5110 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005111 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005112
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005113 if (reg_last_request_cell_base() &&
5114 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5115 NL80211_USER_REG_HINT_CELL_BASE))
5116 goto nla_put_failure;
5117
Johannes Berg458f4f92012-12-06 15:47:38 +01005118 rcu_read_lock();
5119 regdom = rcu_dereference(cfg80211_regdomain);
5120
5121 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5122 (regdom->dfs_region &&
5123 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5124 goto nla_put_failure_rcu;
5125
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005126 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5127 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005128 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005129
Johannes Berg458f4f92012-12-06 15:47:38 +01005130 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005131 struct nlattr *nl_reg_rule;
5132 const struct ieee80211_reg_rule *reg_rule;
5133 const struct ieee80211_freq_range *freq_range;
5134 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005135 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005136
Johannes Berg458f4f92012-12-06 15:47:38 +01005137 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005138 freq_range = &reg_rule->freq_range;
5139 power_rule = &reg_rule->power_rule;
5140
5141 nl_reg_rule = nla_nest_start(msg, i);
5142 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005143 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005144
Janusz Dziedzic97524822014-01-30 09:52:20 +01005145 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5146 if (!max_bandwidth_khz)
5147 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5148 reg_rule);
5149
David S. Miller9360ffd2012-03-29 04:41:26 -04005150 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5151 reg_rule->flags) ||
5152 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5153 freq_range->start_freq_khz) ||
5154 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5155 freq_range->end_freq_khz) ||
5156 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005157 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005158 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5159 power_rule->max_antenna_gain) ||
5160 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005161 power_rule->max_eirp) ||
5162 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5163 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005164 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005165
5166 nla_nest_end(msg, nl_reg_rule);
5167 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005168 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005169
5170 nla_nest_end(msg, nl_reg_rules);
5171
5172 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005173 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005174
Johannes Berg458f4f92012-12-06 15:47:38 +01005175nla_put_failure_rcu:
5176 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005177nla_put_failure:
5178 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005179put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005180 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005181 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005182}
5183
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005184static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5185{
5186 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5187 struct nlattr *nl_reg_rule;
5188 char *alpha2 = NULL;
5189 int rem_reg_rules = 0, r = 0;
5190 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005191 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005192 struct ieee80211_regdomain *rd = NULL;
5193
5194 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5195 return -EINVAL;
5196
5197 if (!info->attrs[NL80211_ATTR_REG_RULES])
5198 return -EINVAL;
5199
5200 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5201
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005202 if (info->attrs[NL80211_ATTR_DFS_REGION])
5203 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5204
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005205 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005206 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005207 num_rules++;
5208 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005209 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005210 }
5211
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005212 if (!reg_is_valid_request(alpha2))
5213 return -EINVAL;
5214
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005215 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005216 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005217
5218 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005219 if (!rd)
5220 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005221
5222 rd->n_reg_rules = num_rules;
5223 rd->alpha2[0] = alpha2[0];
5224 rd->alpha2[1] = alpha2[1];
5225
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005226 /*
5227 * Disable DFS master mode if the DFS region was
5228 * not supported or known on this kernel.
5229 */
5230 if (reg_supported_dfs_region(dfs_region))
5231 rd->dfs_region = dfs_region;
5232
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005233 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005234 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005235 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5236 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5237 reg_rule_policy);
5238 if (r)
5239 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005240 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5241 if (r)
5242 goto bad_reg;
5243
5244 rule_idx++;
5245
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005246 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5247 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005248 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005249 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005250 }
5251
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005252 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005253 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005254 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005255
Johannes Bergd2372b32008-10-24 20:32:20 +02005256 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005257 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005258 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005259}
5260
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005261static int validate_scan_freqs(struct nlattr *freqs)
5262{
5263 struct nlattr *attr1, *attr2;
5264 int n_channels = 0, tmp1, tmp2;
5265
5266 nla_for_each_nested(attr1, freqs, tmp1) {
5267 n_channels++;
5268 /*
5269 * Some hardware has a limited channel list for
5270 * scanning, and it is pretty much nonsensical
5271 * to scan for a channel twice, so disallow that
5272 * and don't require drivers to check that the
5273 * channel list they get isn't longer than what
5274 * they can scan, as long as they can scan all
5275 * the channels they registered at once.
5276 */
5277 nla_for_each_nested(attr2, freqs, tmp2)
5278 if (attr1 != attr2 &&
5279 nla_get_u32(attr1) == nla_get_u32(attr2))
5280 return 0;
5281 }
5282
5283 return n_channels;
5284}
5285
Johannes Berg2a519312009-02-10 21:25:55 +01005286static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5287{
Johannes Berg4c476992010-10-04 21:36:35 +02005288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005289 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005290 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005291 struct nlattr *attr;
5292 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005293 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005294 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005295
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005296 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5297 return -EINVAL;
5298
Johannes Berg79c97e92009-07-07 03:56:12 +02005299 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005300
Johannes Berg4c476992010-10-04 21:36:35 +02005301 if (!rdev->ops->scan)
5302 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005303
Johannes Bergf9d15d12014-01-22 11:14:19 +02005304 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005305 err = -EBUSY;
5306 goto unlock;
5307 }
Johannes Berg2a519312009-02-10 21:25:55 +01005308
5309 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005310 n_channels = validate_scan_freqs(
5311 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005312 if (!n_channels) {
5313 err = -EINVAL;
5314 goto unlock;
5315 }
Johannes Berg2a519312009-02-10 21:25:55 +01005316 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005317 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005318 }
5319
5320 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5321 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5322 n_ssids++;
5323
Johannes Bergf9f47522013-03-19 15:04:07 +01005324 if (n_ssids > wiphy->max_scan_ssids) {
5325 err = -EINVAL;
5326 goto unlock;
5327 }
Johannes Berg2a519312009-02-10 21:25:55 +01005328
Jouni Malinen70692ad2009-02-16 19:39:13 +02005329 if (info->attrs[NL80211_ATTR_IE])
5330 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5331 else
5332 ie_len = 0;
5333
Johannes Bergf9f47522013-03-19 15:04:07 +01005334 if (ie_len > wiphy->max_scan_ie_len) {
5335 err = -EINVAL;
5336 goto unlock;
5337 }
Johannes Berg18a83652009-03-31 12:12:05 +02005338
Johannes Berg2a519312009-02-10 21:25:55 +01005339 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005340 + sizeof(*request->ssids) * n_ssids
5341 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005342 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005343 if (!request) {
5344 err = -ENOMEM;
5345 goto unlock;
5346 }
Johannes Berg2a519312009-02-10 21:25:55 +01005347
Johannes Berg2a519312009-02-10 21:25:55 +01005348 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005349 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005350 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005351 if (ie_len) {
5352 if (request->ssids)
5353 request->ie = (void *)(request->ssids + n_ssids);
5354 else
5355 request->ie = (void *)(request->channels + n_channels);
5356 }
Johannes Berg2a519312009-02-10 21:25:55 +01005357
Johannes Berg584991d2009-11-02 13:32:03 +01005358 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005359 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5360 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005361 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005362 struct ieee80211_channel *chan;
5363
5364 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5365
5366 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005367 err = -EINVAL;
5368 goto out_free;
5369 }
Johannes Berg584991d2009-11-02 13:32:03 +01005370
5371 /* ignore disabled channels */
5372 if (chan->flags & IEEE80211_CHAN_DISABLED)
5373 continue;
5374
5375 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005376 i++;
5377 }
5378 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005379 enum ieee80211_band band;
5380
Johannes Berg2a519312009-02-10 21:25:55 +01005381 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005382 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5383 int j;
5384 if (!wiphy->bands[band])
5385 continue;
5386 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005387 struct ieee80211_channel *chan;
5388
5389 chan = &wiphy->bands[band]->channels[j];
5390
5391 if (chan->flags & IEEE80211_CHAN_DISABLED)
5392 continue;
5393
5394 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005395 i++;
5396 }
5397 }
5398 }
5399
Johannes Berg584991d2009-11-02 13:32:03 +01005400 if (!i) {
5401 err = -EINVAL;
5402 goto out_free;
5403 }
5404
5405 request->n_channels = i;
5406
Johannes Berg2a519312009-02-10 21:25:55 +01005407 i = 0;
5408 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5409 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005410 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005411 err = -EINVAL;
5412 goto out_free;
5413 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005414 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005415 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005416 i++;
5417 }
5418 }
5419
Jouni Malinen70692ad2009-02-16 19:39:13 +02005420 if (info->attrs[NL80211_ATTR_IE]) {
5421 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005422 memcpy((void *)request->ie,
5423 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005424 request->ie_len);
5425 }
5426
Johannes Berg34850ab2011-07-18 18:08:35 +02005427 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005428 if (wiphy->bands[i])
5429 request->rates[i] =
5430 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005431
5432 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5433 nla_for_each_nested(attr,
5434 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5435 tmp) {
5436 enum ieee80211_band band = nla_type(attr);
5437
Dan Carpenter84404622011-07-29 11:52:18 +03005438 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005439 err = -EINVAL;
5440 goto out_free;
5441 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005442
5443 if (!wiphy->bands[band])
5444 continue;
5445
Johannes Berg34850ab2011-07-18 18:08:35 +02005446 err = ieee80211_get_ratemask(wiphy->bands[band],
5447 nla_data(attr),
5448 nla_len(attr),
5449 &request->rates[band]);
5450 if (err)
5451 goto out_free;
5452 }
5453 }
5454
Sam Leffler46856bb2012-10-11 21:03:32 -07005455 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005456 request->flags = nla_get_u32(
5457 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005458 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5459 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005460 err = -EOPNOTSUPP;
5461 goto out_free;
5462 }
5463 }
Sam Lefflered4737712012-10-11 21:03:31 -07005464
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305465 request->no_cck =
5466 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5467
Johannes Bergfd014282012-06-18 19:17:03 +02005468 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005469 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005470 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005471
Johannes Berg79c97e92009-07-07 03:56:12 +02005472 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005473 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005474
Johannes Berg463d0182009-07-14 00:33:35 +02005475 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005476 nl80211_send_scan_start(rdev, wdev);
5477 if (wdev->netdev)
5478 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005479 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005480 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005481 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005482 kfree(request);
5483 }
Johannes Berg3b858752009-03-12 09:55:09 +01005484
Johannes Bergf9f47522013-03-19 15:04:07 +01005485 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005486 return err;
5487}
5488
Luciano Coelho807f8a82011-05-11 17:09:35 +03005489static int nl80211_start_sched_scan(struct sk_buff *skb,
5490 struct genl_info *info)
5491{
5492 struct cfg80211_sched_scan_request *request;
5493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5494 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005495 struct nlattr *attr;
5496 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005497 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005498 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005499 enum ieee80211_band band;
5500 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005501 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005502 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005503
5504 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5505 !rdev->ops->sched_scan_start)
5506 return -EOPNOTSUPP;
5507
5508 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5509 return -EINVAL;
5510
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005511 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5512 return -EINVAL;
5513
5514 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5515 if (interval == 0)
5516 return -EINVAL;
5517
Luciano Coelho807f8a82011-05-11 17:09:35 +03005518 wiphy = &rdev->wiphy;
5519
5520 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5521 n_channels = validate_scan_freqs(
5522 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5523 if (!n_channels)
5524 return -EINVAL;
5525 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005526 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005527 }
5528
5529 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5530 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5531 tmp)
5532 n_ssids++;
5533
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005534 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005535 return -EINVAL;
5536
Johannes Bergea73cbc2014-01-24 10:53:53 +01005537 /*
5538 * First, count the number of 'real' matchsets. Due to an issue with
5539 * the old implementation, matchsets containing only the RSSI attribute
5540 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5541 * RSSI for all matchsets, rather than their own matchset for reporting
5542 * all APs with a strong RSSI. This is needed to be compatible with
5543 * older userspace that treated a matchset with only the RSSI as the
5544 * global RSSI for all other matchsets - if there are other matchsets.
5545 */
5546 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005547 nla_for_each_nested(attr,
5548 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005549 tmp) {
5550 struct nlattr *rssi;
5551
5552 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5553 nla_data(attr), nla_len(attr),
5554 nl80211_match_policy);
5555 if (err)
5556 return err;
5557 /* add other standalone attributes here */
5558 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5559 n_match_sets++;
5560 continue;
5561 }
5562 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5563 if (rssi)
5564 default_match_rssi = nla_get_s32(rssi);
5565 }
5566 }
5567
5568 /* However, if there's no other matchset, add the RSSI one */
5569 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5570 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005571
5572 if (n_match_sets > wiphy->max_match_sets)
5573 return -EINVAL;
5574
Luciano Coelho807f8a82011-05-11 17:09:35 +03005575 if (info->attrs[NL80211_ATTR_IE])
5576 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5577 else
5578 ie_len = 0;
5579
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005580 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005581 return -EINVAL;
5582
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005583 if (rdev->sched_scan_req) {
5584 err = -EINPROGRESS;
5585 goto out;
5586 }
5587
Luciano Coelho807f8a82011-05-11 17:09:35 +03005588 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005589 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005590 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005591 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005592 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005593 if (!request) {
5594 err = -ENOMEM;
5595 goto out;
5596 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005597
5598 if (n_ssids)
5599 request->ssids = (void *)&request->channels[n_channels];
5600 request->n_ssids = n_ssids;
5601 if (ie_len) {
5602 if (request->ssids)
5603 request->ie = (void *)(request->ssids + n_ssids);
5604 else
5605 request->ie = (void *)(request->channels + n_channels);
5606 }
5607
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005608 if (n_match_sets) {
5609 if (request->ie)
5610 request->match_sets = (void *)(request->ie + ie_len);
5611 else if (request->ssids)
5612 request->match_sets =
5613 (void *)(request->ssids + n_ssids);
5614 else
5615 request->match_sets =
5616 (void *)(request->channels + n_channels);
5617 }
5618 request->n_match_sets = n_match_sets;
5619
Luciano Coelho807f8a82011-05-11 17:09:35 +03005620 i = 0;
5621 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5622 /* user specified, bail out if channel not found */
5623 nla_for_each_nested(attr,
5624 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5625 tmp) {
5626 struct ieee80211_channel *chan;
5627
5628 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5629
5630 if (!chan) {
5631 err = -EINVAL;
5632 goto out_free;
5633 }
5634
5635 /* ignore disabled channels */
5636 if (chan->flags & IEEE80211_CHAN_DISABLED)
5637 continue;
5638
5639 request->channels[i] = chan;
5640 i++;
5641 }
5642 } else {
5643 /* all channels */
5644 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5645 int j;
5646 if (!wiphy->bands[band])
5647 continue;
5648 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5649 struct ieee80211_channel *chan;
5650
5651 chan = &wiphy->bands[band]->channels[j];
5652
5653 if (chan->flags & IEEE80211_CHAN_DISABLED)
5654 continue;
5655
5656 request->channels[i] = chan;
5657 i++;
5658 }
5659 }
5660 }
5661
5662 if (!i) {
5663 err = -EINVAL;
5664 goto out_free;
5665 }
5666
5667 request->n_channels = i;
5668
5669 i = 0;
5670 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5671 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5672 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005673 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005674 err = -EINVAL;
5675 goto out_free;
5676 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005677 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005678 memcpy(request->ssids[i].ssid, nla_data(attr),
5679 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005680 i++;
5681 }
5682 }
5683
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005684 i = 0;
5685 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5686 nla_for_each_nested(attr,
5687 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5688 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005689 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005690
Johannes Bergae811e22014-01-24 10:17:47 +01005691 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5692 nla_data(attr), nla_len(attr),
5693 nl80211_match_policy);
5694 if (err)
5695 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005696 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005697 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005698 if (WARN_ON(i >= n_match_sets)) {
5699 /* this indicates a programming error,
5700 * the loop above should have verified
5701 * things properly
5702 */
5703 err = -EINVAL;
5704 goto out_free;
5705 }
5706
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005707 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5708 err = -EINVAL;
5709 goto out_free;
5710 }
5711 memcpy(request->match_sets[i].ssid.ssid,
5712 nla_data(ssid), nla_len(ssid));
5713 request->match_sets[i].ssid.ssid_len =
5714 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005715 /* special attribute - old implemenation w/a */
5716 request->match_sets[i].rssi_thold =
5717 default_match_rssi;
5718 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5719 if (rssi)
5720 request->match_sets[i].rssi_thold =
5721 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005722 }
5723 i++;
5724 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005725
5726 /* there was no other matchset, so the RSSI one is alone */
5727 if (i == 0)
5728 request->match_sets[0].rssi_thold = default_match_rssi;
5729
5730 request->min_rssi_thold = INT_MAX;
5731 for (i = 0; i < n_match_sets; i++)
5732 request->min_rssi_thold =
5733 min(request->match_sets[i].rssi_thold,
5734 request->min_rssi_thold);
5735 } else {
5736 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005737 }
5738
Johannes Berg9900e482014-02-04 21:01:25 +01005739 if (ie_len) {
5740 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005741 memcpy((void *)request->ie,
5742 nla_data(info->attrs[NL80211_ATTR_IE]),
5743 request->ie_len);
5744 }
5745
Sam Leffler46856bb2012-10-11 21:03:32 -07005746 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005747 request->flags = nla_get_u32(
5748 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005749 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5750 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005751 err = -EOPNOTSUPP;
5752 goto out_free;
5753 }
5754 }
Sam Lefflered4737712012-10-11 21:03:31 -07005755
Luciano Coelho807f8a82011-05-11 17:09:35 +03005756 request->dev = dev;
5757 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005758 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005759 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005760
Hila Gonene35e4d22012-06-27 17:19:42 +03005761 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005762 if (!err) {
5763 rdev->sched_scan_req = request;
5764 nl80211_send_sched_scan(rdev, dev,
5765 NL80211_CMD_START_SCHED_SCAN);
5766 goto out;
5767 }
5768
5769out_free:
5770 kfree(request);
5771out:
5772 return err;
5773}
5774
5775static int nl80211_stop_sched_scan(struct sk_buff *skb,
5776 struct genl_info *info)
5777{
5778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5779
5780 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5781 !rdev->ops->sched_scan_stop)
5782 return -EOPNOTSUPP;
5783
Johannes Berg5fe231e2013-05-08 21:45:15 +02005784 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005785}
5786
Simon Wunderlich04f39042013-02-08 18:16:19 +01005787static int nl80211_start_radar_detection(struct sk_buff *skb,
5788 struct genl_info *info)
5789{
5790 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5791 struct net_device *dev = info->user_ptr[1];
5792 struct wireless_dev *wdev = dev->ieee80211_ptr;
5793 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005794 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005795 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005796 int err;
5797
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005798 dfs_region = reg_get_dfs_region(wdev->wiphy);
5799 if (dfs_region == NL80211_DFS_UNSET)
5800 return -EINVAL;
5801
Simon Wunderlich04f39042013-02-08 18:16:19 +01005802 err = nl80211_parse_chandef(rdev, info, &chandef);
5803 if (err)
5804 return err;
5805
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005806 if (netif_carrier_ok(dev))
5807 return -EBUSY;
5808
Simon Wunderlich04f39042013-02-08 18:16:19 +01005809 if (wdev->cac_started)
5810 return -EBUSY;
5811
5812 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5813 if (err < 0)
5814 return err;
5815
5816 if (err == 0)
5817 return -EINVAL;
5818
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005819 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005820 return -EINVAL;
5821
5822 if (!rdev->ops->start_radar_detection)
5823 return -EOPNOTSUPP;
5824
Simon Wunderlich04f39042013-02-08 18:16:19 +01005825 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5826 chandef.chan, CHAN_MODE_SHARED,
5827 BIT(chandef.width));
5828 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005829 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005830
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005831 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
5832 if (WARN_ON(!cac_time_ms))
5833 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
5834
5835 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
5836 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01005837 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005838 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005839 wdev->cac_started = true;
5840 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005841 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005842 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005843 return err;
5844}
5845
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005846static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5847{
5848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5849 struct net_device *dev = info->user_ptr[1];
5850 struct wireless_dev *wdev = dev->ieee80211_ptr;
5851 struct cfg80211_csa_settings params;
5852 /* csa_attrs is defined static to avoid waste of stack size - this
5853 * function is called under RTNL lock, so this should not be a problem.
5854 */
5855 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5856 u8 radar_detect_width = 0;
5857 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005858 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005859
5860 if (!rdev->ops->channel_switch ||
5861 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5862 return -EOPNOTSUPP;
5863
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005864 switch (dev->ieee80211_ptr->iftype) {
5865 case NL80211_IFTYPE_AP:
5866 case NL80211_IFTYPE_P2P_GO:
5867 need_new_beacon = true;
5868
5869 /* useless if AP is not running */
5870 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005871 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005872 break;
5873 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005874 if (!wdev->ssid_len)
5875 return -ENOTCONN;
5876 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005877 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005878 if (!wdev->mesh_id_len)
5879 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005880 break;
5881 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005882 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005883 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005884
5885 memset(&params, 0, sizeof(params));
5886
5887 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5888 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5889 return -EINVAL;
5890
5891 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005892 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005893 return -EINVAL;
5894
5895 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5896
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005897 if (!need_new_beacon)
5898 goto skip_beacons;
5899
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005900 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5901 if (err)
5902 return err;
5903
5904 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5905 info->attrs[NL80211_ATTR_CSA_IES],
5906 nl80211_policy);
5907 if (err)
5908 return err;
5909
5910 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5911 if (err)
5912 return err;
5913
5914 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5915 return -EINVAL;
5916
5917 params.counter_offset_beacon =
5918 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5919 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5920 return -EINVAL;
5921
5922 /* sanity check - counters should be the same */
5923 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5924 params.count)
5925 return -EINVAL;
5926
5927 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5928 params.counter_offset_presp =
5929 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5930 if (params.counter_offset_presp >=
5931 params.beacon_csa.probe_resp_len)
5932 return -EINVAL;
5933
5934 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5935 params.count)
5936 return -EINVAL;
5937 }
5938
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005939skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005940 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5941 if (err)
5942 return err;
5943
5944 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5945 return -EINVAL;
5946
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005947 switch (dev->ieee80211_ptr->iftype) {
5948 case NL80211_IFTYPE_AP:
5949 case NL80211_IFTYPE_P2P_GO:
5950 case NL80211_IFTYPE_ADHOC:
5951 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005952 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5953 &params.chandef);
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005954 if (err < 0)
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005955 return err;
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005956 if (err) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005957 radar_detect_width = BIT(params.chandef.width);
5958 params.radar_required = true;
5959 }
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005960 break;
5961 default:
5962 break;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005963 }
5964
5965 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5966 params.chandef.chan,
5967 CHAN_MODE_SHARED,
5968 radar_detect_width);
5969 if (err)
5970 return err;
5971
5972 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5973 params.block_tx = true;
5974
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005975 wdev_lock(wdev);
5976 err = rdev_channel_switch(rdev, dev, &params);
5977 wdev_unlock(wdev);
5978
5979 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005980}
5981
Johannes Berg9720bb32011-06-21 09:45:33 +02005982static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5983 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005984 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005985 struct wireless_dev *wdev,
5986 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005987{
Johannes Berg48ab9052009-07-10 18:42:31 +02005988 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005989 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005990 void *hdr;
5991 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005992 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005993
5994 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005995
Eric W. Biederman15e47302012-09-07 20:12:54 +00005996 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005997 NL80211_CMD_NEW_SCAN_RESULTS);
5998 if (!hdr)
5999 return -1;
6000
Johannes Berg9720bb32011-06-21 09:45:33 +02006001 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6002
Johannes Berg97990a02013-04-19 01:02:55 +02006003 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6004 goto nla_put_failure;
6005 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04006006 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6007 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02006008 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6009 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006010
6011 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6012 if (!bss)
6013 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006014 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006015 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006016 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006017
6018 rcu_read_lock();
6019 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006020 if (ies) {
6021 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6022 goto fail_unlock_rcu;
6023 tsf = true;
6024 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6025 ies->len, ies->data))
6026 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006027 }
6028 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006029 if (ies) {
6030 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6031 goto fail_unlock_rcu;
6032 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6033 ies->len, ies->data))
6034 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006035 }
6036 rcu_read_unlock();
6037
David S. Miller9360ffd2012-03-29 04:41:26 -04006038 if (res->beacon_interval &&
6039 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6040 goto nla_put_failure;
6041 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6042 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006043 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006044 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6045 jiffies_to_msecs(jiffies - intbss->ts)))
6046 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006047
Johannes Berg77965c92009-02-18 18:45:06 +01006048 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006049 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006050 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6051 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006052 break;
6053 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006054 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6055 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006056 break;
6057 default:
6058 break;
6059 }
6060
Johannes Berg48ab9052009-07-10 18:42:31 +02006061 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006062 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006063 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006064 if (intbss == wdev->current_bss &&
6065 nla_put_u32(msg, NL80211_BSS_STATUS,
6066 NL80211_BSS_STATUS_ASSOCIATED))
6067 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006068 break;
6069 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006070 if (intbss == wdev->current_bss &&
6071 nla_put_u32(msg, NL80211_BSS_STATUS,
6072 NL80211_BSS_STATUS_IBSS_JOINED))
6073 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006074 break;
6075 default:
6076 break;
6077 }
6078
Johannes Berg2a519312009-02-10 21:25:55 +01006079 nla_nest_end(msg, bss);
6080
6081 return genlmsg_end(msg, hdr);
6082
Johannes Berg8cef2c92013-02-05 16:54:31 +01006083 fail_unlock_rcu:
6084 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006085 nla_put_failure:
6086 genlmsg_cancel(msg, hdr);
6087 return -EMSGSIZE;
6088}
6089
Johannes Berg97990a02013-04-19 01:02:55 +02006090static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006091{
Johannes Berg48ab9052009-07-10 18:42:31 +02006092 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006093 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006094 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006095 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006096 int err;
6097
Johannes Berg97990a02013-04-19 01:02:55 +02006098 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006099 if (err)
6100 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006101
Johannes Berg48ab9052009-07-10 18:42:31 +02006102 wdev_lock(wdev);
6103 spin_lock_bh(&rdev->bss_lock);
6104 cfg80211_bss_expire(rdev);
6105
Johannes Berg9720bb32011-06-21 09:45:33 +02006106 cb->seq = rdev->bss_generation;
6107
Johannes Berg48ab9052009-07-10 18:42:31 +02006108 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006109 if (++idx <= start)
6110 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006111 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006112 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006113 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006114 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006115 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006116 }
6117 }
6118
Johannes Berg48ab9052009-07-10 18:42:31 +02006119 spin_unlock_bh(&rdev->bss_lock);
6120 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006121
Johannes Berg97990a02013-04-19 01:02:55 +02006122 cb->args[2] = idx;
6123 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006124
Johannes Berg67748892010-10-04 21:14:06 +02006125 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006126}
6127
Eric W. Biederman15e47302012-09-07 20:12:54 +00006128static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006129 int flags, struct net_device *dev,
6130 struct survey_info *survey)
6131{
6132 void *hdr;
6133 struct nlattr *infoattr;
6134
Eric W. Biederman15e47302012-09-07 20:12:54 +00006135 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006136 NL80211_CMD_NEW_SURVEY_RESULTS);
6137 if (!hdr)
6138 return -ENOMEM;
6139
David S. Miller9360ffd2012-03-29 04:41:26 -04006140 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6141 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006142
6143 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6144 if (!infoattr)
6145 goto nla_put_failure;
6146
David S. Miller9360ffd2012-03-29 04:41:26 -04006147 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6148 survey->channel->center_freq))
6149 goto nla_put_failure;
6150
6151 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6152 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6153 goto nla_put_failure;
6154 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6155 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6156 goto nla_put_failure;
6157 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6158 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6159 survey->channel_time))
6160 goto nla_put_failure;
6161 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6162 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6163 survey->channel_time_busy))
6164 goto nla_put_failure;
6165 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6166 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6167 survey->channel_time_ext_busy))
6168 goto nla_put_failure;
6169 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6170 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6171 survey->channel_time_rx))
6172 goto nla_put_failure;
6173 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6174 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6175 survey->channel_time_tx))
6176 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006177
6178 nla_nest_end(msg, infoattr);
6179
6180 return genlmsg_end(msg, hdr);
6181
6182 nla_put_failure:
6183 genlmsg_cancel(msg, hdr);
6184 return -EMSGSIZE;
6185}
6186
6187static int nl80211_dump_survey(struct sk_buff *skb,
6188 struct netlink_callback *cb)
6189{
6190 struct survey_info survey;
6191 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006192 struct wireless_dev *wdev;
6193 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006194 int res;
6195
Johannes Berg97990a02013-04-19 01:02:55 +02006196 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006197 if (res)
6198 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006199
Johannes Berg97990a02013-04-19 01:02:55 +02006200 if (!wdev->netdev) {
6201 res = -EINVAL;
6202 goto out_err;
6203 }
6204
Holger Schurig61fa7132009-11-11 12:25:40 +01006205 if (!dev->ops->dump_survey) {
6206 res = -EOPNOTSUPP;
6207 goto out_err;
6208 }
6209
6210 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006211 struct ieee80211_channel *chan;
6212
Johannes Berg97990a02013-04-19 01:02:55 +02006213 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006214 if (res == -ENOENT)
6215 break;
6216 if (res)
6217 goto out_err;
6218
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006219 /* Survey without a channel doesn't make sense */
6220 if (!survey.channel) {
6221 res = -EINVAL;
6222 goto out;
6223 }
6224
6225 chan = ieee80211_get_channel(&dev->wiphy,
6226 survey.channel->center_freq);
6227 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6228 survey_idx++;
6229 continue;
6230 }
6231
Holger Schurig61fa7132009-11-11 12:25:40 +01006232 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006233 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006234 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006235 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006236 goto out;
6237 survey_idx++;
6238 }
6239
6240 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006241 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006242 res = skb->len;
6243 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006244 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006245 return res;
6246}
6247
Samuel Ortizb23aa672009-07-01 21:26:54 +02006248static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6249{
6250 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6251 NL80211_WPA_VERSION_2));
6252}
6253
Jouni Malinen636a5d32009-03-19 13:39:22 +02006254static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6255{
Johannes Berg4c476992010-10-04 21:36:35 +02006256 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6257 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006258 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006259 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6260 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006261 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006262 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006263 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006264
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006265 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6266 return -EINVAL;
6267
6268 if (!info->attrs[NL80211_ATTR_MAC])
6269 return -EINVAL;
6270
Jouni Malinen17780922009-03-27 20:52:47 +02006271 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6272 return -EINVAL;
6273
Johannes Berg19957bb2009-07-02 17:20:43 +02006274 if (!info->attrs[NL80211_ATTR_SSID])
6275 return -EINVAL;
6276
6277 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6278 return -EINVAL;
6279
Johannes Bergfffd0932009-07-08 14:22:54 +02006280 err = nl80211_parse_key(info, &key);
6281 if (err)
6282 return err;
6283
6284 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006285 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6286 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006287 if (!key.p.key || !key.p.key_len)
6288 return -EINVAL;
6289 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6290 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6291 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6292 key.p.key_len != WLAN_KEY_LEN_WEP104))
6293 return -EINVAL;
6294 if (key.idx > 4)
6295 return -EINVAL;
6296 } else {
6297 key.p.key_len = 0;
6298 key.p.key = NULL;
6299 }
6300
Johannes Bergafea0b72010-08-10 09:46:42 +02006301 if (key.idx >= 0) {
6302 int i;
6303 bool ok = false;
6304 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6305 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6306 ok = true;
6307 break;
6308 }
6309 }
Johannes Berg4c476992010-10-04 21:36:35 +02006310 if (!ok)
6311 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006312 }
6313
Johannes Berg4c476992010-10-04 21:36:35 +02006314 if (!rdev->ops->auth)
6315 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006316
Johannes Berg074ac8d2010-09-16 14:58:22 +02006317 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006318 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6319 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006320
Johannes Berg19957bb2009-07-02 17:20:43 +02006321 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006322 chan = nl80211_get_valid_chan(&rdev->wiphy,
6323 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6324 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006325 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006326
Johannes Berg19957bb2009-07-02 17:20:43 +02006327 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6328 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6329
6330 if (info->attrs[NL80211_ATTR_IE]) {
6331 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6332 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6333 }
6334
6335 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006336 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006337 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006338
Jouni Malinene39e5b52012-09-30 19:29:39 +03006339 if (auth_type == NL80211_AUTHTYPE_SAE &&
6340 !info->attrs[NL80211_ATTR_SAE_DATA])
6341 return -EINVAL;
6342
6343 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6344 if (auth_type != NL80211_AUTHTYPE_SAE)
6345 return -EINVAL;
6346 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6347 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6348 /* need to include at least Auth Transaction and Status Code */
6349 if (sae_data_len < 4)
6350 return -EINVAL;
6351 }
6352
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006353 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6354
Johannes Berg95de8172012-01-20 13:55:25 +01006355 /*
6356 * Since we no longer track auth state, ignore
6357 * requests to only change local state.
6358 */
6359 if (local_state_change)
6360 return 0;
6361
Johannes Berg91bf9b22013-05-15 17:44:01 +02006362 wdev_lock(dev->ieee80211_ptr);
6363 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6364 ssid, ssid_len, ie, ie_len,
6365 key.p.key, key.p.key_len, key.idx,
6366 sae_data, sae_data_len);
6367 wdev_unlock(dev->ieee80211_ptr);
6368 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006369}
6370
Johannes Bergc0692b82010-08-27 14:26:53 +03006371static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6372 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006373 struct cfg80211_crypto_settings *settings,
6374 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006375{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006376 memset(settings, 0, sizeof(*settings));
6377
Samuel Ortizb23aa672009-07-01 21:26:54 +02006378 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6379
Johannes Bergc0692b82010-08-27 14:26:53 +03006380 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6381 u16 proto;
6382 proto = nla_get_u16(
6383 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6384 settings->control_port_ethertype = cpu_to_be16(proto);
6385 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6386 proto != ETH_P_PAE)
6387 return -EINVAL;
6388 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6389 settings->control_port_no_encrypt = true;
6390 } else
6391 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6392
Samuel Ortizb23aa672009-07-01 21:26:54 +02006393 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6394 void *data;
6395 int len, i;
6396
6397 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6398 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6399 settings->n_ciphers_pairwise = len / sizeof(u32);
6400
6401 if (len % sizeof(u32))
6402 return -EINVAL;
6403
Johannes Berg3dc27d22009-07-02 21:36:37 +02006404 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006405 return -EINVAL;
6406
6407 memcpy(settings->ciphers_pairwise, data, len);
6408
6409 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006410 if (!cfg80211_supported_cipher_suite(
6411 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006412 settings->ciphers_pairwise[i]))
6413 return -EINVAL;
6414 }
6415
6416 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6417 settings->cipher_group =
6418 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006419 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6420 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006421 return -EINVAL;
6422 }
6423
6424 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6425 settings->wpa_versions =
6426 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6427 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6428 return -EINVAL;
6429 }
6430
6431 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6432 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006433 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006434
6435 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6436 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6437 settings->n_akm_suites = len / sizeof(u32);
6438
6439 if (len % sizeof(u32))
6440 return -EINVAL;
6441
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006442 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6443 return -EINVAL;
6444
Samuel Ortizb23aa672009-07-01 21:26:54 +02006445 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006446 }
6447
6448 return 0;
6449}
6450
Jouni Malinen636a5d32009-03-19 13:39:22 +02006451static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6452{
Johannes Berg4c476992010-10-04 21:36:35 +02006453 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6454 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006455 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006456 struct cfg80211_assoc_request req = {};
6457 const u8 *bssid, *ssid;
6458 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006459
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006460 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6461 return -EINVAL;
6462
6463 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006464 !info->attrs[NL80211_ATTR_SSID] ||
6465 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006466 return -EINVAL;
6467
Johannes Berg4c476992010-10-04 21:36:35 +02006468 if (!rdev->ops->assoc)
6469 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006470
Johannes Berg074ac8d2010-09-16 14:58:22 +02006471 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006472 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6473 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006474
Johannes Berg19957bb2009-07-02 17:20:43 +02006475 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006476
Jouni Malinen664834d2014-01-15 00:01:44 +02006477 chan = nl80211_get_valid_chan(&rdev->wiphy,
6478 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6479 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006480 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006481
Johannes Berg19957bb2009-07-02 17:20:43 +02006482 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6483 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006484
6485 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006486 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6487 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006488 }
6489
Jouni Malinendc6382c2009-05-06 22:09:37 +03006490 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006491 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006492 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006493 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006494 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006495 else if (mfp != NL80211_MFP_NO)
6496 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006497 }
6498
Johannes Berg3e5d7642009-07-07 14:37:26 +02006499 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006500 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006501
Ben Greear7e7c8922011-11-18 11:31:59 -08006502 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006503 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006504
6505 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006506 memcpy(&req.ht_capa_mask,
6507 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6508 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006509
6510 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006511 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006512 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006513 memcpy(&req.ht_capa,
6514 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6515 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006516 }
6517
Johannes Bergee2aca32013-02-21 17:36:01 +01006518 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006519 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006520
6521 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006522 memcpy(&req.vht_capa_mask,
6523 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6524 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006525
6526 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006527 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006528 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006529 memcpy(&req.vht_capa,
6530 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6531 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006532 }
6533
Johannes Bergf62fab72013-02-21 20:09:09 +01006534 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006535 if (!err) {
6536 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006537 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6538 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006539 wdev_unlock(dev->ieee80211_ptr);
6540 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006541
Jouni Malinen636a5d32009-03-19 13:39:22 +02006542 return err;
6543}
6544
6545static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6546{
Johannes Berg4c476992010-10-04 21:36:35 +02006547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6548 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006549 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006550 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006551 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006552 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006553
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006554 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6555 return -EINVAL;
6556
6557 if (!info->attrs[NL80211_ATTR_MAC])
6558 return -EINVAL;
6559
6560 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6561 return -EINVAL;
6562
Johannes Berg4c476992010-10-04 21:36:35 +02006563 if (!rdev->ops->deauth)
6564 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006565
Johannes Berg074ac8d2010-09-16 14:58:22 +02006566 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006567 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6568 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006569
Johannes Berg19957bb2009-07-02 17:20:43 +02006570 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006571
Johannes Berg19957bb2009-07-02 17:20:43 +02006572 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6573 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006574 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006575 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006576 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006577
6578 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006579 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6580 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006581 }
6582
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006583 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6584
Johannes Berg91bf9b22013-05-15 17:44:01 +02006585 wdev_lock(dev->ieee80211_ptr);
6586 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6587 local_state_change);
6588 wdev_unlock(dev->ieee80211_ptr);
6589 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006590}
6591
6592static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6593{
Johannes Berg4c476992010-10-04 21:36:35 +02006594 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6595 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006596 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006597 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006598 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006599 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006600
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006601 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6602 return -EINVAL;
6603
6604 if (!info->attrs[NL80211_ATTR_MAC])
6605 return -EINVAL;
6606
6607 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6608 return -EINVAL;
6609
Johannes Berg4c476992010-10-04 21:36:35 +02006610 if (!rdev->ops->disassoc)
6611 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006612
Johannes Berg074ac8d2010-09-16 14:58:22 +02006613 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006614 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6615 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006616
Johannes Berg19957bb2009-07-02 17:20:43 +02006617 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006618
Johannes Berg19957bb2009-07-02 17:20:43 +02006619 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6620 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006621 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006622 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006623 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006624
6625 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006626 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6627 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006628 }
6629
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006630 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6631
Johannes Berg91bf9b22013-05-15 17:44:01 +02006632 wdev_lock(dev->ieee80211_ptr);
6633 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6634 local_state_change);
6635 wdev_unlock(dev->ieee80211_ptr);
6636 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006637}
6638
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006639static bool
6640nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6641 int mcast_rate[IEEE80211_NUM_BANDS],
6642 int rateval)
6643{
6644 struct wiphy *wiphy = &rdev->wiphy;
6645 bool found = false;
6646 int band, i;
6647
6648 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6649 struct ieee80211_supported_band *sband;
6650
6651 sband = wiphy->bands[band];
6652 if (!sband)
6653 continue;
6654
6655 for (i = 0; i < sband->n_bitrates; i++) {
6656 if (sband->bitrates[i].bitrate == rateval) {
6657 mcast_rate[band] = i + 1;
6658 found = true;
6659 break;
6660 }
6661 }
6662 }
6663
6664 return found;
6665}
6666
Johannes Berg04a773a2009-04-19 21:24:32 +02006667static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6668{
Johannes Berg4c476992010-10-04 21:36:35 +02006669 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6670 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006671 struct cfg80211_ibss_params ibss;
6672 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006673 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006674 int err;
6675
Johannes Berg8e30bc52009-04-22 17:45:38 +02006676 memset(&ibss, 0, sizeof(ibss));
6677
Johannes Berg04a773a2009-04-19 21:24:32 +02006678 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6679 return -EINVAL;
6680
Johannes Berg683b6d32012-11-08 21:25:48 +01006681 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006682 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6683 return -EINVAL;
6684
Johannes Berg8e30bc52009-04-22 17:45:38 +02006685 ibss.beacon_interval = 100;
6686
6687 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6688 ibss.beacon_interval =
6689 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6690 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6691 return -EINVAL;
6692 }
6693
Johannes Berg4c476992010-10-04 21:36:35 +02006694 if (!rdev->ops->join_ibss)
6695 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006696
Johannes Berg4c476992010-10-04 21:36:35 +02006697 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6698 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006699
Johannes Berg79c97e92009-07-07 03:56:12 +02006700 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006701
Johannes Berg39193492011-09-16 13:45:25 +02006702 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006703 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006704
6705 if (!is_valid_ether_addr(ibss.bssid))
6706 return -EINVAL;
6707 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006708 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6709 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6710
6711 if (info->attrs[NL80211_ATTR_IE]) {
6712 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6713 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6714 }
6715
Johannes Berg683b6d32012-11-08 21:25:48 +01006716 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6717 if (err)
6718 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006719
Johannes Berg683b6d32012-11-08 21:25:48 +01006720 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006721 return -EINVAL;
6722
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006723 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006724 case NL80211_CHAN_WIDTH_5:
6725 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006726 case NL80211_CHAN_WIDTH_20_NOHT:
6727 break;
6728 case NL80211_CHAN_WIDTH_20:
6729 case NL80211_CHAN_WIDTH_40:
6730 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6731 break;
6732 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006733 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006734 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006735
Johannes Berg04a773a2009-04-19 21:24:32 +02006736 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006737 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006738
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006739 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6740 u8 *rates =
6741 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6742 int n_rates =
6743 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6744 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006745 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006746
Johannes Berg34850ab2011-07-18 18:08:35 +02006747 err = ieee80211_get_ratemask(sband, rates, n_rates,
6748 &ibss.basic_rates);
6749 if (err)
6750 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006751 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006752
Simon Wunderlich803768f2013-06-28 10:39:58 +02006753 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6754 memcpy(&ibss.ht_capa_mask,
6755 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6756 sizeof(ibss.ht_capa_mask));
6757
6758 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6759 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6760 return -EINVAL;
6761 memcpy(&ibss.ht_capa,
6762 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6763 sizeof(ibss.ht_capa));
6764 }
6765
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006766 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6767 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6768 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6769 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006770
Johannes Berg4c476992010-10-04 21:36:35 +02006771 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306772 bool no_ht = false;
6773
Johannes Berg4c476992010-10-04 21:36:35 +02006774 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306775 info->attrs[NL80211_ATTR_KEYS],
6776 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006777 if (IS_ERR(connkeys))
6778 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306779
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006780 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6781 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306782 kfree(connkeys);
6783 return -EINVAL;
6784 }
Johannes Berg4c476992010-10-04 21:36:35 +02006785 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006786
Antonio Quartulli267335d2012-01-31 20:25:47 +01006787 ibss.control_port =
6788 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6789
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006790 ibss.userspace_handles_dfs =
6791 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6792
Johannes Berg4c476992010-10-04 21:36:35 +02006793 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006794 if (err)
6795 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006796 return err;
6797}
6798
6799static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6800{
Johannes Berg4c476992010-10-04 21:36:35 +02006801 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6802 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006803
Johannes Berg4c476992010-10-04 21:36:35 +02006804 if (!rdev->ops->leave_ibss)
6805 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006806
Johannes Berg4c476992010-10-04 21:36:35 +02006807 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6808 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006809
Johannes Berg4c476992010-10-04 21:36:35 +02006810 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006811}
6812
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006813static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6814{
6815 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6816 struct net_device *dev = info->user_ptr[1];
6817 int mcast_rate[IEEE80211_NUM_BANDS];
6818 u32 nla_rate;
6819 int err;
6820
6821 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6822 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6823 return -EOPNOTSUPP;
6824
6825 if (!rdev->ops->set_mcast_rate)
6826 return -EOPNOTSUPP;
6827
6828 memset(mcast_rate, 0, sizeof(mcast_rate));
6829
6830 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6831 return -EINVAL;
6832
6833 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6834 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6835 return -EINVAL;
6836
6837 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6838
6839 return err;
6840}
6841
Johannes Bergad7e7182013-11-13 13:37:47 +01006842static struct sk_buff *
6843__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6844 int approxlen, u32 portid, u32 seq,
6845 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006846 enum nl80211_attrs attr,
6847 const struct nl80211_vendor_cmd_info *info,
6848 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006849{
6850 struct sk_buff *skb;
6851 void *hdr;
6852 struct nlattr *data;
6853
6854 skb = nlmsg_new(approxlen + 100, gfp);
6855 if (!skb)
6856 return NULL;
6857
6858 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6859 if (!hdr) {
6860 kfree_skb(skb);
6861 return NULL;
6862 }
6863
6864 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6865 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006866
6867 if (info) {
6868 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6869 info->vendor_id))
6870 goto nla_put_failure;
6871 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6872 info->subcmd))
6873 goto nla_put_failure;
6874 }
6875
Johannes Bergad7e7182013-11-13 13:37:47 +01006876 data = nla_nest_start(skb, attr);
6877
6878 ((void **)skb->cb)[0] = rdev;
6879 ((void **)skb->cb)[1] = hdr;
6880 ((void **)skb->cb)[2] = data;
6881
6882 return skb;
6883
6884 nla_put_failure:
6885 kfree_skb(skb);
6886 return NULL;
6887}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006888
Johannes Berge03ad6e2014-01-01 17:22:30 +01006889struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6890 enum nl80211_commands cmd,
6891 enum nl80211_attrs attr,
6892 int vendor_event_idx,
6893 int approxlen, gfp_t gfp)
6894{
6895 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6896 const struct nl80211_vendor_cmd_info *info;
6897
6898 switch (cmd) {
6899 case NL80211_CMD_TESTMODE:
6900 if (WARN_ON(vendor_event_idx != -1))
6901 return NULL;
6902 info = NULL;
6903 break;
6904 case NL80211_CMD_VENDOR:
6905 if (WARN_ON(vendor_event_idx < 0 ||
6906 vendor_event_idx >= wiphy->n_vendor_events))
6907 return NULL;
6908 info = &wiphy->vendor_events[vendor_event_idx];
6909 break;
6910 default:
6911 WARN_ON(1);
6912 return NULL;
6913 }
6914
6915 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6916 cmd, attr, info, gfp);
6917}
6918EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6919
6920void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6921{
6922 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6923 void *hdr = ((void **)skb->cb)[1];
6924 struct nlattr *data = ((void **)skb->cb)[2];
6925 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6926
6927 nla_nest_end(skb, data);
6928 genlmsg_end(skb, hdr);
6929
6930 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6931 mcgrp = NL80211_MCGRP_VENDOR;
6932
6933 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6934 mcgrp, gfp);
6935}
6936EXPORT_SYMBOL(__cfg80211_send_event_skb);
6937
Johannes Bergaff89a92009-07-01 21:26:51 +02006938#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006939static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6940{
Johannes Berg4c476992010-10-04 21:36:35 +02006941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006942 struct wireless_dev *wdev =
6943 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006944 int err;
6945
David Spinadelfc73f112013-07-31 18:04:15 +03006946 if (!rdev->ops->testmode_cmd)
6947 return -EOPNOTSUPP;
6948
6949 if (IS_ERR(wdev)) {
6950 err = PTR_ERR(wdev);
6951 if (err != -EINVAL)
6952 return err;
6953 wdev = NULL;
6954 } else if (wdev->wiphy != &rdev->wiphy) {
6955 return -EINVAL;
6956 }
6957
Johannes Bergaff89a92009-07-01 21:26:51 +02006958 if (!info->attrs[NL80211_ATTR_TESTDATA])
6959 return -EINVAL;
6960
Johannes Bergad7e7182013-11-13 13:37:47 +01006961 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006962 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006963 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6964 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006965 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006966
Johannes Bergaff89a92009-07-01 21:26:51 +02006967 return err;
6968}
6969
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006970static int nl80211_testmode_dump(struct sk_buff *skb,
6971 struct netlink_callback *cb)
6972{
Johannes Berg00918d32011-12-13 17:22:05 +01006973 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006974 int err;
6975 long phy_idx;
6976 void *data = NULL;
6977 int data_len = 0;
6978
Johannes Berg5fe231e2013-05-08 21:45:15 +02006979 rtnl_lock();
6980
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006981 if (cb->args[0]) {
6982 /*
6983 * 0 is a valid index, but not valid for args[0],
6984 * so we need to offset by 1.
6985 */
6986 phy_idx = cb->args[0] - 1;
6987 } else {
6988 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6989 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6990 nl80211_policy);
6991 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006992 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006993
Johannes Berg2bd7e352012-06-15 14:23:16 +02006994 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6995 nl80211_fam.attrbuf);
6996 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006997 err = PTR_ERR(rdev);
6998 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006999 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02007000 phy_idx = rdev->wiphy_idx;
7001 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02007002
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007003 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7004 cb->args[1] =
7005 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7006 }
7007
7008 if (cb->args[1]) {
7009 data = nla_data((void *)cb->args[1]);
7010 data_len = nla_len((void *)cb->args[1]);
7011 }
7012
Johannes Berg00918d32011-12-13 17:22:05 +01007013 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7014 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007015 err = -ENOENT;
7016 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007017 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007018
Johannes Berg00918d32011-12-13 17:22:05 +01007019 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007020 err = -EOPNOTSUPP;
7021 goto out_err;
7022 }
7023
7024 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007025 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007026 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7027 NL80211_CMD_TESTMODE);
7028 struct nlattr *tmdata;
7029
Dan Carpentercb35fba2013-08-14 14:50:01 +03007030 if (!hdr)
7031 break;
7032
David S. Miller9360ffd2012-03-29 04:41:26 -04007033 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007034 genlmsg_cancel(skb, hdr);
7035 break;
7036 }
7037
7038 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7039 if (!tmdata) {
7040 genlmsg_cancel(skb, hdr);
7041 break;
7042 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007043 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007044 nla_nest_end(skb, tmdata);
7045
7046 if (err == -ENOBUFS || err == -ENOENT) {
7047 genlmsg_cancel(skb, hdr);
7048 break;
7049 } else if (err) {
7050 genlmsg_cancel(skb, hdr);
7051 goto out_err;
7052 }
7053
7054 genlmsg_end(skb, hdr);
7055 }
7056
7057 err = skb->len;
7058 /* see above */
7059 cb->args[0] = phy_idx + 1;
7060 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007061 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007062 return err;
7063}
Johannes Bergaff89a92009-07-01 21:26:51 +02007064#endif
7065
Samuel Ortizb23aa672009-07-01 21:26:54 +02007066static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7067{
Johannes Berg4c476992010-10-04 21:36:35 +02007068 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7069 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007070 struct cfg80211_connect_params connect;
7071 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007072 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007073 int err;
7074
7075 memset(&connect, 0, sizeof(connect));
7076
7077 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7078 return -EINVAL;
7079
7080 if (!info->attrs[NL80211_ATTR_SSID] ||
7081 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7082 return -EINVAL;
7083
7084 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7085 connect.auth_type =
7086 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007087 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7088 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007089 return -EINVAL;
7090 } else
7091 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7092
7093 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7094
Johannes Bergc0692b82010-08-27 14:26:53 +03007095 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007096 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007097 if (err)
7098 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007099
Johannes Berg074ac8d2010-09-16 14:58:22 +02007100 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007101 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7102 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007103
Johannes Berg79c97e92009-07-07 03:56:12 +02007104 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007105
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307106 connect.bg_scan_period = -1;
7107 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7108 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7109 connect.bg_scan_period =
7110 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7111 }
7112
Samuel Ortizb23aa672009-07-01 21:26:54 +02007113 if (info->attrs[NL80211_ATTR_MAC])
7114 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007115 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7116 connect.bssid_hint =
7117 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007118 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7119 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7120
7121 if (info->attrs[NL80211_ATTR_IE]) {
7122 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7123 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7124 }
7125
Jouni Malinencee00a92013-01-15 17:15:57 +02007126 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7127 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7128 if (connect.mfp != NL80211_MFP_REQUIRED &&
7129 connect.mfp != NL80211_MFP_NO)
7130 return -EINVAL;
7131 } else {
7132 connect.mfp = NL80211_MFP_NO;
7133 }
7134
Samuel Ortizb23aa672009-07-01 21:26:54 +02007135 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007136 connect.channel = nl80211_get_valid_chan(
7137 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7138 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007139 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007140 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007141 connect.channel_hint = nl80211_get_valid_chan(
7142 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7143 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007144 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007145 }
7146
Johannes Bergfffd0932009-07-08 14:22:54 +02007147 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7148 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307149 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007150 if (IS_ERR(connkeys))
7151 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007152 }
7153
Ben Greear7e7c8922011-11-18 11:31:59 -08007154 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7155 connect.flags |= ASSOC_REQ_DISABLE_HT;
7156
7157 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7158 memcpy(&connect.ht_capa_mask,
7159 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7160 sizeof(connect.ht_capa_mask));
7161
7162 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007163 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7164 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007165 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007166 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007167 memcpy(&connect.ht_capa,
7168 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7169 sizeof(connect.ht_capa));
7170 }
7171
Johannes Bergee2aca32013-02-21 17:36:01 +01007172 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7173 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7174
7175 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7176 memcpy(&connect.vht_capa_mask,
7177 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7178 sizeof(connect.vht_capa_mask));
7179
7180 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7181 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7182 kfree(connkeys);
7183 return -EINVAL;
7184 }
7185 memcpy(&connect.vht_capa,
7186 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7187 sizeof(connect.vht_capa));
7188 }
7189
Johannes Berg83739b02013-05-15 17:44:01 +02007190 wdev_lock(dev->ieee80211_ptr);
7191 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7192 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007193 if (err)
7194 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007195 return err;
7196}
7197
7198static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7199{
Johannes Berg4c476992010-10-04 21:36:35 +02007200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7201 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007202 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007203 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007204
7205 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7206 reason = WLAN_REASON_DEAUTH_LEAVING;
7207 else
7208 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7209
7210 if (reason == 0)
7211 return -EINVAL;
7212
Johannes Berg074ac8d2010-09-16 14:58:22 +02007213 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007214 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7215 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007216
Johannes Berg83739b02013-05-15 17:44:01 +02007217 wdev_lock(dev->ieee80211_ptr);
7218 ret = cfg80211_disconnect(rdev, dev, reason, true);
7219 wdev_unlock(dev->ieee80211_ptr);
7220 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007221}
7222
Johannes Berg463d0182009-07-14 00:33:35 +02007223static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7224{
Johannes Berg4c476992010-10-04 21:36:35 +02007225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007226 struct net *net;
7227 int err;
7228 u32 pid;
7229
7230 if (!info->attrs[NL80211_ATTR_PID])
7231 return -EINVAL;
7232
7233 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7234
Johannes Berg463d0182009-07-14 00:33:35 +02007235 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007236 if (IS_ERR(net))
7237 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007238
7239 err = 0;
7240
7241 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007242 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7243 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007244
Johannes Berg463d0182009-07-14 00:33:35 +02007245 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007246 return err;
7247}
7248
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007249static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7250{
Johannes Berg4c476992010-10-04 21:36:35 +02007251 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007252 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7253 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007254 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007255 struct cfg80211_pmksa pmksa;
7256
7257 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7258
7259 if (!info->attrs[NL80211_ATTR_MAC])
7260 return -EINVAL;
7261
7262 if (!info->attrs[NL80211_ATTR_PMKID])
7263 return -EINVAL;
7264
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007265 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7266 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7267
Johannes Berg074ac8d2010-09-16 14:58:22 +02007268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007269 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7270 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007271
7272 switch (info->genlhdr->cmd) {
7273 case NL80211_CMD_SET_PMKSA:
7274 rdev_ops = rdev->ops->set_pmksa;
7275 break;
7276 case NL80211_CMD_DEL_PMKSA:
7277 rdev_ops = rdev->ops->del_pmksa;
7278 break;
7279 default:
7280 WARN_ON(1);
7281 break;
7282 }
7283
Johannes Berg4c476992010-10-04 21:36:35 +02007284 if (!rdev_ops)
7285 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007286
Johannes Berg4c476992010-10-04 21:36:35 +02007287 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007288}
7289
7290static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7291{
Johannes Berg4c476992010-10-04 21:36:35 +02007292 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7293 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007294
Johannes Berg074ac8d2010-09-16 14:58:22 +02007295 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007296 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7297 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007298
Johannes Berg4c476992010-10-04 21:36:35 +02007299 if (!rdev->ops->flush_pmksa)
7300 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007301
Hila Gonene35e4d22012-06-27 17:19:42 +03007302 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007303}
7304
Arik Nemtsov109086c2011-09-28 14:12:50 +03007305static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7306{
7307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7308 struct net_device *dev = info->user_ptr[1];
7309 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307310 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007311 u16 status_code;
7312 u8 *peer;
7313
7314 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7315 !rdev->ops->tdls_mgmt)
7316 return -EOPNOTSUPP;
7317
7318 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7319 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7320 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7321 !info->attrs[NL80211_ATTR_IE] ||
7322 !info->attrs[NL80211_ATTR_MAC])
7323 return -EINVAL;
7324
7325 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7326 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7327 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7328 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307329 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7330 peer_capability =
7331 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007332
Hila Gonene35e4d22012-06-27 17:19:42 +03007333 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307334 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007335 nla_data(info->attrs[NL80211_ATTR_IE]),
7336 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007337}
7338
7339static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7340{
7341 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7342 struct net_device *dev = info->user_ptr[1];
7343 enum nl80211_tdls_operation operation;
7344 u8 *peer;
7345
7346 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7347 !rdev->ops->tdls_oper)
7348 return -EOPNOTSUPP;
7349
7350 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7351 !info->attrs[NL80211_ATTR_MAC])
7352 return -EINVAL;
7353
7354 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7355 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7356
Hila Gonene35e4d22012-06-27 17:19:42 +03007357 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007358}
7359
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007360static int nl80211_remain_on_channel(struct sk_buff *skb,
7361 struct genl_info *info)
7362{
Johannes Berg4c476992010-10-04 21:36:35 +02007363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007364 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007365 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007366 struct sk_buff *msg;
7367 void *hdr;
7368 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007369 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007370 int err;
7371
7372 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7373 !info->attrs[NL80211_ATTR_DURATION])
7374 return -EINVAL;
7375
7376 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7377
Johannes Berg7c4ef712011-11-18 15:33:48 +01007378 if (!rdev->ops->remain_on_channel ||
7379 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007380 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007381
Johannes Bergebf348f2012-06-01 12:50:54 +02007382 /*
7383 * We should be on that channel for at least a minimum amount of
7384 * time (10ms) but no longer than the driver supports.
7385 */
7386 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7387 duration > rdev->wiphy.max_remain_on_channel_duration)
7388 return -EINVAL;
7389
Johannes Berg683b6d32012-11-08 21:25:48 +01007390 err = nl80211_parse_chandef(rdev, info, &chandef);
7391 if (err)
7392 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007393
7394 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007395 if (!msg)
7396 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007397
Eric W. Biederman15e47302012-09-07 20:12:54 +00007398 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007399 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007400 if (!hdr) {
7401 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007402 goto free_msg;
7403 }
7404
Johannes Berg683b6d32012-11-08 21:25:48 +01007405 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7406 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007407
7408 if (err)
7409 goto free_msg;
7410
David S. Miller9360ffd2012-03-29 04:41:26 -04007411 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7412 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007413
7414 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007415
7416 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007417
7418 nla_put_failure:
7419 err = -ENOBUFS;
7420 free_msg:
7421 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007422 return err;
7423}
7424
7425static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7426 struct genl_info *info)
7427{
Johannes Berg4c476992010-10-04 21:36:35 +02007428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007429 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007430 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007431
7432 if (!info->attrs[NL80211_ATTR_COOKIE])
7433 return -EINVAL;
7434
Johannes Berg4c476992010-10-04 21:36:35 +02007435 if (!rdev->ops->cancel_remain_on_channel)
7436 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007437
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007438 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7439
Hila Gonene35e4d22012-06-27 17:19:42 +03007440 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007441}
7442
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007443static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7444 u8 *rates, u8 rates_len)
7445{
7446 u8 i;
7447 u32 mask = 0;
7448
7449 for (i = 0; i < rates_len; i++) {
7450 int rate = (rates[i] & 0x7f) * 5;
7451 int ridx;
7452 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7453 struct ieee80211_rate *srate =
7454 &sband->bitrates[ridx];
7455 if (rate == srate->bitrate) {
7456 mask |= 1 << ridx;
7457 break;
7458 }
7459 }
7460 if (ridx == sband->n_bitrates)
7461 return 0; /* rate not found */
7462 }
7463
7464 return mask;
7465}
7466
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007467static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7468 u8 *rates, u8 rates_len,
7469 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7470{
7471 u8 i;
7472
7473 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7474
7475 for (i = 0; i < rates_len; i++) {
7476 int ridx, rbit;
7477
7478 ridx = rates[i] / 8;
7479 rbit = BIT(rates[i] % 8);
7480
7481 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007482 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007483 return false;
7484
7485 /* check availability */
7486 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7487 mcs[ridx] |= rbit;
7488 else
7489 return false;
7490 }
7491
7492 return true;
7493}
7494
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007495static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7496{
7497 u16 mcs_mask = 0;
7498
7499 switch (vht_mcs_map) {
7500 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7501 break;
7502 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7503 mcs_mask = 0x00FF;
7504 break;
7505 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7506 mcs_mask = 0x01FF;
7507 break;
7508 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7509 mcs_mask = 0x03FF;
7510 break;
7511 default:
7512 break;
7513 }
7514
7515 return mcs_mask;
7516}
7517
7518static void vht_build_mcs_mask(u16 vht_mcs_map,
7519 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7520{
7521 u8 nss;
7522
7523 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7524 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7525 vht_mcs_map >>= 2;
7526 }
7527}
7528
7529static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7530 struct nl80211_txrate_vht *txrate,
7531 u16 mcs[NL80211_VHT_NSS_MAX])
7532{
7533 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7534 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7535 u8 i;
7536
7537 if (!sband->vht_cap.vht_supported)
7538 return false;
7539
7540 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7541
7542 /* Build vht_mcs_mask from VHT capabilities */
7543 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7544
7545 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7546 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7547 mcs[i] = txrate->mcs[i];
7548 else
7549 return false;
7550 }
7551
7552 return true;
7553}
7554
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007555static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007556 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7557 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007558 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7559 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007560 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007561 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007562};
7563
7564static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7565 struct genl_info *info)
7566{
7567 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007568 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007569 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007570 int rem, i;
7571 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007572 struct nlattr *tx_rates;
7573 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007574 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007575
Johannes Berg4c476992010-10-04 21:36:35 +02007576 if (!rdev->ops->set_bitrate_mask)
7577 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007578
7579 memset(&mask, 0, sizeof(mask));
7580 /* Default to all rates enabled */
7581 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7582 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007583
7584 if (!sband)
7585 continue;
7586
7587 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007588 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007589 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007590 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007591
7592 if (!sband->vht_cap.vht_supported)
7593 continue;
7594
7595 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7596 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007597 }
7598
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007599 /* if no rates are given set it back to the defaults */
7600 if (!info->attrs[NL80211_ATTR_TX_RATES])
7601 goto out;
7602
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007603 /*
7604 * The nested attribute uses enum nl80211_band as the index. This maps
7605 * directly to the enum ieee80211_band values used in cfg80211.
7606 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007607 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007608 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007609 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007610 int err;
7611
Johannes Berg4c476992010-10-04 21:36:35 +02007612 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7613 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007614 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007615 if (sband == NULL)
7616 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007617 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7618 nla_len(tx_rates), nl80211_txattr_policy);
7619 if (err)
7620 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007621 if (tb[NL80211_TXRATE_LEGACY]) {
7622 mask.control[band].legacy = rateset_to_mask(
7623 sband,
7624 nla_data(tb[NL80211_TXRATE_LEGACY]),
7625 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307626 if ((mask.control[band].legacy == 0) &&
7627 nla_len(tb[NL80211_TXRATE_LEGACY]))
7628 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007629 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007630 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007631 if (!ht_rateset_to_mask(
7632 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007633 nla_data(tb[NL80211_TXRATE_HT]),
7634 nla_len(tb[NL80211_TXRATE_HT]),
7635 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007636 return -EINVAL;
7637 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007638 if (tb[NL80211_TXRATE_VHT]) {
7639 if (!vht_set_mcs_mask(
7640 sband,
7641 nla_data(tb[NL80211_TXRATE_VHT]),
7642 mask.control[band].vht_mcs))
7643 return -EINVAL;
7644 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007645 if (tb[NL80211_TXRATE_GI]) {
7646 mask.control[band].gi =
7647 nla_get_u8(tb[NL80211_TXRATE_GI]);
7648 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7649 return -EINVAL;
7650 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007651
7652 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007653 /* don't allow empty legacy rates if HT or VHT
7654 * are not even supported.
7655 */
7656 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7657 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007658 return -EINVAL;
7659
7660 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007661 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007662 goto out;
7663
7664 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7665 if (mask.control[band].vht_mcs[i])
7666 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007667
7668 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007669 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007670 }
7671 }
7672
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007673out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007674 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007675}
7676
Johannes Berg2e161f72010-08-12 15:38:38 +02007677static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007678{
Johannes Berg4c476992010-10-04 21:36:35 +02007679 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007680 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007681 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007682
7683 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7684 return -EINVAL;
7685
Johannes Berg2e161f72010-08-12 15:38:38 +02007686 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7687 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007688
Johannes Berg71bbc992012-06-15 15:30:18 +02007689 switch (wdev->iftype) {
7690 case NL80211_IFTYPE_STATION:
7691 case NL80211_IFTYPE_ADHOC:
7692 case NL80211_IFTYPE_P2P_CLIENT:
7693 case NL80211_IFTYPE_AP:
7694 case NL80211_IFTYPE_AP_VLAN:
7695 case NL80211_IFTYPE_MESH_POINT:
7696 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007697 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007698 break;
7699 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007700 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007701 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007702
7703 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007704 if (!rdev->ops->mgmt_tx)
7705 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007706
Eric W. Biederman15e47302012-09-07 20:12:54 +00007707 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007708 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7709 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007710}
7711
Johannes Berg2e161f72010-08-12 15:38:38 +02007712static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007713{
Johannes Berg4c476992010-10-04 21:36:35 +02007714 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007715 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007716 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007717 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007718 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007719 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007720 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007721 struct cfg80211_mgmt_tx_params params = {
7722 .dont_wait_for_ack =
7723 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7724 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007725
Johannes Berg683b6d32012-11-08 21:25:48 +01007726 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007727 return -EINVAL;
7728
Johannes Berg4c476992010-10-04 21:36:35 +02007729 if (!rdev->ops->mgmt_tx)
7730 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007731
Johannes Berg71bbc992012-06-15 15:30:18 +02007732 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007733 case NL80211_IFTYPE_P2P_DEVICE:
7734 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7735 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007736 case NL80211_IFTYPE_STATION:
7737 case NL80211_IFTYPE_ADHOC:
7738 case NL80211_IFTYPE_P2P_CLIENT:
7739 case NL80211_IFTYPE_AP:
7740 case NL80211_IFTYPE_AP_VLAN:
7741 case NL80211_IFTYPE_MESH_POINT:
7742 case NL80211_IFTYPE_P2P_GO:
7743 break;
7744 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007745 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007746 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007747
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007748 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007749 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007750 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007751 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007752
7753 /*
7754 * We should wait on the channel for at least a minimum amount
7755 * of time (10ms) but no longer than the driver supports.
7756 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007757 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7758 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007759 return -EINVAL;
7760
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007761 }
7762
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007763 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007764
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007765 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007766 return -EINVAL;
7767
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007768 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307769
Antonio Quartulliea141b752013-06-11 14:20:03 +02007770 /* get the channel if any has been specified, otherwise pass NULL to
7771 * the driver. The latter will use the current one
7772 */
7773 chandef.chan = NULL;
7774 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7775 err = nl80211_parse_chandef(rdev, info, &chandef);
7776 if (err)
7777 return err;
7778 }
7779
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007780 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007781 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007782
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007783 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007784 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7785 if (!msg)
7786 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007787
Eric W. Biederman15e47302012-09-07 20:12:54 +00007788 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007789 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007790 if (!hdr) {
7791 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007792 goto free_msg;
7793 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007794 }
Johannes Berge247bd902011-11-04 11:18:21 +01007795
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007796 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7797 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7798 params.chan = chandef.chan;
7799 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007800 if (err)
7801 goto free_msg;
7802
Johannes Berge247bd902011-11-04 11:18:21 +01007803 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007804 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7805 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007806
Johannes Berge247bd902011-11-04 11:18:21 +01007807 genlmsg_end(msg, hdr);
7808 return genlmsg_reply(msg, info);
7809 }
7810
7811 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007812
7813 nla_put_failure:
7814 err = -ENOBUFS;
7815 free_msg:
7816 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007817 return err;
7818}
7819
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007820static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7821{
7822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007823 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007824 u64 cookie;
7825
7826 if (!info->attrs[NL80211_ATTR_COOKIE])
7827 return -EINVAL;
7828
7829 if (!rdev->ops->mgmt_tx_cancel_wait)
7830 return -EOPNOTSUPP;
7831
Johannes Berg71bbc992012-06-15 15:30:18 +02007832 switch (wdev->iftype) {
7833 case NL80211_IFTYPE_STATION:
7834 case NL80211_IFTYPE_ADHOC:
7835 case NL80211_IFTYPE_P2P_CLIENT:
7836 case NL80211_IFTYPE_AP:
7837 case NL80211_IFTYPE_AP_VLAN:
7838 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007839 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007840 break;
7841 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007842 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007843 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007844
7845 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7846
Hila Gonene35e4d22012-06-27 17:19:42 +03007847 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007848}
7849
Kalle Valoffb9eb32010-02-17 17:58:10 +02007850static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7851{
Johannes Berg4c476992010-10-04 21:36:35 +02007852 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007853 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007854 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007855 u8 ps_state;
7856 bool state;
7857 int err;
7858
Johannes Berg4c476992010-10-04 21:36:35 +02007859 if (!info->attrs[NL80211_ATTR_PS_STATE])
7860 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007861
7862 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7863
Johannes Berg4c476992010-10-04 21:36:35 +02007864 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7865 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007866
7867 wdev = dev->ieee80211_ptr;
7868
Johannes Berg4c476992010-10-04 21:36:35 +02007869 if (!rdev->ops->set_power_mgmt)
7870 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007871
7872 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7873
7874 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007875 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007876
Hila Gonene35e4d22012-06-27 17:19:42 +03007877 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007878 if (!err)
7879 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007880 return err;
7881}
7882
7883static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7884{
Johannes Berg4c476992010-10-04 21:36:35 +02007885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007886 enum nl80211_ps_state ps_state;
7887 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007888 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007889 struct sk_buff *msg;
7890 void *hdr;
7891 int err;
7892
Kalle Valoffb9eb32010-02-17 17:58:10 +02007893 wdev = dev->ieee80211_ptr;
7894
Johannes Berg4c476992010-10-04 21:36:35 +02007895 if (!rdev->ops->set_power_mgmt)
7896 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007897
7898 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007899 if (!msg)
7900 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007901
Eric W. Biederman15e47302012-09-07 20:12:54 +00007902 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007903 NL80211_CMD_GET_POWER_SAVE);
7904 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007905 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007906 goto free_msg;
7907 }
7908
7909 if (wdev->ps)
7910 ps_state = NL80211_PS_ENABLED;
7911 else
7912 ps_state = NL80211_PS_DISABLED;
7913
David S. Miller9360ffd2012-03-29 04:41:26 -04007914 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7915 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007916
7917 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007918 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007919
Johannes Berg4c476992010-10-04 21:36:35 +02007920 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007921 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007922 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007923 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007924 return err;
7925}
7926
Johannes Berg94e860f2014-01-20 23:58:15 +01007927static const struct nla_policy
7928nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007929 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7930 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7931 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007932 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7933 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7934 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007935};
7936
Thomas Pedersen84f10702012-07-12 16:17:33 -07007937static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007938 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007939{
7940 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007941 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007942 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007943
Johannes Bergd9d8b012012-11-26 12:51:52 +01007944 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007945 return -EINVAL;
7946
Thomas Pedersen84f10702012-07-12 16:17:33 -07007947 if (!rdev->ops->set_cqm_txe_config)
7948 return -EOPNOTSUPP;
7949
7950 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7951 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7952 return -EOPNOTSUPP;
7953
Hila Gonene35e4d22012-06-27 17:19:42 +03007954 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007955}
7956
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007957static int nl80211_set_cqm_rssi(struct genl_info *info,
7958 s32 threshold, u32 hysteresis)
7959{
Johannes Berg4c476992010-10-04 21:36:35 +02007960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007961 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007962 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007963
7964 if (threshold > 0)
7965 return -EINVAL;
7966
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007967 /* disabling - hysteresis should also be zero then */
7968 if (threshold == 0)
7969 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007970
Johannes Berg4c476992010-10-04 21:36:35 +02007971 if (!rdev->ops->set_cqm_rssi_config)
7972 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007973
Johannes Berg074ac8d2010-09-16 14:58:22 +02007974 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007975 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7976 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007977
Hila Gonene35e4d22012-06-27 17:19:42 +03007978 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007979}
7980
7981static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7982{
7983 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7984 struct nlattr *cqm;
7985 int err;
7986
7987 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007988 if (!cqm)
7989 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007990
7991 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7992 nl80211_attr_cqm_policy);
7993 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007994 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007995
7996 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7997 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007998 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7999 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008000
Johannes Berg1da5fcc2013-08-06 14:10:48 +02008001 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8002 }
8003
8004 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8005 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8006 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8007 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8008 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8009 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8010
8011 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8012 }
8013
8014 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008015}
8016
Johannes Berg29cbe682010-12-03 09:20:44 +01008017static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8018{
8019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8020 struct net_device *dev = info->user_ptr[1];
8021 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008022 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008023 int err;
8024
8025 /* start with default */
8026 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008027 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008028
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008029 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008030 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008031 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008032 if (err)
8033 return err;
8034 }
8035
8036 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8037 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8038 return -EINVAL;
8039
Javier Cardonac80d5452010-12-16 17:37:49 -08008040 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8041 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8042
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008043 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8044 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8045 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8046 return -EINVAL;
8047
Marco Porsch9bdbf042013-01-07 16:04:51 +01008048 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8049 setup.beacon_interval =
8050 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8051 if (setup.beacon_interval < 10 ||
8052 setup.beacon_interval > 10000)
8053 return -EINVAL;
8054 }
8055
8056 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8057 setup.dtim_period =
8058 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8059 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8060 return -EINVAL;
8061 }
8062
Javier Cardonac80d5452010-12-16 17:37:49 -08008063 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8064 /* parse additional setup parameters if given */
8065 err = nl80211_parse_mesh_setup(info, &setup);
8066 if (err)
8067 return err;
8068 }
8069
Thomas Pedersend37bb182013-03-04 13:06:13 -08008070 if (setup.user_mpm)
8071 cfg.auto_open_plinks = false;
8072
Johannes Bergcc1d2802012-05-16 23:50:20 +02008073 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008074 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8075 if (err)
8076 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008077 } else {
8078 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008079 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008080 }
8081
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008082 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8083 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8084 int n_rates =
8085 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8086 struct ieee80211_supported_band *sband;
8087
8088 if (!setup.chandef.chan)
8089 return -EINVAL;
8090
8091 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8092
8093 err = ieee80211_get_ratemask(sband, rates, n_rates,
8094 &setup.basic_rates);
8095 if (err)
8096 return err;
8097 }
8098
Javier Cardonac80d5452010-12-16 17:37:49 -08008099 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008100}
8101
8102static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8103{
8104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8105 struct net_device *dev = info->user_ptr[1];
8106
8107 return cfg80211_leave_mesh(rdev, dev);
8108}
8109
Johannes Bergdfb89c52012-06-27 09:23:48 +02008110#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008111static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8112 struct cfg80211_registered_device *rdev)
8113{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008114 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008115 struct nlattr *nl_pats, *nl_pat;
8116 int i, pat_len;
8117
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008118 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008119 return 0;
8120
8121 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8122 if (!nl_pats)
8123 return -ENOBUFS;
8124
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008125 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008126 nl_pat = nla_nest_start(msg, i + 1);
8127 if (!nl_pat)
8128 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008129 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008130 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008131 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008132 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8133 wowlan->patterns[i].pattern) ||
8134 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008135 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008136 return -ENOBUFS;
8137 nla_nest_end(msg, nl_pat);
8138 }
8139 nla_nest_end(msg, nl_pats);
8140
8141 return 0;
8142}
8143
Johannes Berg2a0e0472013-01-23 22:57:40 +01008144static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8145 struct cfg80211_wowlan_tcp *tcp)
8146{
8147 struct nlattr *nl_tcp;
8148
8149 if (!tcp)
8150 return 0;
8151
8152 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8153 if (!nl_tcp)
8154 return -ENOBUFS;
8155
8156 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8157 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8158 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8159 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8160 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8161 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8162 tcp->payload_len, tcp->payload) ||
8163 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8164 tcp->data_interval) ||
8165 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8166 tcp->wake_len, tcp->wake_data) ||
8167 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8168 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8169 return -ENOBUFS;
8170
8171 if (tcp->payload_seq.len &&
8172 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8173 sizeof(tcp->payload_seq), &tcp->payload_seq))
8174 return -ENOBUFS;
8175
8176 if (tcp->payload_tok.len &&
8177 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8178 sizeof(tcp->payload_tok) + tcp->tokens_size,
8179 &tcp->payload_tok))
8180 return -ENOBUFS;
8181
Johannes Berge248ad32013-05-16 10:24:28 +02008182 nla_nest_end(msg, nl_tcp);
8183
Johannes Berg2a0e0472013-01-23 22:57:40 +01008184 return 0;
8185}
8186
Johannes Bergff1b6e62011-05-04 15:37:28 +02008187static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8188{
8189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8190 struct sk_buff *msg;
8191 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008192 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008193
Johannes Berg964dc9e2013-06-03 17:25:34 +02008194 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008195 return -EOPNOTSUPP;
8196
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008197 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008198 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008199 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8200 rdev->wiphy.wowlan_config->tcp->payload_len +
8201 rdev->wiphy.wowlan_config->tcp->wake_len +
8202 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008203 }
8204
8205 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008206 if (!msg)
8207 return -ENOMEM;
8208
Eric W. Biederman15e47302012-09-07 20:12:54 +00008209 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008210 NL80211_CMD_GET_WOWLAN);
8211 if (!hdr)
8212 goto nla_put_failure;
8213
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008214 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008215 struct nlattr *nl_wowlan;
8216
8217 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8218 if (!nl_wowlan)
8219 goto nla_put_failure;
8220
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008221 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008222 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008223 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008224 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008225 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008226 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008227 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008228 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008229 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008230 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008231 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008232 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008233 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008234 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8235 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008236
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008237 if (nl80211_send_wowlan_patterns(msg, rdev))
8238 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008239
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008240 if (nl80211_send_wowlan_tcp(msg,
8241 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008242 goto nla_put_failure;
8243
Johannes Bergff1b6e62011-05-04 15:37:28 +02008244 nla_nest_end(msg, nl_wowlan);
8245 }
8246
8247 genlmsg_end(msg, hdr);
8248 return genlmsg_reply(msg, info);
8249
8250nla_put_failure:
8251 nlmsg_free(msg);
8252 return -ENOBUFS;
8253}
8254
Johannes Berg2a0e0472013-01-23 22:57:40 +01008255static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8256 struct nlattr *attr,
8257 struct cfg80211_wowlan *trig)
8258{
8259 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8260 struct cfg80211_wowlan_tcp *cfg;
8261 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8262 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8263 u32 size;
8264 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8265 int err, port;
8266
Johannes Berg964dc9e2013-06-03 17:25:34 +02008267 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008268 return -EINVAL;
8269
8270 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8271 nla_data(attr), nla_len(attr),
8272 nl80211_wowlan_tcp_policy);
8273 if (err)
8274 return err;
8275
8276 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8277 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8278 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8279 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8280 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8281 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8282 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8283 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8284 return -EINVAL;
8285
8286 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008287 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008288 return -EINVAL;
8289
8290 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008291 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008292 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008293 return -EINVAL;
8294
8295 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008296 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008297 return -EINVAL;
8298
8299 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8300 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8301 return -EINVAL;
8302
8303 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8304 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8305
8306 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8307 tokens_size = tokln - sizeof(*tok);
8308
8309 if (!tok->len || tokens_size % tok->len)
8310 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008311 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008312 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008313 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008314 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008315 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008316 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008317 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008318 return -EINVAL;
8319 if (tok->offset + tok->len > data_size)
8320 return -EINVAL;
8321 }
8322
8323 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8324 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008325 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008326 return -EINVAL;
8327 if (seq->len == 0 || seq->len > 4)
8328 return -EINVAL;
8329 if (seq->len + seq->offset > data_size)
8330 return -EINVAL;
8331 }
8332
8333 size = sizeof(*cfg);
8334 size += data_size;
8335 size += wake_size + wake_mask_size;
8336 size += tokens_size;
8337
8338 cfg = kzalloc(size, GFP_KERNEL);
8339 if (!cfg)
8340 return -ENOMEM;
8341 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8342 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8343 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8344 ETH_ALEN);
8345 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8346 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8347 else
8348 port = 0;
8349#ifdef CONFIG_INET
8350 /* allocate a socket and port for it and use it */
8351 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8352 IPPROTO_TCP, &cfg->sock, 1);
8353 if (err) {
8354 kfree(cfg);
8355 return err;
8356 }
8357 if (inet_csk_get_port(cfg->sock->sk, port)) {
8358 sock_release(cfg->sock);
8359 kfree(cfg);
8360 return -EADDRINUSE;
8361 }
8362 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8363#else
8364 if (!port) {
8365 kfree(cfg);
8366 return -EINVAL;
8367 }
8368 cfg->src_port = port;
8369#endif
8370
8371 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8372 cfg->payload_len = data_size;
8373 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8374 memcpy((void *)cfg->payload,
8375 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8376 data_size);
8377 if (seq)
8378 cfg->payload_seq = *seq;
8379 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8380 cfg->wake_len = wake_size;
8381 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8382 memcpy((void *)cfg->wake_data,
8383 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8384 wake_size);
8385 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8386 data_size + wake_size;
8387 memcpy((void *)cfg->wake_mask,
8388 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8389 wake_mask_size);
8390 if (tok) {
8391 cfg->tokens_size = tokens_size;
8392 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8393 }
8394
8395 trig->tcp = cfg;
8396
8397 return 0;
8398}
8399
Johannes Bergff1b6e62011-05-04 15:37:28 +02008400static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8401{
8402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8403 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008404 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008405 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008406 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008407 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008408 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008409
Johannes Berg964dc9e2013-06-03 17:25:34 +02008410 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008411 return -EOPNOTSUPP;
8412
Johannes Bergae33bd82012-07-12 16:25:02 +02008413 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8414 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008415 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008416 goto set_wakeup;
8417 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008418
8419 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8420 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8421 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8422 nl80211_wowlan_policy);
8423 if (err)
8424 return err;
8425
8426 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8427 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8428 return -EINVAL;
8429 new_triggers.any = true;
8430 }
8431
8432 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8433 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8434 return -EINVAL;
8435 new_triggers.disconnect = true;
8436 }
8437
8438 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8439 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8440 return -EINVAL;
8441 new_triggers.magic_pkt = true;
8442 }
8443
Johannes Berg77dbbb12011-07-13 10:48:55 +02008444 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8445 return -EINVAL;
8446
8447 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8448 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8449 return -EINVAL;
8450 new_triggers.gtk_rekey_failure = true;
8451 }
8452
8453 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8454 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8455 return -EINVAL;
8456 new_triggers.eap_identity_req = true;
8457 }
8458
8459 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8460 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8461 return -EINVAL;
8462 new_triggers.four_way_handshake = true;
8463 }
8464
8465 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8466 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8467 return -EINVAL;
8468 new_triggers.rfkill_release = true;
8469 }
8470
Johannes Bergff1b6e62011-05-04 15:37:28 +02008471 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8472 struct nlattr *pat;
8473 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008474 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008475 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008476
8477 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8478 rem)
8479 n_patterns++;
8480 if (n_patterns > wowlan->n_patterns)
8481 return -EINVAL;
8482
8483 new_triggers.patterns = kcalloc(n_patterns,
8484 sizeof(new_triggers.patterns[0]),
8485 GFP_KERNEL);
8486 if (!new_triggers.patterns)
8487 return -ENOMEM;
8488
8489 new_triggers.n_patterns = n_patterns;
8490 i = 0;
8491
8492 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8493 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008494 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8495 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008496 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008497 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8498 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008499 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008500 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008501 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008502 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008503 goto error;
8504 if (pat_len > wowlan->pattern_max_len ||
8505 pat_len < wowlan->pattern_min_len)
8506 goto error;
8507
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008508 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008509 pkt_offset = 0;
8510 else
8511 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008512 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008513 if (pkt_offset > wowlan->max_pkt_offset)
8514 goto error;
8515 new_triggers.patterns[i].pkt_offset = pkt_offset;
8516
Johannes Bergff1b6e62011-05-04 15:37:28 +02008517 new_triggers.patterns[i].mask =
8518 kmalloc(mask_len + pat_len, GFP_KERNEL);
8519 if (!new_triggers.patterns[i].mask) {
8520 err = -ENOMEM;
8521 goto error;
8522 }
8523 new_triggers.patterns[i].pattern =
8524 new_triggers.patterns[i].mask + mask_len;
8525 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008526 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008527 mask_len);
8528 new_triggers.patterns[i].pattern_len = pat_len;
8529 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008530 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008531 pat_len);
8532 i++;
8533 }
8534 }
8535
Johannes Berg2a0e0472013-01-23 22:57:40 +01008536 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8537 err = nl80211_parse_wowlan_tcp(
8538 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8539 &new_triggers);
8540 if (err)
8541 goto error;
8542 }
8543
Johannes Bergae33bd82012-07-12 16:25:02 +02008544 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8545 if (!ntrig) {
8546 err = -ENOMEM;
8547 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008548 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008549 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008550 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008551
Johannes Bergae33bd82012-07-12 16:25:02 +02008552 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008553 if (rdev->ops->set_wakeup &&
8554 prev_enabled != !!rdev->wiphy.wowlan_config)
8555 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008556
Johannes Bergff1b6e62011-05-04 15:37:28 +02008557 return 0;
8558 error:
8559 for (i = 0; i < new_triggers.n_patterns; i++)
8560 kfree(new_triggers.patterns[i].mask);
8561 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008562 if (new_triggers.tcp && new_triggers.tcp->sock)
8563 sock_release(new_triggers.tcp->sock);
8564 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008565 return err;
8566}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008567#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008568
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008569static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8570 struct cfg80211_registered_device *rdev)
8571{
8572 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8573 int i, j, pat_len;
8574 struct cfg80211_coalesce_rules *rule;
8575
8576 if (!rdev->coalesce->n_rules)
8577 return 0;
8578
8579 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8580 if (!nl_rules)
8581 return -ENOBUFS;
8582
8583 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8584 nl_rule = nla_nest_start(msg, i + 1);
8585 if (!nl_rule)
8586 return -ENOBUFS;
8587
8588 rule = &rdev->coalesce->rules[i];
8589 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8590 rule->delay))
8591 return -ENOBUFS;
8592
8593 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8594 rule->condition))
8595 return -ENOBUFS;
8596
8597 nl_pats = nla_nest_start(msg,
8598 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8599 if (!nl_pats)
8600 return -ENOBUFS;
8601
8602 for (j = 0; j < rule->n_patterns; j++) {
8603 nl_pat = nla_nest_start(msg, j + 1);
8604 if (!nl_pat)
8605 return -ENOBUFS;
8606 pat_len = rule->patterns[j].pattern_len;
8607 if (nla_put(msg, NL80211_PKTPAT_MASK,
8608 DIV_ROUND_UP(pat_len, 8),
8609 rule->patterns[j].mask) ||
8610 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8611 rule->patterns[j].pattern) ||
8612 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8613 rule->patterns[j].pkt_offset))
8614 return -ENOBUFS;
8615 nla_nest_end(msg, nl_pat);
8616 }
8617 nla_nest_end(msg, nl_pats);
8618 nla_nest_end(msg, nl_rule);
8619 }
8620 nla_nest_end(msg, nl_rules);
8621
8622 return 0;
8623}
8624
8625static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8626{
8627 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8628 struct sk_buff *msg;
8629 void *hdr;
8630
8631 if (!rdev->wiphy.coalesce)
8632 return -EOPNOTSUPP;
8633
8634 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8635 if (!msg)
8636 return -ENOMEM;
8637
8638 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8639 NL80211_CMD_GET_COALESCE);
8640 if (!hdr)
8641 goto nla_put_failure;
8642
8643 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8644 goto nla_put_failure;
8645
8646 genlmsg_end(msg, hdr);
8647 return genlmsg_reply(msg, info);
8648
8649nla_put_failure:
8650 nlmsg_free(msg);
8651 return -ENOBUFS;
8652}
8653
8654void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8655{
8656 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8657 int i, j;
8658 struct cfg80211_coalesce_rules *rule;
8659
8660 if (!coalesce)
8661 return;
8662
8663 for (i = 0; i < coalesce->n_rules; i++) {
8664 rule = &coalesce->rules[i];
8665 for (j = 0; j < rule->n_patterns; j++)
8666 kfree(rule->patterns[j].mask);
8667 kfree(rule->patterns);
8668 }
8669 kfree(coalesce->rules);
8670 kfree(coalesce);
8671 rdev->coalesce = NULL;
8672}
8673
8674static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8675 struct nlattr *rule,
8676 struct cfg80211_coalesce_rules *new_rule)
8677{
8678 int err, i;
8679 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8680 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8681 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8682 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8683
8684 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8685 nla_len(rule), nl80211_coalesce_policy);
8686 if (err)
8687 return err;
8688
8689 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8690 new_rule->delay =
8691 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8692 if (new_rule->delay > coalesce->max_delay)
8693 return -EINVAL;
8694
8695 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8696 new_rule->condition =
8697 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8698 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8699 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8700 return -EINVAL;
8701
8702 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8703 return -EINVAL;
8704
8705 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8706 rem)
8707 n_patterns++;
8708 if (n_patterns > coalesce->n_patterns)
8709 return -EINVAL;
8710
8711 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8712 GFP_KERNEL);
8713 if (!new_rule->patterns)
8714 return -ENOMEM;
8715
8716 new_rule->n_patterns = n_patterns;
8717 i = 0;
8718
8719 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8720 rem) {
8721 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8722 nla_len(pat), NULL);
8723 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8724 !pat_tb[NL80211_PKTPAT_PATTERN])
8725 return -EINVAL;
8726 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8727 mask_len = DIV_ROUND_UP(pat_len, 8);
8728 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8729 return -EINVAL;
8730 if (pat_len > coalesce->pattern_max_len ||
8731 pat_len < coalesce->pattern_min_len)
8732 return -EINVAL;
8733
8734 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8735 pkt_offset = 0;
8736 else
8737 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8738 if (pkt_offset > coalesce->max_pkt_offset)
8739 return -EINVAL;
8740 new_rule->patterns[i].pkt_offset = pkt_offset;
8741
8742 new_rule->patterns[i].mask =
8743 kmalloc(mask_len + pat_len, GFP_KERNEL);
8744 if (!new_rule->patterns[i].mask)
8745 return -ENOMEM;
8746 new_rule->patterns[i].pattern =
8747 new_rule->patterns[i].mask + mask_len;
8748 memcpy(new_rule->patterns[i].mask,
8749 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8750 new_rule->patterns[i].pattern_len = pat_len;
8751 memcpy(new_rule->patterns[i].pattern,
8752 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8753 i++;
8754 }
8755
8756 return 0;
8757}
8758
8759static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8760{
8761 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8762 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8763 struct cfg80211_coalesce new_coalesce = {};
8764 struct cfg80211_coalesce *n_coalesce;
8765 int err, rem_rule, n_rules = 0, i, j;
8766 struct nlattr *rule;
8767 struct cfg80211_coalesce_rules *tmp_rule;
8768
8769 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8770 return -EOPNOTSUPP;
8771
8772 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8773 cfg80211_rdev_free_coalesce(rdev);
8774 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8775 return 0;
8776 }
8777
8778 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8779 rem_rule)
8780 n_rules++;
8781 if (n_rules > coalesce->n_rules)
8782 return -EINVAL;
8783
8784 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8785 GFP_KERNEL);
8786 if (!new_coalesce.rules)
8787 return -ENOMEM;
8788
8789 new_coalesce.n_rules = n_rules;
8790 i = 0;
8791
8792 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8793 rem_rule) {
8794 err = nl80211_parse_coalesce_rule(rdev, rule,
8795 &new_coalesce.rules[i]);
8796 if (err)
8797 goto error;
8798
8799 i++;
8800 }
8801
8802 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8803 if (err)
8804 goto error;
8805
8806 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8807 if (!n_coalesce) {
8808 err = -ENOMEM;
8809 goto error;
8810 }
8811 cfg80211_rdev_free_coalesce(rdev);
8812 rdev->coalesce = n_coalesce;
8813
8814 return 0;
8815error:
8816 for (i = 0; i < new_coalesce.n_rules; i++) {
8817 tmp_rule = &new_coalesce.rules[i];
8818 for (j = 0; j < tmp_rule->n_patterns; j++)
8819 kfree(tmp_rule->patterns[j].mask);
8820 kfree(tmp_rule->patterns);
8821 }
8822 kfree(new_coalesce.rules);
8823
8824 return err;
8825}
8826
Johannes Berge5497d72011-07-05 16:35:40 +02008827static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8828{
8829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8830 struct net_device *dev = info->user_ptr[1];
8831 struct wireless_dev *wdev = dev->ieee80211_ptr;
8832 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8833 struct cfg80211_gtk_rekey_data rekey_data;
8834 int err;
8835
8836 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8837 return -EINVAL;
8838
8839 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8840 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8841 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8842 nl80211_rekey_policy);
8843 if (err)
8844 return err;
8845
8846 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8847 return -ERANGE;
8848 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8849 return -ERANGE;
8850 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8851 return -ERANGE;
8852
8853 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8854 NL80211_KEK_LEN);
8855 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8856 NL80211_KCK_LEN);
8857 memcpy(rekey_data.replay_ctr,
8858 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8859 NL80211_REPLAY_CTR_LEN);
8860
8861 wdev_lock(wdev);
8862 if (!wdev->current_bss) {
8863 err = -ENOTCONN;
8864 goto out;
8865 }
8866
8867 if (!rdev->ops->set_rekey_data) {
8868 err = -EOPNOTSUPP;
8869 goto out;
8870 }
8871
Hila Gonene35e4d22012-06-27 17:19:42 +03008872 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008873 out:
8874 wdev_unlock(wdev);
8875 return err;
8876}
8877
Johannes Berg28946da2011-11-04 11:18:12 +01008878static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8879 struct genl_info *info)
8880{
8881 struct net_device *dev = info->user_ptr[1];
8882 struct wireless_dev *wdev = dev->ieee80211_ptr;
8883
8884 if (wdev->iftype != NL80211_IFTYPE_AP &&
8885 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8886 return -EINVAL;
8887
Eric W. Biederman15e47302012-09-07 20:12:54 +00008888 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008889 return -EBUSY;
8890
Eric W. Biederman15e47302012-09-07 20:12:54 +00008891 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008892 return 0;
8893}
8894
Johannes Berg7f6cf312011-11-04 11:18:15 +01008895static int nl80211_probe_client(struct sk_buff *skb,
8896 struct genl_info *info)
8897{
8898 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8899 struct net_device *dev = info->user_ptr[1];
8900 struct wireless_dev *wdev = dev->ieee80211_ptr;
8901 struct sk_buff *msg;
8902 void *hdr;
8903 const u8 *addr;
8904 u64 cookie;
8905 int err;
8906
8907 if (wdev->iftype != NL80211_IFTYPE_AP &&
8908 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8909 return -EOPNOTSUPP;
8910
8911 if (!info->attrs[NL80211_ATTR_MAC])
8912 return -EINVAL;
8913
8914 if (!rdev->ops->probe_client)
8915 return -EOPNOTSUPP;
8916
8917 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8918 if (!msg)
8919 return -ENOMEM;
8920
Eric W. Biederman15e47302012-09-07 20:12:54 +00008921 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008922 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008923 if (!hdr) {
8924 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008925 goto free_msg;
8926 }
8927
8928 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8929
Hila Gonene35e4d22012-06-27 17:19:42 +03008930 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008931 if (err)
8932 goto free_msg;
8933
David S. Miller9360ffd2012-03-29 04:41:26 -04008934 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8935 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008936
8937 genlmsg_end(msg, hdr);
8938
8939 return genlmsg_reply(msg, info);
8940
8941 nla_put_failure:
8942 err = -ENOBUFS;
8943 free_msg:
8944 nlmsg_free(msg);
8945 return err;
8946}
8947
Johannes Berg5e760232011-11-04 11:18:17 +01008948static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8949{
8950 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008951 struct cfg80211_beacon_registration *reg, *nreg;
8952 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008953
8954 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8955 return -EOPNOTSUPP;
8956
Ben Greear37c73b52012-10-26 14:49:25 -07008957 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8958 if (!nreg)
8959 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008960
Ben Greear37c73b52012-10-26 14:49:25 -07008961 /* First, check if already registered. */
8962 spin_lock_bh(&rdev->beacon_registrations_lock);
8963 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8964 if (reg->nlportid == info->snd_portid) {
8965 rv = -EALREADY;
8966 goto out_err;
8967 }
8968 }
8969 /* Add it to the list */
8970 nreg->nlportid = info->snd_portid;
8971 list_add(&nreg->list, &rdev->beacon_registrations);
8972
8973 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008974
8975 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008976out_err:
8977 spin_unlock_bh(&rdev->beacon_registrations_lock);
8978 kfree(nreg);
8979 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008980}
8981
Johannes Berg98104fde2012-06-16 00:19:54 +02008982static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8983{
8984 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8985 struct wireless_dev *wdev = info->user_ptr[1];
8986 int err;
8987
8988 if (!rdev->ops->start_p2p_device)
8989 return -EOPNOTSUPP;
8990
8991 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8992 return -EOPNOTSUPP;
8993
8994 if (wdev->p2p_started)
8995 return 0;
8996
Johannes Berg98104fde2012-06-16 00:19:54 +02008997 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008998 if (err)
8999 return err;
9000
Johannes Bergeeb126e2012-10-23 15:16:50 +02009001 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009002 if (err)
9003 return err;
9004
9005 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02009006 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02009007
9008 return 0;
9009}
9010
9011static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9012{
9013 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9014 struct wireless_dev *wdev = info->user_ptr[1];
9015
9016 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9017 return -EOPNOTSUPP;
9018
9019 if (!rdev->ops->stop_p2p_device)
9020 return -EOPNOTSUPP;
9021
Johannes Bergf9f47522013-03-19 15:04:07 +01009022 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009023
9024 return 0;
9025}
9026
Johannes Berg3713b4e2013-02-14 16:19:38 +01009027static int nl80211_get_protocol_features(struct sk_buff *skb,
9028 struct genl_info *info)
9029{
9030 void *hdr;
9031 struct sk_buff *msg;
9032
9033 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9034 if (!msg)
9035 return -ENOMEM;
9036
9037 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9038 NL80211_CMD_GET_PROTOCOL_FEATURES);
9039 if (!hdr)
9040 goto nla_put_failure;
9041
9042 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9043 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9044 goto nla_put_failure;
9045
9046 genlmsg_end(msg, hdr);
9047 return genlmsg_reply(msg, info);
9048
9049 nla_put_failure:
9050 kfree_skb(msg);
9051 return -ENOBUFS;
9052}
9053
Jouni Malinen355199e2013-02-27 17:14:27 +02009054static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9055{
9056 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9057 struct cfg80211_update_ft_ies_params ft_params;
9058 struct net_device *dev = info->user_ptr[1];
9059
9060 if (!rdev->ops->update_ft_ies)
9061 return -EOPNOTSUPP;
9062
9063 if (!info->attrs[NL80211_ATTR_MDID] ||
9064 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9065 return -EINVAL;
9066
9067 memset(&ft_params, 0, sizeof(ft_params));
9068 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9069 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9070 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9071
9072 return rdev_update_ft_ies(rdev, dev, &ft_params);
9073}
9074
Arend van Spriel5de17982013-04-18 15:49:00 +02009075static int nl80211_crit_protocol_start(struct sk_buff *skb,
9076 struct genl_info *info)
9077{
9078 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9079 struct wireless_dev *wdev = info->user_ptr[1];
9080 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9081 u16 duration;
9082 int ret;
9083
9084 if (!rdev->ops->crit_proto_start)
9085 return -EOPNOTSUPP;
9086
9087 if (WARN_ON(!rdev->ops->crit_proto_stop))
9088 return -EINVAL;
9089
9090 if (rdev->crit_proto_nlportid)
9091 return -EBUSY;
9092
9093 /* determine protocol if provided */
9094 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9095 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9096
9097 if (proto >= NUM_NL80211_CRIT_PROTO)
9098 return -EINVAL;
9099
9100 /* timeout must be provided */
9101 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9102 return -EINVAL;
9103
9104 duration =
9105 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9106
9107 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9108 return -ERANGE;
9109
9110 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9111 if (!ret)
9112 rdev->crit_proto_nlportid = info->snd_portid;
9113
9114 return ret;
9115}
9116
9117static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9118 struct genl_info *info)
9119{
9120 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9121 struct wireless_dev *wdev = info->user_ptr[1];
9122
9123 if (!rdev->ops->crit_proto_stop)
9124 return -EOPNOTSUPP;
9125
9126 if (rdev->crit_proto_nlportid) {
9127 rdev->crit_proto_nlportid = 0;
9128 rdev_crit_proto_stop(rdev, wdev);
9129 }
9130 return 0;
9131}
9132
Johannes Bergad7e7182013-11-13 13:37:47 +01009133static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9134{
9135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9136 struct wireless_dev *wdev =
9137 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9138 int i, err;
9139 u32 vid, subcmd;
9140
9141 if (!rdev->wiphy.vendor_commands)
9142 return -EOPNOTSUPP;
9143
9144 if (IS_ERR(wdev)) {
9145 err = PTR_ERR(wdev);
9146 if (err != -EINVAL)
9147 return err;
9148 wdev = NULL;
9149 } else if (wdev->wiphy != &rdev->wiphy) {
9150 return -EINVAL;
9151 }
9152
9153 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9154 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9155 return -EINVAL;
9156
9157 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9158 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9159 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9160 const struct wiphy_vendor_command *vcmd;
9161 void *data = NULL;
9162 int len = 0;
9163
9164 vcmd = &rdev->wiphy.vendor_commands[i];
9165
9166 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9167 continue;
9168
9169 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9170 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9171 if (!wdev)
9172 return -EINVAL;
9173 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9174 !wdev->netdev)
9175 return -EINVAL;
9176
9177 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9178 if (wdev->netdev &&
9179 !netif_running(wdev->netdev))
9180 return -ENETDOWN;
9181 if (!wdev->netdev && !wdev->p2p_started)
9182 return -ENETDOWN;
9183 }
9184 } else {
9185 wdev = NULL;
9186 }
9187
9188 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9189 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9190 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9191 }
9192
9193 rdev->cur_cmd_info = info;
9194 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9195 data, len);
9196 rdev->cur_cmd_info = NULL;
9197 return err;
9198 }
9199
9200 return -EOPNOTSUPP;
9201}
9202
9203struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9204 enum nl80211_commands cmd,
9205 enum nl80211_attrs attr,
9206 int approxlen)
9207{
9208 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9209
9210 if (WARN_ON(!rdev->cur_cmd_info))
9211 return NULL;
9212
9213 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9214 rdev->cur_cmd_info->snd_portid,
9215 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009216 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009217}
9218EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9219
9220int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9221{
9222 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9223 void *hdr = ((void **)skb->cb)[1];
9224 struct nlattr *data = ((void **)skb->cb)[2];
9225
9226 if (WARN_ON(!rdev->cur_cmd_info)) {
9227 kfree_skb(skb);
9228 return -EINVAL;
9229 }
9230
9231 nla_nest_end(skb, data);
9232 genlmsg_end(skb, hdr);
9233 return genlmsg_reply(skb, rdev->cur_cmd_info);
9234}
9235EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9236
9237
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009238static int nl80211_set_qos_map(struct sk_buff *skb,
9239 struct genl_info *info)
9240{
9241 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9242 struct cfg80211_qos_map *qos_map = NULL;
9243 struct net_device *dev = info->user_ptr[1];
9244 u8 *pos, len, num_des, des_len, des;
9245 int ret;
9246
9247 if (!rdev->ops->set_qos_map)
9248 return -EOPNOTSUPP;
9249
9250 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9251 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9252 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9253
9254 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9255 len > IEEE80211_QOS_MAP_LEN_MAX)
9256 return -EINVAL;
9257
9258 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9259 if (!qos_map)
9260 return -ENOMEM;
9261
9262 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9263 if (num_des) {
9264 des_len = num_des *
9265 sizeof(struct cfg80211_dscp_exception);
9266 memcpy(qos_map->dscp_exception, pos, des_len);
9267 qos_map->num_des = num_des;
9268 for (des = 0; des < num_des; des++) {
9269 if (qos_map->dscp_exception[des].up > 7) {
9270 kfree(qos_map);
9271 return -EINVAL;
9272 }
9273 }
9274 pos += des_len;
9275 }
9276 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9277 }
9278
9279 wdev_lock(dev->ieee80211_ptr);
9280 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9281 if (!ret)
9282 ret = rdev_set_qos_map(rdev, dev, qos_map);
9283 wdev_unlock(dev->ieee80211_ptr);
9284
9285 kfree(qos_map);
9286 return ret;
9287}
9288
Johannes Berg4c476992010-10-04 21:36:35 +02009289#define NL80211_FLAG_NEED_WIPHY 0x01
9290#define NL80211_FLAG_NEED_NETDEV 0x02
9291#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009292#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9293#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9294 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009295#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009296/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009297#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9298 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009299
Johannes Bergf84f7712013-11-14 17:14:45 +01009300static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009301 struct genl_info *info)
9302{
9303 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009304 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009305 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009306 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9307
9308 if (rtnl)
9309 rtnl_lock();
9310
9311 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009312 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009313 if (IS_ERR(rdev)) {
9314 if (rtnl)
9315 rtnl_unlock();
9316 return PTR_ERR(rdev);
9317 }
9318 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009319 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9320 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009321 ASSERT_RTNL();
9322
Johannes Berg89a54e42012-06-15 14:33:17 +02009323 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9324 info->attrs);
9325 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009326 if (rtnl)
9327 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009328 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009329 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009330
Johannes Berg89a54e42012-06-15 14:33:17 +02009331 dev = wdev->netdev;
9332 rdev = wiphy_to_dev(wdev->wiphy);
9333
Johannes Berg1bf614e2012-06-15 15:23:36 +02009334 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9335 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009336 if (rtnl)
9337 rtnl_unlock();
9338 return -EINVAL;
9339 }
9340
9341 info->user_ptr[1] = dev;
9342 } else {
9343 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009344 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009345
Johannes Berg1bf614e2012-06-15 15:23:36 +02009346 if (dev) {
9347 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9348 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009349 if (rtnl)
9350 rtnl_unlock();
9351 return -ENETDOWN;
9352 }
9353
9354 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009355 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9356 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009357 if (rtnl)
9358 rtnl_unlock();
9359 return -ENETDOWN;
9360 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009361 }
9362
Johannes Berg4c476992010-10-04 21:36:35 +02009363 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009364 }
9365
9366 return 0;
9367}
9368
Johannes Bergf84f7712013-11-14 17:14:45 +01009369static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009370 struct genl_info *info)
9371{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009372 if (info->user_ptr[1]) {
9373 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9374 struct wireless_dev *wdev = info->user_ptr[1];
9375
9376 if (wdev->netdev)
9377 dev_put(wdev->netdev);
9378 } else {
9379 dev_put(info->user_ptr[1]);
9380 }
9381 }
Johannes Berg4c476992010-10-04 21:36:35 +02009382 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9383 rtnl_unlock();
9384}
9385
Johannes Berg4534de82013-11-14 17:14:46 +01009386static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009387 {
9388 .cmd = NL80211_CMD_GET_WIPHY,
9389 .doit = nl80211_get_wiphy,
9390 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009391 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009392 .policy = nl80211_policy,
9393 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009394 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9395 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009396 },
9397 {
9398 .cmd = NL80211_CMD_SET_WIPHY,
9399 .doit = nl80211_set_wiphy,
9400 .policy = nl80211_policy,
9401 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009402 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009403 },
9404 {
9405 .cmd = NL80211_CMD_GET_INTERFACE,
9406 .doit = nl80211_get_interface,
9407 .dumpit = nl80211_dump_interface,
9408 .policy = nl80211_policy,
9409 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009410 .internal_flags = NL80211_FLAG_NEED_WDEV |
9411 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009412 },
9413 {
9414 .cmd = NL80211_CMD_SET_INTERFACE,
9415 .doit = nl80211_set_interface,
9416 .policy = nl80211_policy,
9417 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009418 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9419 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009420 },
9421 {
9422 .cmd = NL80211_CMD_NEW_INTERFACE,
9423 .doit = nl80211_new_interface,
9424 .policy = nl80211_policy,
9425 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009426 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9427 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009428 },
9429 {
9430 .cmd = NL80211_CMD_DEL_INTERFACE,
9431 .doit = nl80211_del_interface,
9432 .policy = nl80211_policy,
9433 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009434 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009435 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009436 },
Johannes Berg41ade002007-12-19 02:03:29 +01009437 {
9438 .cmd = NL80211_CMD_GET_KEY,
9439 .doit = nl80211_get_key,
9440 .policy = nl80211_policy,
9441 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009442 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009443 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009444 },
9445 {
9446 .cmd = NL80211_CMD_SET_KEY,
9447 .doit = nl80211_set_key,
9448 .policy = nl80211_policy,
9449 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009450 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009451 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009452 },
9453 {
9454 .cmd = NL80211_CMD_NEW_KEY,
9455 .doit = nl80211_new_key,
9456 .policy = nl80211_policy,
9457 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009458 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009459 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009460 },
9461 {
9462 .cmd = NL80211_CMD_DEL_KEY,
9463 .doit = nl80211_del_key,
9464 .policy = nl80211_policy,
9465 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009466 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009467 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009468 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009469 {
9470 .cmd = NL80211_CMD_SET_BEACON,
9471 .policy = nl80211_policy,
9472 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009473 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009474 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009475 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009476 },
9477 {
Johannes Berg88600202012-02-13 15:17:18 +01009478 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009479 .policy = nl80211_policy,
9480 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009481 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009482 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009483 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009484 },
9485 {
Johannes Berg88600202012-02-13 15:17:18 +01009486 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009487 .policy = nl80211_policy,
9488 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009489 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009490 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009491 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009492 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009493 {
9494 .cmd = NL80211_CMD_GET_STATION,
9495 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009496 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009497 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009498 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9499 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009500 },
9501 {
9502 .cmd = NL80211_CMD_SET_STATION,
9503 .doit = nl80211_set_station,
9504 .policy = nl80211_policy,
9505 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009506 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009507 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009508 },
9509 {
9510 .cmd = NL80211_CMD_NEW_STATION,
9511 .doit = nl80211_new_station,
9512 .policy = nl80211_policy,
9513 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009514 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009515 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009516 },
9517 {
9518 .cmd = NL80211_CMD_DEL_STATION,
9519 .doit = nl80211_del_station,
9520 .policy = nl80211_policy,
9521 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009522 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009523 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009524 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009525 {
9526 .cmd = NL80211_CMD_GET_MPATH,
9527 .doit = nl80211_get_mpath,
9528 .dumpit = nl80211_dump_mpath,
9529 .policy = nl80211_policy,
9530 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009531 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009532 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009533 },
9534 {
9535 .cmd = NL80211_CMD_SET_MPATH,
9536 .doit = nl80211_set_mpath,
9537 .policy = nl80211_policy,
9538 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009539 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009540 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009541 },
9542 {
9543 .cmd = NL80211_CMD_NEW_MPATH,
9544 .doit = nl80211_new_mpath,
9545 .policy = nl80211_policy,
9546 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009547 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009548 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009549 },
9550 {
9551 .cmd = NL80211_CMD_DEL_MPATH,
9552 .doit = nl80211_del_mpath,
9553 .policy = nl80211_policy,
9554 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009555 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009556 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009557 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009558 {
9559 .cmd = NL80211_CMD_SET_BSS,
9560 .doit = nl80211_set_bss,
9561 .policy = nl80211_policy,
9562 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009563 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009564 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009565 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009566 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009567 .cmd = NL80211_CMD_GET_REG,
9568 .doit = nl80211_get_reg,
9569 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009570 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009571 /* can be retrieved by unprivileged users */
9572 },
9573 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009574 .cmd = NL80211_CMD_SET_REG,
9575 .doit = nl80211_set_reg,
9576 .policy = nl80211_policy,
9577 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009578 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009579 },
9580 {
9581 .cmd = NL80211_CMD_REQ_SET_REG,
9582 .doit = nl80211_req_set_reg,
9583 .policy = nl80211_policy,
9584 .flags = GENL_ADMIN_PERM,
9585 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009586 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009587 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9588 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009589 .policy = nl80211_policy,
9590 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009592 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009593 },
9594 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009595 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9596 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009597 .policy = nl80211_policy,
9598 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009600 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009601 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009602 {
Johannes Berg2a519312009-02-10 21:25:55 +01009603 .cmd = NL80211_CMD_TRIGGER_SCAN,
9604 .doit = nl80211_trigger_scan,
9605 .policy = nl80211_policy,
9606 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009607 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009608 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009609 },
9610 {
9611 .cmd = NL80211_CMD_GET_SCAN,
9612 .policy = nl80211_policy,
9613 .dumpit = nl80211_dump_scan,
9614 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009615 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009616 .cmd = NL80211_CMD_START_SCHED_SCAN,
9617 .doit = nl80211_start_sched_scan,
9618 .policy = nl80211_policy,
9619 .flags = GENL_ADMIN_PERM,
9620 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9621 NL80211_FLAG_NEED_RTNL,
9622 },
9623 {
9624 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9625 .doit = nl80211_stop_sched_scan,
9626 .policy = nl80211_policy,
9627 .flags = GENL_ADMIN_PERM,
9628 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9629 NL80211_FLAG_NEED_RTNL,
9630 },
9631 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009632 .cmd = NL80211_CMD_AUTHENTICATE,
9633 .doit = nl80211_authenticate,
9634 .policy = nl80211_policy,
9635 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009636 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009637 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009638 },
9639 {
9640 .cmd = NL80211_CMD_ASSOCIATE,
9641 .doit = nl80211_associate,
9642 .policy = nl80211_policy,
9643 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009644 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009645 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009646 },
9647 {
9648 .cmd = NL80211_CMD_DEAUTHENTICATE,
9649 .doit = nl80211_deauthenticate,
9650 .policy = nl80211_policy,
9651 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009652 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009653 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009654 },
9655 {
9656 .cmd = NL80211_CMD_DISASSOCIATE,
9657 .doit = nl80211_disassociate,
9658 .policy = nl80211_policy,
9659 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009660 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009661 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009662 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009663 {
9664 .cmd = NL80211_CMD_JOIN_IBSS,
9665 .doit = nl80211_join_ibss,
9666 .policy = nl80211_policy,
9667 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009668 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009669 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009670 },
9671 {
9672 .cmd = NL80211_CMD_LEAVE_IBSS,
9673 .doit = nl80211_leave_ibss,
9674 .policy = nl80211_policy,
9675 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009676 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009677 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009678 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009679#ifdef CONFIG_NL80211_TESTMODE
9680 {
9681 .cmd = NL80211_CMD_TESTMODE,
9682 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009683 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009684 .policy = nl80211_policy,
9685 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009686 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9687 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009688 },
9689#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009690 {
9691 .cmd = NL80211_CMD_CONNECT,
9692 .doit = nl80211_connect,
9693 .policy = nl80211_policy,
9694 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009695 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009696 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009697 },
9698 {
9699 .cmd = NL80211_CMD_DISCONNECT,
9700 .doit = nl80211_disconnect,
9701 .policy = nl80211_policy,
9702 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009703 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009704 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009705 },
Johannes Berg463d0182009-07-14 00:33:35 +02009706 {
9707 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9708 .doit = nl80211_wiphy_netns,
9709 .policy = nl80211_policy,
9710 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009711 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9712 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009713 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009714 {
9715 .cmd = NL80211_CMD_GET_SURVEY,
9716 .policy = nl80211_policy,
9717 .dumpit = nl80211_dump_survey,
9718 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009719 {
9720 .cmd = NL80211_CMD_SET_PMKSA,
9721 .doit = nl80211_setdel_pmksa,
9722 .policy = nl80211_policy,
9723 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009724 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009725 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009726 },
9727 {
9728 .cmd = NL80211_CMD_DEL_PMKSA,
9729 .doit = nl80211_setdel_pmksa,
9730 .policy = nl80211_policy,
9731 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009732 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009733 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009734 },
9735 {
9736 .cmd = NL80211_CMD_FLUSH_PMKSA,
9737 .doit = nl80211_flush_pmksa,
9738 .policy = nl80211_policy,
9739 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009740 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009741 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009742 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009743 {
9744 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9745 .doit = nl80211_remain_on_channel,
9746 .policy = nl80211_policy,
9747 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009748 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009749 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009750 },
9751 {
9752 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9753 .doit = nl80211_cancel_remain_on_channel,
9754 .policy = nl80211_policy,
9755 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009756 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009757 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009758 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009759 {
9760 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9761 .doit = nl80211_set_tx_bitrate_mask,
9762 .policy = nl80211_policy,
9763 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009764 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9765 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009766 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009767 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009768 .cmd = NL80211_CMD_REGISTER_FRAME,
9769 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009770 .policy = nl80211_policy,
9771 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009772 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009773 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009774 },
9775 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009776 .cmd = NL80211_CMD_FRAME,
9777 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009778 .policy = nl80211_policy,
9779 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009780 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009781 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009782 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009783 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009784 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9785 .doit = nl80211_tx_mgmt_cancel_wait,
9786 .policy = nl80211_policy,
9787 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009788 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009789 NL80211_FLAG_NEED_RTNL,
9790 },
9791 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009792 .cmd = NL80211_CMD_SET_POWER_SAVE,
9793 .doit = nl80211_set_power_save,
9794 .policy = nl80211_policy,
9795 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009796 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9797 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009798 },
9799 {
9800 .cmd = NL80211_CMD_GET_POWER_SAVE,
9801 .doit = nl80211_get_power_save,
9802 .policy = nl80211_policy,
9803 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009804 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9805 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009806 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009807 {
9808 .cmd = NL80211_CMD_SET_CQM,
9809 .doit = nl80211_set_cqm,
9810 .policy = nl80211_policy,
9811 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009812 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9813 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009814 },
Johannes Bergf444de02010-05-05 15:25:02 +02009815 {
9816 .cmd = NL80211_CMD_SET_CHANNEL,
9817 .doit = nl80211_set_channel,
9818 .policy = nl80211_policy,
9819 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009820 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9821 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009822 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009823 {
9824 .cmd = NL80211_CMD_SET_WDS_PEER,
9825 .doit = nl80211_set_wds_peer,
9826 .policy = nl80211_policy,
9827 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009828 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9829 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009830 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009831 {
9832 .cmd = NL80211_CMD_JOIN_MESH,
9833 .doit = nl80211_join_mesh,
9834 .policy = nl80211_policy,
9835 .flags = GENL_ADMIN_PERM,
9836 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9837 NL80211_FLAG_NEED_RTNL,
9838 },
9839 {
9840 .cmd = NL80211_CMD_LEAVE_MESH,
9841 .doit = nl80211_leave_mesh,
9842 .policy = nl80211_policy,
9843 .flags = GENL_ADMIN_PERM,
9844 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9845 NL80211_FLAG_NEED_RTNL,
9846 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009847#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009848 {
9849 .cmd = NL80211_CMD_GET_WOWLAN,
9850 .doit = nl80211_get_wowlan,
9851 .policy = nl80211_policy,
9852 /* can be retrieved by unprivileged users */
9853 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9854 NL80211_FLAG_NEED_RTNL,
9855 },
9856 {
9857 .cmd = NL80211_CMD_SET_WOWLAN,
9858 .doit = nl80211_set_wowlan,
9859 .policy = nl80211_policy,
9860 .flags = GENL_ADMIN_PERM,
9861 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9862 NL80211_FLAG_NEED_RTNL,
9863 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009864#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009865 {
9866 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9867 .doit = nl80211_set_rekey_data,
9868 .policy = nl80211_policy,
9869 .flags = GENL_ADMIN_PERM,
9870 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9871 NL80211_FLAG_NEED_RTNL,
9872 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009873 {
9874 .cmd = NL80211_CMD_TDLS_MGMT,
9875 .doit = nl80211_tdls_mgmt,
9876 .policy = nl80211_policy,
9877 .flags = GENL_ADMIN_PERM,
9878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9879 NL80211_FLAG_NEED_RTNL,
9880 },
9881 {
9882 .cmd = NL80211_CMD_TDLS_OPER,
9883 .doit = nl80211_tdls_oper,
9884 .policy = nl80211_policy,
9885 .flags = GENL_ADMIN_PERM,
9886 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9887 NL80211_FLAG_NEED_RTNL,
9888 },
Johannes Berg28946da2011-11-04 11:18:12 +01009889 {
9890 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9891 .doit = nl80211_register_unexpected_frame,
9892 .policy = nl80211_policy,
9893 .flags = GENL_ADMIN_PERM,
9894 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9895 NL80211_FLAG_NEED_RTNL,
9896 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009897 {
9898 .cmd = NL80211_CMD_PROBE_CLIENT,
9899 .doit = nl80211_probe_client,
9900 .policy = nl80211_policy,
9901 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009902 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009903 NL80211_FLAG_NEED_RTNL,
9904 },
Johannes Berg5e760232011-11-04 11:18:17 +01009905 {
9906 .cmd = NL80211_CMD_REGISTER_BEACONS,
9907 .doit = nl80211_register_beacons,
9908 .policy = nl80211_policy,
9909 .flags = GENL_ADMIN_PERM,
9910 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9911 NL80211_FLAG_NEED_RTNL,
9912 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009913 {
9914 .cmd = NL80211_CMD_SET_NOACK_MAP,
9915 .doit = nl80211_set_noack_map,
9916 .policy = nl80211_policy,
9917 .flags = GENL_ADMIN_PERM,
9918 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9919 NL80211_FLAG_NEED_RTNL,
9920 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009921 {
9922 .cmd = NL80211_CMD_START_P2P_DEVICE,
9923 .doit = nl80211_start_p2p_device,
9924 .policy = nl80211_policy,
9925 .flags = GENL_ADMIN_PERM,
9926 .internal_flags = NL80211_FLAG_NEED_WDEV |
9927 NL80211_FLAG_NEED_RTNL,
9928 },
9929 {
9930 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9931 .doit = nl80211_stop_p2p_device,
9932 .policy = nl80211_policy,
9933 .flags = GENL_ADMIN_PERM,
9934 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9935 NL80211_FLAG_NEED_RTNL,
9936 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009937 {
9938 .cmd = NL80211_CMD_SET_MCAST_RATE,
9939 .doit = nl80211_set_mcast_rate,
9940 .policy = nl80211_policy,
9941 .flags = GENL_ADMIN_PERM,
9942 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9943 NL80211_FLAG_NEED_RTNL,
9944 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309945 {
9946 .cmd = NL80211_CMD_SET_MAC_ACL,
9947 .doit = nl80211_set_mac_acl,
9948 .policy = nl80211_policy,
9949 .flags = GENL_ADMIN_PERM,
9950 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9951 NL80211_FLAG_NEED_RTNL,
9952 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009953 {
9954 .cmd = NL80211_CMD_RADAR_DETECT,
9955 .doit = nl80211_start_radar_detection,
9956 .policy = nl80211_policy,
9957 .flags = GENL_ADMIN_PERM,
9958 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9959 NL80211_FLAG_NEED_RTNL,
9960 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009961 {
9962 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9963 .doit = nl80211_get_protocol_features,
9964 .policy = nl80211_policy,
9965 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009966 {
9967 .cmd = NL80211_CMD_UPDATE_FT_IES,
9968 .doit = nl80211_update_ft_ies,
9969 .policy = nl80211_policy,
9970 .flags = GENL_ADMIN_PERM,
9971 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9972 NL80211_FLAG_NEED_RTNL,
9973 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009974 {
9975 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9976 .doit = nl80211_crit_protocol_start,
9977 .policy = nl80211_policy,
9978 .flags = GENL_ADMIN_PERM,
9979 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9980 NL80211_FLAG_NEED_RTNL,
9981 },
9982 {
9983 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9984 .doit = nl80211_crit_protocol_stop,
9985 .policy = nl80211_policy,
9986 .flags = GENL_ADMIN_PERM,
9987 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9988 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009989 },
9990 {
9991 .cmd = NL80211_CMD_GET_COALESCE,
9992 .doit = nl80211_get_coalesce,
9993 .policy = nl80211_policy,
9994 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9995 NL80211_FLAG_NEED_RTNL,
9996 },
9997 {
9998 .cmd = NL80211_CMD_SET_COALESCE,
9999 .doit = nl80211_set_coalesce,
10000 .policy = nl80211_policy,
10001 .flags = GENL_ADMIN_PERM,
10002 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10003 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020010004 },
10005 {
10006 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10007 .doit = nl80211_channel_switch,
10008 .policy = nl80211_policy,
10009 .flags = GENL_ADMIN_PERM,
10010 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10011 NL80211_FLAG_NEED_RTNL,
10012 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010013 {
10014 .cmd = NL80211_CMD_VENDOR,
10015 .doit = nl80211_vendor_cmd,
10016 .policy = nl80211_policy,
10017 .flags = GENL_ADMIN_PERM,
10018 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10019 NL80211_FLAG_NEED_RTNL,
10020 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010021 {
10022 .cmd = NL80211_CMD_SET_QOS_MAP,
10023 .doit = nl80211_set_qos_map,
10024 .policy = nl80211_policy,
10025 .flags = GENL_ADMIN_PERM,
10026 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10027 NL80211_FLAG_NEED_RTNL,
10028 },
Johannes Berg55682962007-09-20 13:09:35 -040010029};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010030
Johannes Berg55682962007-09-20 13:09:35 -040010031/* notification functions */
10032
10033void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
10034{
10035 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010036 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010037
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010038 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010039 if (!msg)
10040 return;
10041
Johannes Berg86e8cf92013-06-19 10:57:22 +020010042 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010043 nlmsg_free(msg);
10044 return;
10045 }
10046
Johannes Berg68eb5502013-11-19 15:19:38 +010010047 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010048 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010049}
10050
Johannes Berg362a4152009-05-24 16:43:15 +020010051static int nl80211_add_scan_req(struct sk_buff *msg,
10052 struct cfg80211_registered_device *rdev)
10053{
10054 struct cfg80211_scan_request *req = rdev->scan_req;
10055 struct nlattr *nest;
10056 int i;
10057
10058 if (WARN_ON(!req))
10059 return 0;
10060
10061 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10062 if (!nest)
10063 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010064 for (i = 0; i < req->n_ssids; i++) {
10065 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10066 goto nla_put_failure;
10067 }
Johannes Berg362a4152009-05-24 16:43:15 +020010068 nla_nest_end(msg, nest);
10069
10070 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10071 if (!nest)
10072 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010073 for (i = 0; i < req->n_channels; i++) {
10074 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10075 goto nla_put_failure;
10076 }
Johannes Berg362a4152009-05-24 16:43:15 +020010077 nla_nest_end(msg, nest);
10078
David S. Miller9360ffd2012-03-29 04:41:26 -040010079 if (req->ie &&
10080 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10081 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010082
Johannes Bergae917c92013-10-25 11:05:22 +020010083 if (req->flags &&
10084 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10085 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010086
Johannes Berg362a4152009-05-24 16:43:15 +020010087 return 0;
10088 nla_put_failure:
10089 return -ENOBUFS;
10090}
10091
Johannes Berga538e2d2009-06-16 19:56:42 +020010092static int nl80211_send_scan_msg(struct sk_buff *msg,
10093 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010094 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010095 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010096 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010097{
10098 void *hdr;
10099
Eric W. Biederman15e47302012-09-07 20:12:54 +000010100 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010101 if (!hdr)
10102 return -1;
10103
David S. Miller9360ffd2012-03-29 04:41:26 -040010104 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010105 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10106 wdev->netdev->ifindex)) ||
10107 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010108 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010109
Johannes Berg362a4152009-05-24 16:43:15 +020010110 /* ignore errors and send incomplete event anyway */
10111 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010112
10113 return genlmsg_end(msg, hdr);
10114
10115 nla_put_failure:
10116 genlmsg_cancel(msg, hdr);
10117 return -EMSGSIZE;
10118}
10119
Luciano Coelho807f8a82011-05-11 17:09:35 +030010120static int
10121nl80211_send_sched_scan_msg(struct sk_buff *msg,
10122 struct cfg80211_registered_device *rdev,
10123 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010124 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010125{
10126 void *hdr;
10127
Eric W. Biederman15e47302012-09-07 20:12:54 +000010128 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010129 if (!hdr)
10130 return -1;
10131
David S. Miller9360ffd2012-03-29 04:41:26 -040010132 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10133 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10134 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010135
10136 return genlmsg_end(msg, hdr);
10137
10138 nla_put_failure:
10139 genlmsg_cancel(msg, hdr);
10140 return -EMSGSIZE;
10141}
10142
Johannes Berga538e2d2009-06-16 19:56:42 +020010143void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010144 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010145{
10146 struct sk_buff *msg;
10147
Thomas Graf58050fc2012-06-28 03:57:45 +000010148 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010149 if (!msg)
10150 return;
10151
Johannes Bergfd014282012-06-18 19:17:03 +020010152 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010153 NL80211_CMD_TRIGGER_SCAN) < 0) {
10154 nlmsg_free(msg);
10155 return;
10156 }
10157
Johannes Berg68eb5502013-11-19 15:19:38 +010010158 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010159 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010160}
10161
Johannes Bergf9d15d12014-01-22 11:14:19 +020010162struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10163 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010164{
10165 struct sk_buff *msg;
10166
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010167 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010168 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010169 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010170
Johannes Bergfd014282012-06-18 19:17:03 +020010171 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010172 aborted ? NL80211_CMD_SCAN_ABORTED :
10173 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010174 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010175 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010176 }
10177
Johannes Bergf9d15d12014-01-22 11:14:19 +020010178 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010179}
10180
Johannes Bergf9d15d12014-01-22 11:14:19 +020010181void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10182 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010183{
Johannes Berg2a519312009-02-10 21:25:55 +010010184 if (!msg)
10185 return;
10186
Johannes Berg68eb5502013-11-19 15:19:38 +010010187 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010188 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010189}
10190
Luciano Coelho807f8a82011-05-11 17:09:35 +030010191void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10192 struct net_device *netdev)
10193{
10194 struct sk_buff *msg;
10195
10196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10197 if (!msg)
10198 return;
10199
10200 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10201 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10202 nlmsg_free(msg);
10203 return;
10204 }
10205
Johannes Berg68eb5502013-11-19 15:19:38 +010010206 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010207 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010208}
10209
10210void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10211 struct net_device *netdev, u32 cmd)
10212{
10213 struct sk_buff *msg;
10214
Thomas Graf58050fc2012-06-28 03:57:45 +000010215 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010216 if (!msg)
10217 return;
10218
10219 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10220 nlmsg_free(msg);
10221 return;
10222 }
10223
Johannes Berg68eb5502013-11-19 15:19:38 +010010224 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010225 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010226}
10227
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010228/*
10229 * This can happen on global regulatory changes or device specific settings
10230 * based on custom world regulatory domains.
10231 */
10232void nl80211_send_reg_change_event(struct regulatory_request *request)
10233{
10234 struct sk_buff *msg;
10235 void *hdr;
10236
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010237 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010238 if (!msg)
10239 return;
10240
10241 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10242 if (!hdr) {
10243 nlmsg_free(msg);
10244 return;
10245 }
10246
10247 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010248 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10249 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010250
David S. Miller9360ffd2012-03-29 04:41:26 -040010251 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10252 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10253 NL80211_REGDOM_TYPE_WORLD))
10254 goto nla_put_failure;
10255 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10256 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10257 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10258 goto nla_put_failure;
10259 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10260 request->intersect) {
10261 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10262 NL80211_REGDOM_TYPE_INTERSECTION))
10263 goto nla_put_failure;
10264 } else {
10265 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10266 NL80211_REGDOM_TYPE_COUNTRY) ||
10267 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10268 request->alpha2))
10269 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010270 }
10271
Johannes Bergf4173762012-12-03 18:23:37 +010010272 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010273 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10274 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010275
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010276 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010277
Johannes Bergbc43b282009-07-25 10:54:13 +020010278 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010279 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010280 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010281 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010282
10283 return;
10284
10285nla_put_failure:
10286 genlmsg_cancel(msg, hdr);
10287 nlmsg_free(msg);
10288}
10289
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010290static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10291 struct net_device *netdev,
10292 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010293 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010294{
10295 struct sk_buff *msg;
10296 void *hdr;
10297
Johannes Berge6d6e342009-07-01 21:26:47 +020010298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010299 if (!msg)
10300 return;
10301
10302 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10303 if (!hdr) {
10304 nlmsg_free(msg);
10305 return;
10306 }
10307
David S. Miller9360ffd2012-03-29 04:41:26 -040010308 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10309 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10310 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10311 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010312
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010313 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010314
Johannes Berg68eb5502013-11-19 15:19:38 +010010315 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010316 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010317 return;
10318
10319 nla_put_failure:
10320 genlmsg_cancel(msg, hdr);
10321 nlmsg_free(msg);
10322}
10323
10324void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010325 struct net_device *netdev, const u8 *buf,
10326 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010327{
10328 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010329 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010330}
10331
10332void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10333 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010334 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010335{
Johannes Berge6d6e342009-07-01 21:26:47 +020010336 nl80211_send_mlme_event(rdev, netdev, buf, len,
10337 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010338}
10339
Jouni Malinen53b46b82009-03-27 20:53:56 +020010340void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010341 struct net_device *netdev, const u8 *buf,
10342 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010343{
10344 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010345 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010346}
10347
Jouni Malinen53b46b82009-03-27 20:53:56 +020010348void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10349 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010350 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010351{
10352 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010353 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010354}
10355
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010356void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10357 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010358{
Johannes Berg947add32013-02-22 22:05:20 +010010359 struct wireless_dev *wdev = dev->ieee80211_ptr;
10360 struct wiphy *wiphy = wdev->wiphy;
10361 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010362 const struct ieee80211_mgmt *mgmt = (void *)buf;
10363 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010364
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010365 if (WARN_ON(len < 2))
10366 return;
10367
10368 if (ieee80211_is_deauth(mgmt->frame_control))
10369 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10370 else
10371 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10372
10373 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10374 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010375}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010376EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010377
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010378static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10379 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010380 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010381{
10382 struct sk_buff *msg;
10383 void *hdr;
10384
Johannes Berge6d6e342009-07-01 21:26:47 +020010385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010386 if (!msg)
10387 return;
10388
10389 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10390 if (!hdr) {
10391 nlmsg_free(msg);
10392 return;
10393 }
10394
David S. Miller9360ffd2012-03-29 04:41:26 -040010395 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10396 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10397 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10398 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10399 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010400
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010401 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010402
Johannes Berg68eb5502013-11-19 15:19:38 +010010403 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010404 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010405 return;
10406
10407 nla_put_failure:
10408 genlmsg_cancel(msg, hdr);
10409 nlmsg_free(msg);
10410}
10411
10412void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010413 struct net_device *netdev, const u8 *addr,
10414 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010415{
10416 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010417 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010418}
10419
10420void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010421 struct net_device *netdev, const u8 *addr,
10422 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010423{
Johannes Berge6d6e342009-07-01 21:26:47 +020010424 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10425 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010426}
10427
Samuel Ortizb23aa672009-07-01 21:26:54 +020010428void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10429 struct net_device *netdev, const u8 *bssid,
10430 const u8 *req_ie, size_t req_ie_len,
10431 const u8 *resp_ie, size_t resp_ie_len,
10432 u16 status, gfp_t gfp)
10433{
10434 struct sk_buff *msg;
10435 void *hdr;
10436
Thomas Graf58050fc2012-06-28 03:57:45 +000010437 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010438 if (!msg)
10439 return;
10440
10441 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10442 if (!hdr) {
10443 nlmsg_free(msg);
10444 return;
10445 }
10446
David S. Miller9360ffd2012-03-29 04:41:26 -040010447 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10448 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10449 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10450 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10451 (req_ie &&
10452 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10453 (resp_ie &&
10454 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10455 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010456
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010457 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010458
Johannes Berg68eb5502013-11-19 15:19:38 +010010459 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010460 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010461 return;
10462
10463 nla_put_failure:
10464 genlmsg_cancel(msg, hdr);
10465 nlmsg_free(msg);
10466
10467}
10468
10469void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10470 struct net_device *netdev, const u8 *bssid,
10471 const u8 *req_ie, size_t req_ie_len,
10472 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10473{
10474 struct sk_buff *msg;
10475 void *hdr;
10476
Thomas Graf58050fc2012-06-28 03:57:45 +000010477 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010478 if (!msg)
10479 return;
10480
10481 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10482 if (!hdr) {
10483 nlmsg_free(msg);
10484 return;
10485 }
10486
David S. Miller9360ffd2012-03-29 04:41:26 -040010487 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10488 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10489 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10490 (req_ie &&
10491 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10492 (resp_ie &&
10493 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10494 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010495
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010496 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010497
Johannes Berg68eb5502013-11-19 15:19:38 +010010498 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010499 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010500 return;
10501
10502 nla_put_failure:
10503 genlmsg_cancel(msg, hdr);
10504 nlmsg_free(msg);
10505
10506}
10507
10508void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10509 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010510 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010511{
10512 struct sk_buff *msg;
10513 void *hdr;
10514
Thomas Graf58050fc2012-06-28 03:57:45 +000010515 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010516 if (!msg)
10517 return;
10518
10519 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10520 if (!hdr) {
10521 nlmsg_free(msg);
10522 return;
10523 }
10524
David S. Miller9360ffd2012-03-29 04:41:26 -040010525 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10526 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10527 (from_ap && reason &&
10528 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10529 (from_ap &&
10530 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10531 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10532 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010533
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010534 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010535
Johannes Berg68eb5502013-11-19 15:19:38 +010010536 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010537 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010538 return;
10539
10540 nla_put_failure:
10541 genlmsg_cancel(msg, hdr);
10542 nlmsg_free(msg);
10543
10544}
10545
Johannes Berg04a773a2009-04-19 21:24:32 +020010546void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10547 struct net_device *netdev, const u8 *bssid,
10548 gfp_t gfp)
10549{
10550 struct sk_buff *msg;
10551 void *hdr;
10552
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010553 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010554 if (!msg)
10555 return;
10556
10557 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10558 if (!hdr) {
10559 nlmsg_free(msg);
10560 return;
10561 }
10562
David S. Miller9360ffd2012-03-29 04:41:26 -040010563 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10564 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10565 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10566 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010567
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010568 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010569
Johannes Berg68eb5502013-11-19 15:19:38 +010010570 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010571 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010572 return;
10573
10574 nla_put_failure:
10575 genlmsg_cancel(msg, hdr);
10576 nlmsg_free(msg);
10577}
10578
Johannes Berg947add32013-02-22 22:05:20 +010010579void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10580 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010581{
Johannes Berg947add32013-02-22 22:05:20 +010010582 struct wireless_dev *wdev = dev->ieee80211_ptr;
10583 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010584 struct sk_buff *msg;
10585 void *hdr;
10586
Johannes Berg947add32013-02-22 22:05:20 +010010587 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10588 return;
10589
10590 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10591
Javier Cardonac93b5e72011-04-07 15:08:34 -070010592 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10593 if (!msg)
10594 return;
10595
10596 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10597 if (!hdr) {
10598 nlmsg_free(msg);
10599 return;
10600 }
10601
David S. Miller9360ffd2012-03-29 04:41:26 -040010602 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010603 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10604 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010605 (ie_len && ie &&
10606 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10607 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010608
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010609 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010610
Johannes Berg68eb5502013-11-19 15:19:38 +010010611 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010612 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010613 return;
10614
10615 nla_put_failure:
10616 genlmsg_cancel(msg, hdr);
10617 nlmsg_free(msg);
10618}
Johannes Berg947add32013-02-22 22:05:20 +010010619EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010620
Jouni Malinena3b8b052009-03-27 21:59:49 +020010621void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10622 struct net_device *netdev, const u8 *addr,
10623 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010624 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010625{
10626 struct sk_buff *msg;
10627 void *hdr;
10628
Johannes Berge6d6e342009-07-01 21:26:47 +020010629 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010630 if (!msg)
10631 return;
10632
10633 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10634 if (!hdr) {
10635 nlmsg_free(msg);
10636 return;
10637 }
10638
David S. Miller9360ffd2012-03-29 04:41:26 -040010639 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10640 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10641 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10642 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10643 (key_id != -1 &&
10644 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10645 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10646 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010647
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010648 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010649
Johannes Berg68eb5502013-11-19 15:19:38 +010010650 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010651 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010652 return;
10653
10654 nla_put_failure:
10655 genlmsg_cancel(msg, hdr);
10656 nlmsg_free(msg);
10657}
10658
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010659void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10660 struct ieee80211_channel *channel_before,
10661 struct ieee80211_channel *channel_after)
10662{
10663 struct sk_buff *msg;
10664 void *hdr;
10665 struct nlattr *nl_freq;
10666
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010667 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010668 if (!msg)
10669 return;
10670
10671 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10672 if (!hdr) {
10673 nlmsg_free(msg);
10674 return;
10675 }
10676
10677 /*
10678 * Since we are applying the beacon hint to a wiphy we know its
10679 * wiphy_idx is valid
10680 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010681 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10682 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010683
10684 /* Before */
10685 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10686 if (!nl_freq)
10687 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010688 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010689 goto nla_put_failure;
10690 nla_nest_end(msg, nl_freq);
10691
10692 /* After */
10693 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10694 if (!nl_freq)
10695 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010696 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010697 goto nla_put_failure;
10698 nla_nest_end(msg, nl_freq);
10699
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010700 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010701
Johannes Berg463d0182009-07-14 00:33:35 +020010702 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010703 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010704 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010705 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010706
10707 return;
10708
10709nla_put_failure:
10710 genlmsg_cancel(msg, hdr);
10711 nlmsg_free(msg);
10712}
10713
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010714static void nl80211_send_remain_on_chan_event(
10715 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010716 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010717 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010718 unsigned int duration, gfp_t gfp)
10719{
10720 struct sk_buff *msg;
10721 void *hdr;
10722
10723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10724 if (!msg)
10725 return;
10726
10727 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10728 if (!hdr) {
10729 nlmsg_free(msg);
10730 return;
10731 }
10732
David S. Miller9360ffd2012-03-29 04:41:26 -040010733 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010734 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10735 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010736 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010737 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010738 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10739 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010740 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10741 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010742
David S. Miller9360ffd2012-03-29 04:41:26 -040010743 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10744 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10745 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010746
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010747 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010748
Johannes Berg68eb5502013-11-19 15:19:38 +010010749 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010750 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010751 return;
10752
10753 nla_put_failure:
10754 genlmsg_cancel(msg, hdr);
10755 nlmsg_free(msg);
10756}
10757
Johannes Berg947add32013-02-22 22:05:20 +010010758void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10759 struct ieee80211_channel *chan,
10760 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010761{
Johannes Berg947add32013-02-22 22:05:20 +010010762 struct wiphy *wiphy = wdev->wiphy;
10763 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10764
10765 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010766 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010767 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010768 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010769}
Johannes Berg947add32013-02-22 22:05:20 +010010770EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010771
Johannes Berg947add32013-02-22 22:05:20 +010010772void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10773 struct ieee80211_channel *chan,
10774 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010775{
Johannes Berg947add32013-02-22 22:05:20 +010010776 struct wiphy *wiphy = wdev->wiphy;
10777 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10778
10779 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010780 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010781 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010782}
Johannes Berg947add32013-02-22 22:05:20 +010010783EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010784
Johannes Berg947add32013-02-22 22:05:20 +010010785void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10786 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010787{
Johannes Berg947add32013-02-22 22:05:20 +010010788 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10789 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010790 struct sk_buff *msg;
10791
Johannes Berg947add32013-02-22 22:05:20 +010010792 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10793
Thomas Graf58050fc2012-06-28 03:57:45 +000010794 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010795 if (!msg)
10796 return;
10797
John W. Linville66266b32012-03-15 13:25:41 -040010798 if (nl80211_send_station(msg, 0, 0, 0,
10799 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010800 nlmsg_free(msg);
10801 return;
10802 }
10803
Johannes Berg68eb5502013-11-19 15:19:38 +010010804 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010805 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010806}
Johannes Berg947add32013-02-22 22:05:20 +010010807EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010808
Johannes Berg947add32013-02-22 22:05:20 +010010809void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010810{
Johannes Berg947add32013-02-22 22:05:20 +010010811 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10812 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010813 struct sk_buff *msg;
10814 void *hdr;
10815
Johannes Berg947add32013-02-22 22:05:20 +010010816 trace_cfg80211_del_sta(dev, mac_addr);
10817
Thomas Graf58050fc2012-06-28 03:57:45 +000010818 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010819 if (!msg)
10820 return;
10821
10822 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10823 if (!hdr) {
10824 nlmsg_free(msg);
10825 return;
10826 }
10827
David S. Miller9360ffd2012-03-29 04:41:26 -040010828 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10829 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10830 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010831
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010832 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010833
Johannes Berg68eb5502013-11-19 15:19:38 +010010834 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010835 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010836 return;
10837
10838 nla_put_failure:
10839 genlmsg_cancel(msg, hdr);
10840 nlmsg_free(msg);
10841}
Johannes Berg947add32013-02-22 22:05:20 +010010842EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010843
Johannes Berg947add32013-02-22 22:05:20 +010010844void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10845 enum nl80211_connect_failed_reason reason,
10846 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010847{
Johannes Berg947add32013-02-22 22:05:20 +010010848 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10849 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010850 struct sk_buff *msg;
10851 void *hdr;
10852
10853 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10854 if (!msg)
10855 return;
10856
10857 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10858 if (!hdr) {
10859 nlmsg_free(msg);
10860 return;
10861 }
10862
10863 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10864 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10865 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10866 goto nla_put_failure;
10867
10868 genlmsg_end(msg, hdr);
10869
Johannes Berg68eb5502013-11-19 15:19:38 +010010870 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010871 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010872 return;
10873
10874 nla_put_failure:
10875 genlmsg_cancel(msg, hdr);
10876 nlmsg_free(msg);
10877}
Johannes Berg947add32013-02-22 22:05:20 +010010878EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010879
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010880static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10881 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010882{
10883 struct wireless_dev *wdev = dev->ieee80211_ptr;
10884 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10885 struct sk_buff *msg;
10886 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010887 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010888
Eric W. Biederman15e47302012-09-07 20:12:54 +000010889 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010890 return false;
10891
10892 msg = nlmsg_new(100, gfp);
10893 if (!msg)
10894 return true;
10895
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010896 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010897 if (!hdr) {
10898 nlmsg_free(msg);
10899 return true;
10900 }
10901
David S. Miller9360ffd2012-03-29 04:41:26 -040010902 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10903 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10904 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10905 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010906
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010907 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010908 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010909 return true;
10910
10911 nla_put_failure:
10912 genlmsg_cancel(msg, hdr);
10913 nlmsg_free(msg);
10914 return true;
10915}
10916
Johannes Berg947add32013-02-22 22:05:20 +010010917bool cfg80211_rx_spurious_frame(struct net_device *dev,
10918 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010919{
Johannes Berg947add32013-02-22 22:05:20 +010010920 struct wireless_dev *wdev = dev->ieee80211_ptr;
10921 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010922
Johannes Berg947add32013-02-22 22:05:20 +010010923 trace_cfg80211_rx_spurious_frame(dev, addr);
10924
10925 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10926 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10927 trace_cfg80211_return_bool(false);
10928 return false;
10929 }
10930 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10931 addr, gfp);
10932 trace_cfg80211_return_bool(ret);
10933 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010934}
Johannes Berg947add32013-02-22 22:05:20 +010010935EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10936
10937bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10938 const u8 *addr, gfp_t gfp)
10939{
10940 struct wireless_dev *wdev = dev->ieee80211_ptr;
10941 bool ret;
10942
10943 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10944
10945 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10946 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10947 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10948 trace_cfg80211_return_bool(false);
10949 return false;
10950 }
10951 ret = __nl80211_unexpected_frame(dev,
10952 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10953 addr, gfp);
10954 trace_cfg80211_return_bool(ret);
10955 return ret;
10956}
10957EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010958
Johannes Berg2e161f72010-08-12 15:38:38 +020010959int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010960 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010961 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010962 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010963{
Johannes Berg71bbc992012-06-15 15:30:18 +020010964 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010965 struct sk_buff *msg;
10966 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010967
10968 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10969 if (!msg)
10970 return -ENOMEM;
10971
Johannes Berg2e161f72010-08-12 15:38:38 +020010972 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010973 if (!hdr) {
10974 nlmsg_free(msg);
10975 return -ENOMEM;
10976 }
10977
David S. Miller9360ffd2012-03-29 04:41:26 -040010978 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010979 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10980 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010981 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010982 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10983 (sig_dbm &&
10984 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010985 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10986 (flags &&
10987 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010988 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010989
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010990 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010991
Eric W. Biederman15e47302012-09-07 20:12:54 +000010992 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010993
10994 nla_put_failure:
10995 genlmsg_cancel(msg, hdr);
10996 nlmsg_free(msg);
10997 return -ENOBUFS;
10998}
10999
Johannes Berg947add32013-02-22 22:05:20 +010011000void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
11001 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020011002{
Johannes Berg947add32013-02-22 22:05:20 +010011003 struct wiphy *wiphy = wdev->wiphy;
11004 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020011005 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011006 struct sk_buff *msg;
11007 void *hdr;
11008
Johannes Berg947add32013-02-22 22:05:20 +010011009 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11010
Jouni Malinen026331c2010-02-15 12:53:10 +020011011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11012 if (!msg)
11013 return;
11014
Johannes Berg2e161f72010-08-12 15:38:38 +020011015 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011016 if (!hdr) {
11017 nlmsg_free(msg);
11018 return;
11019 }
11020
David S. Miller9360ffd2012-03-29 04:41:26 -040011021 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011022 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11023 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011024 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011025 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11026 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11027 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11028 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011029
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011030 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011031
Johannes Berg68eb5502013-11-19 15:19:38 +010011032 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011033 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011034 return;
11035
11036 nla_put_failure:
11037 genlmsg_cancel(msg, hdr);
11038 nlmsg_free(msg);
11039}
Johannes Berg947add32013-02-22 22:05:20 +010011040EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011041
Johannes Berg947add32013-02-22 22:05:20 +010011042void cfg80211_cqm_rssi_notify(struct net_device *dev,
11043 enum nl80211_cqm_rssi_threshold_event rssi_event,
11044 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011045{
Johannes Berg947add32013-02-22 22:05:20 +010011046 struct wireless_dev *wdev = dev->ieee80211_ptr;
11047 struct wiphy *wiphy = wdev->wiphy;
11048 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011049 struct sk_buff *msg;
11050 struct nlattr *pinfoattr;
11051 void *hdr;
11052
Johannes Berg947add32013-02-22 22:05:20 +010011053 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11054
Thomas Graf58050fc2012-06-28 03:57:45 +000011055 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011056 if (!msg)
11057 return;
11058
11059 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11060 if (!hdr) {
11061 nlmsg_free(msg);
11062 return;
11063 }
11064
David S. Miller9360ffd2012-03-29 04:41:26 -040011065 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011066 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011067 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011068
11069 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11070 if (!pinfoattr)
11071 goto nla_put_failure;
11072
David S. Miller9360ffd2012-03-29 04:41:26 -040011073 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11074 rssi_event))
11075 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011076
11077 nla_nest_end(msg, pinfoattr);
11078
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011079 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011080
Johannes Berg68eb5502013-11-19 15:19:38 +010011081 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011082 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011083 return;
11084
11085 nla_put_failure:
11086 genlmsg_cancel(msg, hdr);
11087 nlmsg_free(msg);
11088}
Johannes Berg947add32013-02-22 22:05:20 +010011089EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011090
Johannes Berg947add32013-02-22 22:05:20 +010011091static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11092 struct net_device *netdev, const u8 *bssid,
11093 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011094{
11095 struct sk_buff *msg;
11096 struct nlattr *rekey_attr;
11097 void *hdr;
11098
Thomas Graf58050fc2012-06-28 03:57:45 +000011099 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011100 if (!msg)
11101 return;
11102
11103 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11104 if (!hdr) {
11105 nlmsg_free(msg);
11106 return;
11107 }
11108
David S. Miller9360ffd2012-03-29 04:41:26 -040011109 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11110 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11111 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11112 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011113
11114 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11115 if (!rekey_attr)
11116 goto nla_put_failure;
11117
David S. Miller9360ffd2012-03-29 04:41:26 -040011118 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11119 NL80211_REPLAY_CTR_LEN, replay_ctr))
11120 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011121
11122 nla_nest_end(msg, rekey_attr);
11123
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011124 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011125
Johannes Berg68eb5502013-11-19 15:19:38 +010011126 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011127 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011128 return;
11129
11130 nla_put_failure:
11131 genlmsg_cancel(msg, hdr);
11132 nlmsg_free(msg);
11133}
11134
Johannes Berg947add32013-02-22 22:05:20 +010011135void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11136 const u8 *replay_ctr, gfp_t gfp)
11137{
11138 struct wireless_dev *wdev = dev->ieee80211_ptr;
11139 struct wiphy *wiphy = wdev->wiphy;
11140 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11141
11142 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11143 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11144}
11145EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11146
11147static void
11148nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11149 struct net_device *netdev, int index,
11150 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011151{
11152 struct sk_buff *msg;
11153 struct nlattr *attr;
11154 void *hdr;
11155
Thomas Graf58050fc2012-06-28 03:57:45 +000011156 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011157 if (!msg)
11158 return;
11159
11160 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11161 if (!hdr) {
11162 nlmsg_free(msg);
11163 return;
11164 }
11165
David S. Miller9360ffd2012-03-29 04:41:26 -040011166 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11167 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11168 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011169
11170 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11171 if (!attr)
11172 goto nla_put_failure;
11173
David S. Miller9360ffd2012-03-29 04:41:26 -040011174 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11175 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11176 (preauth &&
11177 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11178 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011179
11180 nla_nest_end(msg, attr);
11181
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011182 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011183
Johannes Berg68eb5502013-11-19 15:19:38 +010011184 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011185 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011186 return;
11187
11188 nla_put_failure:
11189 genlmsg_cancel(msg, hdr);
11190 nlmsg_free(msg);
11191}
11192
Johannes Berg947add32013-02-22 22:05:20 +010011193void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11194 const u8 *bssid, bool preauth, gfp_t gfp)
11195{
11196 struct wireless_dev *wdev = dev->ieee80211_ptr;
11197 struct wiphy *wiphy = wdev->wiphy;
11198 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11199
11200 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11201 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11202}
11203EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11204
11205static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11206 struct net_device *netdev,
11207 struct cfg80211_chan_def *chandef,
11208 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011209{
11210 struct sk_buff *msg;
11211 void *hdr;
11212
Thomas Graf58050fc2012-06-28 03:57:45 +000011213 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011214 if (!msg)
11215 return;
11216
11217 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11218 if (!hdr) {
11219 nlmsg_free(msg);
11220 return;
11221 }
11222
Johannes Berg683b6d32012-11-08 21:25:48 +010011223 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11224 goto nla_put_failure;
11225
11226 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011227 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011228
11229 genlmsg_end(msg, hdr);
11230
Johannes Berg68eb5502013-11-19 15:19:38 +010011231 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011232 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011233 return;
11234
11235 nla_put_failure:
11236 genlmsg_cancel(msg, hdr);
11237 nlmsg_free(msg);
11238}
11239
Johannes Berg947add32013-02-22 22:05:20 +010011240void cfg80211_ch_switch_notify(struct net_device *dev,
11241 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011242{
Johannes Berg947add32013-02-22 22:05:20 +010011243 struct wireless_dev *wdev = dev->ieee80211_ptr;
11244 struct wiphy *wiphy = wdev->wiphy;
11245 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11246
Simon Wunderliche487eae2013-11-21 18:19:51 +010011247 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011248
Simon Wunderliche487eae2013-11-21 18:19:51 +010011249 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011250
11251 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011252 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011253 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11254 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011255 return;
Johannes Berg947add32013-02-22 22:05:20 +010011256
Michal Kazior9e0e2962014-01-29 14:22:27 +010011257 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011258 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011259 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011260}
11261EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11262
11263void cfg80211_cqm_txe_notify(struct net_device *dev,
11264 const u8 *peer, u32 num_packets,
11265 u32 rate, u32 intvl, gfp_t gfp)
11266{
11267 struct wireless_dev *wdev = dev->ieee80211_ptr;
11268 struct wiphy *wiphy = wdev->wiphy;
11269 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011270 struct sk_buff *msg;
11271 struct nlattr *pinfoattr;
11272 void *hdr;
11273
11274 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11275 if (!msg)
11276 return;
11277
11278 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11279 if (!hdr) {
11280 nlmsg_free(msg);
11281 return;
11282 }
11283
11284 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011285 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011286 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11287 goto nla_put_failure;
11288
11289 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11290 if (!pinfoattr)
11291 goto nla_put_failure;
11292
11293 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11294 goto nla_put_failure;
11295
11296 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11297 goto nla_put_failure;
11298
11299 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11300 goto nla_put_failure;
11301
11302 nla_nest_end(msg, pinfoattr);
11303
11304 genlmsg_end(msg, hdr);
11305
Johannes Berg68eb5502013-11-19 15:19:38 +010011306 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011307 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011308 return;
11309
11310 nla_put_failure:
11311 genlmsg_cancel(msg, hdr);
11312 nlmsg_free(msg);
11313}
Johannes Berg947add32013-02-22 22:05:20 +010011314EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011315
11316void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011317nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011318 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011319 enum nl80211_radar_event event,
11320 struct net_device *netdev, gfp_t gfp)
11321{
11322 struct sk_buff *msg;
11323 void *hdr;
11324
11325 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11326 if (!msg)
11327 return;
11328
11329 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11330 if (!hdr) {
11331 nlmsg_free(msg);
11332 return;
11333 }
11334
11335 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11336 goto nla_put_failure;
11337
11338 /* NOP and radar events don't need a netdev parameter */
11339 if (netdev) {
11340 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11341
11342 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11343 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11344 goto nla_put_failure;
11345 }
11346
11347 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11348 goto nla_put_failure;
11349
11350 if (nl80211_send_chandef(msg, chandef))
11351 goto nla_put_failure;
11352
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011353 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011354
Johannes Berg68eb5502013-11-19 15:19:38 +010011355 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011356 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011357 return;
11358
11359 nla_put_failure:
11360 genlmsg_cancel(msg, hdr);
11361 nlmsg_free(msg);
11362}
11363
Johannes Berg947add32013-02-22 22:05:20 +010011364void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11365 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011366{
Johannes Berg947add32013-02-22 22:05:20 +010011367 struct wireless_dev *wdev = dev->ieee80211_ptr;
11368 struct wiphy *wiphy = wdev->wiphy;
11369 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011370 struct sk_buff *msg;
11371 struct nlattr *pinfoattr;
11372 void *hdr;
11373
Johannes Berg947add32013-02-22 22:05:20 +010011374 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11375
Thomas Graf58050fc2012-06-28 03:57:45 +000011376 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011377 if (!msg)
11378 return;
11379
11380 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11381 if (!hdr) {
11382 nlmsg_free(msg);
11383 return;
11384 }
11385
David S. Miller9360ffd2012-03-29 04:41:26 -040011386 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011387 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011388 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11389 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011390
11391 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11392 if (!pinfoattr)
11393 goto nla_put_failure;
11394
David S. Miller9360ffd2012-03-29 04:41:26 -040011395 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11396 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011397
11398 nla_nest_end(msg, pinfoattr);
11399
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011400 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011401
Johannes Berg68eb5502013-11-19 15:19:38 +010011402 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011403 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011404 return;
11405
11406 nla_put_failure:
11407 genlmsg_cancel(msg, hdr);
11408 nlmsg_free(msg);
11409}
Johannes Berg947add32013-02-22 22:05:20 +010011410EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011411
Johannes Berg7f6cf312011-11-04 11:18:15 +010011412void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11413 u64 cookie, bool acked, gfp_t gfp)
11414{
11415 struct wireless_dev *wdev = dev->ieee80211_ptr;
11416 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11417 struct sk_buff *msg;
11418 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011419
Beni Lev4ee3e062012-08-27 12:49:39 +030011420 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11421
Thomas Graf58050fc2012-06-28 03:57:45 +000011422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011423
Johannes Berg7f6cf312011-11-04 11:18:15 +010011424 if (!msg)
11425 return;
11426
11427 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11428 if (!hdr) {
11429 nlmsg_free(msg);
11430 return;
11431 }
11432
David S. Miller9360ffd2012-03-29 04:41:26 -040011433 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11434 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11435 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11436 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11437 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11438 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011439
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011440 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011441
Johannes Berg68eb5502013-11-19 15:19:38 +010011442 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011443 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011444 return;
11445
11446 nla_put_failure:
11447 genlmsg_cancel(msg, hdr);
11448 nlmsg_free(msg);
11449}
11450EXPORT_SYMBOL(cfg80211_probe_status);
11451
Johannes Berg5e760232011-11-04 11:18:17 +010011452void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11453 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011454 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010011455{
11456 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11457 struct sk_buff *msg;
11458 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011459 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010011460
Beni Lev4ee3e062012-08-27 12:49:39 +030011461 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11462
Ben Greear37c73b52012-10-26 14:49:25 -070011463 spin_lock_bh(&rdev->beacon_registrations_lock);
11464 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11465 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11466 if (!msg) {
11467 spin_unlock_bh(&rdev->beacon_registrations_lock);
11468 return;
11469 }
Johannes Berg5e760232011-11-04 11:18:17 +010011470
Ben Greear37c73b52012-10-26 14:49:25 -070011471 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11472 if (!hdr)
11473 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010011474
Ben Greear37c73b52012-10-26 14:49:25 -070011475 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11476 (freq &&
11477 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11478 (sig_dbm &&
11479 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11480 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11481 goto nla_put_failure;
11482
11483 genlmsg_end(msg, hdr);
11484
11485 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010011486 }
Ben Greear37c73b52012-10-26 14:49:25 -070011487 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011488 return;
11489
11490 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011491 spin_unlock_bh(&rdev->beacon_registrations_lock);
11492 if (hdr)
11493 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010011494 nlmsg_free(msg);
11495}
11496EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11497
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011498#ifdef CONFIG_PM
11499void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11500 struct cfg80211_wowlan_wakeup *wakeup,
11501 gfp_t gfp)
11502{
11503 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11504 struct sk_buff *msg;
11505 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011506 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011507
11508 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11509
11510 if (wakeup)
11511 size += wakeup->packet_present_len;
11512
11513 msg = nlmsg_new(size, gfp);
11514 if (!msg)
11515 return;
11516
11517 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11518 if (!hdr)
11519 goto free_msg;
11520
11521 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11522 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11523 goto free_msg;
11524
11525 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11526 wdev->netdev->ifindex))
11527 goto free_msg;
11528
11529 if (wakeup) {
11530 struct nlattr *reasons;
11531
11532 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011533 if (!reasons)
11534 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011535
11536 if (wakeup->disconnect &&
11537 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11538 goto free_msg;
11539 if (wakeup->magic_pkt &&
11540 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11541 goto free_msg;
11542 if (wakeup->gtk_rekey_failure &&
11543 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11544 goto free_msg;
11545 if (wakeup->eap_identity_req &&
11546 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11547 goto free_msg;
11548 if (wakeup->four_way_handshake &&
11549 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11550 goto free_msg;
11551 if (wakeup->rfkill_release &&
11552 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11553 goto free_msg;
11554
11555 if (wakeup->pattern_idx >= 0 &&
11556 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11557 wakeup->pattern_idx))
11558 goto free_msg;
11559
Johannes Bergae917c92013-10-25 11:05:22 +020011560 if (wakeup->tcp_match &&
11561 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11562 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011563
Johannes Bergae917c92013-10-25 11:05:22 +020011564 if (wakeup->tcp_connlost &&
11565 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11566 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011567
Johannes Bergae917c92013-10-25 11:05:22 +020011568 if (wakeup->tcp_nomoretokens &&
11569 nla_put_flag(msg,
11570 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11571 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011572
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011573 if (wakeup->packet) {
11574 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11575 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11576
11577 if (!wakeup->packet_80211) {
11578 pkt_attr =
11579 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11580 len_attr =
11581 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11582 }
11583
11584 if (wakeup->packet_len &&
11585 nla_put_u32(msg, len_attr, wakeup->packet_len))
11586 goto free_msg;
11587
11588 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11589 wakeup->packet))
11590 goto free_msg;
11591 }
11592
11593 nla_nest_end(msg, reasons);
11594 }
11595
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011596 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011597
Johannes Berg68eb5502013-11-19 15:19:38 +010011598 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011599 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011600 return;
11601
11602 free_msg:
11603 nlmsg_free(msg);
11604}
11605EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11606#endif
11607
Jouni Malinen3475b092012-11-16 22:49:57 +020011608void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11609 enum nl80211_tdls_operation oper,
11610 u16 reason_code, gfp_t gfp)
11611{
11612 struct wireless_dev *wdev = dev->ieee80211_ptr;
11613 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11614 struct sk_buff *msg;
11615 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011616
11617 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11618 reason_code);
11619
11620 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11621 if (!msg)
11622 return;
11623
11624 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11625 if (!hdr) {
11626 nlmsg_free(msg);
11627 return;
11628 }
11629
11630 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11631 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11632 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11633 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11634 (reason_code > 0 &&
11635 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11636 goto nla_put_failure;
11637
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011638 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011639
Johannes Berg68eb5502013-11-19 15:19:38 +010011640 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011641 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011642 return;
11643
11644 nla_put_failure:
11645 genlmsg_cancel(msg, hdr);
11646 nlmsg_free(msg);
11647}
11648EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11649
Jouni Malinen026331c2010-02-15 12:53:10 +020011650static int nl80211_netlink_notify(struct notifier_block * nb,
11651 unsigned long state,
11652 void *_notify)
11653{
11654 struct netlink_notify *notify = _notify;
11655 struct cfg80211_registered_device *rdev;
11656 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011657 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011658
11659 if (state != NETLINK_URELEASE)
11660 return NOTIFY_DONE;
11661
11662 rcu_read_lock();
11663
Johannes Berg5e760232011-11-04 11:18:17 +010011664 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010011665 bool schedule_destroy_work = false;
11666
11667 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000011668 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011669
Johannes Berg78f22b62014-03-24 17:57:27 +010011670 if (wdev->owner_nlportid == notify->portid)
11671 schedule_destroy_work = true;
11672 }
11673
Ben Greear37c73b52012-10-26 14:49:25 -070011674 spin_lock_bh(&rdev->beacon_registrations_lock);
11675 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11676 list) {
11677 if (reg->nlportid == notify->portid) {
11678 list_del(&reg->list);
11679 kfree(reg);
11680 break;
11681 }
11682 }
11683 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010011684
11685 if (schedule_destroy_work) {
11686 struct cfg80211_iface_destroy *destroy;
11687
11688 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
11689 if (destroy) {
11690 destroy->nlportid = notify->portid;
11691 spin_lock(&rdev->destroy_list_lock);
11692 list_add(&destroy->list, &rdev->destroy_list);
11693 spin_unlock(&rdev->destroy_list_lock);
11694 schedule_work(&rdev->destroy_work);
11695 }
11696 }
Johannes Berg5e760232011-11-04 11:18:17 +010011697 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011698
11699 rcu_read_unlock();
11700
11701 return NOTIFY_DONE;
11702}
11703
11704static struct notifier_block nl80211_netlink_notifier = {
11705 .notifier_call = nl80211_netlink_notify,
11706};
11707
Jouni Malinen355199e2013-02-27 17:14:27 +020011708void cfg80211_ft_event(struct net_device *netdev,
11709 struct cfg80211_ft_event_params *ft_event)
11710{
11711 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11712 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11713 struct sk_buff *msg;
11714 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011715
11716 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11717
11718 if (!ft_event->target_ap)
11719 return;
11720
11721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11722 if (!msg)
11723 return;
11724
11725 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011726 if (!hdr)
11727 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011728
Johannes Bergae917c92013-10-25 11:05:22 +020011729 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11730 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11731 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11732 goto out;
11733
11734 if (ft_event->ies &&
11735 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11736 goto out;
11737 if (ft_event->ric_ies &&
11738 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11739 ft_event->ric_ies))
11740 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011741
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011742 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011743
Johannes Berg68eb5502013-11-19 15:19:38 +010011744 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011745 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011746 return;
11747 out:
11748 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011749}
11750EXPORT_SYMBOL(cfg80211_ft_event);
11751
Arend van Spriel5de17982013-04-18 15:49:00 +020011752void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11753{
11754 struct cfg80211_registered_device *rdev;
11755 struct sk_buff *msg;
11756 void *hdr;
11757 u32 nlportid;
11758
11759 rdev = wiphy_to_dev(wdev->wiphy);
11760 if (!rdev->crit_proto_nlportid)
11761 return;
11762
11763 nlportid = rdev->crit_proto_nlportid;
11764 rdev->crit_proto_nlportid = 0;
11765
11766 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11767 if (!msg)
11768 return;
11769
11770 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11771 if (!hdr)
11772 goto nla_put_failure;
11773
11774 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11775 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11776 goto nla_put_failure;
11777
11778 genlmsg_end(msg, hdr);
11779
11780 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11781 return;
11782
11783 nla_put_failure:
11784 if (hdr)
11785 genlmsg_cancel(msg, hdr);
11786 nlmsg_free(msg);
11787
11788}
11789EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11790
Johannes Berg348baf02014-01-24 14:06:29 +010011791void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11792{
11793 struct wiphy *wiphy = wdev->wiphy;
11794 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11795 struct sk_buff *msg;
11796 void *hdr;
11797
11798 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11799 if (!msg)
11800 return;
11801
11802 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11803 if (!hdr)
11804 goto out;
11805
11806 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11807 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11808 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11809 goto out;
11810
11811 genlmsg_end(msg, hdr);
11812
11813 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11814 NL80211_MCGRP_MLME, GFP_KERNEL);
11815 return;
11816 out:
11817 nlmsg_free(msg);
11818}
11819
Johannes Berg55682962007-09-20 13:09:35 -040011820/* initialisation/exit functions */
11821
11822int nl80211_init(void)
11823{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011824 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011825
Johannes Berg2a94fe42013-11-19 15:19:39 +010011826 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11827 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011828 if (err)
11829 return err;
11830
Jouni Malinen026331c2010-02-15 12:53:10 +020011831 err = netlink_register_notifier(&nl80211_netlink_notifier);
11832 if (err)
11833 goto err_out;
11834
Johannes Berg55682962007-09-20 13:09:35 -040011835 return 0;
11836 err_out:
11837 genl_unregister_family(&nl80211_fam);
11838 return err;
11839}
11840
11841void nl80211_exit(void)
11842{
Jouni Malinen026331c2010-02-15 12:53:10 +020011843 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011844 genl_unregister_family(&nl80211_fam);
11845}