blob: 2c38b28a85b9b1d800caaf617be18f06517b63cd [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 Malinendc6382ce2009-05-06 22:09:37 +0300290 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300291 [NL80211_ATTR_STA_FLAGS2] = {
292 .len = sizeof(struct nl80211_sta_flag_update),
293 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300294 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300295 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
296 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200297 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
299 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200300 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100301 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100302 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
303 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100304 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
305 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200306 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200307 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
308 .len = IEEE80211_MAX_DATA_LEN },
309 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200310 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200311 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300312 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200313 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300314 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
315 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200316 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900317 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100319 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100320 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100321 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200322 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700323 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300324 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200325 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200326 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300327 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300328 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
329 .len = IEEE80211_MAX_DATA_LEN },
330 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530332 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300333 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530334 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300335 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
336 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
337 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
339 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100340 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200341 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700343 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800344 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
346 .len = NL80211_HT_CAPABILITY_LEN
347 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100348 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530349 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530350 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200351 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700352 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300353 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000354 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700355 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100356 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
357 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530358 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
359 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200360 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
361 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100362 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100363 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
364 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
365 .len = NL80211_VHT_CAPABILITY_LEN,
366 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200367 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
368 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
369 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300370 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200371 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
372 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100379 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100380 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
381 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
382 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800383 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
384 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200385 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
386 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530387 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400388};
389
Johannes Berge31b8212010-10-05 19:39:30 +0200390/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000391static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200392 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200393 [NL80211_KEY_IDX] = { .type = NLA_U8 },
394 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200395 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200396 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
397 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200398 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100399 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
400};
401
402/* policy for the key default flags */
403static const struct nla_policy
404nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
405 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
406 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200407};
408
Johannes Bergff1b6e62011-05-04 15:37:28 +0200409/* policy for WoWLAN attributes */
410static const struct nla_policy
411nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
412 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
413 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200416 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
417 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
418 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100420 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
421};
422
423static const struct nla_policy
424nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
425 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
426 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
427 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
428 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
429 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
430 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
432 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
433 },
434 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
435 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
436 },
437 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
438 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
439 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200440};
441
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700442/* policy for coalesce rule attributes */
443static const struct nla_policy
444nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
445 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
446 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
447 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
448};
449
Johannes Berge5497d72011-07-05 16:35:40 +0200450/* policy for GTK rekey offload attributes */
451static const struct nla_policy
452nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
453 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
454 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
455 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
456};
457
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300458static const struct nla_policy
459nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200460 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300461 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700462 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300463};
464
Johannes Berg97990a02013-04-19 01:02:55 +0200465static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
466 struct netlink_callback *cb,
467 struct cfg80211_registered_device **rdev,
468 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100469{
Johannes Berg67748892010-10-04 21:14:06 +0200470 int err;
471
Johannes Berg67748892010-10-04 21:14:06 +0200472 rtnl_lock();
473
Johannes Berg97990a02013-04-19 01:02:55 +0200474 if (!cb->args[0]) {
475 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
476 nl80211_fam.attrbuf, nl80211_fam.maxattr,
477 nl80211_policy);
478 if (err)
479 goto out_unlock;
480
481 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
482 nl80211_fam.attrbuf);
483 if (IS_ERR(*wdev)) {
484 err = PTR_ERR(*wdev);
485 goto out_unlock;
486 }
487 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200488 /* 0 is the first index - add 1 to parse only once */
489 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200490 cb->args[1] = (*wdev)->identifier;
491 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200492 /* subtract the 1 again here */
493 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200494 struct wireless_dev *tmp;
495
496 if (!wiphy) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
500 *rdev = wiphy_to_dev(wiphy);
501 *wdev = NULL;
502
Johannes Berg97990a02013-04-19 01:02:55 +0200503 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
504 if (tmp->identifier == cb->args[1]) {
505 *wdev = tmp;
506 break;
507 }
508 }
Johannes Berg97990a02013-04-19 01:02:55 +0200509
510 if (!*wdev) {
511 err = -ENODEV;
512 goto out_unlock;
513 }
Johannes Berg67748892010-10-04 21:14:06 +0200514 }
515
Johannes Berg67748892010-10-04 21:14:06 +0200516 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200517 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200518 rtnl_unlock();
519 return err;
520}
521
Johannes Berg97990a02013-04-19 01:02:55 +0200522static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200523{
Johannes Berg67748892010-10-04 21:14:06 +0200524 rtnl_unlock();
525}
526
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100527/* IE validation */
528static bool is_valid_ie_attr(const struct nlattr *attr)
529{
530 const u8 *pos;
531 int len;
532
533 if (!attr)
534 return true;
535
536 pos = nla_data(attr);
537 len = nla_len(attr);
538
539 while (len) {
540 u8 elemlen;
541
542 if (len < 2)
543 return false;
544 len -= 2;
545
546 elemlen = pos[1];
547 if (elemlen > len)
548 return false;
549
550 len -= elemlen;
551 pos += 2 + elemlen;
552 }
553
554 return true;
555}
556
Johannes Berg55682962007-09-20 13:09:35 -0400557/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000558static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400559 int flags, u8 cmd)
560{
561 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000562 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400563}
564
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400565static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100566 struct ieee80211_channel *chan,
567 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400568{
David S. Miller9360ffd2012-03-29 04:41:26 -0400569 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
570 chan->center_freq))
571 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400572
David S. Miller9360ffd2012-03-29 04:41:26 -0400573 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
574 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
575 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200576 if (chan->flags & IEEE80211_CHAN_NO_IR) {
577 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
578 goto nla_put_failure;
579 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
580 goto nla_put_failure;
581 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100582 if (chan->flags & IEEE80211_CHAN_RADAR) {
583 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
584 goto nla_put_failure;
585 if (large) {
586 u32 time;
587
588 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
589
590 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
591 chan->dfs_state))
592 goto nla_put_failure;
593 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
594 time))
595 goto nla_put_failure;
596 }
597 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400598
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100599 if (large) {
600 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
602 goto nla_put_failure;
603 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
604 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
605 goto nla_put_failure;
606 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
607 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
608 goto nla_put_failure;
609 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
610 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
611 goto nla_put_failure;
612 }
613
David S. Miller9360ffd2012-03-29 04:41:26 -0400614 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
615 DBM_TO_MBM(chan->max_power)))
616 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400617
618 return 0;
619
620 nla_put_failure:
621 return -ENOBUFS;
622}
623
Johannes Berg55682962007-09-20 13:09:35 -0400624/* netlink command implementations */
625
Johannes Bergb9454e82009-07-08 13:29:08 +0200626struct key_parse {
627 struct key_params p;
628 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200629 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200630 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100631 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200632};
633
634static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
635{
636 struct nlattr *tb[NL80211_KEY_MAX + 1];
637 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
638 nl80211_key_policy);
639 if (err)
640 return err;
641
642 k->def = !!tb[NL80211_KEY_DEFAULT];
643 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
644
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (k->def) {
646 k->def_uni = true;
647 k->def_multi = true;
648 }
649 if (k->defmgmt)
650 k->def_multi = true;
651
Johannes Bergb9454e82009-07-08 13:29:08 +0200652 if (tb[NL80211_KEY_IDX])
653 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
654
655 if (tb[NL80211_KEY_DATA]) {
656 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
657 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
658 }
659
660 if (tb[NL80211_KEY_SEQ]) {
661 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
662 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
663 }
664
665 if (tb[NL80211_KEY_CIPHER])
666 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
667
Johannes Berge31b8212010-10-05 19:39:30 +0200668 if (tb[NL80211_KEY_TYPE]) {
669 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
670 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
671 return -EINVAL;
672 }
673
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100674 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
675 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100676 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
677 tb[NL80211_KEY_DEFAULT_TYPES],
678 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100679 if (err)
680 return err;
681
682 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
683 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
684 }
685
Johannes Bergb9454e82009-07-08 13:29:08 +0200686 return 0;
687}
688
689static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
690{
691 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
692 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
693 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
694 }
695
696 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
697 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
698 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
699 }
700
701 if (info->attrs[NL80211_ATTR_KEY_IDX])
702 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
703
704 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
705 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
706
707 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
708 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
709
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100710 if (k->def) {
711 k->def_uni = true;
712 k->def_multi = true;
713 }
714 if (k->defmgmt)
715 k->def_multi = true;
716
Johannes Berge31b8212010-10-05 19:39:30 +0200717 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
718 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
719 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
720 return -EINVAL;
721 }
722
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
724 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
725 int err = nla_parse_nested(
726 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
727 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
728 nl80211_key_default_policy);
729 if (err)
730 return err;
731
732 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
733 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
734 }
735
Johannes Bergb9454e82009-07-08 13:29:08 +0200736 return 0;
737}
738
739static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
740{
741 int err;
742
743 memset(k, 0, sizeof(*k));
744 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200745 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200746
747 if (info->attrs[NL80211_ATTR_KEY])
748 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
749 else
750 err = nl80211_parse_key_old(info, k);
751
752 if (err)
753 return err;
754
755 if (k->def && k->defmgmt)
756 return -EINVAL;
757
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100758 if (k->defmgmt) {
759 if (k->def_uni || !k->def_multi)
760 return -EINVAL;
761 }
762
Johannes Bergb9454e82009-07-08 13:29:08 +0200763 if (k->idx != -1) {
764 if (k->defmgmt) {
765 if (k->idx < 4 || k->idx > 5)
766 return -EINVAL;
767 } else if (k->def) {
768 if (k->idx < 0 || k->idx > 3)
769 return -EINVAL;
770 } else {
771 if (k->idx < 0 || k->idx > 5)
772 return -EINVAL;
773 }
774 }
775
776 return 0;
777}
778
Johannes Bergfffd0932009-07-08 14:22:54 +0200779static struct cfg80211_cached_keys *
780nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530781 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200782{
783 struct key_parse parse;
784 struct nlattr *key;
785 struct cfg80211_cached_keys *result;
786 int rem, err, def = 0;
787
788 result = kzalloc(sizeof(*result), GFP_KERNEL);
789 if (!result)
790 return ERR_PTR(-ENOMEM);
791
792 result->def = -1;
793 result->defmgmt = -1;
794
795 nla_for_each_nested(key, keys, rem) {
796 memset(&parse, 0, sizeof(parse));
797 parse.idx = -1;
798
799 err = nl80211_parse_key_new(key, &parse);
800 if (err)
801 goto error;
802 err = -EINVAL;
803 if (!parse.p.key)
804 goto error;
805 if (parse.idx < 0 || parse.idx > 4)
806 goto error;
807 if (parse.def) {
808 if (def)
809 goto error;
810 def = 1;
811 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100812 if (!parse.def_uni || !parse.def_multi)
813 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200814 } else if (parse.defmgmt)
815 goto error;
816 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200817 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 if (err)
819 goto error;
820 result->params[parse.idx].cipher = parse.p.cipher;
821 result->params[parse.idx].key_len = parse.p.key_len;
822 result->params[parse.idx].key = result->data[parse.idx];
823 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530824
825 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
826 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
827 if (no_ht)
828 *no_ht = true;
829 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200830 }
831
832 return result;
833 error:
834 kfree(result);
835 return ERR_PTR(err);
836}
837
838static int nl80211_key_allowed(struct wireless_dev *wdev)
839{
840 ASSERT_WDEV_LOCK(wdev);
841
Johannes Bergfffd0932009-07-08 14:22:54 +0200842 switch (wdev->iftype) {
843 case NL80211_IFTYPE_AP:
844 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200845 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700846 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200847 break;
848 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200850 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200851 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200852 return -ENOLINK;
853 break;
854 default:
855 return -EINVAL;
856 }
857
858 return 0;
859}
860
Jouni Malinen664834d2014-01-15 00:01:44 +0200861static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
862 struct nlattr *tb)
863{
864 struct ieee80211_channel *chan;
865
866 if (tb == NULL)
867 return NULL;
868 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
869 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
870 return NULL;
871 return chan;
872}
873
Johannes Berg7527a782011-05-13 10:58:57 +0200874static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
875{
876 struct nlattr *nl_modes = nla_nest_start(msg, attr);
877 int i;
878
879 if (!nl_modes)
880 goto nla_put_failure;
881
882 i = 0;
883 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if ((ifmodes & 1) && nla_put_flag(msg, i))
885 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200886 ifmodes >>= 1;
887 i++;
888 }
889
890 nla_nest_end(msg, nl_modes);
891 return 0;
892
893nla_put_failure:
894 return -ENOBUFS;
895}
896
897static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100898 struct sk_buff *msg,
899 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200900{
901 struct nlattr *nl_combis;
902 int i, j;
903
904 nl_combis = nla_nest_start(msg,
905 NL80211_ATTR_INTERFACE_COMBINATIONS);
906 if (!nl_combis)
907 goto nla_put_failure;
908
909 for (i = 0; i < wiphy->n_iface_combinations; i++) {
910 const struct ieee80211_iface_combination *c;
911 struct nlattr *nl_combi, *nl_limits;
912
913 c = &wiphy->iface_combinations[i];
914
915 nl_combi = nla_nest_start(msg, i + 1);
916 if (!nl_combi)
917 goto nla_put_failure;
918
919 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
920 if (!nl_limits)
921 goto nla_put_failure;
922
923 for (j = 0; j < c->n_limits; j++) {
924 struct nlattr *nl_limit;
925
926 nl_limit = nla_nest_start(msg, j + 1);
927 if (!nl_limit)
928 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400929 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
930 c->limits[j].max))
931 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200932 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
933 c->limits[j].types))
934 goto nla_put_failure;
935 nla_nest_end(msg, nl_limit);
936 }
937
938 nla_nest_end(msg, nl_limits);
939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (c->beacon_int_infra_match &&
941 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
942 goto nla_put_failure;
943 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
944 c->num_different_channels) ||
945 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
946 c->max_interfaces))
947 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100948 if (large &&
949 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
950 c->radar_detect_widths))
951 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200952
953 nla_nest_end(msg, nl_combi);
954 }
955
956 nla_nest_end(msg, nl_combis);
957
958 return 0;
959nla_put_failure:
960 return -ENOBUFS;
961}
962
Johannes Berg3713b4e2013-02-14 16:19:38 +0100963#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100964static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
965 struct sk_buff *msg)
966{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200967 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100968 struct nlattr *nl_tcp;
969
970 if (!tcp)
971 return 0;
972
973 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
974 if (!nl_tcp)
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
978 tcp->data_payload_max))
979 return -ENOBUFS;
980
981 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
982 tcp->data_payload_max))
983 return -ENOBUFS;
984
985 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
986 return -ENOBUFS;
987
988 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
989 sizeof(*tcp->tok), tcp->tok))
990 return -ENOBUFS;
991
992 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
993 tcp->data_interval_max))
994 return -ENOBUFS;
995
996 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
997 tcp->wake_payload_max))
998 return -ENOBUFS;
999
1000 nla_nest_end(msg, nl_tcp);
1001 return 0;
1002}
1003
Johannes Berg3713b4e2013-02-14 16:19:38 +01001004static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001005 struct cfg80211_registered_device *dev,
1006 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001007{
1008 struct nlattr *nl_wowlan;
1009
Johannes Berg964dc9e2013-06-03 17:25:34 +02001010 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001011 return 0;
1012
1013 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1014 if (!nl_wowlan)
1015 return -ENOBUFS;
1016
Johannes Berg964dc9e2013-06-03 17:25:34 +02001017 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001018 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001019 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001020 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001023 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001024 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001025 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001026 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001027 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001028 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001029 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001031 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1033 return -ENOBUFS;
1034
Johannes Berg964dc9e2013-06-03 17:25:34 +02001035 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001036 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001037 .max_patterns = dev->wiphy.wowlan->n_patterns,
1038 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1039 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1040 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041 };
1042
1043 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1044 sizeof(pat), &pat))
1045 return -ENOBUFS;
1046 }
1047
Johannes Bergb56cf722013-02-20 01:02:38 +01001048 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1049 return -ENOBUFS;
1050
Johannes Berg3713b4e2013-02-14 16:19:38 +01001051 nla_nest_end(msg, nl_wowlan);
1052
1053 return 0;
1054}
1055#endif
1056
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001057static int nl80211_send_coalesce(struct sk_buff *msg,
1058 struct cfg80211_registered_device *dev)
1059{
1060 struct nl80211_coalesce_rule_support rule;
1061
1062 if (!dev->wiphy.coalesce)
1063 return 0;
1064
1065 rule.max_rules = dev->wiphy.coalesce->n_rules;
1066 rule.max_delay = dev->wiphy.coalesce->max_delay;
1067 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1068 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1069 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1070 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1071
1072 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1073 return -ENOBUFS;
1074
1075 return 0;
1076}
1077
Johannes Berg3713b4e2013-02-14 16:19:38 +01001078static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1079 struct ieee80211_supported_band *sband)
1080{
1081 struct nlattr *nl_rates, *nl_rate;
1082 struct ieee80211_rate *rate;
1083 int i;
1084
1085 /* add HT info */
1086 if (sband->ht_cap.ht_supported &&
1087 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1088 sizeof(sband->ht_cap.mcs),
1089 &sband->ht_cap.mcs) ||
1090 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1091 sband->ht_cap.cap) ||
1092 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1093 sband->ht_cap.ampdu_factor) ||
1094 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1095 sband->ht_cap.ampdu_density)))
1096 return -ENOBUFS;
1097
1098 /* add VHT info */
1099 if (sband->vht_cap.vht_supported &&
1100 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1101 sizeof(sband->vht_cap.vht_mcs),
1102 &sband->vht_cap.vht_mcs) ||
1103 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1104 sband->vht_cap.cap)))
1105 return -ENOBUFS;
1106
1107 /* add bitrates */
1108 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1109 if (!nl_rates)
1110 return -ENOBUFS;
1111
1112 for (i = 0; i < sband->n_bitrates; i++) {
1113 nl_rate = nla_nest_start(msg, i);
1114 if (!nl_rate)
1115 return -ENOBUFS;
1116
1117 rate = &sband->bitrates[i];
1118 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1119 rate->bitrate))
1120 return -ENOBUFS;
1121 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1122 nla_put_flag(msg,
1123 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1124 return -ENOBUFS;
1125
1126 nla_nest_end(msg, nl_rate);
1127 }
1128
1129 nla_nest_end(msg, nl_rates);
1130
1131 return 0;
1132}
1133
1134static int
1135nl80211_send_mgmt_stypes(struct sk_buff *msg,
1136 const struct ieee80211_txrx_stypes *mgmt_stypes)
1137{
1138 u16 stypes;
1139 struct nlattr *nl_ftypes, *nl_ifs;
1140 enum nl80211_iftype ift;
1141 int i;
1142
1143 if (!mgmt_stypes)
1144 return 0;
1145
1146 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1147 if (!nl_ifs)
1148 return -ENOBUFS;
1149
1150 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1151 nl_ftypes = nla_nest_start(msg, ift);
1152 if (!nl_ftypes)
1153 return -ENOBUFS;
1154 i = 0;
1155 stypes = mgmt_stypes[ift].tx;
1156 while (stypes) {
1157 if ((stypes & 1) &&
1158 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1159 (i << 4) | IEEE80211_FTYPE_MGMT))
1160 return -ENOBUFS;
1161 stypes >>= 1;
1162 i++;
1163 }
1164 nla_nest_end(msg, nl_ftypes);
1165 }
1166
1167 nla_nest_end(msg, nl_ifs);
1168
1169 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1170 if (!nl_ifs)
1171 return -ENOBUFS;
1172
1173 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1174 nl_ftypes = nla_nest_start(msg, ift);
1175 if (!nl_ftypes)
1176 return -ENOBUFS;
1177 i = 0;
1178 stypes = mgmt_stypes[ift].rx;
1179 while (stypes) {
1180 if ((stypes & 1) &&
1181 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1182 (i << 4) | IEEE80211_FTYPE_MGMT))
1183 return -ENOBUFS;
1184 stypes >>= 1;
1185 i++;
1186 }
1187 nla_nest_end(msg, nl_ftypes);
1188 }
1189 nla_nest_end(msg, nl_ifs);
1190
1191 return 0;
1192}
1193
Johannes Berg86e8cf92013-06-19 10:57:22 +02001194struct nl80211_dump_wiphy_state {
1195 s64 filter_wiphy;
1196 long start;
1197 long split_start, band_start, chan_start;
1198 bool split;
1199};
1200
Johannes Berg3713b4e2013-02-14 16:19:38 +01001201static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1202 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001203 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001204{
1205 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001206 struct nlattr *nl_bands, *nl_band;
1207 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001208 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001209 enum ieee80211_band band;
1210 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001211 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001212 const struct ieee80211_txrx_stypes *mgmt_stypes =
1213 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001214 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001215
Eric W. Biederman15e47302012-09-07 20:12:54 +00001216 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001217 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 return -ENOBUFS;
1219
Johannes Berg86e8cf92013-06-19 10:57:22 +02001220 if (WARN_ON(!state))
1221 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001222
David S. Miller9360ffd2012-03-29 04:41:26 -04001223 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001224 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1225 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001226 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001227 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001228 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001229
Johannes Berg86e8cf92013-06-19 10:57:22 +02001230 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001231 case 0:
1232 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1233 dev->wiphy.retry_short) ||
1234 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1235 dev->wiphy.retry_long) ||
1236 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1237 dev->wiphy.frag_threshold) ||
1238 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1239 dev->wiphy.rts_threshold) ||
1240 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1241 dev->wiphy.coverage_class) ||
1242 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1243 dev->wiphy.max_scan_ssids) ||
1244 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1245 dev->wiphy.max_sched_scan_ssids) ||
1246 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1247 dev->wiphy.max_scan_ie_len) ||
1248 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1249 dev->wiphy.max_sched_scan_ie_len) ||
1250 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1251 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001252 goto nla_put_failure;
1253
Johannes Berg3713b4e2013-02-14 16:19:38 +01001254 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1255 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1256 goto nla_put_failure;
1257 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1258 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1259 goto nla_put_failure;
1260 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1261 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1262 goto nla_put_failure;
1263 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1264 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1265 goto nla_put_failure;
1266 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1267 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1268 goto nla_put_failure;
1269 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1270 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001271 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001272 state->split_start++;
1273 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001274 break;
1275 case 1:
1276 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1277 sizeof(u32) * dev->wiphy.n_cipher_suites,
1278 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001279 goto nla_put_failure;
1280
Johannes Berg3713b4e2013-02-14 16:19:38 +01001281 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1282 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001283 goto nla_put_failure;
1284
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1286 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1287 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001288
Johannes Berg3713b4e2013-02-14 16:19:38 +01001289 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1290 dev->wiphy.available_antennas_tx) ||
1291 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1292 dev->wiphy.available_antennas_rx))
1293 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001294
Johannes Berg3713b4e2013-02-14 16:19:38 +01001295 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1296 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1297 dev->wiphy.probe_resp_offload))
1298 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001299
Johannes Berg3713b4e2013-02-14 16:19:38 +01001300 if ((dev->wiphy.available_antennas_tx ||
1301 dev->wiphy.available_antennas_rx) &&
1302 dev->ops->get_antenna) {
1303 u32 tx_ant = 0, rx_ant = 0;
1304 int res;
1305 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1306 if (!res) {
1307 if (nla_put_u32(msg,
1308 NL80211_ATTR_WIPHY_ANTENNA_TX,
1309 tx_ant) ||
1310 nla_put_u32(msg,
1311 NL80211_ATTR_WIPHY_ANTENNA_RX,
1312 rx_ant))
1313 goto nla_put_failure;
1314 }
Johannes Bergee688b002008-01-24 19:38:39 +01001315 }
1316
Johannes Berg86e8cf92013-06-19 10:57:22 +02001317 state->split_start++;
1318 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001319 break;
1320 case 2:
1321 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1322 dev->wiphy.interface_modes))
1323 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001324 state->split_start++;
1325 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001326 break;
1327 case 3:
1328 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1329 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001330 goto nla_put_failure;
1331
Johannes Berg86e8cf92013-06-19 10:57:22 +02001332 for (band = state->band_start;
1333 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001334 struct ieee80211_supported_band *sband;
1335
1336 sband = dev->wiphy.bands[band];
1337
1338 if (!sband)
1339 continue;
1340
1341 nl_band = nla_nest_start(msg, band);
1342 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001343 goto nla_put_failure;
1344
Johannes Berg86e8cf92013-06-19 10:57:22 +02001345 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001346 case 0:
1347 if (nl80211_send_band_rateinfo(msg, sband))
1348 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001349 state->chan_start++;
1350 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 break;
1352 default:
1353 /* add frequencies */
1354 nl_freqs = nla_nest_start(
1355 msg, NL80211_BAND_ATTR_FREQS);
1356 if (!nl_freqs)
1357 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001358
Johannes Berg86e8cf92013-06-19 10:57:22 +02001359 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001360 i < sband->n_channels;
1361 i++) {
1362 nl_freq = nla_nest_start(msg, i);
1363 if (!nl_freq)
1364 goto nla_put_failure;
1365
1366 chan = &sband->channels[i];
1367
Johannes Berg86e8cf92013-06-19 10:57:22 +02001368 if (nl80211_msg_put_channel(
1369 msg, chan,
1370 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 goto nla_put_failure;
1372
1373 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001374 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001375 break;
1376 }
1377 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001378 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001379 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001380 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001381 nla_nest_end(msg, nl_freqs);
1382 }
1383
1384 nla_nest_end(msg, nl_band);
1385
Johannes Berg86e8cf92013-06-19 10:57:22 +02001386 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001387 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001388 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001389 band--;
1390 break;
1391 }
Johannes Bergee688b002008-01-24 19:38:39 +01001392 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001393 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001394
Johannes Berg3713b4e2013-02-14 16:19:38 +01001395 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001396 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001397 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001398 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001399
Johannes Berg3713b4e2013-02-14 16:19:38 +01001400 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001401 if (state->band_start == 0 && state->chan_start == 0)
1402 state->split_start++;
1403 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 break;
1405 case 4:
1406 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1407 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001408 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001409
1410 i = 0;
1411#define CMD(op, n) \
1412 do { \
1413 if (dev->ops->op) { \
1414 i++; \
1415 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1416 goto nla_put_failure; \
1417 } \
1418 } while (0)
1419
1420 CMD(add_virtual_intf, NEW_INTERFACE);
1421 CMD(change_virtual_intf, SET_INTERFACE);
1422 CMD(add_key, NEW_KEY);
1423 CMD(start_ap, START_AP);
1424 CMD(add_station, NEW_STATION);
1425 CMD(add_mpath, NEW_MPATH);
1426 CMD(update_mesh_config, SET_MESH_CONFIG);
1427 CMD(change_bss, SET_BSS);
1428 CMD(auth, AUTHENTICATE);
1429 CMD(assoc, ASSOCIATE);
1430 CMD(deauth, DEAUTHENTICATE);
1431 CMD(disassoc, DISASSOCIATE);
1432 CMD(join_ibss, JOIN_IBSS);
1433 CMD(join_mesh, JOIN_MESH);
1434 CMD(set_pmksa, SET_PMKSA);
1435 CMD(del_pmksa, DEL_PMKSA);
1436 CMD(flush_pmksa, FLUSH_PMKSA);
1437 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1438 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1439 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1440 CMD(mgmt_tx, FRAME);
1441 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1442 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1443 i++;
1444 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1445 goto nla_put_failure;
1446 }
1447 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1448 dev->ops->join_mesh) {
1449 i++;
1450 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1451 goto nla_put_failure;
1452 }
1453 CMD(set_wds_peer, SET_WDS_PEER);
1454 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1455 CMD(tdls_mgmt, TDLS_MGMT);
1456 CMD(tdls_oper, TDLS_OPER);
1457 }
1458 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1459 CMD(sched_scan_start, START_SCHED_SCAN);
1460 CMD(probe_client, PROBE_CLIENT);
1461 CMD(set_noack_map, SET_NOACK_MAP);
1462 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1463 i++;
1464 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1465 goto nla_put_failure;
1466 }
1467 CMD(start_p2p_device, START_P2P_DEVICE);
1468 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001469 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001470 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1471 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001472 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1473 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001474 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001475 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001476
Kalle Valo4745fc02011-11-17 19:06:10 +02001477#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001478 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001479#endif
1480
Johannes Berg8fdc6212009-03-14 09:34:01 +01001481#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001482
Johannes Berg3713b4e2013-02-14 16:19:38 +01001483 if (dev->ops->connect || dev->ops->auth) {
1484 i++;
1485 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001486 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001487 }
1488
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 if (dev->ops->disconnect || dev->ops->deauth) {
1490 i++;
1491 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1492 goto nla_put_failure;
1493 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001494
Johannes Berg3713b4e2013-02-14 16:19:38 +01001495 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001496 state->split_start++;
1497 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001498 break;
1499 case 5:
1500 if (dev->ops->remain_on_channel &&
1501 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1502 nla_put_u32(msg,
1503 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1504 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001505 goto nla_put_failure;
1506
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1508 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1509 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001510
Johannes Berg3713b4e2013-02-14 16:19:38 +01001511 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1512 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001513 state->split_start++;
1514 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001515 break;
1516 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001517#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001518 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001519 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001520 state->split_start++;
1521 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001522 break;
1523#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001525#endif
1526 case 7:
1527 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1528 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001529 goto nla_put_failure;
1530
Johannes Berg86e8cf92013-06-19 10:57:22 +02001531 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1532 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001534
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 state->split_start++;
1536 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001537 break;
1538 case 8:
1539 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1540 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1541 dev->wiphy.ap_sme_capa))
1542 goto nla_put_failure;
1543
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001544 features = dev->wiphy.features;
1545 /*
1546 * We can only add the per-channel limit information if the
1547 * dump is split, otherwise it makes it too big. Therefore
1548 * only advertise it in that case.
1549 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001550 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001551 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1552 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001553 goto nla_put_failure;
1554
1555 if (dev->wiphy.ht_capa_mod_mask &&
1556 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1557 sizeof(*dev->wiphy.ht_capa_mod_mask),
1558 dev->wiphy.ht_capa_mod_mask))
1559 goto nla_put_failure;
1560
1561 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1562 dev->wiphy.max_acl_mac_addrs &&
1563 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1564 dev->wiphy.max_acl_mac_addrs))
1565 goto nla_put_failure;
1566
1567 /*
1568 * Any information below this point is only available to
1569 * applications that can deal with it being split. This
1570 * helps ensure that newly added capabilities don't break
1571 * older tools by overrunning their buffers.
1572 *
1573 * We still increment split_start so that in the split
1574 * case we'll continue with more data in the next round,
1575 * but break unconditionally so unsplit data stops here.
1576 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001577 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001578 break;
1579 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001580 if (dev->wiphy.extended_capabilities &&
1581 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1582 dev->wiphy.extended_capabilities_len,
1583 dev->wiphy.extended_capabilities) ||
1584 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1585 dev->wiphy.extended_capabilities_len,
1586 dev->wiphy.extended_capabilities_mask)))
1587 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001588
Johannes Bergee2aca32013-02-21 17:36:01 +01001589 if (dev->wiphy.vht_capa_mod_mask &&
1590 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1591 sizeof(*dev->wiphy.vht_capa_mod_mask),
1592 dev->wiphy.vht_capa_mod_mask))
1593 goto nla_put_failure;
1594
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001595 state->split_start++;
1596 break;
1597 case 10:
1598 if (nl80211_send_coalesce(msg, dev))
1599 goto nla_put_failure;
1600
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001601 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1602 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1603 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1604 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001605
1606 if (dev->wiphy.max_ap_assoc_sta &&
1607 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1608 dev->wiphy.max_ap_assoc_sta))
1609 goto nla_put_failure;
1610
Johannes Bergad7e7182013-11-13 13:37:47 +01001611 state->split_start++;
1612 break;
1613 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001614 if (dev->wiphy.n_vendor_commands) {
1615 const struct nl80211_vendor_cmd_info *info;
1616 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001617
Johannes Berg567ffc32013-12-18 14:43:31 +01001618 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1619 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001620 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001621
1622 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1623 info = &dev->wiphy.vendor_commands[i].info;
1624 if (nla_put(msg, i + 1, sizeof(*info), info))
1625 goto nla_put_failure;
1626 }
1627 nla_nest_end(msg, nested);
1628 }
1629
1630 if (dev->wiphy.n_vendor_events) {
1631 const struct nl80211_vendor_cmd_info *info;
1632 struct nlattr *nested;
1633
1634 nested = nla_nest_start(msg,
1635 NL80211_ATTR_VENDOR_EVENTS);
1636 if (!nested)
1637 goto nla_put_failure;
1638
1639 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1640 info = &dev->wiphy.vendor_events[i];
1641 if (nla_put(msg, i + 1, sizeof(*info), info))
1642 goto nla_put_failure;
1643 }
1644 nla_nest_end(msg, nested);
1645 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001646
Johannes Berg3713b4e2013-02-14 16:19:38 +01001647 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001648 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001649 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001650 }
Johannes Berg55682962007-09-20 13:09:35 -04001651 return genlmsg_end(msg, hdr);
1652
1653 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001654 genlmsg_cancel(msg, hdr);
1655 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001656}
1657
Johannes Berg86e8cf92013-06-19 10:57:22 +02001658static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1659 struct netlink_callback *cb,
1660 struct nl80211_dump_wiphy_state *state)
1661{
1662 struct nlattr **tb = nl80211_fam.attrbuf;
1663 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1664 tb, nl80211_fam.maxattr, nl80211_policy);
1665 /* ignore parse errors for backward compatibility */
1666 if (ret)
1667 return 0;
1668
1669 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1670 if (tb[NL80211_ATTR_WIPHY])
1671 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1672 if (tb[NL80211_ATTR_WDEV])
1673 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1674 if (tb[NL80211_ATTR_IFINDEX]) {
1675 struct net_device *netdev;
1676 struct cfg80211_registered_device *rdev;
1677 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1678
Ying Xue7f2b8562014-01-15 10:23:45 +08001679 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001680 if (!netdev)
1681 return -ENODEV;
1682 if (netdev->ieee80211_ptr) {
1683 rdev = wiphy_to_dev(
1684 netdev->ieee80211_ptr->wiphy);
1685 state->filter_wiphy = rdev->wiphy_idx;
1686 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001687 }
1688
1689 return 0;
1690}
1691
Johannes Berg55682962007-09-20 13:09:35 -04001692static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1693{
Johannes Berg645e77d2013-03-01 14:03:49 +01001694 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001695 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001696 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001697
Johannes Berg5fe231e2013-05-08 21:45:15 +02001698 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699 if (!state) {
1700 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001701 if (!state) {
1702 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001703 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001704 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001705 state->filter_wiphy = -1;
1706 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1707 if (ret) {
1708 kfree(state);
1709 rtnl_unlock();
1710 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001711 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001712 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001713 }
1714
Johannes Berg79c97e92009-07-07 03:56:12 +02001715 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001716 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1717 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001718 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001719 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001720 if (state->filter_wiphy != -1 &&
1721 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001722 continue;
1723 /* attempt to fit multiple wiphy data chunks into the skb */
1724 do {
1725 ret = nl80211_send_wiphy(dev, skb,
1726 NETLINK_CB(cb->skb).portid,
1727 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001728 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001729 if (ret < 0) {
1730 /*
1731 * If sending the wiphy data didn't fit (ENOBUFS
1732 * or EMSGSIZE returned), this SKB is still
1733 * empty (so it's not too big because another
1734 * wiphy dataset is already in the skb) and
1735 * we've not tried to adjust the dump allocation
1736 * yet ... then adjust the alloc size to be
1737 * bigger, and return 1 but with the empty skb.
1738 * This results in an empty message being RX'ed
1739 * in userspace, but that is ignored.
1740 *
1741 * We can then retry with the larger buffer.
1742 */
1743 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001744 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001745 cb->min_dump_alloc < 4096) {
1746 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001747 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001748 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001749 return 1;
1750 }
1751 idx--;
1752 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001753 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001754 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001755 break;
Johannes Berg55682962007-09-20 13:09:35 -04001756 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001757 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001758
Johannes Berg86e8cf92013-06-19 10:57:22 +02001759 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001760
1761 return skb->len;
1762}
1763
Johannes Berg86e8cf92013-06-19 10:57:22 +02001764static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1765{
1766 kfree((void *)cb->args[0]);
1767 return 0;
1768}
1769
Johannes Berg55682962007-09-20 13:09:35 -04001770static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1771{
1772 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001773 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001774 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001775
Johannes Berg645e77d2013-03-01 14:03:49 +01001776 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001777 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001778 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001779
Johannes Berg3713b4e2013-02-14 16:19:38 +01001780 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001781 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001782 nlmsg_free(msg);
1783 return -ENOBUFS;
1784 }
Johannes Berg55682962007-09-20 13:09:35 -04001785
Johannes Berg134e6372009-07-10 09:51:34 +00001786 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001787}
1788
Jouni Malinen31888482008-10-30 16:59:24 +02001789static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1790 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1791 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1792 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1793 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1794 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1795};
1796
1797static int parse_txq_params(struct nlattr *tb[],
1798 struct ieee80211_txq_params *txq_params)
1799{
Johannes Berga3304b02012-03-28 11:04:24 +02001800 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001801 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1802 !tb[NL80211_TXQ_ATTR_AIFS])
1803 return -EINVAL;
1804
Johannes Berga3304b02012-03-28 11:04:24 +02001805 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001806 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1807 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1808 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1809 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1810
Johannes Berga3304b02012-03-28 11:04:24 +02001811 if (txq_params->ac >= NL80211_NUM_ACS)
1812 return -EINVAL;
1813
Jouni Malinen31888482008-10-30 16:59:24 +02001814 return 0;
1815}
1816
Johannes Bergf444de02010-05-05 15:25:02 +02001817static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1818{
1819 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001820 * You can only set the channel explicitly for WDS interfaces,
1821 * all others have their channel managed via their respective
1822 * "establish a connection" command (connect, join, ...)
1823 *
1824 * For AP/GO and mesh mode, the channel can be set with the
1825 * channel userspace API, but is only stored and passed to the
1826 * low-level driver when the AP starts or the mesh is joined.
1827 * This is for backward compatibility, userspace can also give
1828 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001829 *
1830 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001831 * whatever else is going on, so they have their own special
1832 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001833 */
1834 return !wdev ||
1835 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001836 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001837 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1838 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001839}
1840
Johannes Berg683b6d32012-11-08 21:25:48 +01001841static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1842 struct genl_info *info,
1843 struct cfg80211_chan_def *chandef)
1844{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301845 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001846
1847 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1848 return -EINVAL;
1849
1850 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1851
1852 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001853 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1854 chandef->center_freq1 = control_freq;
1855 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001856
1857 /* Primary channel not allowed */
1858 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1859 return -EINVAL;
1860
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001861 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1862 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001863
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001864 chantype = nla_get_u32(
1865 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1866
1867 switch (chantype) {
1868 case NL80211_CHAN_NO_HT:
1869 case NL80211_CHAN_HT20:
1870 case NL80211_CHAN_HT40PLUS:
1871 case NL80211_CHAN_HT40MINUS:
1872 cfg80211_chandef_create(chandef, chandef->chan,
1873 chantype);
1874 break;
1875 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001876 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001877 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001878 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1879 chandef->width =
1880 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1881 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1882 chandef->center_freq1 =
1883 nla_get_u32(
1884 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1885 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1886 chandef->center_freq2 =
1887 nla_get_u32(
1888 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1889 }
1890
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001891 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001892 return -EINVAL;
1893
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001894 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1895 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001896 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001897
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001898 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1899 chandef->width == NL80211_CHAN_WIDTH_10) &&
1900 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1901 return -EINVAL;
1902
Johannes Berg683b6d32012-11-08 21:25:48 +01001903 return 0;
1904}
1905
Johannes Bergf444de02010-05-05 15:25:02 +02001906static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1907 struct wireless_dev *wdev,
1908 struct genl_info *info)
1909{
Johannes Berg683b6d32012-11-08 21:25:48 +01001910 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001911 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001912 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1913
1914 if (wdev)
1915 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001916
Johannes Bergf444de02010-05-05 15:25:02 +02001917 if (!nl80211_can_set_dev_channel(wdev))
1918 return -EOPNOTSUPP;
1919
Johannes Berg683b6d32012-11-08 21:25:48 +01001920 result = nl80211_parse_chandef(rdev, info, &chandef);
1921 if (result)
1922 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001923
Johannes Berge8c9bd52012-06-06 08:18:22 +02001924 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001925 case NL80211_IFTYPE_AP:
1926 case NL80211_IFTYPE_P2P_GO:
1927 if (wdev->beacon_interval) {
1928 result = -EBUSY;
1929 break;
1930 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001931 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001932 result = -EINVAL;
1933 break;
1934 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001935 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 result = 0;
1937 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001938 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001939 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001940 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001941 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001942 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001943 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001944 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001945 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001946 }
Johannes Bergf444de02010-05-05 15:25:02 +02001947
1948 return result;
1949}
1950
1951static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1952{
Johannes Berg4c476992010-10-04 21:36:35 +02001953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1954 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001955
Johannes Berg4c476992010-10-04 21:36:35 +02001956 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001957}
1958
Bill Jordane8347eb2010-10-01 13:54:28 -04001959static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1960{
Johannes Berg43b19952010-10-07 13:10:30 +02001961 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1962 struct net_device *dev = info->user_ptr[1];
1963 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001964 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001965
1966 if (!info->attrs[NL80211_ATTR_MAC])
1967 return -EINVAL;
1968
Johannes Berg43b19952010-10-07 13:10:30 +02001969 if (netif_running(dev))
1970 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001971
Johannes Berg43b19952010-10-07 13:10:30 +02001972 if (!rdev->ops->set_wds_peer)
1973 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001974
Johannes Berg43b19952010-10-07 13:10:30 +02001975 if (wdev->iftype != NL80211_IFTYPE_WDS)
1976 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001977
1978 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001979 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001980}
1981
1982
Johannes Berg55682962007-09-20 13:09:35 -04001983static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1984{
1985 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001986 struct net_device *netdev = NULL;
1987 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001988 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001989 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001990 u32 changed;
1991 u8 retry_short = 0, retry_long = 0;
1992 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001993 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001994
Johannes Berg5fe231e2013-05-08 21:45:15 +02001995 ASSERT_RTNL();
1996
Johannes Bergf444de02010-05-05 15:25:02 +02001997 /*
1998 * Try to find the wiphy and netdev. Normally this
1999 * function shouldn't need the netdev, but this is
2000 * done for backward compatibility -- previously
2001 * setting the channel was done per wiphy, but now
2002 * it is per netdev. Previous userland like hostapd
2003 * also passed a netdev to set_wiphy, so that it is
2004 * possible to let that go to the right netdev!
2005 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002006
Johannes Bergf444de02010-05-05 15:25:02 +02002007 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2008 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2009
Ying Xue7f2b8562014-01-15 10:23:45 +08002010 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002011 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002012 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002013 else
Johannes Bergf444de02010-05-05 15:25:02 +02002014 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002015 }
2016
Johannes Bergf444de02010-05-05 15:25:02 +02002017 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002018 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2019 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002020 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002021 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002022 wdev = NULL;
2023 netdev = NULL;
2024 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002025 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002026 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002027
2028 /*
2029 * end workaround code, by now the rdev is available
2030 * and locked, and wdev may or may not be NULL.
2031 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002032
2033 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002034 result = cfg80211_dev_rename(
2035 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002036
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002037 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002038 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002039
Jouni Malinen31888482008-10-30 16:59:24 +02002040 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2041 struct ieee80211_txq_params txq_params;
2042 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2043
Ying Xue7f2b8562014-01-15 10:23:45 +08002044 if (!rdev->ops->set_txq_params)
2045 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002046
Ying Xue7f2b8562014-01-15 10:23:45 +08002047 if (!netdev)
2048 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002049
Johannes Berg133a3ff2011-11-03 14:50:13 +01002050 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002051 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2052 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002053
Ying Xue7f2b8562014-01-15 10:23:45 +08002054 if (!netif_running(netdev))
2055 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002056
Jouni Malinen31888482008-10-30 16:59:24 +02002057 nla_for_each_nested(nl_txq_params,
2058 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2059 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002060 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2061 nla_data(nl_txq_params),
2062 nla_len(nl_txq_params),
2063 txq_params_policy);
2064 if (result)
2065 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002066 result = parse_txq_params(tb, &txq_params);
2067 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002068 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002069
Hila Gonene35e4d22012-06-27 17:19:42 +03002070 result = rdev_set_txq_params(rdev, netdev,
2071 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002072 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002073 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002074 }
2075 }
2076
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002077 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002078 result = __nl80211_set_channel(rdev,
2079 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2080 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002081 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002082 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002083 }
2084
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002085 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002086 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002087 enum nl80211_tx_power_setting type;
2088 int idx, mbm = 0;
2089
Johannes Bergc8442112012-10-24 10:17:18 +02002090 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2091 txp_wdev = NULL;
2092
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 if (!rdev->ops->set_tx_power)
2094 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002095
2096 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2097 type = nla_get_u32(info->attrs[idx]);
2098
2099 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002100 (type != NL80211_TX_POWER_AUTOMATIC))
2101 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002102
2103 if (type != NL80211_TX_POWER_AUTOMATIC) {
2104 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2105 mbm = nla_get_u32(info->attrs[idx]);
2106 }
2107
Johannes Bergc8442112012-10-24 10:17:18 +02002108 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002109 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002110 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002111 }
2112
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002113 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2114 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2115 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002116 if ((!rdev->wiphy.available_antennas_tx &&
2117 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002118 !rdev->ops->set_antenna)
2119 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002120
2121 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2122 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2123
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002124 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002125 * available antenna masks, except for the "all" mask */
2126 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002127 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2128 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002129
Bruno Randolf7f531e02010-12-16 11:30:22 +09002130 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2131 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002132
Hila Gonene35e4d22012-06-27 17:19:42 +03002133 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002134 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002135 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002136 }
2137
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002138 changed = 0;
2139
2140 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2141 retry_short = nla_get_u8(
2142 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002143 if (retry_short == 0)
2144 return -EINVAL;
2145
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002146 changed |= WIPHY_PARAM_RETRY_SHORT;
2147 }
2148
2149 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2150 retry_long = nla_get_u8(
2151 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002152 if (retry_long == 0)
2153 return -EINVAL;
2154
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002155 changed |= WIPHY_PARAM_RETRY_LONG;
2156 }
2157
2158 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2159 frag_threshold = nla_get_u32(
2160 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002161 if (frag_threshold < 256)
2162 return -EINVAL;
2163
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002164 if (frag_threshold != (u32) -1) {
2165 /*
2166 * Fragments (apart from the last one) are required to
2167 * have even length. Make the fragmentation code
2168 * simpler by stripping LSB should someone try to use
2169 * odd threshold value.
2170 */
2171 frag_threshold &= ~0x1;
2172 }
2173 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2174 }
2175
2176 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2177 rts_threshold = nla_get_u32(
2178 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2179 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2180 }
2181
Lukáš Turek81077e82009-12-21 22:50:47 +01002182 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2183 coverage_class = nla_get_u8(
2184 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2185 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2186 }
2187
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002188 if (changed) {
2189 u8 old_retry_short, old_retry_long;
2190 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002191 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002192
Ying Xue7f2b8562014-01-15 10:23:45 +08002193 if (!rdev->ops->set_wiphy_params)
2194 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002195
2196 old_retry_short = rdev->wiphy.retry_short;
2197 old_retry_long = rdev->wiphy.retry_long;
2198 old_frag_threshold = rdev->wiphy.frag_threshold;
2199 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002200 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002201
2202 if (changed & WIPHY_PARAM_RETRY_SHORT)
2203 rdev->wiphy.retry_short = retry_short;
2204 if (changed & WIPHY_PARAM_RETRY_LONG)
2205 rdev->wiphy.retry_long = retry_long;
2206 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2207 rdev->wiphy.frag_threshold = frag_threshold;
2208 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2209 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002210 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2211 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002212
Hila Gonene35e4d22012-06-27 17:19:42 +03002213 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002214 if (result) {
2215 rdev->wiphy.retry_short = old_retry_short;
2216 rdev->wiphy.retry_long = old_retry_long;
2217 rdev->wiphy.frag_threshold = old_frag_threshold;
2218 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002219 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002220 }
2221 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002222 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002223}
2224
Johannes Berg71bbc992012-06-15 15:30:18 +02002225static inline u64 wdev_id(struct wireless_dev *wdev)
2226{
2227 return (u64)wdev->identifier |
2228 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2229}
Johannes Berg55682962007-09-20 13:09:35 -04002230
Johannes Berg683b6d32012-11-08 21:25:48 +01002231static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002232 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002233{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002234 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002235
Johannes Berg683b6d32012-11-08 21:25:48 +01002236 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2237 chandef->chan->center_freq))
2238 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002239 switch (chandef->width) {
2240 case NL80211_CHAN_WIDTH_20_NOHT:
2241 case NL80211_CHAN_WIDTH_20:
2242 case NL80211_CHAN_WIDTH_40:
2243 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2244 cfg80211_get_chandef_type(chandef)))
2245 return -ENOBUFS;
2246 break;
2247 default:
2248 break;
2249 }
2250 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2251 return -ENOBUFS;
2252 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2253 return -ENOBUFS;
2254 if (chandef->center_freq2 &&
2255 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002256 return -ENOBUFS;
2257 return 0;
2258}
2259
Eric W. Biederman15e47302012-09-07 20:12:54 +00002260static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002261 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002262 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002263{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002264 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002265 void *hdr;
2266
Eric W. Biederman15e47302012-09-07 20:12:54 +00002267 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002268 if (!hdr)
2269 return -1;
2270
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002271 if (dev &&
2272 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002273 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002274 goto nla_put_failure;
2275
2276 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2277 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002278 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002279 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002280 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2281 rdev->devlist_generation ^
2282 (cfg80211_rdev_list_generation << 2)))
2283 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002284
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002285 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002286 int ret;
2287 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002288
Johannes Berg683b6d32012-11-08 21:25:48 +01002289 ret = rdev_get_channel(rdev, wdev, &chandef);
2290 if (ret == 0) {
2291 if (nl80211_send_chandef(msg, &chandef))
2292 goto nla_put_failure;
2293 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002294 }
2295
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002296 if (wdev->ssid_len) {
2297 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2298 goto nla_put_failure;
2299 }
2300
Johannes Berg55682962007-09-20 13:09:35 -04002301 return genlmsg_end(msg, hdr);
2302
2303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002304 genlmsg_cancel(msg, hdr);
2305 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002306}
2307
2308static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2309{
2310 int wp_idx = 0;
2311 int if_idx = 0;
2312 int wp_start = cb->args[0];
2313 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002314 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002315 struct wireless_dev *wdev;
2316
Johannes Berg5fe231e2013-05-08 21:45:15 +02002317 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002318 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2319 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002320 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002321 if (wp_idx < wp_start) {
2322 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002323 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002324 }
Johannes Berg55682962007-09-20 13:09:35 -04002325 if_idx = 0;
2326
Johannes Berg89a54e42012-06-15 14:33:17 +02002327 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002328 if (if_idx < if_start) {
2329 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002330 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002331 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002332 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002333 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002334 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 goto out;
2336 }
2337 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002338 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339
2340 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002341 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002343 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002344
2345 cb->args[0] = wp_idx;
2346 cb->args[1] = if_idx;
2347
2348 return skb->len;
2349}
2350
2351static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2352{
2353 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002354 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002355 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002356
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002358 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002359 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002360
Eric W. Biederman15e47302012-09-07 20:12:54 +00002361 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002362 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002363 nlmsg_free(msg);
2364 return -ENOBUFS;
2365 }
Johannes Berg55682962007-09-20 13:09:35 -04002366
Johannes Berg134e6372009-07-10 09:51:34 +00002367 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002368}
2369
Michael Wu66f7ac52008-01-31 19:48:22 +01002370static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2371 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2372 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2373 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2374 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2375 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002376 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002377};
2378
2379static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2380{
2381 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2382 int flag;
2383
2384 *mntrflags = 0;
2385
2386 if (!nla)
2387 return -EINVAL;
2388
2389 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2390 nla, mntr_flags_policy))
2391 return -EINVAL;
2392
2393 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2394 if (flags[flag])
2395 *mntrflags |= (1<<flag);
2396
2397 return 0;
2398}
2399
Johannes Berg9bc383d2009-11-19 11:55:19 +01002400static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002401 struct net_device *netdev, u8 use_4addr,
2402 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002403{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002404 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002405 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002406 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002407 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002408 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002409
2410 switch (iftype) {
2411 case NL80211_IFTYPE_AP_VLAN:
2412 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2413 return 0;
2414 break;
2415 case NL80211_IFTYPE_STATION:
2416 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2417 return 0;
2418 break;
2419 default:
2420 break;
2421 }
2422
2423 return -EOPNOTSUPP;
2424}
2425
Johannes Berg55682962007-09-20 13:09:35 -04002426static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2427{
Johannes Berg4c476992010-10-04 21:36:35 +02002428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002429 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002430 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002431 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002433 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002434 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002435
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002436 memset(&params, 0, sizeof(params));
2437
Johannes Berg04a773a2009-04-19 21:24:32 +02002438 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002439
Johannes Berg723b0382008-09-16 20:22:09 +02002440 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002441 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002443 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 if (ntype > NL80211_IFTYPE_MAX)
2445 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002446 }
2447
Johannes Berg92ffe052008-09-16 20:39:36 +02002448 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002449 struct wireless_dev *wdev = dev->ieee80211_ptr;
2450
Johannes Berg4c476992010-10-04 21:36:35 +02002451 if (ntype != NL80211_IFTYPE_MESH_POINT)
2452 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002453 if (netif_running(dev))
2454 return -EBUSY;
2455
2456 wdev_lock(wdev);
2457 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2458 IEEE80211_MAX_MESH_ID_LEN);
2459 wdev->mesh_id_up_len =
2460 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2461 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2462 wdev->mesh_id_up_len);
2463 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002464 }
2465
Felix Fietkau8b787642009-11-10 18:53:10 +01002466 if (info->attrs[NL80211_ATTR_4ADDR]) {
2467 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2468 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002469 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002470 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002471 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002472 } else {
2473 params.use_4addr = -1;
2474 }
2475
Johannes Berg92ffe052008-09-16 20:39:36 +02002476 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002477 if (ntype != NL80211_IFTYPE_MONITOR)
2478 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002479 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2480 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002483
2484 flags = &_flags;
2485 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002486 }
Johannes Berg3b858752009-03-12 09:55:09 +01002487
Luciano Coelho18003292013-08-29 13:26:57 +03002488 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002489 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2490 return -EOPNOTSUPP;
2491
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002492 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002493 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002494 else
2495 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002496
Johannes Berg9bc383d2009-11-19 11:55:19 +01002497 if (!err && params.use_4addr != -1)
2498 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2499
Johannes Berg55682962007-09-20 13:09:35 -04002500 return err;
2501}
2502
2503static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2504{
Johannes Berg4c476992010-10-04 21:36:35 +02002505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002506 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002507 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002508 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002509 int err;
2510 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002511 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002512
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002513 memset(&params, 0, sizeof(params));
2514
Johannes Berg55682962007-09-20 13:09:35 -04002515 if (!info->attrs[NL80211_ATTR_IFNAME])
2516 return -EINVAL;
2517
2518 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2519 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2520 if (type > NL80211_IFTYPE_MAX)
2521 return -EINVAL;
2522 }
2523
Johannes Berg79c97e92009-07-07 03:56:12 +02002524 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002525 !(rdev->wiphy.interface_modes & (1 << type)))
2526 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002527
Arend van Spriel1c18f142013-01-08 10:17:27 +01002528 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2529 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2530 ETH_ALEN);
2531 if (!is_valid_ether_addr(params.macaddr))
2532 return -EADDRNOTAVAIL;
2533 }
2534
Johannes Berg9bc383d2009-11-19 11:55:19 +01002535 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002536 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002537 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002538 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002539 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002540 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002541
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002542 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2543 if (!msg)
2544 return -ENOMEM;
2545
Michael Wu66f7ac52008-01-31 19:48:22 +01002546 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2547 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2548 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002549
Luciano Coelho18003292013-08-29 13:26:57 +03002550 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002551 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2552 return -EOPNOTSUPP;
2553
Hila Gonene35e4d22012-06-27 17:19:42 +03002554 wdev = rdev_add_virtual_intf(rdev,
2555 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2556 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002557 if (IS_ERR(wdev)) {
2558 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002559 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002560 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002561
Johannes Berg98104fde2012-06-16 00:19:54 +02002562 switch (type) {
2563 case NL80211_IFTYPE_MESH_POINT:
2564 if (!info->attrs[NL80211_ATTR_MESH_ID])
2565 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002566 wdev_lock(wdev);
2567 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2568 IEEE80211_MAX_MESH_ID_LEN);
2569 wdev->mesh_id_up_len =
2570 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2571 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2572 wdev->mesh_id_up_len);
2573 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002574 break;
2575 case NL80211_IFTYPE_P2P_DEVICE:
2576 /*
2577 * P2P Device doesn't have a netdev, so doesn't go
2578 * through the netdev notifier and must be added here
2579 */
2580 mutex_init(&wdev->mtx);
2581 INIT_LIST_HEAD(&wdev->event_list);
2582 spin_lock_init(&wdev->event_lock);
2583 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2584 spin_lock_init(&wdev->mgmt_registrations_lock);
2585
Johannes Berg98104fde2012-06-16 00:19:54 +02002586 wdev->identifier = ++rdev->wdev_id;
2587 list_add_rcu(&wdev->list, &rdev->wdev_list);
2588 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002589 break;
2590 default:
2591 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002592 }
2593
Eric W. Biederman15e47302012-09-07 20:12:54 +00002594 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002595 rdev, wdev) < 0) {
2596 nlmsg_free(msg);
2597 return -ENOBUFS;
2598 }
2599
2600 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002601}
2602
2603static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2604{
Johannes Berg4c476992010-10-04 21:36:35 +02002605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002606 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002607
Johannes Berg4c476992010-10-04 21:36:35 +02002608 if (!rdev->ops->del_virtual_intf)
2609 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002610
Johannes Berg84efbb82012-06-16 00:00:26 +02002611 /*
2612 * If we remove a wireless device without a netdev then clear
2613 * user_ptr[1] so that nl80211_post_doit won't dereference it
2614 * to check if it needs to do dev_put(). Otherwise it crashes
2615 * since the wdev has been freed, unlike with a netdev where
2616 * we need the dev_put() for the netdev to really be freed.
2617 */
2618 if (!wdev->netdev)
2619 info->user_ptr[1] = NULL;
2620
Hila Gonene35e4d22012-06-27 17:19:42 +03002621 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002622}
2623
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002624static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2625{
2626 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2627 struct net_device *dev = info->user_ptr[1];
2628 u16 noack_map;
2629
2630 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2631 return -EINVAL;
2632
2633 if (!rdev->ops->set_noack_map)
2634 return -EOPNOTSUPP;
2635
2636 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2637
Hila Gonene35e4d22012-06-27 17:19:42 +03002638 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002639}
2640
Johannes Berg41ade002007-12-19 02:03:29 +01002641struct get_key_cookie {
2642 struct sk_buff *msg;
2643 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002644 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002645};
2646
2647static void get_key_callback(void *c, struct key_params *params)
2648{
Johannes Bergb9454e82009-07-08 13:29:08 +02002649 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002650 struct get_key_cookie *cookie = c;
2651
David S. Miller9360ffd2012-03-29 04:41:26 -04002652 if ((params->key &&
2653 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2654 params->key_len, params->key)) ||
2655 (params->seq &&
2656 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2657 params->seq_len, params->seq)) ||
2658 (params->cipher &&
2659 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2660 params->cipher)))
2661 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002662
Johannes Bergb9454e82009-07-08 13:29:08 +02002663 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2664 if (!key)
2665 goto nla_put_failure;
2666
David S. Miller9360ffd2012-03-29 04:41:26 -04002667 if ((params->key &&
2668 nla_put(cookie->msg, NL80211_KEY_DATA,
2669 params->key_len, params->key)) ||
2670 (params->seq &&
2671 nla_put(cookie->msg, NL80211_KEY_SEQ,
2672 params->seq_len, params->seq)) ||
2673 (params->cipher &&
2674 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2675 params->cipher)))
2676 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002677
David S. Miller9360ffd2012-03-29 04:41:26 -04002678 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2679 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002680
2681 nla_nest_end(cookie->msg, key);
2682
Johannes Berg41ade002007-12-19 02:03:29 +01002683 return;
2684 nla_put_failure:
2685 cookie->error = 1;
2686}
2687
2688static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2689{
Johannes Berg4c476992010-10-04 21:36:35 +02002690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002691 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002692 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002693 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002694 const u8 *mac_addr = NULL;
2695 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002696 struct get_key_cookie cookie = {
2697 .error = 0,
2698 };
2699 void *hdr;
2700 struct sk_buff *msg;
2701
2702 if (info->attrs[NL80211_ATTR_KEY_IDX])
2703 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2704
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002705 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002706 return -EINVAL;
2707
2708 if (info->attrs[NL80211_ATTR_MAC])
2709 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2710
Johannes Berge31b8212010-10-05 19:39:30 +02002711 pairwise = !!mac_addr;
2712 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2713 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2714 if (kt >= NUM_NL80211_KEYTYPES)
2715 return -EINVAL;
2716 if (kt != NL80211_KEYTYPE_GROUP &&
2717 kt != NL80211_KEYTYPE_PAIRWISE)
2718 return -EINVAL;
2719 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2720 }
2721
Johannes Berg4c476992010-10-04 21:36:35 +02002722 if (!rdev->ops->get_key)
2723 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002724
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002726 if (!msg)
2727 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002728
Eric W. Biederman15e47302012-09-07 20:12:54 +00002729 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002730 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002731 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002732 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002733
2734 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002735 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002736
David S. Miller9360ffd2012-03-29 04:41:26 -04002737 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2738 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2739 goto nla_put_failure;
2740 if (mac_addr &&
2741 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2742 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002743
Johannes Berge31b8212010-10-05 19:39:30 +02002744 if (pairwise && mac_addr &&
2745 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2746 return -ENOENT;
2747
Hila Gonene35e4d22012-06-27 17:19:42 +03002748 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2749 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002752 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002753
2754 if (cookie.error)
2755 goto nla_put_failure;
2756
2757 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002758 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002759
2760 nla_put_failure:
2761 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002762 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002763 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002764 return err;
2765}
2766
2767static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2768{
Johannes Berg4c476992010-10-04 21:36:35 +02002769 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002770 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002771 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002772 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002773
Johannes Bergb9454e82009-07-08 13:29:08 +02002774 err = nl80211_parse_key(info, &key);
2775 if (err)
2776 return err;
2777
2778 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002779 return -EINVAL;
2780
Johannes Bergb9454e82009-07-08 13:29:08 +02002781 /* only support setting default key */
2782 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002783 return -EINVAL;
2784
Johannes Bergfffd0932009-07-08 14:22:54 +02002785 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002786
2787 if (key.def) {
2788 if (!rdev->ops->set_default_key) {
2789 err = -EOPNOTSUPP;
2790 goto out;
2791 }
2792
2793 err = nl80211_key_allowed(dev->ieee80211_ptr);
2794 if (err)
2795 goto out;
2796
Hila Gonene35e4d22012-06-27 17:19:42 +03002797 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002798 key.def_uni, key.def_multi);
2799
2800 if (err)
2801 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002802
Johannes Berg3d23e342009-09-29 23:27:28 +02002803#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002804 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002805#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002806 } else {
2807 if (key.def_uni || !key.def_multi) {
2808 err = -EINVAL;
2809 goto out;
2810 }
2811
2812 if (!rdev->ops->set_default_mgmt_key) {
2813 err = -EOPNOTSUPP;
2814 goto out;
2815 }
2816
2817 err = nl80211_key_allowed(dev->ieee80211_ptr);
2818 if (err)
2819 goto out;
2820
Hila Gonene35e4d22012-06-27 17:19:42 +03002821 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002822 if (err)
2823 goto out;
2824
2825#ifdef CONFIG_CFG80211_WEXT
2826 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2827#endif
2828 }
2829
2830 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002831 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002832
Johannes Berg41ade002007-12-19 02:03:29 +01002833 return err;
2834}
2835
2836static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2837{
Johannes Berg4c476992010-10-04 21:36:35 +02002838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002839 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002840 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002841 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002842 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002843
Johannes Bergb9454e82009-07-08 13:29:08 +02002844 err = nl80211_parse_key(info, &key);
2845 if (err)
2846 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002847
Johannes Bergb9454e82009-07-08 13:29:08 +02002848 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002849 return -EINVAL;
2850
Johannes Berg41ade002007-12-19 02:03:29 +01002851 if (info->attrs[NL80211_ATTR_MAC])
2852 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2853
Johannes Berge31b8212010-10-05 19:39:30 +02002854 if (key.type == -1) {
2855 if (mac_addr)
2856 key.type = NL80211_KEYTYPE_PAIRWISE;
2857 else
2858 key.type = NL80211_KEYTYPE_GROUP;
2859 }
2860
2861 /* for now */
2862 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2863 key.type != NL80211_KEYTYPE_GROUP)
2864 return -EINVAL;
2865
Johannes Berg4c476992010-10-04 21:36:35 +02002866 if (!rdev->ops->add_key)
2867 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002868
Johannes Berge31b8212010-10-05 19:39:30 +02002869 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2870 key.type == NL80211_KEYTYPE_PAIRWISE,
2871 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002872 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002873
2874 wdev_lock(dev->ieee80211_ptr);
2875 err = nl80211_key_allowed(dev->ieee80211_ptr);
2876 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002877 err = rdev_add_key(rdev, dev, key.idx,
2878 key.type == NL80211_KEYTYPE_PAIRWISE,
2879 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002880 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002881
Johannes Berg41ade002007-12-19 02:03:29 +01002882 return err;
2883}
2884
2885static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2886{
Johannes Berg4c476992010-10-04 21:36:35 +02002887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002888 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002889 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002890 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002891 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002892
Johannes Bergb9454e82009-07-08 13:29:08 +02002893 err = nl80211_parse_key(info, &key);
2894 if (err)
2895 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002896
2897 if (info->attrs[NL80211_ATTR_MAC])
2898 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2899
Johannes Berge31b8212010-10-05 19:39:30 +02002900 if (key.type == -1) {
2901 if (mac_addr)
2902 key.type = NL80211_KEYTYPE_PAIRWISE;
2903 else
2904 key.type = NL80211_KEYTYPE_GROUP;
2905 }
2906
2907 /* for now */
2908 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2909 key.type != NL80211_KEYTYPE_GROUP)
2910 return -EINVAL;
2911
Johannes Berg4c476992010-10-04 21:36:35 +02002912 if (!rdev->ops->del_key)
2913 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002914
Johannes Bergfffd0932009-07-08 14:22:54 +02002915 wdev_lock(dev->ieee80211_ptr);
2916 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002917
2918 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2919 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2920 err = -ENOENT;
2921
Johannes Bergfffd0932009-07-08 14:22:54 +02002922 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002923 err = rdev_del_key(rdev, dev, key.idx,
2924 key.type == NL80211_KEYTYPE_PAIRWISE,
2925 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002926
Johannes Berg3d23e342009-09-29 23:27:28 +02002927#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002928 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002929 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002930 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002931 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002932 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2933 }
2934#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002935 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002936
Johannes Berg41ade002007-12-19 02:03:29 +01002937 return err;
2938}
2939
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302940/* This function returns an error or the number of nested attributes */
2941static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2942{
2943 struct nlattr *attr;
2944 int n_entries = 0, tmp;
2945
2946 nla_for_each_nested(attr, nl_attr, tmp) {
2947 if (nla_len(attr) != ETH_ALEN)
2948 return -EINVAL;
2949
2950 n_entries++;
2951 }
2952
2953 return n_entries;
2954}
2955
2956/*
2957 * This function parses ACL information and allocates memory for ACL data.
2958 * On successful return, the calling function is responsible to free the
2959 * ACL buffer returned by this function.
2960 */
2961static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2962 struct genl_info *info)
2963{
2964 enum nl80211_acl_policy acl_policy;
2965 struct nlattr *attr;
2966 struct cfg80211_acl_data *acl;
2967 int i = 0, n_entries, tmp;
2968
2969 if (!wiphy->max_acl_mac_addrs)
2970 return ERR_PTR(-EOPNOTSUPP);
2971
2972 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2973 return ERR_PTR(-EINVAL);
2974
2975 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2976 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2977 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2978 return ERR_PTR(-EINVAL);
2979
2980 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2981 return ERR_PTR(-EINVAL);
2982
2983 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2984 if (n_entries < 0)
2985 return ERR_PTR(n_entries);
2986
2987 if (n_entries > wiphy->max_acl_mac_addrs)
2988 return ERR_PTR(-ENOTSUPP);
2989
2990 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2991 GFP_KERNEL);
2992 if (!acl)
2993 return ERR_PTR(-ENOMEM);
2994
2995 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2996 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2997 i++;
2998 }
2999
3000 acl->n_acl_entries = n_entries;
3001 acl->acl_policy = acl_policy;
3002
3003 return acl;
3004}
3005
3006static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3007{
3008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3009 struct net_device *dev = info->user_ptr[1];
3010 struct cfg80211_acl_data *acl;
3011 int err;
3012
3013 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3014 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3015 return -EOPNOTSUPP;
3016
3017 if (!dev->ieee80211_ptr->beacon_interval)
3018 return -EINVAL;
3019
3020 acl = parse_acl_data(&rdev->wiphy, info);
3021 if (IS_ERR(acl))
3022 return PTR_ERR(acl);
3023
3024 err = rdev_set_mac_acl(rdev, dev, acl);
3025
3026 kfree(acl);
3027
3028 return err;
3029}
3030
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003031static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003032 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003033{
Johannes Berg88600202012-02-13 15:17:18 +01003034 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003035
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003036 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3037 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3038 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3039 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003040 return -EINVAL;
3041
Johannes Berg88600202012-02-13 15:17:18 +01003042 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003043
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003044 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3045 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3046 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003047 if (!bcn->head_len)
3048 return -EINVAL;
3049 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003050 }
3051
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003052 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3053 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3054 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003055 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003056 }
3057
Johannes Berg4c476992010-10-04 21:36:35 +02003058 if (!haveinfo)
3059 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003061 if (attrs[NL80211_ATTR_IE]) {
3062 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3063 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003064 }
3065
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003066 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003067 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003068 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003069 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003070 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003071 }
3072
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003073 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003074 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003075 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003076 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003077 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003078 }
3079
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003080 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3081 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3082 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003083 }
3084
Johannes Berg88600202012-02-13 15:17:18 +01003085 return 0;
3086}
3087
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003088static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3089 struct cfg80211_ap_settings *params)
3090{
3091 struct wireless_dev *wdev;
3092 bool ret = false;
3093
Johannes Berg89a54e42012-06-15 14:33:17 +02003094 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003095 if (wdev->iftype != NL80211_IFTYPE_AP &&
3096 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3097 continue;
3098
Johannes Berg683b6d32012-11-08 21:25:48 +01003099 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003100 continue;
3101
Johannes Berg683b6d32012-11-08 21:25:48 +01003102 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003103 ret = true;
3104 break;
3105 }
3106
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003107 return ret;
3108}
3109
Jouni Malinene39e5b52012-09-30 19:29:39 +03003110static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3111 enum nl80211_auth_type auth_type,
3112 enum nl80211_commands cmd)
3113{
3114 if (auth_type > NL80211_AUTHTYPE_MAX)
3115 return false;
3116
3117 switch (cmd) {
3118 case NL80211_CMD_AUTHENTICATE:
3119 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3120 auth_type == NL80211_AUTHTYPE_SAE)
3121 return false;
3122 return true;
3123 case NL80211_CMD_CONNECT:
3124 case NL80211_CMD_START_AP:
3125 /* SAE not supported yet */
3126 if (auth_type == NL80211_AUTHTYPE_SAE)
3127 return false;
3128 return true;
3129 default:
3130 return false;
3131 }
3132}
3133
Johannes Berg88600202012-02-13 15:17:18 +01003134static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3135{
3136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3137 struct net_device *dev = info->user_ptr[1];
3138 struct wireless_dev *wdev = dev->ieee80211_ptr;
3139 struct cfg80211_ap_settings params;
3140 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003141 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003142
3143 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3144 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3145 return -EOPNOTSUPP;
3146
3147 if (!rdev->ops->start_ap)
3148 return -EOPNOTSUPP;
3149
3150 if (wdev->beacon_interval)
3151 return -EALREADY;
3152
3153 memset(&params, 0, sizeof(params));
3154
3155 /* these are required for START_AP */
3156 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3157 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3158 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3159 return -EINVAL;
3160
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003161 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003162 if (err)
3163 return err;
3164
3165 params.beacon_interval =
3166 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3167 params.dtim_period =
3168 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3169
3170 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3171 if (err)
3172 return err;
3173
3174 /*
3175 * In theory, some of these attributes should be required here
3176 * but since they were not used when the command was originally
3177 * added, keep them optional for old user space programs to let
3178 * them continue to work with drivers that do not need the
3179 * additional information -- drivers must check!
3180 */
3181 if (info->attrs[NL80211_ATTR_SSID]) {
3182 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3183 params.ssid_len =
3184 nla_len(info->attrs[NL80211_ATTR_SSID]);
3185 if (params.ssid_len == 0 ||
3186 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3187 return -EINVAL;
3188 }
3189
3190 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3191 params.hidden_ssid = nla_get_u32(
3192 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3193 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3194 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3195 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3196 return -EINVAL;
3197 }
3198
3199 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3200
3201 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3202 params.auth_type = nla_get_u32(
3203 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003204 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3205 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003206 return -EINVAL;
3207 } else
3208 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3209
3210 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3211 NL80211_MAX_NR_CIPHER_SUITES);
3212 if (err)
3213 return err;
3214
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303215 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3216 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3217 return -EOPNOTSUPP;
3218 params.inactivity_timeout = nla_get_u16(
3219 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3220 }
3221
Johannes Berg53cabad2012-11-14 15:17:28 +01003222 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3223 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3224 return -EINVAL;
3225 params.p2p_ctwindow =
3226 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3227 if (params.p2p_ctwindow > 127)
3228 return -EINVAL;
3229 if (params.p2p_ctwindow != 0 &&
3230 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3231 return -EINVAL;
3232 }
3233
3234 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3235 u8 tmp;
3236
3237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3238 return -EINVAL;
3239 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3240 if (tmp > 1)
3241 return -EINVAL;
3242 params.p2p_opp_ps = tmp;
3243 if (params.p2p_opp_ps != 0 &&
3244 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3245 return -EINVAL;
3246 }
3247
Johannes Bergaa430da2012-05-16 23:50:18 +02003248 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003249 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3250 if (err)
3251 return err;
3252 } else if (wdev->preset_chandef.chan) {
3253 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003254 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003255 return -EINVAL;
3256
Johannes Berg683b6d32012-11-08 21:25:48 +01003257 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003258 return -EINVAL;
3259
Simon Wunderlich04f39042013-02-08 18:16:19 +01003260 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3261 if (err < 0)
3262 return err;
3263 if (err) {
3264 radar_detect_width = BIT(params.chandef.width);
3265 params.radar_required = true;
3266 }
3267
Simon Wunderlich04f39042013-02-08 18:16:19 +01003268 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3269 params.chandef.chan,
3270 CHAN_MODE_SHARED,
3271 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003272 if (err)
3273 return err;
3274
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303275 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3276 params.acl = parse_acl_data(&rdev->wiphy, info);
3277 if (IS_ERR(params.acl))
3278 return PTR_ERR(params.acl);
3279 }
3280
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003281 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003282 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003283 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003284 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003285 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003286 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003287 wdev->ssid_len = params.ssid_len;
3288 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003289 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003290 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303291
3292 kfree(params.acl);
3293
Johannes Berg56d18932011-05-09 18:41:15 +02003294 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003295}
3296
Johannes Berg88600202012-02-13 15:17:18 +01003297static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3298{
3299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3300 struct net_device *dev = info->user_ptr[1];
3301 struct wireless_dev *wdev = dev->ieee80211_ptr;
3302 struct cfg80211_beacon_data params;
3303 int err;
3304
3305 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3306 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3307 return -EOPNOTSUPP;
3308
3309 if (!rdev->ops->change_beacon)
3310 return -EOPNOTSUPP;
3311
3312 if (!wdev->beacon_interval)
3313 return -EINVAL;
3314
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003315 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003316 if (err)
3317 return err;
3318
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003319 wdev_lock(wdev);
3320 err = rdev_change_beacon(rdev, dev, &params);
3321 wdev_unlock(wdev);
3322
3323 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003324}
3325
3326static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003327{
Johannes Berg4c476992010-10-04 21:36:35 +02003328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3329 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003330
Michal Kazior60771782012-06-29 12:46:56 +02003331 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003332}
3333
Johannes Berg5727ef12007-12-19 02:03:34 +01003334static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3335 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3336 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3337 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003338 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003339 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003340 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003341};
3342
Johannes Bergeccb8e82009-05-11 21:57:56 +03003343static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003344 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003345 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003346{
3347 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003348 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003349 int flag;
3350
Johannes Bergeccb8e82009-05-11 21:57:56 +03003351 /*
3352 * Try parsing the new attribute first so userspace
3353 * can specify both for older kernels.
3354 */
3355 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3356 if (nla) {
3357 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003358
Johannes Bergeccb8e82009-05-11 21:57:56 +03003359 sta_flags = nla_data(nla);
3360 params->sta_flags_mask = sta_flags->mask;
3361 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003362 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003363 if ((params->sta_flags_mask |
3364 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3365 return -EINVAL;
3366 return 0;
3367 }
3368
3369 /* if present, parse the old attribute */
3370
3371 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003372 if (!nla)
3373 return 0;
3374
3375 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3376 nla, sta_flags_policy))
3377 return -EINVAL;
3378
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003379 /*
3380 * Only allow certain flags for interface types so that
3381 * other attributes are silently ignored. Remember that
3382 * this is backward compatibility code with old userspace
3383 * and shouldn't be hit in other cases anyway.
3384 */
3385 switch (iftype) {
3386 case NL80211_IFTYPE_AP:
3387 case NL80211_IFTYPE_AP_VLAN:
3388 case NL80211_IFTYPE_P2P_GO:
3389 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3390 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3391 BIT(NL80211_STA_FLAG_WME) |
3392 BIT(NL80211_STA_FLAG_MFP);
3393 break;
3394 case NL80211_IFTYPE_P2P_CLIENT:
3395 case NL80211_IFTYPE_STATION:
3396 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3397 BIT(NL80211_STA_FLAG_TDLS_PEER);
3398 break;
3399 case NL80211_IFTYPE_MESH_POINT:
3400 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3401 BIT(NL80211_STA_FLAG_MFP) |
3402 BIT(NL80211_STA_FLAG_AUTHORIZED);
3403 default:
3404 return -EINVAL;
3405 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003406
Johannes Berg3383b5a2012-05-10 20:14:43 +02003407 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3408 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003409 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003410
Johannes Berg3383b5a2012-05-10 20:14:43 +02003411 /* no longer support new API additions in old API */
3412 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3413 return -EINVAL;
3414 }
3415 }
3416
Johannes Berg5727ef12007-12-19 02:03:34 +01003417 return 0;
3418}
3419
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003420static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3421 int attr)
3422{
3423 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003424 u32 bitrate;
3425 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003426
3427 rate = nla_nest_start(msg, attr);
3428 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003429 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003430
3431 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3432 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003433 /* report 16-bit bitrate only if we can */
3434 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003435 if (bitrate > 0 &&
3436 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3437 return false;
3438 if (bitrate_compat > 0 &&
3439 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3440 return false;
3441
3442 if (info->flags & RATE_INFO_FLAGS_MCS) {
3443 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3444 return false;
3445 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3446 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3447 return false;
3448 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3449 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3450 return false;
3451 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3452 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3453 return false;
3454 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3455 return false;
3456 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3457 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3458 return false;
3459 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3460 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3461 return false;
3462 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3463 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3464 return false;
3465 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3466 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3467 return false;
3468 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3469 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3470 return false;
3471 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003472
3473 nla_nest_end(msg, rate);
3474 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003475}
3476
Felix Fietkau119363c2013-04-22 16:29:30 +02003477static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3478 int id)
3479{
3480 void *attr;
3481 int i = 0;
3482
3483 if (!mask)
3484 return true;
3485
3486 attr = nla_nest_start(msg, id);
3487 if (!attr)
3488 return false;
3489
3490 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3491 if (!(mask & BIT(i)))
3492 continue;
3493
3494 if (nla_put_u8(msg, i, signal[i]))
3495 return false;
3496 }
3497
3498 nla_nest_end(msg, attr);
3499
3500 return true;
3501}
3502
Eric W. Biederman15e47302012-09-07 20:12:54 +00003503static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003504 int flags,
3505 struct cfg80211_registered_device *rdev,
3506 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003507 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003508{
3509 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003510 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003511
Eric W. Biederman15e47302012-09-07 20:12:54 +00003512 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003513 if (!hdr)
3514 return -1;
3515
David S. Miller9360ffd2012-03-29 04:41:26 -04003516 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3517 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3518 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3519 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003520
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003521 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3522 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003523 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003524 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3525 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3526 sinfo->connected_time))
3527 goto nla_put_failure;
3528 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3529 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3530 sinfo->inactive_time))
3531 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003532 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3533 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003534 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003535 (u32)sinfo->rx_bytes))
3536 goto nla_put_failure;
3537 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003538 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003539 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3540 (u32)sinfo->tx_bytes))
3541 goto nla_put_failure;
3542 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3543 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003544 sinfo->rx_bytes))
3545 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003546 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3547 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003548 sinfo->tx_bytes))
3549 goto nla_put_failure;
3550 if ((sinfo->filled & STATION_INFO_LLID) &&
3551 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3552 goto nla_put_failure;
3553 if ((sinfo->filled & STATION_INFO_PLID) &&
3554 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3555 goto nla_put_failure;
3556 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3557 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3558 sinfo->plink_state))
3559 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003560 switch (rdev->wiphy.signal_type) {
3561 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003562 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3563 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3564 sinfo->signal))
3565 goto nla_put_failure;
3566 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3567 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3568 sinfo->signal_avg))
3569 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003570 break;
3571 default:
3572 break;
3573 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003574 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3575 if (!nl80211_put_signal(msg, sinfo->chains,
3576 sinfo->chain_signal,
3577 NL80211_STA_INFO_CHAIN_SIGNAL))
3578 goto nla_put_failure;
3579 }
3580 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3581 if (!nl80211_put_signal(msg, sinfo->chains,
3582 sinfo->chain_signal_avg,
3583 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3584 goto nla_put_failure;
3585 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003586 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003587 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3588 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003589 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003590 }
3591 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3592 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3593 NL80211_STA_INFO_RX_BITRATE))
3594 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003595 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003596 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3597 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3598 sinfo->rx_packets))
3599 goto nla_put_failure;
3600 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3601 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3602 sinfo->tx_packets))
3603 goto nla_put_failure;
3604 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3605 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3606 sinfo->tx_retries))
3607 goto nla_put_failure;
3608 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3609 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3610 sinfo->tx_failed))
3611 goto nla_put_failure;
3612 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3613 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3614 sinfo->beacon_loss_count))
3615 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003616 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3617 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3618 sinfo->local_pm))
3619 goto nla_put_failure;
3620 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3621 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3622 sinfo->peer_pm))
3623 goto nla_put_failure;
3624 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3625 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3626 sinfo->nonpeer_pm))
3627 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003628 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3629 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3630 if (!bss_param)
3631 goto nla_put_failure;
3632
David S. Miller9360ffd2012-03-29 04:41:26 -04003633 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3634 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3635 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3636 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3637 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3638 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3639 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3640 sinfo->bss_param.dtim_period) ||
3641 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3642 sinfo->bss_param.beacon_interval))
3643 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003644
3645 nla_nest_end(msg, bss_param);
3646 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003647 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3648 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3649 sizeof(struct nl80211_sta_flag_update),
3650 &sinfo->sta_flags))
3651 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003652 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3653 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3654 sinfo->t_offset))
3655 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003656 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003657
David S. Miller9360ffd2012-03-29 04:41:26 -04003658 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3659 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3660 sinfo->assoc_req_ies))
3661 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003662
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003663 return genlmsg_end(msg, hdr);
3664
3665 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003666 genlmsg_cancel(msg, hdr);
3667 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003668}
3669
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003670static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003671 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003672{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003673 struct station_info sinfo;
3674 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003675 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003676 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003677 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003678 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003679
Johannes Berg97990a02013-04-19 01:02:55 +02003680 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003681 if (err)
3682 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003683
Johannes Berg97990a02013-04-19 01:02:55 +02003684 if (!wdev->netdev) {
3685 err = -EINVAL;
3686 goto out_err;
3687 }
3688
Johannes Bergbba95fe2008-07-29 13:22:51 +02003689 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003690 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003691 goto out_err;
3692 }
3693
Johannes Bergbba95fe2008-07-29 13:22:51 +02003694 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003695 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003696 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003697 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003698 if (err == -ENOENT)
3699 break;
3700 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003701 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003702
3703 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003704 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003705 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003706 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003707 &sinfo) < 0)
3708 goto out;
3709
3710 sta_idx++;
3711 }
3712
3713
3714 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003715 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003716 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003717 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003718 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003719
3720 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003721}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003722
Johannes Berg5727ef12007-12-19 02:03:34 +01003723static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3724{
Johannes Berg4c476992010-10-04 21:36:35 +02003725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3726 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003727 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003728 struct sk_buff *msg;
3729 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003730 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003731
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003732 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003733
3734 if (!info->attrs[NL80211_ATTR_MAC])
3735 return -EINVAL;
3736
3737 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3738
Johannes Berg4c476992010-10-04 21:36:35 +02003739 if (!rdev->ops->get_station)
3740 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003741
Hila Gonene35e4d22012-06-27 17:19:42 +03003742 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003743 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003744 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003745
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003747 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003748 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003749
Eric W. Biederman15e47302012-09-07 20:12:54 +00003750 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003751 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003752 nlmsg_free(msg);
3753 return -ENOBUFS;
3754 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003755
Johannes Berg4c476992010-10-04 21:36:35 +02003756 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003757}
3758
Johannes Berg77ee7c82013-02-15 00:48:33 +01003759int cfg80211_check_station_change(struct wiphy *wiphy,
3760 struct station_parameters *params,
3761 enum cfg80211_station_type statype)
3762{
3763 if (params->listen_interval != -1)
3764 return -EINVAL;
3765 if (params->aid)
3766 return -EINVAL;
3767
3768 /* When you run into this, adjust the code below for the new flag */
3769 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3770
3771 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003772 case CFG80211_STA_MESH_PEER_KERNEL:
3773 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003774 /*
3775 * No ignoring the TDLS flag here -- the userspace mesh
3776 * code doesn't have the bug of including TDLS in the
3777 * mask everywhere.
3778 */
3779 if (params->sta_flags_mask &
3780 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3781 BIT(NL80211_STA_FLAG_MFP) |
3782 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3783 return -EINVAL;
3784 break;
3785 case CFG80211_STA_TDLS_PEER_SETUP:
3786 case CFG80211_STA_TDLS_PEER_ACTIVE:
3787 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3788 return -EINVAL;
3789 /* ignore since it can't change */
3790 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3791 break;
3792 default:
3793 /* disallow mesh-specific things */
3794 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3795 return -EINVAL;
3796 if (params->local_pm)
3797 return -EINVAL;
3798 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3799 return -EINVAL;
3800 }
3801
3802 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3803 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3804 /* TDLS can't be set, ... */
3805 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3806 return -EINVAL;
3807 /*
3808 * ... but don't bother the driver with it. This works around
3809 * a hostapd/wpa_supplicant issue -- it always includes the
3810 * TLDS_PEER flag in the mask even for AP mode.
3811 */
3812 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3813 }
3814
3815 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3816 /* reject other things that can't change */
3817 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3818 return -EINVAL;
3819 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3820 return -EINVAL;
3821 if (params->supported_rates)
3822 return -EINVAL;
3823 if (params->ext_capab || params->ht_capa || params->vht_capa)
3824 return -EINVAL;
3825 }
3826
3827 if (statype != CFG80211_STA_AP_CLIENT) {
3828 if (params->vlan)
3829 return -EINVAL;
3830 }
3831
3832 switch (statype) {
3833 case CFG80211_STA_AP_MLME_CLIENT:
3834 /* Use this only for authorizing/unauthorizing a station */
3835 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3836 return -EOPNOTSUPP;
3837 break;
3838 case CFG80211_STA_AP_CLIENT:
3839 /* accept only the listed bits */
3840 if (params->sta_flags_mask &
3841 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3842 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3843 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3844 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3845 BIT(NL80211_STA_FLAG_WME) |
3846 BIT(NL80211_STA_FLAG_MFP)))
3847 return -EINVAL;
3848
3849 /* but authenticated/associated only if driver handles it */
3850 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3851 params->sta_flags_mask &
3852 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3853 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3854 return -EINVAL;
3855 break;
3856 case CFG80211_STA_IBSS:
3857 case CFG80211_STA_AP_STA:
3858 /* reject any changes other than AUTHORIZED */
3859 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3860 return -EINVAL;
3861 break;
3862 case CFG80211_STA_TDLS_PEER_SETUP:
3863 /* reject any changes other than AUTHORIZED or WME */
3864 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3865 BIT(NL80211_STA_FLAG_WME)))
3866 return -EINVAL;
3867 /* force (at least) rates when authorizing */
3868 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3869 !params->supported_rates)
3870 return -EINVAL;
3871 break;
3872 case CFG80211_STA_TDLS_PEER_ACTIVE:
3873 /* reject any changes */
3874 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003875 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003876 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3877 return -EINVAL;
3878 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003879 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003880 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3881 return -EINVAL;
3882 break;
3883 }
3884
3885 return 0;
3886}
3887EXPORT_SYMBOL(cfg80211_check_station_change);
3888
Johannes Berg5727ef12007-12-19 02:03:34 +01003889/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003890 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003891 */
Johannes Berg80b99892011-11-18 16:23:01 +01003892static struct net_device *get_vlan(struct genl_info *info,
3893 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003894{
Johannes Berg463d0182009-07-14 00:33:35 +02003895 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003896 struct net_device *v;
3897 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003898
Johannes Berg80b99892011-11-18 16:23:01 +01003899 if (!vlanattr)
3900 return NULL;
3901
3902 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3903 if (!v)
3904 return ERR_PTR(-ENODEV);
3905
3906 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3907 ret = -EINVAL;
3908 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003909 }
Johannes Berg80b99892011-11-18 16:23:01 +01003910
Johannes Berg77ee7c82013-02-15 00:48:33 +01003911 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3912 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3913 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3914 ret = -EINVAL;
3915 goto error;
3916 }
3917
Johannes Berg80b99892011-11-18 16:23:01 +01003918 if (!netif_running(v)) {
3919 ret = -ENETDOWN;
3920 goto error;
3921 }
3922
3923 return v;
3924 error:
3925 dev_put(v);
3926 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003927}
3928
Johannes Berg94e860f2014-01-20 23:58:15 +01003929static const struct nla_policy
3930nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003931 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3932 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3933};
3934
Johannes Bergff276692013-02-15 00:09:01 +01003935static int nl80211_parse_sta_wme(struct genl_info *info,
3936 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003937{
Jouni Malinendf881292013-02-14 21:10:54 +02003938 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3939 struct nlattr *nla;
3940 int err;
3941
Jouni Malinendf881292013-02-14 21:10:54 +02003942 /* parse WME attributes if present */
3943 if (!info->attrs[NL80211_ATTR_STA_WME])
3944 return 0;
3945
3946 nla = info->attrs[NL80211_ATTR_STA_WME];
3947 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3948 nl80211_sta_wme_policy);
3949 if (err)
3950 return err;
3951
3952 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3953 params->uapsd_queues = nla_get_u8(
3954 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3955 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3956 return -EINVAL;
3957
3958 if (tb[NL80211_STA_WME_MAX_SP])
3959 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3960
3961 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3962 return -EINVAL;
3963
3964 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3965
3966 return 0;
3967}
3968
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303969static int nl80211_parse_sta_channel_info(struct genl_info *info,
3970 struct station_parameters *params)
3971{
3972 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3973 params->supported_channels =
3974 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3975 params->supported_channels_len =
3976 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3977 /*
3978 * Need to include at least one (first channel, number of
3979 * channels) tuple for each subband, and must have proper
3980 * tuples for the rest of the data as well.
3981 */
3982 if (params->supported_channels_len < 2)
3983 return -EINVAL;
3984 if (params->supported_channels_len % 2)
3985 return -EINVAL;
3986 }
3987
3988 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3989 params->supported_oper_classes =
3990 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3991 params->supported_oper_classes_len =
3992 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3993 /*
3994 * The value of the Length field of the Supported Operating
3995 * Classes element is between 2 and 253.
3996 */
3997 if (params->supported_oper_classes_len < 2 ||
3998 params->supported_oper_classes_len > 253)
3999 return -EINVAL;
4000 }
4001 return 0;
4002}
4003
Johannes Bergff276692013-02-15 00:09:01 +01004004static int nl80211_set_station_tdls(struct genl_info *info,
4005 struct station_parameters *params)
4006{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304007 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004008 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004009 if (info->attrs[NL80211_ATTR_PEER_AID])
4010 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004011 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4012 params->ht_capa =
4013 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4014 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4015 params->vht_capa =
4016 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4017
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304018 err = nl80211_parse_sta_channel_info(info, params);
4019 if (err)
4020 return err;
4021
Johannes Bergff276692013-02-15 00:09:01 +01004022 return nl80211_parse_sta_wme(info, params);
4023}
4024
Johannes Berg5727ef12007-12-19 02:03:34 +01004025static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4026{
Johannes Berg4c476992010-10-04 21:36:35 +02004027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004028 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004029 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004030 u8 *mac_addr;
4031 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004032
4033 memset(&params, 0, sizeof(params));
4034
4035 params.listen_interval = -1;
4036
Johannes Berg77ee7c82013-02-15 00:48:33 +01004037 if (!rdev->ops->change_station)
4038 return -EOPNOTSUPP;
4039
Johannes Berg5727ef12007-12-19 02:03:34 +01004040 if (info->attrs[NL80211_ATTR_STA_AID])
4041 return -EINVAL;
4042
4043 if (!info->attrs[NL80211_ATTR_MAC])
4044 return -EINVAL;
4045
4046 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4047
4048 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4049 params.supported_rates =
4050 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4051 params.supported_rates_len =
4052 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4053 }
4054
Jouni Malinen9d62a982013-02-14 21:10:13 +02004055 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4056 params.capability =
4057 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4058 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4059 }
4060
4061 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4062 params.ext_capab =
4063 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4064 params.ext_capab_len =
4065 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4066 }
4067
Jouni Malinendf881292013-02-14 21:10:54 +02004068 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004069 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03004070
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004071 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004072 return -EINVAL;
4073
Johannes Bergf8bacc22013-02-14 23:27:01 +01004074 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004075 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004076 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4077 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4078 return -EINVAL;
4079 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004080
Johannes Bergf8bacc22013-02-14 23:27:01 +01004081 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004082 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004083 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4084 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4085 return -EINVAL;
4086 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4087 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004088
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004089 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4090 enum nl80211_mesh_power_mode pm = nla_get_u32(
4091 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4092
4093 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4094 pm > NL80211_MESH_POWER_MAX)
4095 return -EINVAL;
4096
4097 params.local_pm = pm;
4098 }
4099
Johannes Berg77ee7c82013-02-15 00:48:33 +01004100 /* Include parameters for TDLS peer (will check later) */
4101 err = nl80211_set_station_tdls(info, &params);
4102 if (err)
4103 return err;
4104
4105 params.vlan = get_vlan(info, rdev);
4106 if (IS_ERR(params.vlan))
4107 return PTR_ERR(params.vlan);
4108
Johannes Berga97f4422009-06-18 17:23:43 +02004109 switch (dev->ieee80211_ptr->iftype) {
4110 case NL80211_IFTYPE_AP:
4111 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004112 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004113 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004114 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004115 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004116 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004117 break;
4118 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004119 err = -EOPNOTSUPP;
4120 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004121 }
4122
Johannes Berg77ee7c82013-02-15 00:48:33 +01004123 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004124 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004125
Johannes Berg77ee7c82013-02-15 00:48:33 +01004126 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004127 if (params.vlan)
4128 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004129
Johannes Berg5727ef12007-12-19 02:03:34 +01004130 return err;
4131}
4132
4133static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4134{
Johannes Berg4c476992010-10-04 21:36:35 +02004135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004136 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004137 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004138 struct station_parameters params;
4139 u8 *mac_addr = NULL;
4140
4141 memset(&params, 0, sizeof(params));
4142
Johannes Berg984c3112013-02-14 23:43:25 +01004143 if (!rdev->ops->add_station)
4144 return -EOPNOTSUPP;
4145
Johannes Berg5727ef12007-12-19 02:03:34 +01004146 if (!info->attrs[NL80211_ATTR_MAC])
4147 return -EINVAL;
4148
Johannes Berg5727ef12007-12-19 02:03:34 +01004149 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4150 return -EINVAL;
4151
4152 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4153 return -EINVAL;
4154
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004155 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4156 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004157 return -EINVAL;
4158
Johannes Berg5727ef12007-12-19 02:03:34 +01004159 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4160 params.supported_rates =
4161 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4162 params.supported_rates_len =
4163 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4164 params.listen_interval =
4165 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004166
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004167 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004168 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004169 else
4170 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004171 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4172 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004173
Jouni Malinen9d62a982013-02-14 21:10:13 +02004174 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4175 params.capability =
4176 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4177 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4178 }
4179
4180 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4181 params.ext_capab =
4182 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4183 params.ext_capab_len =
4184 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4185 }
4186
Jouni Malinen36aedc92008-08-25 11:58:58 +03004187 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4188 params.ht_capa =
4189 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004190
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004191 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4192 params.vht_capa =
4193 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4194
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004195 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4196 params.opmode_notif_used = true;
4197 params.opmode_notif =
4198 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4199 }
4200
Johannes Bergf8bacc22013-02-14 23:27:01 +01004201 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004202 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004203 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4204 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4205 return -EINVAL;
4206 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004207
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304208 err = nl80211_parse_sta_channel_info(info, &params);
4209 if (err)
4210 return err;
4211
Johannes Bergff276692013-02-15 00:09:01 +01004212 err = nl80211_parse_sta_wme(info, &params);
4213 if (err)
4214 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004215
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004216 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004217 return -EINVAL;
4218
Johannes Berg77ee7c82013-02-15 00:48:33 +01004219 /* When you run into this, adjust the code below for the new flag */
4220 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4221
Johannes Bergbdd90d52011-12-14 12:20:27 +01004222 switch (dev->ieee80211_ptr->iftype) {
4223 case NL80211_IFTYPE_AP:
4224 case NL80211_IFTYPE_AP_VLAN:
4225 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004226 /* ignore WME attributes if iface/sta is not capable */
4227 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4228 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4229 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004230
Johannes Bergbdd90d52011-12-14 12:20:27 +01004231 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004232 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4233 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004234 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004235 /* but don't bother the driver with it */
4236 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004237
Johannes Bergd582cff2012-10-26 17:53:44 +02004238 /* allow authenticated/associated only if driver handles it */
4239 if (!(rdev->wiphy.features &
4240 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4241 params.sta_flags_mask &
4242 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4243 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4244 return -EINVAL;
4245
Johannes Bergbdd90d52011-12-14 12:20:27 +01004246 /* must be last in here for error handling */
4247 params.vlan = get_vlan(info, rdev);
4248 if (IS_ERR(params.vlan))
4249 return PTR_ERR(params.vlan);
4250 break;
4251 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004252 /* ignore uAPSD data */
4253 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4254
Johannes Bergd582cff2012-10-26 17:53:44 +02004255 /* associated is disallowed */
4256 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4257 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004258 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004259 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4260 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004261 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004262 break;
4263 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004264 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004265 /* ignore uAPSD data */
4266 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4267
Johannes Berg77ee7c82013-02-15 00:48:33 +01004268 /* these are disallowed */
4269 if (params.sta_flags_mask &
4270 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4271 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004272 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004273 /* Only TDLS peers can be added */
4274 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4275 return -EINVAL;
4276 /* Can only add if TDLS ... */
4277 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4278 return -EOPNOTSUPP;
4279 /* ... with external setup is supported */
4280 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4281 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004282 /*
4283 * Older wpa_supplicant versions always mark the TDLS peer
4284 * as authorized, but it shouldn't yet be.
4285 */
4286 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004287 break;
4288 default:
4289 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004290 }
4291
Johannes Bergbdd90d52011-12-14 12:20:27 +01004292 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004293
Hila Gonene35e4d22012-06-27 17:19:42 +03004294 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004295
Johannes Berg5727ef12007-12-19 02:03:34 +01004296 if (params.vlan)
4297 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004298 return err;
4299}
4300
4301static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4302{
Johannes Berg4c476992010-10-04 21:36:35 +02004303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4304 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004305 u8 *mac_addr = NULL;
4306
4307 if (info->attrs[NL80211_ATTR_MAC])
4308 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4309
Johannes Berge80cf852009-05-11 14:43:13 +02004310 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004311 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004312 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4314 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004315
Johannes Berg4c476992010-10-04 21:36:35 +02004316 if (!rdev->ops->del_station)
4317 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004318
Hila Gonene35e4d22012-06-27 17:19:42 +03004319 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004320}
4321
Eric W. Biederman15e47302012-09-07 20:12:54 +00004322static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004323 int flags, struct net_device *dev,
4324 u8 *dst, u8 *next_hop,
4325 struct mpath_info *pinfo)
4326{
4327 void *hdr;
4328 struct nlattr *pinfoattr;
4329
Eric W. Biederman15e47302012-09-07 20:12:54 +00004330 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331 if (!hdr)
4332 return -1;
4333
David S. Miller9360ffd2012-03-29 04:41:26 -04004334 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4335 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4336 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4337 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4338 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004339
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004340 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4341 if (!pinfoattr)
4342 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004343 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4344 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4345 pinfo->frame_qlen))
4346 goto nla_put_failure;
4347 if (((pinfo->filled & MPATH_INFO_SN) &&
4348 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4349 ((pinfo->filled & MPATH_INFO_METRIC) &&
4350 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4351 pinfo->metric)) ||
4352 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4353 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4354 pinfo->exptime)) ||
4355 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4356 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4357 pinfo->flags)) ||
4358 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4359 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4360 pinfo->discovery_timeout)) ||
4361 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4362 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4363 pinfo->discovery_retries)))
4364 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004365
4366 nla_nest_end(msg, pinfoattr);
4367
4368 return genlmsg_end(msg, hdr);
4369
4370 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004371 genlmsg_cancel(msg, hdr);
4372 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004373}
4374
4375static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004376 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004377{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004378 struct mpath_info pinfo;
4379 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004380 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004381 u8 dst[ETH_ALEN];
4382 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004383 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004384 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004385
Johannes Berg97990a02013-04-19 01:02:55 +02004386 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004387 if (err)
4388 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004389
4390 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004391 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004392 goto out_err;
4393 }
4394
Johannes Berg97990a02013-04-19 01:02:55 +02004395 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004396 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004397 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004398 }
4399
Johannes Bergbba95fe2008-07-29 13:22:51 +02004400 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004401 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4402 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004403 if (err == -ENOENT)
4404 break;
4405 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004406 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004407
Eric W. Biederman15e47302012-09-07 20:12:54 +00004408 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004409 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004410 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004411 &pinfo) < 0)
4412 goto out;
4413
4414 path_idx++;
4415 }
4416
4417
4418 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004419 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004420 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004421 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004422 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004423 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004424}
4425
4426static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4427{
Johannes Berg4c476992010-10-04 21:36:35 +02004428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004429 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004430 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004431 struct mpath_info pinfo;
4432 struct sk_buff *msg;
4433 u8 *dst = NULL;
4434 u8 next_hop[ETH_ALEN];
4435
4436 memset(&pinfo, 0, sizeof(pinfo));
4437
4438 if (!info->attrs[NL80211_ATTR_MAC])
4439 return -EINVAL;
4440
4441 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4442
Johannes Berg4c476992010-10-04 21:36:35 +02004443 if (!rdev->ops->get_mpath)
4444 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004445
Johannes Berg4c476992010-10-04 21:36:35 +02004446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4447 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004448
Hila Gonene35e4d22012-06-27 17:19:42 +03004449 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004450 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004451 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004452
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004454 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004455 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004456
Eric W. Biederman15e47302012-09-07 20:12:54 +00004457 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004458 dev, dst, next_hop, &pinfo) < 0) {
4459 nlmsg_free(msg);
4460 return -ENOBUFS;
4461 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004464}
4465
4466static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4467{
Johannes Berg4c476992010-10-04 21:36:35 +02004468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4469 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470 u8 *dst = NULL;
4471 u8 *next_hop = NULL;
4472
4473 if (!info->attrs[NL80211_ATTR_MAC])
4474 return -EINVAL;
4475
4476 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4477 return -EINVAL;
4478
4479 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4480 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4481
Johannes Berg4c476992010-10-04 21:36:35 +02004482 if (!rdev->ops->change_mpath)
4483 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004484
Johannes Berg4c476992010-10-04 21:36:35 +02004485 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4486 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487
Hila Gonene35e4d22012-06-27 17:19:42 +03004488 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004489}
Johannes Berg4c476992010-10-04 21:36:35 +02004490
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4494 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004495 u8 *dst = NULL;
4496 u8 *next_hop = NULL;
4497
4498 if (!info->attrs[NL80211_ATTR_MAC])
4499 return -EINVAL;
4500
4501 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4502 return -EINVAL;
4503
4504 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4505 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4506
Johannes Berg4c476992010-10-04 21:36:35 +02004507 if (!rdev->ops->add_mpath)
4508 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004509
Johannes Berg4c476992010-10-04 21:36:35 +02004510 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4511 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004512
Hila Gonene35e4d22012-06-27 17:19:42 +03004513 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004514}
4515
4516static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4517{
Johannes Berg4c476992010-10-04 21:36:35 +02004518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4519 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004520 u8 *dst = NULL;
4521
4522 if (info->attrs[NL80211_ATTR_MAC])
4523 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4524
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (!rdev->ops->del_mpath)
4526 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004527
Hila Gonene35e4d22012-06-27 17:19:42 +03004528 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004529}
4530
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004531static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4532{
Johannes Berg4c476992010-10-04 21:36:35 +02004533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4534 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004535 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004536 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004537 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004538
4539 memset(&params, 0, sizeof(params));
4540 /* default to not changing parameters */
4541 params.use_cts_prot = -1;
4542 params.use_short_preamble = -1;
4543 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004544 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004545 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004546 params.p2p_ctwindow = -1;
4547 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004548
4549 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4550 params.use_cts_prot =
4551 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4552 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4553 params.use_short_preamble =
4554 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4555 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4556 params.use_short_slot_time =
4557 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004558 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4559 params.basic_rates =
4560 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4561 params.basic_rates_len =
4562 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4563 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004564 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4565 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004566 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4567 params.ht_opmode =
4568 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004569
Johannes Berg53cabad2012-11-14 15:17:28 +01004570 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4571 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4572 return -EINVAL;
4573 params.p2p_ctwindow =
4574 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4575 if (params.p2p_ctwindow < 0)
4576 return -EINVAL;
4577 if (params.p2p_ctwindow != 0 &&
4578 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4579 return -EINVAL;
4580 }
4581
4582 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4583 u8 tmp;
4584
4585 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4586 return -EINVAL;
4587 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4588 if (tmp > 1)
4589 return -EINVAL;
4590 params.p2p_opp_ps = tmp;
4591 if (params.p2p_opp_ps &&
4592 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4593 return -EINVAL;
4594 }
4595
Johannes Berg4c476992010-10-04 21:36:35 +02004596 if (!rdev->ops->change_bss)
4597 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004598
Johannes Berg074ac8d2010-09-16 14:58:22 +02004599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004600 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4601 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004602
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004603 wdev_lock(wdev);
4604 err = rdev_change_bss(rdev, dev, &params);
4605 wdev_unlock(wdev);
4606
4607 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004608}
4609
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004610static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004611 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4612 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4613 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4614 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4615 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4616 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4617};
4618
4619static int parse_reg_rule(struct nlattr *tb[],
4620 struct ieee80211_reg_rule *reg_rule)
4621{
4622 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4623 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4624
4625 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4626 return -EINVAL;
4627 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4628 return -EINVAL;
4629 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4630 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004631 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4632 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004633 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4634 return -EINVAL;
4635
4636 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4637
4638 freq_range->start_freq_khz =
4639 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4640 freq_range->end_freq_khz =
4641 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004642 freq_range->max_bandwidth_khz =
4643 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004644
4645 power_rule->max_eirp =
4646 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4647
4648 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4649 power_rule->max_antenna_gain =
4650 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4651
4652 return 0;
4653}
4654
4655static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4656{
4657 int r;
4658 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004659 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004660
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004661 /*
4662 * You should only get this when cfg80211 hasn't yet initialized
4663 * completely when built-in to the kernel right between the time
4664 * window between nl80211_init() and regulatory_init(), if that is
4665 * even possible.
4666 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004667 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004668 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004669
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004670 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4671 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004672
4673 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4674
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004675 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4676 user_reg_hint_type =
4677 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4678 else
4679 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4680
4681 switch (user_reg_hint_type) {
4682 case NL80211_USER_REG_HINT_USER:
4683 case NL80211_USER_REG_HINT_CELL_BASE:
4684 break;
4685 default:
4686 return -EINVAL;
4687 }
4688
4689 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004690
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004691 return r;
4692}
4693
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004694static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004695 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004696{
Johannes Berg4c476992010-10-04 21:36:35 +02004697 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004698 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004699 struct wireless_dev *wdev = dev->ieee80211_ptr;
4700 struct mesh_config cur_params;
4701 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004702 void *hdr;
4703 struct nlattr *pinfoattr;
4704 struct sk_buff *msg;
4705
Johannes Berg29cbe682010-12-03 09:20:44 +01004706 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4707 return -EOPNOTSUPP;
4708
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004709 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004710 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004711
Johannes Berg29cbe682010-12-03 09:20:44 +01004712 wdev_lock(wdev);
4713 /* If not connected, get default parameters */
4714 if (!wdev->mesh_id_len)
4715 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4716 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004717 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004718 wdev_unlock(wdev);
4719
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004720 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004721 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004722
4723 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004724 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004725 if (!msg)
4726 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004727 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004728 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004729 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004730 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004731 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004732 if (!pinfoattr)
4733 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004734 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4735 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4736 cur_params.dot11MeshRetryTimeout) ||
4737 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4738 cur_params.dot11MeshConfirmTimeout) ||
4739 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4740 cur_params.dot11MeshHoldingTimeout) ||
4741 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4742 cur_params.dot11MeshMaxPeerLinks) ||
4743 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4744 cur_params.dot11MeshMaxRetries) ||
4745 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4746 cur_params.dot11MeshTTL) ||
4747 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4748 cur_params.element_ttl) ||
4749 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4750 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004751 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4752 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004753 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4754 cur_params.dot11MeshHWMPmaxPREQretries) ||
4755 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4756 cur_params.path_refresh_time) ||
4757 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4758 cur_params.min_discovery_timeout) ||
4759 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4760 cur_params.dot11MeshHWMPactivePathTimeout) ||
4761 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4762 cur_params.dot11MeshHWMPpreqMinInterval) ||
4763 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4764 cur_params.dot11MeshHWMPperrMinInterval) ||
4765 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4766 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4767 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4768 cur_params.dot11MeshHWMPRootMode) ||
4769 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4770 cur_params.dot11MeshHWMPRannInterval) ||
4771 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4772 cur_params.dot11MeshGateAnnouncementProtocol) ||
4773 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4774 cur_params.dot11MeshForwarding) ||
4775 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004776 cur_params.rssi_threshold) ||
4777 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004778 cur_params.ht_opmode) ||
4779 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4780 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4781 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004782 cur_params.dot11MeshHWMProotInterval) ||
4783 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004784 cur_params.dot11MeshHWMPconfirmationInterval) ||
4785 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4786 cur_params.power_mode) ||
4787 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004788 cur_params.dot11MeshAwakeWindowDuration) ||
4789 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4790 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004791 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004792 nla_nest_end(msg, pinfoattr);
4793 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004794 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004795
Johannes Berg3b858752009-03-12 09:55:09 +01004796 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004797 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004798 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004799 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004800 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004801}
4802
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004803static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004804 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4805 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4806 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4807 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4808 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4809 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004810 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004811 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004812 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004813 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4814 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4815 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4816 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4817 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004818 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004819 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004820 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004821 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004822 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004823 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004824 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4825 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004826 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4827 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004828 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004829 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4830 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004831 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004832};
4833
Javier Cardonac80d5452010-12-16 17:37:49 -08004834static const struct nla_policy
4835 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004836 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004837 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4838 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004839 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004840 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004841 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004842 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004843 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004844 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004845};
4846
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004847static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004848 struct mesh_config *cfg,
4849 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004850{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004851 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004852 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004853
Marco Porschea54fba2013-01-07 16:04:48 +01004854#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4855do { \
4856 if (tb[attr]) { \
4857 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4858 return -EINVAL; \
4859 cfg->param = fn(tb[attr]); \
4860 mask |= (1 << (attr - 1)); \
4861 } \
4862} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004863
4864
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004865 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004866 return -EINVAL;
4867 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004868 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004869 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004870 return -EINVAL;
4871
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004872 /* This makes sure that there aren't more than 32 mesh config
4873 * parameters (otherwise our bitfield scheme would not work.) */
4874 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4875
4876 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004877 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004878 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4879 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004880 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004881 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4882 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004883 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004884 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4885 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004886 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004887 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4888 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004889 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004890 mask, NL80211_MESHCONF_MAX_RETRIES,
4891 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004892 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004893 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004895 mask, NL80211_MESHCONF_ELEMENT_TTL,
4896 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004897 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004898 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4899 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4901 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004902 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4903 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004904 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004905 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4906 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004907 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004908 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4909 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004910 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004911 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4912 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4914 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004915 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4916 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004917 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004918 1, 65535, mask,
4919 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004920 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004921 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004922 1, 65535, mask,
4923 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004924 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004926 dot11MeshHWMPnetDiameterTraversalTime,
4927 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004928 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4929 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004930 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4931 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4932 nla_get_u8);
4933 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4934 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004935 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004936 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004937 dot11MeshGateAnnouncementProtocol, 0, 1,
4938 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004939 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004941 mask, NL80211_MESHCONF_FORWARDING,
4942 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004944 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004945 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004946 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004947 mask, NL80211_MESHCONF_HT_OPMODE,
4948 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004949 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004950 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004951 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4952 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004953 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004954 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4955 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004956 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004957 dot11MeshHWMPconfirmationInterval,
4958 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004959 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4960 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004961 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4962 NL80211_MESH_POWER_ACTIVE,
4963 NL80211_MESH_POWER_MAX,
4964 mask, NL80211_MESHCONF_POWER_MODE,
4965 nla_get_u32);
4966 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4967 0, 65535, mask,
4968 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004969 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4970 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4971 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004972 if (mask_out)
4973 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004974
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004975 return 0;
4976
4977#undef FILL_IN_MESH_PARAM_IF_SET
4978}
4979
Javier Cardonac80d5452010-12-16 17:37:49 -08004980static int nl80211_parse_mesh_setup(struct genl_info *info,
4981 struct mesh_setup *setup)
4982{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004984 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4985
4986 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4987 return -EINVAL;
4988 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4989 info->attrs[NL80211_ATTR_MESH_SETUP],
4990 nl80211_mesh_setup_params_policy))
4991 return -EINVAL;
4992
Javier Cardonad299a1f2012-03-31 11:31:33 -07004993 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4994 setup->sync_method =
4995 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4996 IEEE80211_SYNC_METHOD_VENDOR :
4997 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4998
Javier Cardonac80d5452010-12-16 17:37:49 -08004999 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5000 setup->path_sel_proto =
5001 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5002 IEEE80211_PATH_PROTOCOL_VENDOR :
5003 IEEE80211_PATH_PROTOCOL_HWMP;
5004
5005 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5006 setup->path_metric =
5007 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5008 IEEE80211_PATH_METRIC_VENDOR :
5009 IEEE80211_PATH_METRIC_AIRTIME;
5010
Javier Cardona581a8b02011-04-07 15:08:27 -07005011
5012 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005013 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005014 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005015 if (!is_valid_ie_attr(ieattr))
5016 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005017 setup->ie = nla_data(ieattr);
5018 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005019 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005020 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5021 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5022 return -EINVAL;
5023 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005024 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5025 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005026 if (setup->is_secure)
5027 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005028
Colleen Twitty6e16d902013-05-08 11:45:59 -07005029 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5030 if (!setup->user_mpm)
5031 return -EINVAL;
5032 setup->auth_id =
5033 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5034 }
5035
Javier Cardonac80d5452010-12-16 17:37:49 -08005036 return 0;
5037}
5038
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005039static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005040 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005041{
5042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5043 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005044 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005045 struct mesh_config cfg;
5046 u32 mask;
5047 int err;
5048
Johannes Berg29cbe682010-12-03 09:20:44 +01005049 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5050 return -EOPNOTSUPP;
5051
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005052 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005053 return -EOPNOTSUPP;
5054
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005055 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005056 if (err)
5057 return err;
5058
Johannes Berg29cbe682010-12-03 09:20:44 +01005059 wdev_lock(wdev);
5060 if (!wdev->mesh_id_len)
5061 err = -ENOLINK;
5062
5063 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005064 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005065
5066 wdev_unlock(wdev);
5067
5068 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005069}
5070
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005071static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5072{
Johannes Berg458f4f92012-12-06 15:47:38 +01005073 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005074 struct sk_buff *msg;
5075 void *hdr = NULL;
5076 struct nlattr *nl_reg_rules;
5077 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005078
5079 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005080 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005081
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005082 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005083 if (!msg)
5084 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005085
Eric W. Biederman15e47302012-09-07 20:12:54 +00005086 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005087 NL80211_CMD_GET_REG);
5088 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005089 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005090
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005091 if (reg_last_request_cell_base() &&
5092 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5093 NL80211_USER_REG_HINT_CELL_BASE))
5094 goto nla_put_failure;
5095
Johannes Berg458f4f92012-12-06 15:47:38 +01005096 rcu_read_lock();
5097 regdom = rcu_dereference(cfg80211_regdomain);
5098
5099 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5100 (regdom->dfs_region &&
5101 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5102 goto nla_put_failure_rcu;
5103
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005104 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5105 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005106 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005107
Johannes Berg458f4f92012-12-06 15:47:38 +01005108 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005109 struct nlattr *nl_reg_rule;
5110 const struct ieee80211_reg_rule *reg_rule;
5111 const struct ieee80211_freq_range *freq_range;
5112 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005113 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005114
Johannes Berg458f4f92012-12-06 15:47:38 +01005115 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005116 freq_range = &reg_rule->freq_range;
5117 power_rule = &reg_rule->power_rule;
5118
5119 nl_reg_rule = nla_nest_start(msg, i);
5120 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005121 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005122
Janusz Dziedzic97524822014-01-30 09:52:20 +01005123 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5124 if (!max_bandwidth_khz)
5125 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5126 reg_rule);
5127
David S. Miller9360ffd2012-03-29 04:41:26 -04005128 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5129 reg_rule->flags) ||
5130 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5131 freq_range->start_freq_khz) ||
5132 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5133 freq_range->end_freq_khz) ||
5134 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005135 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005136 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5137 power_rule->max_antenna_gain) ||
5138 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5139 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005140 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005141
5142 nla_nest_end(msg, nl_reg_rule);
5143 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005144 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005145
5146 nla_nest_end(msg, nl_reg_rules);
5147
5148 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005149 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005150
Johannes Berg458f4f92012-12-06 15:47:38 +01005151nla_put_failure_rcu:
5152 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005153nla_put_failure:
5154 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005155put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005156 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005157 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005158}
5159
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005160static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5161{
5162 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5163 struct nlattr *nl_reg_rule;
5164 char *alpha2 = NULL;
5165 int rem_reg_rules = 0, r = 0;
5166 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005167 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005168 struct ieee80211_regdomain *rd = NULL;
5169
5170 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5171 return -EINVAL;
5172
5173 if (!info->attrs[NL80211_ATTR_REG_RULES])
5174 return -EINVAL;
5175
5176 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5177
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005178 if (info->attrs[NL80211_ATTR_DFS_REGION])
5179 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5180
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005181 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005182 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005183 num_rules++;
5184 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005185 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005186 }
5187
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005188 if (!reg_is_valid_request(alpha2))
5189 return -EINVAL;
5190
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005191 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005192 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005193
5194 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005195 if (!rd)
5196 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005197
5198 rd->n_reg_rules = num_rules;
5199 rd->alpha2[0] = alpha2[0];
5200 rd->alpha2[1] = alpha2[1];
5201
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005202 /*
5203 * Disable DFS master mode if the DFS region was
5204 * not supported or known on this kernel.
5205 */
5206 if (reg_supported_dfs_region(dfs_region))
5207 rd->dfs_region = dfs_region;
5208
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005209 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005210 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005211 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5212 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5213 reg_rule_policy);
5214 if (r)
5215 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005216 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5217 if (r)
5218 goto bad_reg;
5219
5220 rule_idx++;
5221
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005222 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5223 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005224 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005225 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005226 }
5227
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005228 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005229 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005230 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005231
Johannes Bergd2372b32008-10-24 20:32:20 +02005232 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005233 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005234 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005235}
5236
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005237static int validate_scan_freqs(struct nlattr *freqs)
5238{
5239 struct nlattr *attr1, *attr2;
5240 int n_channels = 0, tmp1, tmp2;
5241
5242 nla_for_each_nested(attr1, freqs, tmp1) {
5243 n_channels++;
5244 /*
5245 * Some hardware has a limited channel list for
5246 * scanning, and it is pretty much nonsensical
5247 * to scan for a channel twice, so disallow that
5248 * and don't require drivers to check that the
5249 * channel list they get isn't longer than what
5250 * they can scan, as long as they can scan all
5251 * the channels they registered at once.
5252 */
5253 nla_for_each_nested(attr2, freqs, tmp2)
5254 if (attr1 != attr2 &&
5255 nla_get_u32(attr1) == nla_get_u32(attr2))
5256 return 0;
5257 }
5258
5259 return n_channels;
5260}
5261
Johannes Berg2a519312009-02-10 21:25:55 +01005262static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5263{
Johannes Berg4c476992010-10-04 21:36:35 +02005264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005265 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005266 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005267 struct nlattr *attr;
5268 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005269 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005270 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005271
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005272 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5273 return -EINVAL;
5274
Johannes Berg79c97e92009-07-07 03:56:12 +02005275 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005276
Johannes Berg4c476992010-10-04 21:36:35 +02005277 if (!rdev->ops->scan)
5278 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005279
Johannes Bergf9d15d12014-01-22 11:14:19 +02005280 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005281 err = -EBUSY;
5282 goto unlock;
5283 }
Johannes Berg2a519312009-02-10 21:25:55 +01005284
5285 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005286 n_channels = validate_scan_freqs(
5287 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005288 if (!n_channels) {
5289 err = -EINVAL;
5290 goto unlock;
5291 }
Johannes Berg2a519312009-02-10 21:25:55 +01005292 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005293 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005294 }
5295
5296 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5297 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5298 n_ssids++;
5299
Johannes Bergf9f47522013-03-19 15:04:07 +01005300 if (n_ssids > wiphy->max_scan_ssids) {
5301 err = -EINVAL;
5302 goto unlock;
5303 }
Johannes Berg2a519312009-02-10 21:25:55 +01005304
Jouni Malinen70692ad2009-02-16 19:39:13 +02005305 if (info->attrs[NL80211_ATTR_IE])
5306 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5307 else
5308 ie_len = 0;
5309
Johannes Bergf9f47522013-03-19 15:04:07 +01005310 if (ie_len > wiphy->max_scan_ie_len) {
5311 err = -EINVAL;
5312 goto unlock;
5313 }
Johannes Berg18a83652009-03-31 12:12:05 +02005314
Johannes Berg2a519312009-02-10 21:25:55 +01005315 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005316 + sizeof(*request->ssids) * n_ssids
5317 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005318 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005319 if (!request) {
5320 err = -ENOMEM;
5321 goto unlock;
5322 }
Johannes Berg2a519312009-02-10 21:25:55 +01005323
Johannes Berg2a519312009-02-10 21:25:55 +01005324 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005325 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005326 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005327 if (ie_len) {
5328 if (request->ssids)
5329 request->ie = (void *)(request->ssids + n_ssids);
5330 else
5331 request->ie = (void *)(request->channels + n_channels);
5332 }
Johannes Berg2a519312009-02-10 21:25:55 +01005333
Johannes Berg584991d2009-11-02 13:32:03 +01005334 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005335 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5336 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005337 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005338 struct ieee80211_channel *chan;
5339
5340 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5341
5342 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005343 err = -EINVAL;
5344 goto out_free;
5345 }
Johannes Berg584991d2009-11-02 13:32:03 +01005346
5347 /* ignore disabled channels */
5348 if (chan->flags & IEEE80211_CHAN_DISABLED)
5349 continue;
5350
5351 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005352 i++;
5353 }
5354 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005355 enum ieee80211_band band;
5356
Johannes Berg2a519312009-02-10 21:25:55 +01005357 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005358 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5359 int j;
5360 if (!wiphy->bands[band])
5361 continue;
5362 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005363 struct ieee80211_channel *chan;
5364
5365 chan = &wiphy->bands[band]->channels[j];
5366
5367 if (chan->flags & IEEE80211_CHAN_DISABLED)
5368 continue;
5369
5370 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005371 i++;
5372 }
5373 }
5374 }
5375
Johannes Berg584991d2009-11-02 13:32:03 +01005376 if (!i) {
5377 err = -EINVAL;
5378 goto out_free;
5379 }
5380
5381 request->n_channels = i;
5382
Johannes Berg2a519312009-02-10 21:25:55 +01005383 i = 0;
5384 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5385 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005386 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005387 err = -EINVAL;
5388 goto out_free;
5389 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005390 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005391 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005392 i++;
5393 }
5394 }
5395
Jouni Malinen70692ad2009-02-16 19:39:13 +02005396 if (info->attrs[NL80211_ATTR_IE]) {
5397 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005398 memcpy((void *)request->ie,
5399 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005400 request->ie_len);
5401 }
5402
Johannes Berg34850ab2011-07-18 18:08:35 +02005403 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005404 if (wiphy->bands[i])
5405 request->rates[i] =
5406 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005407
5408 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5409 nla_for_each_nested(attr,
5410 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5411 tmp) {
5412 enum ieee80211_band band = nla_type(attr);
5413
Dan Carpenter84404622011-07-29 11:52:18 +03005414 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005415 err = -EINVAL;
5416 goto out_free;
5417 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005418
5419 if (!wiphy->bands[band])
5420 continue;
5421
Johannes Berg34850ab2011-07-18 18:08:35 +02005422 err = ieee80211_get_ratemask(wiphy->bands[band],
5423 nla_data(attr),
5424 nla_len(attr),
5425 &request->rates[band]);
5426 if (err)
5427 goto out_free;
5428 }
5429 }
5430
Sam Leffler46856bb2012-10-11 21:03:32 -07005431 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005432 request->flags = nla_get_u32(
5433 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005434 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5435 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005436 err = -EOPNOTSUPP;
5437 goto out_free;
5438 }
5439 }
Sam Lefflered4737712012-10-11 21:03:31 -07005440
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305441 request->no_cck =
5442 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5443
Johannes Bergfd014282012-06-18 19:17:03 +02005444 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005445 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005446 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005447
Johannes Berg79c97e92009-07-07 03:56:12 +02005448 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005449 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005450
Johannes Berg463d0182009-07-14 00:33:35 +02005451 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005452 nl80211_send_scan_start(rdev, wdev);
5453 if (wdev->netdev)
5454 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005455 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005456 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005457 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005458 kfree(request);
5459 }
Johannes Berg3b858752009-03-12 09:55:09 +01005460
Johannes Bergf9f47522013-03-19 15:04:07 +01005461 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005462 return err;
5463}
5464
Luciano Coelho807f8a82011-05-11 17:09:35 +03005465static int nl80211_start_sched_scan(struct sk_buff *skb,
5466 struct genl_info *info)
5467{
5468 struct cfg80211_sched_scan_request *request;
5469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5470 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005471 struct nlattr *attr;
5472 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005473 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005474 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005475 enum ieee80211_band band;
5476 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005477 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005478 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005479
5480 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5481 !rdev->ops->sched_scan_start)
5482 return -EOPNOTSUPP;
5483
5484 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5485 return -EINVAL;
5486
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005487 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5488 return -EINVAL;
5489
5490 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5491 if (interval == 0)
5492 return -EINVAL;
5493
Luciano Coelho807f8a82011-05-11 17:09:35 +03005494 wiphy = &rdev->wiphy;
5495
5496 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5497 n_channels = validate_scan_freqs(
5498 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5499 if (!n_channels)
5500 return -EINVAL;
5501 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005502 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005503 }
5504
5505 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5506 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5507 tmp)
5508 n_ssids++;
5509
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005510 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005511 return -EINVAL;
5512
Johannes Bergea73cbc2014-01-24 10:53:53 +01005513 /*
5514 * First, count the number of 'real' matchsets. Due to an issue with
5515 * the old implementation, matchsets containing only the RSSI attribute
5516 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5517 * RSSI for all matchsets, rather than their own matchset for reporting
5518 * all APs with a strong RSSI. This is needed to be compatible with
5519 * older userspace that treated a matchset with only the RSSI as the
5520 * global RSSI for all other matchsets - if there are other matchsets.
5521 */
5522 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005523 nla_for_each_nested(attr,
5524 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005525 tmp) {
5526 struct nlattr *rssi;
5527
5528 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5529 nla_data(attr), nla_len(attr),
5530 nl80211_match_policy);
5531 if (err)
5532 return err;
5533 /* add other standalone attributes here */
5534 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5535 n_match_sets++;
5536 continue;
5537 }
5538 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5539 if (rssi)
5540 default_match_rssi = nla_get_s32(rssi);
5541 }
5542 }
5543
5544 /* However, if there's no other matchset, add the RSSI one */
5545 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5546 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005547
5548 if (n_match_sets > wiphy->max_match_sets)
5549 return -EINVAL;
5550
Luciano Coelho807f8a82011-05-11 17:09:35 +03005551 if (info->attrs[NL80211_ATTR_IE])
5552 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5553 else
5554 ie_len = 0;
5555
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005556 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005557 return -EINVAL;
5558
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005559 if (rdev->sched_scan_req) {
5560 err = -EINPROGRESS;
5561 goto out;
5562 }
5563
Luciano Coelho807f8a82011-05-11 17:09:35 +03005564 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005565 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005566 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005567 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005568 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005569 if (!request) {
5570 err = -ENOMEM;
5571 goto out;
5572 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005573
5574 if (n_ssids)
5575 request->ssids = (void *)&request->channels[n_channels];
5576 request->n_ssids = n_ssids;
5577 if (ie_len) {
5578 if (request->ssids)
5579 request->ie = (void *)(request->ssids + n_ssids);
5580 else
5581 request->ie = (void *)(request->channels + n_channels);
5582 }
5583
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005584 if (n_match_sets) {
5585 if (request->ie)
5586 request->match_sets = (void *)(request->ie + ie_len);
5587 else if (request->ssids)
5588 request->match_sets =
5589 (void *)(request->ssids + n_ssids);
5590 else
5591 request->match_sets =
5592 (void *)(request->channels + n_channels);
5593 }
5594 request->n_match_sets = n_match_sets;
5595
Luciano Coelho807f8a82011-05-11 17:09:35 +03005596 i = 0;
5597 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5598 /* user specified, bail out if channel not found */
5599 nla_for_each_nested(attr,
5600 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5601 tmp) {
5602 struct ieee80211_channel *chan;
5603
5604 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5605
5606 if (!chan) {
5607 err = -EINVAL;
5608 goto out_free;
5609 }
5610
5611 /* ignore disabled channels */
5612 if (chan->flags & IEEE80211_CHAN_DISABLED)
5613 continue;
5614
5615 request->channels[i] = chan;
5616 i++;
5617 }
5618 } else {
5619 /* all channels */
5620 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5621 int j;
5622 if (!wiphy->bands[band])
5623 continue;
5624 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5625 struct ieee80211_channel *chan;
5626
5627 chan = &wiphy->bands[band]->channels[j];
5628
5629 if (chan->flags & IEEE80211_CHAN_DISABLED)
5630 continue;
5631
5632 request->channels[i] = chan;
5633 i++;
5634 }
5635 }
5636 }
5637
5638 if (!i) {
5639 err = -EINVAL;
5640 goto out_free;
5641 }
5642
5643 request->n_channels = i;
5644
5645 i = 0;
5646 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5647 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5648 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005649 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005650 err = -EINVAL;
5651 goto out_free;
5652 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005653 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005654 memcpy(request->ssids[i].ssid, nla_data(attr),
5655 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005656 i++;
5657 }
5658 }
5659
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005660 i = 0;
5661 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5662 nla_for_each_nested(attr,
5663 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5664 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005665 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005666
Johannes Bergae811e22014-01-24 10:17:47 +01005667 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5668 nla_data(attr), nla_len(attr),
5669 nl80211_match_policy);
5670 if (err)
5671 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005672 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005673 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005674 if (WARN_ON(i >= n_match_sets)) {
5675 /* this indicates a programming error,
5676 * the loop above should have verified
5677 * things properly
5678 */
5679 err = -EINVAL;
5680 goto out_free;
5681 }
5682
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005683 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5684 err = -EINVAL;
5685 goto out_free;
5686 }
5687 memcpy(request->match_sets[i].ssid.ssid,
5688 nla_data(ssid), nla_len(ssid));
5689 request->match_sets[i].ssid.ssid_len =
5690 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005691 /* special attribute - old implemenation w/a */
5692 request->match_sets[i].rssi_thold =
5693 default_match_rssi;
5694 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5695 if (rssi)
5696 request->match_sets[i].rssi_thold =
5697 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005698 }
5699 i++;
5700 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005701
5702 /* there was no other matchset, so the RSSI one is alone */
5703 if (i == 0)
5704 request->match_sets[0].rssi_thold = default_match_rssi;
5705
5706 request->min_rssi_thold = INT_MAX;
5707 for (i = 0; i < n_match_sets; i++)
5708 request->min_rssi_thold =
5709 min(request->match_sets[i].rssi_thold,
5710 request->min_rssi_thold);
5711 } else {
5712 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005713 }
5714
Johannes Berg9900e482014-02-04 21:01:25 +01005715 if (ie_len) {
5716 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005717 memcpy((void *)request->ie,
5718 nla_data(info->attrs[NL80211_ATTR_IE]),
5719 request->ie_len);
5720 }
5721
Sam Leffler46856bb2012-10-11 21:03:32 -07005722 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005723 request->flags = nla_get_u32(
5724 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005725 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5726 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005727 err = -EOPNOTSUPP;
5728 goto out_free;
5729 }
5730 }
Sam Lefflered4737712012-10-11 21:03:31 -07005731
Luciano Coelho807f8a82011-05-11 17:09:35 +03005732 request->dev = dev;
5733 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005734 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005735 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005736
Hila Gonene35e4d22012-06-27 17:19:42 +03005737 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005738 if (!err) {
5739 rdev->sched_scan_req = request;
5740 nl80211_send_sched_scan(rdev, dev,
5741 NL80211_CMD_START_SCHED_SCAN);
5742 goto out;
5743 }
5744
5745out_free:
5746 kfree(request);
5747out:
5748 return err;
5749}
5750
5751static int nl80211_stop_sched_scan(struct sk_buff *skb,
5752 struct genl_info *info)
5753{
5754 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5755
5756 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5757 !rdev->ops->sched_scan_stop)
5758 return -EOPNOTSUPP;
5759
Johannes Berg5fe231e2013-05-08 21:45:15 +02005760 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005761}
5762
Simon Wunderlich04f39042013-02-08 18:16:19 +01005763static int nl80211_start_radar_detection(struct sk_buff *skb,
5764 struct genl_info *info)
5765{
5766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5767 struct net_device *dev = info->user_ptr[1];
5768 struct wireless_dev *wdev = dev->ieee80211_ptr;
5769 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005770 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005771 int err;
5772
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005773 dfs_region = reg_get_dfs_region(wdev->wiphy);
5774 if (dfs_region == NL80211_DFS_UNSET)
5775 return -EINVAL;
5776
Simon Wunderlich04f39042013-02-08 18:16:19 +01005777 err = nl80211_parse_chandef(rdev, info, &chandef);
5778 if (err)
5779 return err;
5780
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005781 if (netif_carrier_ok(dev))
5782 return -EBUSY;
5783
Simon Wunderlich04f39042013-02-08 18:16:19 +01005784 if (wdev->cac_started)
5785 return -EBUSY;
5786
5787 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5788 if (err < 0)
5789 return err;
5790
5791 if (err == 0)
5792 return -EINVAL;
5793
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005794 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005795 return -EINVAL;
5796
5797 if (!rdev->ops->start_radar_detection)
5798 return -EOPNOTSUPP;
5799
Simon Wunderlich04f39042013-02-08 18:16:19 +01005800 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5801 chandef.chan, CHAN_MODE_SHARED,
5802 BIT(chandef.width));
5803 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005804 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005805
5806 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5807 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005808 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005809 wdev->cac_started = true;
5810 wdev->cac_start_time = jiffies;
5811 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005812 return err;
5813}
5814
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005815static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5816{
5817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5818 struct net_device *dev = info->user_ptr[1];
5819 struct wireless_dev *wdev = dev->ieee80211_ptr;
5820 struct cfg80211_csa_settings params;
5821 /* csa_attrs is defined static to avoid waste of stack size - this
5822 * function is called under RTNL lock, so this should not be a problem.
5823 */
5824 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5825 u8 radar_detect_width = 0;
5826 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005827 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005828
5829 if (!rdev->ops->channel_switch ||
5830 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5831 return -EOPNOTSUPP;
5832
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005833 switch (dev->ieee80211_ptr->iftype) {
5834 case NL80211_IFTYPE_AP:
5835 case NL80211_IFTYPE_P2P_GO:
5836 need_new_beacon = true;
5837
5838 /* useless if AP is not running */
5839 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005840 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005841 break;
5842 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005843 if (!wdev->ssid_len)
5844 return -ENOTCONN;
5845 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005846 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005847 if (!wdev->mesh_id_len)
5848 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005849 break;
5850 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005851 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005852 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005853
5854 memset(&params, 0, sizeof(params));
5855
5856 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5857 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5858 return -EINVAL;
5859
5860 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005861 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005862 return -EINVAL;
5863
5864 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5865
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005866 if (!need_new_beacon)
5867 goto skip_beacons;
5868
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005869 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5870 if (err)
5871 return err;
5872
5873 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5874 info->attrs[NL80211_ATTR_CSA_IES],
5875 nl80211_policy);
5876 if (err)
5877 return err;
5878
5879 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5880 if (err)
5881 return err;
5882
5883 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5884 return -EINVAL;
5885
5886 params.counter_offset_beacon =
5887 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5888 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5889 return -EINVAL;
5890
5891 /* sanity check - counters should be the same */
5892 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5893 params.count)
5894 return -EINVAL;
5895
5896 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5897 params.counter_offset_presp =
5898 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5899 if (params.counter_offset_presp >=
5900 params.beacon_csa.probe_resp_len)
5901 return -EINVAL;
5902
5903 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5904 params.count)
5905 return -EINVAL;
5906 }
5907
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005908skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005909 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5910 if (err)
5911 return err;
5912
5913 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5914 return -EINVAL;
5915
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005916 switch (dev->ieee80211_ptr->iftype) {
5917 case NL80211_IFTYPE_AP:
5918 case NL80211_IFTYPE_P2P_GO:
5919 case NL80211_IFTYPE_ADHOC:
5920 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005921 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5922 &params.chandef);
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005923 if (err < 0)
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005924 return err;
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005925 if (err) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005926 radar_detect_width = BIT(params.chandef.width);
5927 params.radar_required = true;
5928 }
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005929 break;
5930 default:
5931 break;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005932 }
5933
5934 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5935 params.chandef.chan,
5936 CHAN_MODE_SHARED,
5937 radar_detect_width);
5938 if (err)
5939 return err;
5940
5941 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5942 params.block_tx = true;
5943
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005944 wdev_lock(wdev);
5945 err = rdev_channel_switch(rdev, dev, &params);
5946 wdev_unlock(wdev);
5947
5948 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005949}
5950
Johannes Berg9720bb32011-06-21 09:45:33 +02005951static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5952 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005953 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005954 struct wireless_dev *wdev,
5955 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005956{
Johannes Berg48ab9052009-07-10 18:42:31 +02005957 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005958 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005959 void *hdr;
5960 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005961 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005962
5963 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005964
Eric W. Biederman15e47302012-09-07 20:12:54 +00005965 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005966 NL80211_CMD_NEW_SCAN_RESULTS);
5967 if (!hdr)
5968 return -1;
5969
Johannes Berg9720bb32011-06-21 09:45:33 +02005970 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5971
Johannes Berg97990a02013-04-19 01:02:55 +02005972 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5973 goto nla_put_failure;
5974 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005975 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5976 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005977 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5978 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005979
5980 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5981 if (!bss)
5982 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005983 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005984 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005985 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005986
5987 rcu_read_lock();
5988 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005989 if (ies) {
5990 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5991 goto fail_unlock_rcu;
5992 tsf = true;
5993 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5994 ies->len, ies->data))
5995 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005996 }
5997 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005998 if (ies) {
5999 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6000 goto fail_unlock_rcu;
6001 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6002 ies->len, ies->data))
6003 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006004 }
6005 rcu_read_unlock();
6006
David S. Miller9360ffd2012-03-29 04:41:26 -04006007 if (res->beacon_interval &&
6008 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6009 goto nla_put_failure;
6010 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6011 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006012 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006013 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6014 jiffies_to_msecs(jiffies - intbss->ts)))
6015 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006016
Johannes Berg77965c92009-02-18 18:45:06 +01006017 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006018 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006019 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6020 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006021 break;
6022 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006023 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6024 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006025 break;
6026 default:
6027 break;
6028 }
6029
Johannes Berg48ab9052009-07-10 18:42:31 +02006030 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006031 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006032 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006033 if (intbss == wdev->current_bss &&
6034 nla_put_u32(msg, NL80211_BSS_STATUS,
6035 NL80211_BSS_STATUS_ASSOCIATED))
6036 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006037 break;
6038 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006039 if (intbss == wdev->current_bss &&
6040 nla_put_u32(msg, NL80211_BSS_STATUS,
6041 NL80211_BSS_STATUS_IBSS_JOINED))
6042 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006043 break;
6044 default:
6045 break;
6046 }
6047
Johannes Berg2a519312009-02-10 21:25:55 +01006048 nla_nest_end(msg, bss);
6049
6050 return genlmsg_end(msg, hdr);
6051
Johannes Berg8cef2c92013-02-05 16:54:31 +01006052 fail_unlock_rcu:
6053 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006054 nla_put_failure:
6055 genlmsg_cancel(msg, hdr);
6056 return -EMSGSIZE;
6057}
6058
Johannes Berg97990a02013-04-19 01:02:55 +02006059static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006060{
Johannes Berg48ab9052009-07-10 18:42:31 +02006061 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006062 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006063 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006064 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006065 int err;
6066
Johannes Berg97990a02013-04-19 01:02:55 +02006067 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006068 if (err)
6069 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006070
Johannes Berg48ab9052009-07-10 18:42:31 +02006071 wdev_lock(wdev);
6072 spin_lock_bh(&rdev->bss_lock);
6073 cfg80211_bss_expire(rdev);
6074
Johannes Berg9720bb32011-06-21 09:45:33 +02006075 cb->seq = rdev->bss_generation;
6076
Johannes Berg48ab9052009-07-10 18:42:31 +02006077 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006078 if (++idx <= start)
6079 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006080 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006081 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006082 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006083 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006084 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006085 }
6086 }
6087
Johannes Berg48ab9052009-07-10 18:42:31 +02006088 spin_unlock_bh(&rdev->bss_lock);
6089 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006090
Johannes Berg97990a02013-04-19 01:02:55 +02006091 cb->args[2] = idx;
6092 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006093
Johannes Berg67748892010-10-04 21:14:06 +02006094 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006095}
6096
Eric W. Biederman15e47302012-09-07 20:12:54 +00006097static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006098 int flags, struct net_device *dev,
6099 struct survey_info *survey)
6100{
6101 void *hdr;
6102 struct nlattr *infoattr;
6103
Eric W. Biederman15e47302012-09-07 20:12:54 +00006104 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006105 NL80211_CMD_NEW_SURVEY_RESULTS);
6106 if (!hdr)
6107 return -ENOMEM;
6108
David S. Miller9360ffd2012-03-29 04:41:26 -04006109 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6110 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006111
6112 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6113 if (!infoattr)
6114 goto nla_put_failure;
6115
David S. Miller9360ffd2012-03-29 04:41:26 -04006116 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6117 survey->channel->center_freq))
6118 goto nla_put_failure;
6119
6120 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6121 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6122 goto nla_put_failure;
6123 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6124 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6125 goto nla_put_failure;
6126 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6127 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6128 survey->channel_time))
6129 goto nla_put_failure;
6130 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6131 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6132 survey->channel_time_busy))
6133 goto nla_put_failure;
6134 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6135 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6136 survey->channel_time_ext_busy))
6137 goto nla_put_failure;
6138 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6139 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6140 survey->channel_time_rx))
6141 goto nla_put_failure;
6142 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6143 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6144 survey->channel_time_tx))
6145 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006146
6147 nla_nest_end(msg, infoattr);
6148
6149 return genlmsg_end(msg, hdr);
6150
6151 nla_put_failure:
6152 genlmsg_cancel(msg, hdr);
6153 return -EMSGSIZE;
6154}
6155
6156static int nl80211_dump_survey(struct sk_buff *skb,
6157 struct netlink_callback *cb)
6158{
6159 struct survey_info survey;
6160 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006161 struct wireless_dev *wdev;
6162 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006163 int res;
6164
Johannes Berg97990a02013-04-19 01:02:55 +02006165 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006166 if (res)
6167 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006168
Johannes Berg97990a02013-04-19 01:02:55 +02006169 if (!wdev->netdev) {
6170 res = -EINVAL;
6171 goto out_err;
6172 }
6173
Holger Schurig61fa7132009-11-11 12:25:40 +01006174 if (!dev->ops->dump_survey) {
6175 res = -EOPNOTSUPP;
6176 goto out_err;
6177 }
6178
6179 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006180 struct ieee80211_channel *chan;
6181
Johannes Berg97990a02013-04-19 01:02:55 +02006182 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006183 if (res == -ENOENT)
6184 break;
6185 if (res)
6186 goto out_err;
6187
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006188 /* Survey without a channel doesn't make sense */
6189 if (!survey.channel) {
6190 res = -EINVAL;
6191 goto out;
6192 }
6193
6194 chan = ieee80211_get_channel(&dev->wiphy,
6195 survey.channel->center_freq);
6196 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6197 survey_idx++;
6198 continue;
6199 }
6200
Holger Schurig61fa7132009-11-11 12:25:40 +01006201 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006202 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006203 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006204 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006205 goto out;
6206 survey_idx++;
6207 }
6208
6209 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006210 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006211 res = skb->len;
6212 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006213 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006214 return res;
6215}
6216
Samuel Ortizb23aa672009-07-01 21:26:54 +02006217static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6218{
6219 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6220 NL80211_WPA_VERSION_2));
6221}
6222
Jouni Malinen636a5d32009-03-19 13:39:22 +02006223static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6224{
Johannes Berg4c476992010-10-04 21:36:35 +02006225 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6226 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006227 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006228 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6229 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006230 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006231 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006232 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006233
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006234 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6235 return -EINVAL;
6236
6237 if (!info->attrs[NL80211_ATTR_MAC])
6238 return -EINVAL;
6239
Jouni Malinen17780922009-03-27 20:52:47 +02006240 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6241 return -EINVAL;
6242
Johannes Berg19957bb2009-07-02 17:20:43 +02006243 if (!info->attrs[NL80211_ATTR_SSID])
6244 return -EINVAL;
6245
6246 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6247 return -EINVAL;
6248
Johannes Bergfffd0932009-07-08 14:22:54 +02006249 err = nl80211_parse_key(info, &key);
6250 if (err)
6251 return err;
6252
6253 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006254 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6255 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006256 if (!key.p.key || !key.p.key_len)
6257 return -EINVAL;
6258 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6259 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6260 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6261 key.p.key_len != WLAN_KEY_LEN_WEP104))
6262 return -EINVAL;
6263 if (key.idx > 4)
6264 return -EINVAL;
6265 } else {
6266 key.p.key_len = 0;
6267 key.p.key = NULL;
6268 }
6269
Johannes Bergafea0b72010-08-10 09:46:42 +02006270 if (key.idx >= 0) {
6271 int i;
6272 bool ok = false;
6273 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6274 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6275 ok = true;
6276 break;
6277 }
6278 }
Johannes Berg4c476992010-10-04 21:36:35 +02006279 if (!ok)
6280 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006281 }
6282
Johannes Berg4c476992010-10-04 21:36:35 +02006283 if (!rdev->ops->auth)
6284 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006285
Johannes Berg074ac8d2010-09-16 14:58:22 +02006286 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006287 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6288 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006289
Johannes Berg19957bb2009-07-02 17:20:43 +02006290 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006291 chan = nl80211_get_valid_chan(&rdev->wiphy,
6292 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6293 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006294 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006295
Johannes Berg19957bb2009-07-02 17:20:43 +02006296 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6297 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6298
6299 if (info->attrs[NL80211_ATTR_IE]) {
6300 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6301 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6302 }
6303
6304 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006305 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006306 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006307
Jouni Malinene39e5b52012-09-30 19:29:39 +03006308 if (auth_type == NL80211_AUTHTYPE_SAE &&
6309 !info->attrs[NL80211_ATTR_SAE_DATA])
6310 return -EINVAL;
6311
6312 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6313 if (auth_type != NL80211_AUTHTYPE_SAE)
6314 return -EINVAL;
6315 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6316 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6317 /* need to include at least Auth Transaction and Status Code */
6318 if (sae_data_len < 4)
6319 return -EINVAL;
6320 }
6321
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006322 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6323
Johannes Berg95de8172012-01-20 13:55:25 +01006324 /*
6325 * Since we no longer track auth state, ignore
6326 * requests to only change local state.
6327 */
6328 if (local_state_change)
6329 return 0;
6330
Johannes Berg91bf9b22013-05-15 17:44:01 +02006331 wdev_lock(dev->ieee80211_ptr);
6332 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6333 ssid, ssid_len, ie, ie_len,
6334 key.p.key, key.p.key_len, key.idx,
6335 sae_data, sae_data_len);
6336 wdev_unlock(dev->ieee80211_ptr);
6337 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006338}
6339
Johannes Bergc0692b82010-08-27 14:26:53 +03006340static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6341 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006342 struct cfg80211_crypto_settings *settings,
6343 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006344{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006345 memset(settings, 0, sizeof(*settings));
6346
Samuel Ortizb23aa672009-07-01 21:26:54 +02006347 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6348
Johannes Bergc0692b82010-08-27 14:26:53 +03006349 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6350 u16 proto;
6351 proto = nla_get_u16(
6352 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6353 settings->control_port_ethertype = cpu_to_be16(proto);
6354 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6355 proto != ETH_P_PAE)
6356 return -EINVAL;
6357 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6358 settings->control_port_no_encrypt = true;
6359 } else
6360 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6361
Samuel Ortizb23aa672009-07-01 21:26:54 +02006362 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6363 void *data;
6364 int len, i;
6365
6366 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6367 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6368 settings->n_ciphers_pairwise = len / sizeof(u32);
6369
6370 if (len % sizeof(u32))
6371 return -EINVAL;
6372
Johannes Berg3dc27d22009-07-02 21:36:37 +02006373 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006374 return -EINVAL;
6375
6376 memcpy(settings->ciphers_pairwise, data, len);
6377
6378 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006379 if (!cfg80211_supported_cipher_suite(
6380 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006381 settings->ciphers_pairwise[i]))
6382 return -EINVAL;
6383 }
6384
6385 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6386 settings->cipher_group =
6387 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006388 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6389 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006390 return -EINVAL;
6391 }
6392
6393 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6394 settings->wpa_versions =
6395 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6396 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6397 return -EINVAL;
6398 }
6399
6400 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6401 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006402 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006403
6404 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6405 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6406 settings->n_akm_suites = len / sizeof(u32);
6407
6408 if (len % sizeof(u32))
6409 return -EINVAL;
6410
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006411 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6412 return -EINVAL;
6413
Samuel Ortizb23aa672009-07-01 21:26:54 +02006414 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006415 }
6416
6417 return 0;
6418}
6419
Jouni Malinen636a5d32009-03-19 13:39:22 +02006420static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6421{
Johannes Berg4c476992010-10-04 21:36:35 +02006422 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6423 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006424 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006425 struct cfg80211_assoc_request req = {};
6426 const u8 *bssid, *ssid;
6427 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006428
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006429 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6430 return -EINVAL;
6431
6432 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006433 !info->attrs[NL80211_ATTR_SSID] ||
6434 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006435 return -EINVAL;
6436
Johannes Berg4c476992010-10-04 21:36:35 +02006437 if (!rdev->ops->assoc)
6438 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006439
Johannes Berg074ac8d2010-09-16 14:58:22 +02006440 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006441 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6442 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006443
Johannes Berg19957bb2009-07-02 17:20:43 +02006444 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006445
Jouni Malinen664834d2014-01-15 00:01:44 +02006446 chan = nl80211_get_valid_chan(&rdev->wiphy,
6447 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6448 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006449 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006450
Johannes Berg19957bb2009-07-02 17:20:43 +02006451 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6452 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006453
6454 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006455 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6456 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006457 }
6458
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006459 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006460 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006461 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006462 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006463 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006464 else if (mfp != NL80211_MFP_NO)
6465 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006466 }
6467
Johannes Berg3e5d7642009-07-07 14:37:26 +02006468 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006469 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006470
Ben Greear7e7c8922011-11-18 11:31:59 -08006471 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006472 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006473
6474 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006475 memcpy(&req.ht_capa_mask,
6476 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6477 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006478
6479 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006480 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006481 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006482 memcpy(&req.ht_capa,
6483 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6484 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006485 }
6486
Johannes Bergee2aca32013-02-21 17:36:01 +01006487 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006488 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006489
6490 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006491 memcpy(&req.vht_capa_mask,
6492 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6493 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006494
6495 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006496 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006497 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006498 memcpy(&req.vht_capa,
6499 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6500 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006501 }
6502
Johannes Bergf62fab72013-02-21 20:09:09 +01006503 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006504 if (!err) {
6505 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006506 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6507 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006508 wdev_unlock(dev->ieee80211_ptr);
6509 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006510
Jouni Malinen636a5d32009-03-19 13:39:22 +02006511 return err;
6512}
6513
6514static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6515{
Johannes Berg4c476992010-10-04 21:36:35 +02006516 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6517 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006518 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006519 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006520 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006521 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006522
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006523 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6524 return -EINVAL;
6525
6526 if (!info->attrs[NL80211_ATTR_MAC])
6527 return -EINVAL;
6528
6529 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6530 return -EINVAL;
6531
Johannes Berg4c476992010-10-04 21:36:35 +02006532 if (!rdev->ops->deauth)
6533 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006534
Johannes Berg074ac8d2010-09-16 14:58:22 +02006535 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006536 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6537 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006538
Johannes Berg19957bb2009-07-02 17:20:43 +02006539 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006540
Johannes Berg19957bb2009-07-02 17:20:43 +02006541 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6542 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006543 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006544 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006545 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006546
6547 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006548 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6549 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006550 }
6551
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006552 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6553
Johannes Berg91bf9b22013-05-15 17:44:01 +02006554 wdev_lock(dev->ieee80211_ptr);
6555 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6556 local_state_change);
6557 wdev_unlock(dev->ieee80211_ptr);
6558 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006559}
6560
6561static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6562{
Johannes Berg4c476992010-10-04 21:36:35 +02006563 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6564 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006565 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006566 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006567 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006568 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006569
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006570 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6571 return -EINVAL;
6572
6573 if (!info->attrs[NL80211_ATTR_MAC])
6574 return -EINVAL;
6575
6576 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6577 return -EINVAL;
6578
Johannes Berg4c476992010-10-04 21:36:35 +02006579 if (!rdev->ops->disassoc)
6580 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006581
Johannes Berg074ac8d2010-09-16 14:58:22 +02006582 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006583 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6584 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006585
Johannes Berg19957bb2009-07-02 17:20:43 +02006586 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006587
Johannes Berg19957bb2009-07-02 17:20:43 +02006588 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6589 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006590 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006591 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006592 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006593
6594 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006595 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6596 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006597 }
6598
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006599 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6600
Johannes Berg91bf9b22013-05-15 17:44:01 +02006601 wdev_lock(dev->ieee80211_ptr);
6602 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6603 local_state_change);
6604 wdev_unlock(dev->ieee80211_ptr);
6605 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006606}
6607
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006608static bool
6609nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6610 int mcast_rate[IEEE80211_NUM_BANDS],
6611 int rateval)
6612{
6613 struct wiphy *wiphy = &rdev->wiphy;
6614 bool found = false;
6615 int band, i;
6616
6617 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6618 struct ieee80211_supported_band *sband;
6619
6620 sband = wiphy->bands[band];
6621 if (!sband)
6622 continue;
6623
6624 for (i = 0; i < sband->n_bitrates; i++) {
6625 if (sband->bitrates[i].bitrate == rateval) {
6626 mcast_rate[band] = i + 1;
6627 found = true;
6628 break;
6629 }
6630 }
6631 }
6632
6633 return found;
6634}
6635
Johannes Berg04a773a2009-04-19 21:24:32 +02006636static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6637{
Johannes Berg4c476992010-10-04 21:36:35 +02006638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6639 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006640 struct cfg80211_ibss_params ibss;
6641 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006642 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006643 int err;
6644
Johannes Berg8e30bc52009-04-22 17:45:38 +02006645 memset(&ibss, 0, sizeof(ibss));
6646
Johannes Berg04a773a2009-04-19 21:24:32 +02006647 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6648 return -EINVAL;
6649
Johannes Berg683b6d32012-11-08 21:25:48 +01006650 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006651 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6652 return -EINVAL;
6653
Johannes Berg8e30bc52009-04-22 17:45:38 +02006654 ibss.beacon_interval = 100;
6655
6656 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6657 ibss.beacon_interval =
6658 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6659 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6660 return -EINVAL;
6661 }
6662
Johannes Berg4c476992010-10-04 21:36:35 +02006663 if (!rdev->ops->join_ibss)
6664 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006665
Johannes Berg4c476992010-10-04 21:36:35 +02006666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6667 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006668
Johannes Berg79c97e92009-07-07 03:56:12 +02006669 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006670
Johannes Berg39193492011-09-16 13:45:25 +02006671 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006672 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006673
6674 if (!is_valid_ether_addr(ibss.bssid))
6675 return -EINVAL;
6676 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006677 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6678 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6679
6680 if (info->attrs[NL80211_ATTR_IE]) {
6681 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6682 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6683 }
6684
Johannes Berg683b6d32012-11-08 21:25:48 +01006685 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6686 if (err)
6687 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006688
Johannes Berg683b6d32012-11-08 21:25:48 +01006689 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006690 return -EINVAL;
6691
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006692 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006693 case NL80211_CHAN_WIDTH_5:
6694 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006695 case NL80211_CHAN_WIDTH_20_NOHT:
6696 break;
6697 case NL80211_CHAN_WIDTH_20:
6698 case NL80211_CHAN_WIDTH_40:
6699 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6700 break;
6701 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006702 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006703 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006704
Johannes Berg04a773a2009-04-19 21:24:32 +02006705 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006706 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006707
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006708 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6709 u8 *rates =
6710 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6711 int n_rates =
6712 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6713 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006714 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006715
Johannes Berg34850ab2011-07-18 18:08:35 +02006716 err = ieee80211_get_ratemask(sband, rates, n_rates,
6717 &ibss.basic_rates);
6718 if (err)
6719 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006720 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006721
Simon Wunderlich803768f2013-06-28 10:39:58 +02006722 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6723 memcpy(&ibss.ht_capa_mask,
6724 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6725 sizeof(ibss.ht_capa_mask));
6726
6727 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6728 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6729 return -EINVAL;
6730 memcpy(&ibss.ht_capa,
6731 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6732 sizeof(ibss.ht_capa));
6733 }
6734
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006735 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6736 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6737 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6738 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006739
Johannes Berg4c476992010-10-04 21:36:35 +02006740 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306741 bool no_ht = false;
6742
Johannes Berg4c476992010-10-04 21:36:35 +02006743 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306744 info->attrs[NL80211_ATTR_KEYS],
6745 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006746 if (IS_ERR(connkeys))
6747 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306748
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006749 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6750 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306751 kfree(connkeys);
6752 return -EINVAL;
6753 }
Johannes Berg4c476992010-10-04 21:36:35 +02006754 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006755
Antonio Quartulli267335d2012-01-31 20:25:47 +01006756 ibss.control_port =
6757 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6758
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006759 ibss.userspace_handles_dfs =
6760 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6761
Johannes Berg4c476992010-10-04 21:36:35 +02006762 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006763 if (err)
6764 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006765 return err;
6766}
6767
6768static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6769{
Johannes Berg4c476992010-10-04 21:36:35 +02006770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6771 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006772
Johannes Berg4c476992010-10-04 21:36:35 +02006773 if (!rdev->ops->leave_ibss)
6774 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006775
Johannes Berg4c476992010-10-04 21:36:35 +02006776 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6777 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006778
Johannes Berg4c476992010-10-04 21:36:35 +02006779 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006780}
6781
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006782static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6783{
6784 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6785 struct net_device *dev = info->user_ptr[1];
6786 int mcast_rate[IEEE80211_NUM_BANDS];
6787 u32 nla_rate;
6788 int err;
6789
6790 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6791 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6792 return -EOPNOTSUPP;
6793
6794 if (!rdev->ops->set_mcast_rate)
6795 return -EOPNOTSUPP;
6796
6797 memset(mcast_rate, 0, sizeof(mcast_rate));
6798
6799 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6800 return -EINVAL;
6801
6802 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6803 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6804 return -EINVAL;
6805
6806 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6807
6808 return err;
6809}
6810
Johannes Bergad7e7182013-11-13 13:37:47 +01006811static struct sk_buff *
6812__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6813 int approxlen, u32 portid, u32 seq,
6814 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006815 enum nl80211_attrs attr,
6816 const struct nl80211_vendor_cmd_info *info,
6817 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006818{
6819 struct sk_buff *skb;
6820 void *hdr;
6821 struct nlattr *data;
6822
6823 skb = nlmsg_new(approxlen + 100, gfp);
6824 if (!skb)
6825 return NULL;
6826
6827 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6828 if (!hdr) {
6829 kfree_skb(skb);
6830 return NULL;
6831 }
6832
6833 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6834 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006835
6836 if (info) {
6837 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6838 info->vendor_id))
6839 goto nla_put_failure;
6840 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6841 info->subcmd))
6842 goto nla_put_failure;
6843 }
6844
Johannes Bergad7e7182013-11-13 13:37:47 +01006845 data = nla_nest_start(skb, attr);
6846
6847 ((void **)skb->cb)[0] = rdev;
6848 ((void **)skb->cb)[1] = hdr;
6849 ((void **)skb->cb)[2] = data;
6850
6851 return skb;
6852
6853 nla_put_failure:
6854 kfree_skb(skb);
6855 return NULL;
6856}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006857
Johannes Berge03ad6e2014-01-01 17:22:30 +01006858struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6859 enum nl80211_commands cmd,
6860 enum nl80211_attrs attr,
6861 int vendor_event_idx,
6862 int approxlen, gfp_t gfp)
6863{
6864 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6865 const struct nl80211_vendor_cmd_info *info;
6866
6867 switch (cmd) {
6868 case NL80211_CMD_TESTMODE:
6869 if (WARN_ON(vendor_event_idx != -1))
6870 return NULL;
6871 info = NULL;
6872 break;
6873 case NL80211_CMD_VENDOR:
6874 if (WARN_ON(vendor_event_idx < 0 ||
6875 vendor_event_idx >= wiphy->n_vendor_events))
6876 return NULL;
6877 info = &wiphy->vendor_events[vendor_event_idx];
6878 break;
6879 default:
6880 WARN_ON(1);
6881 return NULL;
6882 }
6883
6884 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6885 cmd, attr, info, gfp);
6886}
6887EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6888
6889void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6890{
6891 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6892 void *hdr = ((void **)skb->cb)[1];
6893 struct nlattr *data = ((void **)skb->cb)[2];
6894 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6895
6896 nla_nest_end(skb, data);
6897 genlmsg_end(skb, hdr);
6898
6899 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6900 mcgrp = NL80211_MCGRP_VENDOR;
6901
6902 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6903 mcgrp, gfp);
6904}
6905EXPORT_SYMBOL(__cfg80211_send_event_skb);
6906
Johannes Bergaff89a92009-07-01 21:26:51 +02006907#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006908static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6909{
Johannes Berg4c476992010-10-04 21:36:35 +02006910 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006911 struct wireless_dev *wdev =
6912 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006913 int err;
6914
David Spinadelfc73f112013-07-31 18:04:15 +03006915 if (!rdev->ops->testmode_cmd)
6916 return -EOPNOTSUPP;
6917
6918 if (IS_ERR(wdev)) {
6919 err = PTR_ERR(wdev);
6920 if (err != -EINVAL)
6921 return err;
6922 wdev = NULL;
6923 } else if (wdev->wiphy != &rdev->wiphy) {
6924 return -EINVAL;
6925 }
6926
Johannes Bergaff89a92009-07-01 21:26:51 +02006927 if (!info->attrs[NL80211_ATTR_TESTDATA])
6928 return -EINVAL;
6929
Johannes Bergad7e7182013-11-13 13:37:47 +01006930 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006931 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006932 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6933 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006934 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006935
Johannes Bergaff89a92009-07-01 21:26:51 +02006936 return err;
6937}
6938
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006939static int nl80211_testmode_dump(struct sk_buff *skb,
6940 struct netlink_callback *cb)
6941{
Johannes Berg00918d32011-12-13 17:22:05 +01006942 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006943 int err;
6944 long phy_idx;
6945 void *data = NULL;
6946 int data_len = 0;
6947
Johannes Berg5fe231e2013-05-08 21:45:15 +02006948 rtnl_lock();
6949
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006950 if (cb->args[0]) {
6951 /*
6952 * 0 is a valid index, but not valid for args[0],
6953 * so we need to offset by 1.
6954 */
6955 phy_idx = cb->args[0] - 1;
6956 } else {
6957 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6958 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6959 nl80211_policy);
6960 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006961 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006962
Johannes Berg2bd7e352012-06-15 14:23:16 +02006963 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6964 nl80211_fam.attrbuf);
6965 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006966 err = PTR_ERR(rdev);
6967 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006968 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006969 phy_idx = rdev->wiphy_idx;
6970 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006971
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006972 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6973 cb->args[1] =
6974 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6975 }
6976
6977 if (cb->args[1]) {
6978 data = nla_data((void *)cb->args[1]);
6979 data_len = nla_len((void *)cb->args[1]);
6980 }
6981
Johannes Berg00918d32011-12-13 17:22:05 +01006982 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6983 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006984 err = -ENOENT;
6985 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006986 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006987
Johannes Berg00918d32011-12-13 17:22:05 +01006988 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006989 err = -EOPNOTSUPP;
6990 goto out_err;
6991 }
6992
6993 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006994 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006995 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6996 NL80211_CMD_TESTMODE);
6997 struct nlattr *tmdata;
6998
Dan Carpentercb35fba2013-08-14 14:50:01 +03006999 if (!hdr)
7000 break;
7001
David S. Miller9360ffd2012-03-29 04:41:26 -04007002 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007003 genlmsg_cancel(skb, hdr);
7004 break;
7005 }
7006
7007 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7008 if (!tmdata) {
7009 genlmsg_cancel(skb, hdr);
7010 break;
7011 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007012 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007013 nla_nest_end(skb, tmdata);
7014
7015 if (err == -ENOBUFS || err == -ENOENT) {
7016 genlmsg_cancel(skb, hdr);
7017 break;
7018 } else if (err) {
7019 genlmsg_cancel(skb, hdr);
7020 goto out_err;
7021 }
7022
7023 genlmsg_end(skb, hdr);
7024 }
7025
7026 err = skb->len;
7027 /* see above */
7028 cb->args[0] = phy_idx + 1;
7029 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007030 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007031 return err;
7032}
Johannes Bergaff89a92009-07-01 21:26:51 +02007033#endif
7034
Samuel Ortizb23aa672009-07-01 21:26:54 +02007035static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7036{
Johannes Berg4c476992010-10-04 21:36:35 +02007037 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7038 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007039 struct cfg80211_connect_params connect;
7040 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007041 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007042 int err;
7043
7044 memset(&connect, 0, sizeof(connect));
7045
7046 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7047 return -EINVAL;
7048
7049 if (!info->attrs[NL80211_ATTR_SSID] ||
7050 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7051 return -EINVAL;
7052
7053 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7054 connect.auth_type =
7055 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007056 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7057 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007058 return -EINVAL;
7059 } else
7060 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7061
7062 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7063
Johannes Bergc0692b82010-08-27 14:26:53 +03007064 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007065 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007066 if (err)
7067 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007068
Johannes Berg074ac8d2010-09-16 14:58:22 +02007069 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007070 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7071 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007072
Johannes Berg79c97e92009-07-07 03:56:12 +02007073 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007074
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307075 connect.bg_scan_period = -1;
7076 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7077 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7078 connect.bg_scan_period =
7079 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7080 }
7081
Samuel Ortizb23aa672009-07-01 21:26:54 +02007082 if (info->attrs[NL80211_ATTR_MAC])
7083 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007084 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7085 connect.bssid_hint =
7086 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007087 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7088 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7089
7090 if (info->attrs[NL80211_ATTR_IE]) {
7091 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7092 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7093 }
7094
Jouni Malinencee00a92013-01-15 17:15:57 +02007095 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7096 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7097 if (connect.mfp != NL80211_MFP_REQUIRED &&
7098 connect.mfp != NL80211_MFP_NO)
7099 return -EINVAL;
7100 } else {
7101 connect.mfp = NL80211_MFP_NO;
7102 }
7103
Samuel Ortizb23aa672009-07-01 21:26:54 +02007104 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007105 connect.channel = nl80211_get_valid_chan(
7106 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7107 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007108 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007109 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007110 connect.channel_hint = nl80211_get_valid_chan(
7111 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7112 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007113 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007114 }
7115
Johannes Bergfffd0932009-07-08 14:22:54 +02007116 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7117 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307118 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007119 if (IS_ERR(connkeys))
7120 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007121 }
7122
Ben Greear7e7c8922011-11-18 11:31:59 -08007123 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7124 connect.flags |= ASSOC_REQ_DISABLE_HT;
7125
7126 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7127 memcpy(&connect.ht_capa_mask,
7128 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7129 sizeof(connect.ht_capa_mask));
7130
7131 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007132 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7133 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007134 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007135 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007136 memcpy(&connect.ht_capa,
7137 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7138 sizeof(connect.ht_capa));
7139 }
7140
Johannes Bergee2aca32013-02-21 17:36:01 +01007141 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7142 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7143
7144 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7145 memcpy(&connect.vht_capa_mask,
7146 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7147 sizeof(connect.vht_capa_mask));
7148
7149 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7150 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7151 kfree(connkeys);
7152 return -EINVAL;
7153 }
7154 memcpy(&connect.vht_capa,
7155 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7156 sizeof(connect.vht_capa));
7157 }
7158
Johannes Berg83739b02013-05-15 17:44:01 +02007159 wdev_lock(dev->ieee80211_ptr);
7160 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7161 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007162 if (err)
7163 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007164 return err;
7165}
7166
7167static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7168{
Johannes Berg4c476992010-10-04 21:36:35 +02007169 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7170 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007171 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007172 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007173
7174 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7175 reason = WLAN_REASON_DEAUTH_LEAVING;
7176 else
7177 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7178
7179 if (reason == 0)
7180 return -EINVAL;
7181
Johannes Berg074ac8d2010-09-16 14:58:22 +02007182 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007183 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7184 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007185
Johannes Berg83739b02013-05-15 17:44:01 +02007186 wdev_lock(dev->ieee80211_ptr);
7187 ret = cfg80211_disconnect(rdev, dev, reason, true);
7188 wdev_unlock(dev->ieee80211_ptr);
7189 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007190}
7191
Johannes Berg463d0182009-07-14 00:33:35 +02007192static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7193{
Johannes Berg4c476992010-10-04 21:36:35 +02007194 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007195 struct net *net;
7196 int err;
7197 u32 pid;
7198
7199 if (!info->attrs[NL80211_ATTR_PID])
7200 return -EINVAL;
7201
7202 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7203
Johannes Berg463d0182009-07-14 00:33:35 +02007204 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007205 if (IS_ERR(net))
7206 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007207
7208 err = 0;
7209
7210 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007211 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7212 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007213
Johannes Berg463d0182009-07-14 00:33:35 +02007214 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007215 return err;
7216}
7217
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007218static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7219{
Johannes Berg4c476992010-10-04 21:36:35 +02007220 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007221 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7222 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007223 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007224 struct cfg80211_pmksa pmksa;
7225
7226 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7227
7228 if (!info->attrs[NL80211_ATTR_MAC])
7229 return -EINVAL;
7230
7231 if (!info->attrs[NL80211_ATTR_PMKID])
7232 return -EINVAL;
7233
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007234 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7235 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7236
Johannes Berg074ac8d2010-09-16 14:58:22 +02007237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007238 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7239 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007240
7241 switch (info->genlhdr->cmd) {
7242 case NL80211_CMD_SET_PMKSA:
7243 rdev_ops = rdev->ops->set_pmksa;
7244 break;
7245 case NL80211_CMD_DEL_PMKSA:
7246 rdev_ops = rdev->ops->del_pmksa;
7247 break;
7248 default:
7249 WARN_ON(1);
7250 break;
7251 }
7252
Johannes Berg4c476992010-10-04 21:36:35 +02007253 if (!rdev_ops)
7254 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007255
Johannes Berg4c476992010-10-04 21:36:35 +02007256 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007257}
7258
7259static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7260{
Johannes Berg4c476992010-10-04 21:36:35 +02007261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7262 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007263
Johannes Berg074ac8d2010-09-16 14:58:22 +02007264 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007265 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7266 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007267
Johannes Berg4c476992010-10-04 21:36:35 +02007268 if (!rdev->ops->flush_pmksa)
7269 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007270
Hila Gonene35e4d22012-06-27 17:19:42 +03007271 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007272}
7273
Arik Nemtsov109086c2011-09-28 14:12:50 +03007274static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7275{
7276 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7277 struct net_device *dev = info->user_ptr[1];
7278 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307279 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007280 u16 status_code;
7281 u8 *peer;
7282
7283 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7284 !rdev->ops->tdls_mgmt)
7285 return -EOPNOTSUPP;
7286
7287 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7288 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7289 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7290 !info->attrs[NL80211_ATTR_IE] ||
7291 !info->attrs[NL80211_ATTR_MAC])
7292 return -EINVAL;
7293
7294 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7295 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7296 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7297 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307298 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7299 peer_capability =
7300 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007301
Hila Gonene35e4d22012-06-27 17:19:42 +03007302 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307303 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007304 nla_data(info->attrs[NL80211_ATTR_IE]),
7305 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007306}
7307
7308static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7309{
7310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7311 struct net_device *dev = info->user_ptr[1];
7312 enum nl80211_tdls_operation operation;
7313 u8 *peer;
7314
7315 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7316 !rdev->ops->tdls_oper)
7317 return -EOPNOTSUPP;
7318
7319 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7320 !info->attrs[NL80211_ATTR_MAC])
7321 return -EINVAL;
7322
7323 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7324 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7325
Hila Gonene35e4d22012-06-27 17:19:42 +03007326 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007327}
7328
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007329static int nl80211_remain_on_channel(struct sk_buff *skb,
7330 struct genl_info *info)
7331{
Johannes Berg4c476992010-10-04 21:36:35 +02007332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007333 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007334 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007335 struct sk_buff *msg;
7336 void *hdr;
7337 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007338 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007339 int err;
7340
7341 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7342 !info->attrs[NL80211_ATTR_DURATION])
7343 return -EINVAL;
7344
7345 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7346
Johannes Berg7c4ef712011-11-18 15:33:48 +01007347 if (!rdev->ops->remain_on_channel ||
7348 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007349 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007350
Johannes Bergebf348f2012-06-01 12:50:54 +02007351 /*
7352 * We should be on that channel for at least a minimum amount of
7353 * time (10ms) but no longer than the driver supports.
7354 */
7355 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7356 duration > rdev->wiphy.max_remain_on_channel_duration)
7357 return -EINVAL;
7358
Johannes Berg683b6d32012-11-08 21:25:48 +01007359 err = nl80211_parse_chandef(rdev, info, &chandef);
7360 if (err)
7361 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007362
7363 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007364 if (!msg)
7365 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007366
Eric W. Biederman15e47302012-09-07 20:12:54 +00007367 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007368 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007369 if (!hdr) {
7370 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007371 goto free_msg;
7372 }
7373
Johannes Berg683b6d32012-11-08 21:25:48 +01007374 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7375 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007376
7377 if (err)
7378 goto free_msg;
7379
David S. Miller9360ffd2012-03-29 04:41:26 -04007380 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7381 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007382
7383 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007384
7385 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007386
7387 nla_put_failure:
7388 err = -ENOBUFS;
7389 free_msg:
7390 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007391 return err;
7392}
7393
7394static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7395 struct genl_info *info)
7396{
Johannes Berg4c476992010-10-04 21:36:35 +02007397 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007398 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007399 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007400
7401 if (!info->attrs[NL80211_ATTR_COOKIE])
7402 return -EINVAL;
7403
Johannes Berg4c476992010-10-04 21:36:35 +02007404 if (!rdev->ops->cancel_remain_on_channel)
7405 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007406
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007407 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7408
Hila Gonene35e4d22012-06-27 17:19:42 +03007409 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007410}
7411
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007412static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7413 u8 *rates, u8 rates_len)
7414{
7415 u8 i;
7416 u32 mask = 0;
7417
7418 for (i = 0; i < rates_len; i++) {
7419 int rate = (rates[i] & 0x7f) * 5;
7420 int ridx;
7421 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7422 struct ieee80211_rate *srate =
7423 &sband->bitrates[ridx];
7424 if (rate == srate->bitrate) {
7425 mask |= 1 << ridx;
7426 break;
7427 }
7428 }
7429 if (ridx == sband->n_bitrates)
7430 return 0; /* rate not found */
7431 }
7432
7433 return mask;
7434}
7435
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007436static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7437 u8 *rates, u8 rates_len,
7438 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7439{
7440 u8 i;
7441
7442 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7443
7444 for (i = 0; i < rates_len; i++) {
7445 int ridx, rbit;
7446
7447 ridx = rates[i] / 8;
7448 rbit = BIT(rates[i] % 8);
7449
7450 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007451 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007452 return false;
7453
7454 /* check availability */
7455 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7456 mcs[ridx] |= rbit;
7457 else
7458 return false;
7459 }
7460
7461 return true;
7462}
7463
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007464static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7465{
7466 u16 mcs_mask = 0;
7467
7468 switch (vht_mcs_map) {
7469 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7470 break;
7471 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7472 mcs_mask = 0x00FF;
7473 break;
7474 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7475 mcs_mask = 0x01FF;
7476 break;
7477 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7478 mcs_mask = 0x03FF;
7479 break;
7480 default:
7481 break;
7482 }
7483
7484 return mcs_mask;
7485}
7486
7487static void vht_build_mcs_mask(u16 vht_mcs_map,
7488 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7489{
7490 u8 nss;
7491
7492 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7493 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7494 vht_mcs_map >>= 2;
7495 }
7496}
7497
7498static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7499 struct nl80211_txrate_vht *txrate,
7500 u16 mcs[NL80211_VHT_NSS_MAX])
7501{
7502 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7503 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7504 u8 i;
7505
7506 if (!sband->vht_cap.vht_supported)
7507 return false;
7508
7509 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7510
7511 /* Build vht_mcs_mask from VHT capabilities */
7512 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7513
7514 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7515 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7516 mcs[i] = txrate->mcs[i];
7517 else
7518 return false;
7519 }
7520
7521 return true;
7522}
7523
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007524static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007525 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7526 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007527 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7528 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007529 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007530 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007531};
7532
7533static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7534 struct genl_info *info)
7535{
7536 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007538 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007539 int rem, i;
7540 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007541 struct nlattr *tx_rates;
7542 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007543 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007544
Johannes Berg4c476992010-10-04 21:36:35 +02007545 if (!rdev->ops->set_bitrate_mask)
7546 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007547
7548 memset(&mask, 0, sizeof(mask));
7549 /* Default to all rates enabled */
7550 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7551 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007552
7553 if (!sband)
7554 continue;
7555
7556 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007557 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007558 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007559 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007560
7561 if (!sband->vht_cap.vht_supported)
7562 continue;
7563
7564 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7565 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007566 }
7567
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007568 /* if no rates are given set it back to the defaults */
7569 if (!info->attrs[NL80211_ATTR_TX_RATES])
7570 goto out;
7571
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007572 /*
7573 * The nested attribute uses enum nl80211_band as the index. This maps
7574 * directly to the enum ieee80211_band values used in cfg80211.
7575 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007576 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007577 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007578 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007579 int err;
7580
Johannes Berg4c476992010-10-04 21:36:35 +02007581 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7582 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007583 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007584 if (sband == NULL)
7585 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007586 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7587 nla_len(tx_rates), nl80211_txattr_policy);
7588 if (err)
7589 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007590 if (tb[NL80211_TXRATE_LEGACY]) {
7591 mask.control[band].legacy = rateset_to_mask(
7592 sband,
7593 nla_data(tb[NL80211_TXRATE_LEGACY]),
7594 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307595 if ((mask.control[band].legacy == 0) &&
7596 nla_len(tb[NL80211_TXRATE_LEGACY]))
7597 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007598 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007599 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007600 if (!ht_rateset_to_mask(
7601 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007602 nla_data(tb[NL80211_TXRATE_HT]),
7603 nla_len(tb[NL80211_TXRATE_HT]),
7604 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007605 return -EINVAL;
7606 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007607 if (tb[NL80211_TXRATE_VHT]) {
7608 if (!vht_set_mcs_mask(
7609 sband,
7610 nla_data(tb[NL80211_TXRATE_VHT]),
7611 mask.control[band].vht_mcs))
7612 return -EINVAL;
7613 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007614 if (tb[NL80211_TXRATE_GI]) {
7615 mask.control[band].gi =
7616 nla_get_u8(tb[NL80211_TXRATE_GI]);
7617 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7618 return -EINVAL;
7619 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007620
7621 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007622 /* don't allow empty legacy rates if HT or VHT
7623 * are not even supported.
7624 */
7625 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7626 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007627 return -EINVAL;
7628
7629 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007630 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007631 goto out;
7632
7633 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7634 if (mask.control[band].vht_mcs[i])
7635 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007636
7637 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007638 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007639 }
7640 }
7641
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007642out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007643 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007644}
7645
Johannes Berg2e161f72010-08-12 15:38:38 +02007646static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007647{
Johannes Berg4c476992010-10-04 21:36:35 +02007648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007649 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007650 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007651
7652 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7653 return -EINVAL;
7654
Johannes Berg2e161f72010-08-12 15:38:38 +02007655 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7656 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007657
Johannes Berg71bbc992012-06-15 15:30:18 +02007658 switch (wdev->iftype) {
7659 case NL80211_IFTYPE_STATION:
7660 case NL80211_IFTYPE_ADHOC:
7661 case NL80211_IFTYPE_P2P_CLIENT:
7662 case NL80211_IFTYPE_AP:
7663 case NL80211_IFTYPE_AP_VLAN:
7664 case NL80211_IFTYPE_MESH_POINT:
7665 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007666 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007667 break;
7668 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007669 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007670 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007671
7672 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007673 if (!rdev->ops->mgmt_tx)
7674 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007675
Eric W. Biederman15e47302012-09-07 20:12:54 +00007676 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007677 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7678 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007679}
7680
Johannes Berg2e161f72010-08-12 15:38:38 +02007681static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007682{
Johannes Berg4c476992010-10-04 21:36:35 +02007683 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007684 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007685 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007686 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007687 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007688 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007689 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007690 struct cfg80211_mgmt_tx_params params = {
7691 .dont_wait_for_ack =
7692 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7693 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007694
Johannes Berg683b6d32012-11-08 21:25:48 +01007695 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007696 return -EINVAL;
7697
Johannes Berg4c476992010-10-04 21:36:35 +02007698 if (!rdev->ops->mgmt_tx)
7699 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007700
Johannes Berg71bbc992012-06-15 15:30:18 +02007701 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007702 case NL80211_IFTYPE_P2P_DEVICE:
7703 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7704 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007705 case NL80211_IFTYPE_STATION:
7706 case NL80211_IFTYPE_ADHOC:
7707 case NL80211_IFTYPE_P2P_CLIENT:
7708 case NL80211_IFTYPE_AP:
7709 case NL80211_IFTYPE_AP_VLAN:
7710 case NL80211_IFTYPE_MESH_POINT:
7711 case NL80211_IFTYPE_P2P_GO:
7712 break;
7713 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007714 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007715 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007716
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007717 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007718 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007719 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007720 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007721
7722 /*
7723 * We should wait on the channel for at least a minimum amount
7724 * of time (10ms) but no longer than the driver supports.
7725 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007726 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7727 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007728 return -EINVAL;
7729
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007730 }
7731
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007732 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007733
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007734 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007735 return -EINVAL;
7736
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007737 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307738
Antonio Quartulliea141b752013-06-11 14:20:03 +02007739 /* get the channel if any has been specified, otherwise pass NULL to
7740 * the driver. The latter will use the current one
7741 */
7742 chandef.chan = NULL;
7743 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7744 err = nl80211_parse_chandef(rdev, info, &chandef);
7745 if (err)
7746 return err;
7747 }
7748
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007749 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007750 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007751
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007752 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007753 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7754 if (!msg)
7755 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007756
Eric W. Biederman15e47302012-09-07 20:12:54 +00007757 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007758 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007759 if (!hdr) {
7760 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007761 goto free_msg;
7762 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007763 }
Johannes Berge247bd902011-11-04 11:18:21 +01007764
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007765 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7766 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7767 params.chan = chandef.chan;
7768 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007769 if (err)
7770 goto free_msg;
7771
Johannes Berge247bd902011-11-04 11:18:21 +01007772 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007773 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7774 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007775
Johannes Berge247bd902011-11-04 11:18:21 +01007776 genlmsg_end(msg, hdr);
7777 return genlmsg_reply(msg, info);
7778 }
7779
7780 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007781
7782 nla_put_failure:
7783 err = -ENOBUFS;
7784 free_msg:
7785 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007786 return err;
7787}
7788
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007789static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7790{
7791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007792 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007793 u64 cookie;
7794
7795 if (!info->attrs[NL80211_ATTR_COOKIE])
7796 return -EINVAL;
7797
7798 if (!rdev->ops->mgmt_tx_cancel_wait)
7799 return -EOPNOTSUPP;
7800
Johannes Berg71bbc992012-06-15 15:30:18 +02007801 switch (wdev->iftype) {
7802 case NL80211_IFTYPE_STATION:
7803 case NL80211_IFTYPE_ADHOC:
7804 case NL80211_IFTYPE_P2P_CLIENT:
7805 case NL80211_IFTYPE_AP:
7806 case NL80211_IFTYPE_AP_VLAN:
7807 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007808 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007809 break;
7810 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007811 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007812 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007813
7814 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7815
Hila Gonene35e4d22012-06-27 17:19:42 +03007816 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007817}
7818
Kalle Valoffb9eb32010-02-17 17:58:10 +02007819static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7820{
Johannes Berg4c476992010-10-04 21:36:35 +02007821 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007822 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007823 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007824 u8 ps_state;
7825 bool state;
7826 int err;
7827
Johannes Berg4c476992010-10-04 21:36:35 +02007828 if (!info->attrs[NL80211_ATTR_PS_STATE])
7829 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007830
7831 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7832
Johannes Berg4c476992010-10-04 21:36:35 +02007833 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7834 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007835
7836 wdev = dev->ieee80211_ptr;
7837
Johannes Berg4c476992010-10-04 21:36:35 +02007838 if (!rdev->ops->set_power_mgmt)
7839 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007840
7841 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7842
7843 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007844 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007845
Hila Gonene35e4d22012-06-27 17:19:42 +03007846 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007847 if (!err)
7848 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007849 return err;
7850}
7851
7852static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7853{
Johannes Berg4c476992010-10-04 21:36:35 +02007854 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007855 enum nl80211_ps_state ps_state;
7856 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007857 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007858 struct sk_buff *msg;
7859 void *hdr;
7860 int err;
7861
Kalle Valoffb9eb32010-02-17 17:58:10 +02007862 wdev = dev->ieee80211_ptr;
7863
Johannes Berg4c476992010-10-04 21:36:35 +02007864 if (!rdev->ops->set_power_mgmt)
7865 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007866
7867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007868 if (!msg)
7869 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007870
Eric W. Biederman15e47302012-09-07 20:12:54 +00007871 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007872 NL80211_CMD_GET_POWER_SAVE);
7873 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007874 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007875 goto free_msg;
7876 }
7877
7878 if (wdev->ps)
7879 ps_state = NL80211_PS_ENABLED;
7880 else
7881 ps_state = NL80211_PS_DISABLED;
7882
David S. Miller9360ffd2012-03-29 04:41:26 -04007883 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7884 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007885
7886 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007887 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007888
Johannes Berg4c476992010-10-04 21:36:35 +02007889 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007890 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007891 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007892 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007893 return err;
7894}
7895
Johannes Berg94e860f2014-01-20 23:58:15 +01007896static const struct nla_policy
7897nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007898 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7899 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7900 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007901 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7902 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7903 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007904};
7905
Thomas Pedersen84f10702012-07-12 16:17:33 -07007906static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007907 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007908{
7909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007910 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007911 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007912
Johannes Bergd9d8b012012-11-26 12:51:52 +01007913 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007914 return -EINVAL;
7915
Thomas Pedersen84f10702012-07-12 16:17:33 -07007916 if (!rdev->ops->set_cqm_txe_config)
7917 return -EOPNOTSUPP;
7918
7919 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7920 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7921 return -EOPNOTSUPP;
7922
Hila Gonene35e4d22012-06-27 17:19:42 +03007923 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007924}
7925
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007926static int nl80211_set_cqm_rssi(struct genl_info *info,
7927 s32 threshold, u32 hysteresis)
7928{
Johannes Berg4c476992010-10-04 21:36:35 +02007929 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007930 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007931 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007932
7933 if (threshold > 0)
7934 return -EINVAL;
7935
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007936 /* disabling - hysteresis should also be zero then */
7937 if (threshold == 0)
7938 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007939
Johannes Berg4c476992010-10-04 21:36:35 +02007940 if (!rdev->ops->set_cqm_rssi_config)
7941 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007942
Johannes Berg074ac8d2010-09-16 14:58:22 +02007943 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007944 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7945 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007946
Hila Gonene35e4d22012-06-27 17:19:42 +03007947 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007948}
7949
7950static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7951{
7952 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7953 struct nlattr *cqm;
7954 int err;
7955
7956 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007957 if (!cqm)
7958 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007959
7960 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7961 nl80211_attr_cqm_policy);
7962 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007963 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007964
7965 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7966 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007967 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7968 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007969
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007970 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7971 }
7972
7973 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7974 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7975 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7976 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7977 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7978 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7979
7980 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7981 }
7982
7983 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007984}
7985
Johannes Berg29cbe682010-12-03 09:20:44 +01007986static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7987{
7988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7989 struct net_device *dev = info->user_ptr[1];
7990 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007991 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007992 int err;
7993
7994 /* start with default */
7995 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007996 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007997
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007998 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007999 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008000 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008001 if (err)
8002 return err;
8003 }
8004
8005 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8006 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8007 return -EINVAL;
8008
Javier Cardonac80d5452010-12-16 17:37:49 -08008009 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8010 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8011
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008012 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8013 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8014 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8015 return -EINVAL;
8016
Marco Porsch9bdbf042013-01-07 16:04:51 +01008017 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8018 setup.beacon_interval =
8019 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8020 if (setup.beacon_interval < 10 ||
8021 setup.beacon_interval > 10000)
8022 return -EINVAL;
8023 }
8024
8025 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8026 setup.dtim_period =
8027 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8028 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8029 return -EINVAL;
8030 }
8031
Javier Cardonac80d5452010-12-16 17:37:49 -08008032 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8033 /* parse additional setup parameters if given */
8034 err = nl80211_parse_mesh_setup(info, &setup);
8035 if (err)
8036 return err;
8037 }
8038
Thomas Pedersend37bb182013-03-04 13:06:13 -08008039 if (setup.user_mpm)
8040 cfg.auto_open_plinks = false;
8041
Johannes Bergcc1d2802012-05-16 23:50:20 +02008042 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008043 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8044 if (err)
8045 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008046 } else {
8047 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008048 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008049 }
8050
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008051 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8052 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8053 int n_rates =
8054 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8055 struct ieee80211_supported_band *sband;
8056
8057 if (!setup.chandef.chan)
8058 return -EINVAL;
8059
8060 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8061
8062 err = ieee80211_get_ratemask(sband, rates, n_rates,
8063 &setup.basic_rates);
8064 if (err)
8065 return err;
8066 }
8067
Javier Cardonac80d5452010-12-16 17:37:49 -08008068 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008069}
8070
8071static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8072{
8073 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8074 struct net_device *dev = info->user_ptr[1];
8075
8076 return cfg80211_leave_mesh(rdev, dev);
8077}
8078
Johannes Bergdfb89c52012-06-27 09:23:48 +02008079#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008080static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8081 struct cfg80211_registered_device *rdev)
8082{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008083 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008084 struct nlattr *nl_pats, *nl_pat;
8085 int i, pat_len;
8086
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008087 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008088 return 0;
8089
8090 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8091 if (!nl_pats)
8092 return -ENOBUFS;
8093
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008094 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008095 nl_pat = nla_nest_start(msg, i + 1);
8096 if (!nl_pat)
8097 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008098 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008099 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008100 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008101 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8102 wowlan->patterns[i].pattern) ||
8103 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008104 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008105 return -ENOBUFS;
8106 nla_nest_end(msg, nl_pat);
8107 }
8108 nla_nest_end(msg, nl_pats);
8109
8110 return 0;
8111}
8112
Johannes Berg2a0e0472013-01-23 22:57:40 +01008113static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8114 struct cfg80211_wowlan_tcp *tcp)
8115{
8116 struct nlattr *nl_tcp;
8117
8118 if (!tcp)
8119 return 0;
8120
8121 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8122 if (!nl_tcp)
8123 return -ENOBUFS;
8124
8125 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8126 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8127 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8128 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8129 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8130 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8131 tcp->payload_len, tcp->payload) ||
8132 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8133 tcp->data_interval) ||
8134 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8135 tcp->wake_len, tcp->wake_data) ||
8136 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8137 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8138 return -ENOBUFS;
8139
8140 if (tcp->payload_seq.len &&
8141 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8142 sizeof(tcp->payload_seq), &tcp->payload_seq))
8143 return -ENOBUFS;
8144
8145 if (tcp->payload_tok.len &&
8146 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8147 sizeof(tcp->payload_tok) + tcp->tokens_size,
8148 &tcp->payload_tok))
8149 return -ENOBUFS;
8150
Johannes Berge248ad32013-05-16 10:24:28 +02008151 nla_nest_end(msg, nl_tcp);
8152
Johannes Berg2a0e0472013-01-23 22:57:40 +01008153 return 0;
8154}
8155
Johannes Bergff1b6e62011-05-04 15:37:28 +02008156static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8157{
8158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8159 struct sk_buff *msg;
8160 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008161 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008162
Johannes Berg964dc9e2013-06-03 17:25:34 +02008163 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008164 return -EOPNOTSUPP;
8165
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008166 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008167 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008168 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8169 rdev->wiphy.wowlan_config->tcp->payload_len +
8170 rdev->wiphy.wowlan_config->tcp->wake_len +
8171 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008172 }
8173
8174 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008175 if (!msg)
8176 return -ENOMEM;
8177
Eric W. Biederman15e47302012-09-07 20:12:54 +00008178 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008179 NL80211_CMD_GET_WOWLAN);
8180 if (!hdr)
8181 goto nla_put_failure;
8182
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008183 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008184 struct nlattr *nl_wowlan;
8185
8186 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8187 if (!nl_wowlan)
8188 goto nla_put_failure;
8189
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008190 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008191 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008192 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008193 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008194 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008195 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008196 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008197 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008198 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008199 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008200 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008201 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008202 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008203 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8204 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008205
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008206 if (nl80211_send_wowlan_patterns(msg, rdev))
8207 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008208
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008209 if (nl80211_send_wowlan_tcp(msg,
8210 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008211 goto nla_put_failure;
8212
Johannes Bergff1b6e62011-05-04 15:37:28 +02008213 nla_nest_end(msg, nl_wowlan);
8214 }
8215
8216 genlmsg_end(msg, hdr);
8217 return genlmsg_reply(msg, info);
8218
8219nla_put_failure:
8220 nlmsg_free(msg);
8221 return -ENOBUFS;
8222}
8223
Johannes Berg2a0e0472013-01-23 22:57:40 +01008224static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8225 struct nlattr *attr,
8226 struct cfg80211_wowlan *trig)
8227{
8228 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8229 struct cfg80211_wowlan_tcp *cfg;
8230 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8231 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8232 u32 size;
8233 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8234 int err, port;
8235
Johannes Berg964dc9e2013-06-03 17:25:34 +02008236 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008237 return -EINVAL;
8238
8239 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8240 nla_data(attr), nla_len(attr),
8241 nl80211_wowlan_tcp_policy);
8242 if (err)
8243 return err;
8244
8245 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8246 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8247 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8248 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8249 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8250 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8251 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8252 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8253 return -EINVAL;
8254
8255 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008256 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008257 return -EINVAL;
8258
8259 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008260 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008261 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008262 return -EINVAL;
8263
8264 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008265 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008266 return -EINVAL;
8267
8268 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8269 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8270 return -EINVAL;
8271
8272 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8273 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8274
8275 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8276 tokens_size = tokln - sizeof(*tok);
8277
8278 if (!tok->len || tokens_size % tok->len)
8279 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008280 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008281 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008282 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008283 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008284 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008285 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008286 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008287 return -EINVAL;
8288 if (tok->offset + tok->len > data_size)
8289 return -EINVAL;
8290 }
8291
8292 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8293 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008294 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008295 return -EINVAL;
8296 if (seq->len == 0 || seq->len > 4)
8297 return -EINVAL;
8298 if (seq->len + seq->offset > data_size)
8299 return -EINVAL;
8300 }
8301
8302 size = sizeof(*cfg);
8303 size += data_size;
8304 size += wake_size + wake_mask_size;
8305 size += tokens_size;
8306
8307 cfg = kzalloc(size, GFP_KERNEL);
8308 if (!cfg)
8309 return -ENOMEM;
8310 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8311 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8312 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8313 ETH_ALEN);
8314 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8315 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8316 else
8317 port = 0;
8318#ifdef CONFIG_INET
8319 /* allocate a socket and port for it and use it */
8320 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8321 IPPROTO_TCP, &cfg->sock, 1);
8322 if (err) {
8323 kfree(cfg);
8324 return err;
8325 }
8326 if (inet_csk_get_port(cfg->sock->sk, port)) {
8327 sock_release(cfg->sock);
8328 kfree(cfg);
8329 return -EADDRINUSE;
8330 }
8331 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8332#else
8333 if (!port) {
8334 kfree(cfg);
8335 return -EINVAL;
8336 }
8337 cfg->src_port = port;
8338#endif
8339
8340 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8341 cfg->payload_len = data_size;
8342 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8343 memcpy((void *)cfg->payload,
8344 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8345 data_size);
8346 if (seq)
8347 cfg->payload_seq = *seq;
8348 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8349 cfg->wake_len = wake_size;
8350 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8351 memcpy((void *)cfg->wake_data,
8352 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8353 wake_size);
8354 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8355 data_size + wake_size;
8356 memcpy((void *)cfg->wake_mask,
8357 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8358 wake_mask_size);
8359 if (tok) {
8360 cfg->tokens_size = tokens_size;
8361 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8362 }
8363
8364 trig->tcp = cfg;
8365
8366 return 0;
8367}
8368
Johannes Bergff1b6e62011-05-04 15:37:28 +02008369static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8370{
8371 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8372 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008373 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008374 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008375 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008376 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008377 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008378
Johannes Berg964dc9e2013-06-03 17:25:34 +02008379 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008380 return -EOPNOTSUPP;
8381
Johannes Bergae33bd82012-07-12 16:25:02 +02008382 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8383 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008384 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008385 goto set_wakeup;
8386 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008387
8388 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8389 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8390 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8391 nl80211_wowlan_policy);
8392 if (err)
8393 return err;
8394
8395 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8396 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8397 return -EINVAL;
8398 new_triggers.any = true;
8399 }
8400
8401 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8402 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8403 return -EINVAL;
8404 new_triggers.disconnect = true;
8405 }
8406
8407 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8408 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8409 return -EINVAL;
8410 new_triggers.magic_pkt = true;
8411 }
8412
Johannes Berg77dbbb12011-07-13 10:48:55 +02008413 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8414 return -EINVAL;
8415
8416 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8417 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8418 return -EINVAL;
8419 new_triggers.gtk_rekey_failure = true;
8420 }
8421
8422 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8423 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8424 return -EINVAL;
8425 new_triggers.eap_identity_req = true;
8426 }
8427
8428 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8429 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8430 return -EINVAL;
8431 new_triggers.four_way_handshake = true;
8432 }
8433
8434 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8435 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8436 return -EINVAL;
8437 new_triggers.rfkill_release = true;
8438 }
8439
Johannes Bergff1b6e62011-05-04 15:37:28 +02008440 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8441 struct nlattr *pat;
8442 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008443 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008444 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008445
8446 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8447 rem)
8448 n_patterns++;
8449 if (n_patterns > wowlan->n_patterns)
8450 return -EINVAL;
8451
8452 new_triggers.patterns = kcalloc(n_patterns,
8453 sizeof(new_triggers.patterns[0]),
8454 GFP_KERNEL);
8455 if (!new_triggers.patterns)
8456 return -ENOMEM;
8457
8458 new_triggers.n_patterns = n_patterns;
8459 i = 0;
8460
8461 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8462 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008463 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8464 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008465 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008466 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8467 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008468 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008469 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008470 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008471 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008472 goto error;
8473 if (pat_len > wowlan->pattern_max_len ||
8474 pat_len < wowlan->pattern_min_len)
8475 goto error;
8476
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008477 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008478 pkt_offset = 0;
8479 else
8480 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008481 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008482 if (pkt_offset > wowlan->max_pkt_offset)
8483 goto error;
8484 new_triggers.patterns[i].pkt_offset = pkt_offset;
8485
Johannes Bergff1b6e62011-05-04 15:37:28 +02008486 new_triggers.patterns[i].mask =
8487 kmalloc(mask_len + pat_len, GFP_KERNEL);
8488 if (!new_triggers.patterns[i].mask) {
8489 err = -ENOMEM;
8490 goto error;
8491 }
8492 new_triggers.patterns[i].pattern =
8493 new_triggers.patterns[i].mask + mask_len;
8494 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008495 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008496 mask_len);
8497 new_triggers.patterns[i].pattern_len = pat_len;
8498 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008499 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008500 pat_len);
8501 i++;
8502 }
8503 }
8504
Johannes Berg2a0e0472013-01-23 22:57:40 +01008505 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8506 err = nl80211_parse_wowlan_tcp(
8507 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8508 &new_triggers);
8509 if (err)
8510 goto error;
8511 }
8512
Johannes Bergae33bd82012-07-12 16:25:02 +02008513 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8514 if (!ntrig) {
8515 err = -ENOMEM;
8516 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008517 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008518 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008519 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008520
Johannes Bergae33bd82012-07-12 16:25:02 +02008521 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008522 if (rdev->ops->set_wakeup &&
8523 prev_enabled != !!rdev->wiphy.wowlan_config)
8524 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008525
Johannes Bergff1b6e62011-05-04 15:37:28 +02008526 return 0;
8527 error:
8528 for (i = 0; i < new_triggers.n_patterns; i++)
8529 kfree(new_triggers.patterns[i].mask);
8530 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008531 if (new_triggers.tcp && new_triggers.tcp->sock)
8532 sock_release(new_triggers.tcp->sock);
8533 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008534 return err;
8535}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008536#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008537
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008538static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8539 struct cfg80211_registered_device *rdev)
8540{
8541 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8542 int i, j, pat_len;
8543 struct cfg80211_coalesce_rules *rule;
8544
8545 if (!rdev->coalesce->n_rules)
8546 return 0;
8547
8548 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8549 if (!nl_rules)
8550 return -ENOBUFS;
8551
8552 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8553 nl_rule = nla_nest_start(msg, i + 1);
8554 if (!nl_rule)
8555 return -ENOBUFS;
8556
8557 rule = &rdev->coalesce->rules[i];
8558 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8559 rule->delay))
8560 return -ENOBUFS;
8561
8562 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8563 rule->condition))
8564 return -ENOBUFS;
8565
8566 nl_pats = nla_nest_start(msg,
8567 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8568 if (!nl_pats)
8569 return -ENOBUFS;
8570
8571 for (j = 0; j < rule->n_patterns; j++) {
8572 nl_pat = nla_nest_start(msg, j + 1);
8573 if (!nl_pat)
8574 return -ENOBUFS;
8575 pat_len = rule->patterns[j].pattern_len;
8576 if (nla_put(msg, NL80211_PKTPAT_MASK,
8577 DIV_ROUND_UP(pat_len, 8),
8578 rule->patterns[j].mask) ||
8579 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8580 rule->patterns[j].pattern) ||
8581 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8582 rule->patterns[j].pkt_offset))
8583 return -ENOBUFS;
8584 nla_nest_end(msg, nl_pat);
8585 }
8586 nla_nest_end(msg, nl_pats);
8587 nla_nest_end(msg, nl_rule);
8588 }
8589 nla_nest_end(msg, nl_rules);
8590
8591 return 0;
8592}
8593
8594static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8595{
8596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8597 struct sk_buff *msg;
8598 void *hdr;
8599
8600 if (!rdev->wiphy.coalesce)
8601 return -EOPNOTSUPP;
8602
8603 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8604 if (!msg)
8605 return -ENOMEM;
8606
8607 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8608 NL80211_CMD_GET_COALESCE);
8609 if (!hdr)
8610 goto nla_put_failure;
8611
8612 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8613 goto nla_put_failure;
8614
8615 genlmsg_end(msg, hdr);
8616 return genlmsg_reply(msg, info);
8617
8618nla_put_failure:
8619 nlmsg_free(msg);
8620 return -ENOBUFS;
8621}
8622
8623void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8624{
8625 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8626 int i, j;
8627 struct cfg80211_coalesce_rules *rule;
8628
8629 if (!coalesce)
8630 return;
8631
8632 for (i = 0; i < coalesce->n_rules; i++) {
8633 rule = &coalesce->rules[i];
8634 for (j = 0; j < rule->n_patterns; j++)
8635 kfree(rule->patterns[j].mask);
8636 kfree(rule->patterns);
8637 }
8638 kfree(coalesce->rules);
8639 kfree(coalesce);
8640 rdev->coalesce = NULL;
8641}
8642
8643static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8644 struct nlattr *rule,
8645 struct cfg80211_coalesce_rules *new_rule)
8646{
8647 int err, i;
8648 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8649 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8650 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8651 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8652
8653 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8654 nla_len(rule), nl80211_coalesce_policy);
8655 if (err)
8656 return err;
8657
8658 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8659 new_rule->delay =
8660 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8661 if (new_rule->delay > coalesce->max_delay)
8662 return -EINVAL;
8663
8664 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8665 new_rule->condition =
8666 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8667 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8668 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8669 return -EINVAL;
8670
8671 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8672 return -EINVAL;
8673
8674 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8675 rem)
8676 n_patterns++;
8677 if (n_patterns > coalesce->n_patterns)
8678 return -EINVAL;
8679
8680 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8681 GFP_KERNEL);
8682 if (!new_rule->patterns)
8683 return -ENOMEM;
8684
8685 new_rule->n_patterns = n_patterns;
8686 i = 0;
8687
8688 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8689 rem) {
8690 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8691 nla_len(pat), NULL);
8692 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8693 !pat_tb[NL80211_PKTPAT_PATTERN])
8694 return -EINVAL;
8695 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8696 mask_len = DIV_ROUND_UP(pat_len, 8);
8697 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8698 return -EINVAL;
8699 if (pat_len > coalesce->pattern_max_len ||
8700 pat_len < coalesce->pattern_min_len)
8701 return -EINVAL;
8702
8703 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8704 pkt_offset = 0;
8705 else
8706 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8707 if (pkt_offset > coalesce->max_pkt_offset)
8708 return -EINVAL;
8709 new_rule->patterns[i].pkt_offset = pkt_offset;
8710
8711 new_rule->patterns[i].mask =
8712 kmalloc(mask_len + pat_len, GFP_KERNEL);
8713 if (!new_rule->patterns[i].mask)
8714 return -ENOMEM;
8715 new_rule->patterns[i].pattern =
8716 new_rule->patterns[i].mask + mask_len;
8717 memcpy(new_rule->patterns[i].mask,
8718 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8719 new_rule->patterns[i].pattern_len = pat_len;
8720 memcpy(new_rule->patterns[i].pattern,
8721 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8722 i++;
8723 }
8724
8725 return 0;
8726}
8727
8728static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8729{
8730 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8731 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8732 struct cfg80211_coalesce new_coalesce = {};
8733 struct cfg80211_coalesce *n_coalesce;
8734 int err, rem_rule, n_rules = 0, i, j;
8735 struct nlattr *rule;
8736 struct cfg80211_coalesce_rules *tmp_rule;
8737
8738 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8739 return -EOPNOTSUPP;
8740
8741 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8742 cfg80211_rdev_free_coalesce(rdev);
8743 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8744 return 0;
8745 }
8746
8747 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8748 rem_rule)
8749 n_rules++;
8750 if (n_rules > coalesce->n_rules)
8751 return -EINVAL;
8752
8753 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8754 GFP_KERNEL);
8755 if (!new_coalesce.rules)
8756 return -ENOMEM;
8757
8758 new_coalesce.n_rules = n_rules;
8759 i = 0;
8760
8761 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8762 rem_rule) {
8763 err = nl80211_parse_coalesce_rule(rdev, rule,
8764 &new_coalesce.rules[i]);
8765 if (err)
8766 goto error;
8767
8768 i++;
8769 }
8770
8771 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8772 if (err)
8773 goto error;
8774
8775 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8776 if (!n_coalesce) {
8777 err = -ENOMEM;
8778 goto error;
8779 }
8780 cfg80211_rdev_free_coalesce(rdev);
8781 rdev->coalesce = n_coalesce;
8782
8783 return 0;
8784error:
8785 for (i = 0; i < new_coalesce.n_rules; i++) {
8786 tmp_rule = &new_coalesce.rules[i];
8787 for (j = 0; j < tmp_rule->n_patterns; j++)
8788 kfree(tmp_rule->patterns[j].mask);
8789 kfree(tmp_rule->patterns);
8790 }
8791 kfree(new_coalesce.rules);
8792
8793 return err;
8794}
8795
Johannes Berge5497d72011-07-05 16:35:40 +02008796static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8797{
8798 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8799 struct net_device *dev = info->user_ptr[1];
8800 struct wireless_dev *wdev = dev->ieee80211_ptr;
8801 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8802 struct cfg80211_gtk_rekey_data rekey_data;
8803 int err;
8804
8805 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8806 return -EINVAL;
8807
8808 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8809 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8810 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8811 nl80211_rekey_policy);
8812 if (err)
8813 return err;
8814
8815 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8816 return -ERANGE;
8817 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8818 return -ERANGE;
8819 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8820 return -ERANGE;
8821
8822 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8823 NL80211_KEK_LEN);
8824 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8825 NL80211_KCK_LEN);
8826 memcpy(rekey_data.replay_ctr,
8827 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8828 NL80211_REPLAY_CTR_LEN);
8829
8830 wdev_lock(wdev);
8831 if (!wdev->current_bss) {
8832 err = -ENOTCONN;
8833 goto out;
8834 }
8835
8836 if (!rdev->ops->set_rekey_data) {
8837 err = -EOPNOTSUPP;
8838 goto out;
8839 }
8840
Hila Gonene35e4d22012-06-27 17:19:42 +03008841 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008842 out:
8843 wdev_unlock(wdev);
8844 return err;
8845}
8846
Johannes Berg28946da2011-11-04 11:18:12 +01008847static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8848 struct genl_info *info)
8849{
8850 struct net_device *dev = info->user_ptr[1];
8851 struct wireless_dev *wdev = dev->ieee80211_ptr;
8852
8853 if (wdev->iftype != NL80211_IFTYPE_AP &&
8854 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8855 return -EINVAL;
8856
Eric W. Biederman15e47302012-09-07 20:12:54 +00008857 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008858 return -EBUSY;
8859
Eric W. Biederman15e47302012-09-07 20:12:54 +00008860 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008861 return 0;
8862}
8863
Johannes Berg7f6cf312011-11-04 11:18:15 +01008864static int nl80211_probe_client(struct sk_buff *skb,
8865 struct genl_info *info)
8866{
8867 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8868 struct net_device *dev = info->user_ptr[1];
8869 struct wireless_dev *wdev = dev->ieee80211_ptr;
8870 struct sk_buff *msg;
8871 void *hdr;
8872 const u8 *addr;
8873 u64 cookie;
8874 int err;
8875
8876 if (wdev->iftype != NL80211_IFTYPE_AP &&
8877 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8878 return -EOPNOTSUPP;
8879
8880 if (!info->attrs[NL80211_ATTR_MAC])
8881 return -EINVAL;
8882
8883 if (!rdev->ops->probe_client)
8884 return -EOPNOTSUPP;
8885
8886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8887 if (!msg)
8888 return -ENOMEM;
8889
Eric W. Biederman15e47302012-09-07 20:12:54 +00008890 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008891 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008892 if (!hdr) {
8893 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008894 goto free_msg;
8895 }
8896
8897 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8898
Hila Gonene35e4d22012-06-27 17:19:42 +03008899 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008900 if (err)
8901 goto free_msg;
8902
David S. Miller9360ffd2012-03-29 04:41:26 -04008903 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8904 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008905
8906 genlmsg_end(msg, hdr);
8907
8908 return genlmsg_reply(msg, info);
8909
8910 nla_put_failure:
8911 err = -ENOBUFS;
8912 free_msg:
8913 nlmsg_free(msg);
8914 return err;
8915}
8916
Johannes Berg5e760232011-11-04 11:18:17 +01008917static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8918{
8919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008920 struct cfg80211_beacon_registration *reg, *nreg;
8921 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008922
8923 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8924 return -EOPNOTSUPP;
8925
Ben Greear37c73b52012-10-26 14:49:25 -07008926 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8927 if (!nreg)
8928 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008929
Ben Greear37c73b52012-10-26 14:49:25 -07008930 /* First, check if already registered. */
8931 spin_lock_bh(&rdev->beacon_registrations_lock);
8932 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8933 if (reg->nlportid == info->snd_portid) {
8934 rv = -EALREADY;
8935 goto out_err;
8936 }
8937 }
8938 /* Add it to the list */
8939 nreg->nlportid = info->snd_portid;
8940 list_add(&nreg->list, &rdev->beacon_registrations);
8941
8942 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008943
8944 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008945out_err:
8946 spin_unlock_bh(&rdev->beacon_registrations_lock);
8947 kfree(nreg);
8948 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008949}
8950
Johannes Berg98104fde2012-06-16 00:19:54 +02008951static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8952{
8953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8954 struct wireless_dev *wdev = info->user_ptr[1];
8955 int err;
8956
8957 if (!rdev->ops->start_p2p_device)
8958 return -EOPNOTSUPP;
8959
8960 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8961 return -EOPNOTSUPP;
8962
8963 if (wdev->p2p_started)
8964 return 0;
8965
Johannes Berg98104fde2012-06-16 00:19:54 +02008966 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008967 if (err)
8968 return err;
8969
Johannes Bergeeb126e2012-10-23 15:16:50 +02008970 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008971 if (err)
8972 return err;
8973
8974 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008975 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008976
8977 return 0;
8978}
8979
8980static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8981{
8982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8983 struct wireless_dev *wdev = info->user_ptr[1];
8984
8985 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8986 return -EOPNOTSUPP;
8987
8988 if (!rdev->ops->stop_p2p_device)
8989 return -EOPNOTSUPP;
8990
Johannes Bergf9f47522013-03-19 15:04:07 +01008991 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008992
8993 return 0;
8994}
8995
Johannes Berg3713b4e2013-02-14 16:19:38 +01008996static int nl80211_get_protocol_features(struct sk_buff *skb,
8997 struct genl_info *info)
8998{
8999 void *hdr;
9000 struct sk_buff *msg;
9001
9002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9003 if (!msg)
9004 return -ENOMEM;
9005
9006 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9007 NL80211_CMD_GET_PROTOCOL_FEATURES);
9008 if (!hdr)
9009 goto nla_put_failure;
9010
9011 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9012 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9013 goto nla_put_failure;
9014
9015 genlmsg_end(msg, hdr);
9016 return genlmsg_reply(msg, info);
9017
9018 nla_put_failure:
9019 kfree_skb(msg);
9020 return -ENOBUFS;
9021}
9022
Jouni Malinen355199e2013-02-27 17:14:27 +02009023static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9024{
9025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9026 struct cfg80211_update_ft_ies_params ft_params;
9027 struct net_device *dev = info->user_ptr[1];
9028
9029 if (!rdev->ops->update_ft_ies)
9030 return -EOPNOTSUPP;
9031
9032 if (!info->attrs[NL80211_ATTR_MDID] ||
9033 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9034 return -EINVAL;
9035
9036 memset(&ft_params, 0, sizeof(ft_params));
9037 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9038 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9039 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9040
9041 return rdev_update_ft_ies(rdev, dev, &ft_params);
9042}
9043
Arend van Spriel5de17982013-04-18 15:49:00 +02009044static int nl80211_crit_protocol_start(struct sk_buff *skb,
9045 struct genl_info *info)
9046{
9047 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9048 struct wireless_dev *wdev = info->user_ptr[1];
9049 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9050 u16 duration;
9051 int ret;
9052
9053 if (!rdev->ops->crit_proto_start)
9054 return -EOPNOTSUPP;
9055
9056 if (WARN_ON(!rdev->ops->crit_proto_stop))
9057 return -EINVAL;
9058
9059 if (rdev->crit_proto_nlportid)
9060 return -EBUSY;
9061
9062 /* determine protocol if provided */
9063 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9064 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9065
9066 if (proto >= NUM_NL80211_CRIT_PROTO)
9067 return -EINVAL;
9068
9069 /* timeout must be provided */
9070 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9071 return -EINVAL;
9072
9073 duration =
9074 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9075
9076 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9077 return -ERANGE;
9078
9079 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9080 if (!ret)
9081 rdev->crit_proto_nlportid = info->snd_portid;
9082
9083 return ret;
9084}
9085
9086static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9087 struct genl_info *info)
9088{
9089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9090 struct wireless_dev *wdev = info->user_ptr[1];
9091
9092 if (!rdev->ops->crit_proto_stop)
9093 return -EOPNOTSUPP;
9094
9095 if (rdev->crit_proto_nlportid) {
9096 rdev->crit_proto_nlportid = 0;
9097 rdev_crit_proto_stop(rdev, wdev);
9098 }
9099 return 0;
9100}
9101
Johannes Bergad7e7182013-11-13 13:37:47 +01009102static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9103{
9104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9105 struct wireless_dev *wdev =
9106 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9107 int i, err;
9108 u32 vid, subcmd;
9109
9110 if (!rdev->wiphy.vendor_commands)
9111 return -EOPNOTSUPP;
9112
9113 if (IS_ERR(wdev)) {
9114 err = PTR_ERR(wdev);
9115 if (err != -EINVAL)
9116 return err;
9117 wdev = NULL;
9118 } else if (wdev->wiphy != &rdev->wiphy) {
9119 return -EINVAL;
9120 }
9121
9122 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9123 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9124 return -EINVAL;
9125
9126 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9127 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9128 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9129 const struct wiphy_vendor_command *vcmd;
9130 void *data = NULL;
9131 int len = 0;
9132
9133 vcmd = &rdev->wiphy.vendor_commands[i];
9134
9135 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9136 continue;
9137
9138 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9139 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9140 if (!wdev)
9141 return -EINVAL;
9142 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9143 !wdev->netdev)
9144 return -EINVAL;
9145
9146 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9147 if (wdev->netdev &&
9148 !netif_running(wdev->netdev))
9149 return -ENETDOWN;
9150 if (!wdev->netdev && !wdev->p2p_started)
9151 return -ENETDOWN;
9152 }
9153 } else {
9154 wdev = NULL;
9155 }
9156
9157 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9158 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9159 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9160 }
9161
9162 rdev->cur_cmd_info = info;
9163 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9164 data, len);
9165 rdev->cur_cmd_info = NULL;
9166 return err;
9167 }
9168
9169 return -EOPNOTSUPP;
9170}
9171
9172struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9173 enum nl80211_commands cmd,
9174 enum nl80211_attrs attr,
9175 int approxlen)
9176{
9177 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9178
9179 if (WARN_ON(!rdev->cur_cmd_info))
9180 return NULL;
9181
9182 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9183 rdev->cur_cmd_info->snd_portid,
9184 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009185 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009186}
9187EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9188
9189int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9190{
9191 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9192 void *hdr = ((void **)skb->cb)[1];
9193 struct nlattr *data = ((void **)skb->cb)[2];
9194
9195 if (WARN_ON(!rdev->cur_cmd_info)) {
9196 kfree_skb(skb);
9197 return -EINVAL;
9198 }
9199
9200 nla_nest_end(skb, data);
9201 genlmsg_end(skb, hdr);
9202 return genlmsg_reply(skb, rdev->cur_cmd_info);
9203}
9204EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9205
9206
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009207static int nl80211_set_qos_map(struct sk_buff *skb,
9208 struct genl_info *info)
9209{
9210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9211 struct cfg80211_qos_map *qos_map = NULL;
9212 struct net_device *dev = info->user_ptr[1];
9213 u8 *pos, len, num_des, des_len, des;
9214 int ret;
9215
9216 if (!rdev->ops->set_qos_map)
9217 return -EOPNOTSUPP;
9218
9219 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9220 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9221 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9222
9223 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9224 len > IEEE80211_QOS_MAP_LEN_MAX)
9225 return -EINVAL;
9226
9227 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9228 if (!qos_map)
9229 return -ENOMEM;
9230
9231 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9232 if (num_des) {
9233 des_len = num_des *
9234 sizeof(struct cfg80211_dscp_exception);
9235 memcpy(qos_map->dscp_exception, pos, des_len);
9236 qos_map->num_des = num_des;
9237 for (des = 0; des < num_des; des++) {
9238 if (qos_map->dscp_exception[des].up > 7) {
9239 kfree(qos_map);
9240 return -EINVAL;
9241 }
9242 }
9243 pos += des_len;
9244 }
9245 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9246 }
9247
9248 wdev_lock(dev->ieee80211_ptr);
9249 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9250 if (!ret)
9251 ret = rdev_set_qos_map(rdev, dev, qos_map);
9252 wdev_unlock(dev->ieee80211_ptr);
9253
9254 kfree(qos_map);
9255 return ret;
9256}
9257
Johannes Berg4c476992010-10-04 21:36:35 +02009258#define NL80211_FLAG_NEED_WIPHY 0x01
9259#define NL80211_FLAG_NEED_NETDEV 0x02
9260#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009261#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9262#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9263 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009264#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009265/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009266#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9267 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009268
Johannes Bergf84f7712013-11-14 17:14:45 +01009269static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009270 struct genl_info *info)
9271{
9272 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009273 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009274 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009275 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9276
9277 if (rtnl)
9278 rtnl_lock();
9279
9280 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009281 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009282 if (IS_ERR(rdev)) {
9283 if (rtnl)
9284 rtnl_unlock();
9285 return PTR_ERR(rdev);
9286 }
9287 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009288 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9289 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009290 ASSERT_RTNL();
9291
Johannes Berg89a54e42012-06-15 14:33:17 +02009292 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9293 info->attrs);
9294 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009295 if (rtnl)
9296 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009297 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009298 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009299
Johannes Berg89a54e42012-06-15 14:33:17 +02009300 dev = wdev->netdev;
9301 rdev = wiphy_to_dev(wdev->wiphy);
9302
Johannes Berg1bf614e2012-06-15 15:23:36 +02009303 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9304 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009305 if (rtnl)
9306 rtnl_unlock();
9307 return -EINVAL;
9308 }
9309
9310 info->user_ptr[1] = dev;
9311 } else {
9312 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009313 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009314
Johannes Berg1bf614e2012-06-15 15:23:36 +02009315 if (dev) {
9316 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9317 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009318 if (rtnl)
9319 rtnl_unlock();
9320 return -ENETDOWN;
9321 }
9322
9323 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009324 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9325 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009326 if (rtnl)
9327 rtnl_unlock();
9328 return -ENETDOWN;
9329 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009330 }
9331
Johannes Berg4c476992010-10-04 21:36:35 +02009332 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009333 }
9334
9335 return 0;
9336}
9337
Johannes Bergf84f7712013-11-14 17:14:45 +01009338static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009339 struct genl_info *info)
9340{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009341 if (info->user_ptr[1]) {
9342 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9343 struct wireless_dev *wdev = info->user_ptr[1];
9344
9345 if (wdev->netdev)
9346 dev_put(wdev->netdev);
9347 } else {
9348 dev_put(info->user_ptr[1]);
9349 }
9350 }
Johannes Berg4c476992010-10-04 21:36:35 +02009351 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9352 rtnl_unlock();
9353}
9354
Johannes Berg4534de82013-11-14 17:14:46 +01009355static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009356 {
9357 .cmd = NL80211_CMD_GET_WIPHY,
9358 .doit = nl80211_get_wiphy,
9359 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009360 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009361 .policy = nl80211_policy,
9362 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009363 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9364 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009365 },
9366 {
9367 .cmd = NL80211_CMD_SET_WIPHY,
9368 .doit = nl80211_set_wiphy,
9369 .policy = nl80211_policy,
9370 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009371 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009372 },
9373 {
9374 .cmd = NL80211_CMD_GET_INTERFACE,
9375 .doit = nl80211_get_interface,
9376 .dumpit = nl80211_dump_interface,
9377 .policy = nl80211_policy,
9378 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009379 .internal_flags = NL80211_FLAG_NEED_WDEV |
9380 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009381 },
9382 {
9383 .cmd = NL80211_CMD_SET_INTERFACE,
9384 .doit = nl80211_set_interface,
9385 .policy = nl80211_policy,
9386 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009387 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9388 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009389 },
9390 {
9391 .cmd = NL80211_CMD_NEW_INTERFACE,
9392 .doit = nl80211_new_interface,
9393 .policy = nl80211_policy,
9394 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009395 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9396 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009397 },
9398 {
9399 .cmd = NL80211_CMD_DEL_INTERFACE,
9400 .doit = nl80211_del_interface,
9401 .policy = nl80211_policy,
9402 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009403 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009404 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009405 },
Johannes Berg41ade002007-12-19 02:03:29 +01009406 {
9407 .cmd = NL80211_CMD_GET_KEY,
9408 .doit = nl80211_get_key,
9409 .policy = nl80211_policy,
9410 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009411 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009412 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009413 },
9414 {
9415 .cmd = NL80211_CMD_SET_KEY,
9416 .doit = nl80211_set_key,
9417 .policy = nl80211_policy,
9418 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009419 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009420 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009421 },
9422 {
9423 .cmd = NL80211_CMD_NEW_KEY,
9424 .doit = nl80211_new_key,
9425 .policy = nl80211_policy,
9426 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009427 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009428 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009429 },
9430 {
9431 .cmd = NL80211_CMD_DEL_KEY,
9432 .doit = nl80211_del_key,
9433 .policy = nl80211_policy,
9434 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009435 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009436 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009437 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009438 {
9439 .cmd = NL80211_CMD_SET_BEACON,
9440 .policy = nl80211_policy,
9441 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009442 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009443 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009444 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009445 },
9446 {
Johannes Berg88600202012-02-13 15:17:18 +01009447 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009448 .policy = nl80211_policy,
9449 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009450 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009451 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009452 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009453 },
9454 {
Johannes Berg88600202012-02-13 15:17:18 +01009455 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009456 .policy = nl80211_policy,
9457 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009458 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009459 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009460 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009461 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009462 {
9463 .cmd = NL80211_CMD_GET_STATION,
9464 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009465 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009466 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009467 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9468 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009469 },
9470 {
9471 .cmd = NL80211_CMD_SET_STATION,
9472 .doit = nl80211_set_station,
9473 .policy = nl80211_policy,
9474 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009475 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009476 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009477 },
9478 {
9479 .cmd = NL80211_CMD_NEW_STATION,
9480 .doit = nl80211_new_station,
9481 .policy = nl80211_policy,
9482 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009483 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009484 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009485 },
9486 {
9487 .cmd = NL80211_CMD_DEL_STATION,
9488 .doit = nl80211_del_station,
9489 .policy = nl80211_policy,
9490 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009491 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009492 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009493 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009494 {
9495 .cmd = NL80211_CMD_GET_MPATH,
9496 .doit = nl80211_get_mpath,
9497 .dumpit = nl80211_dump_mpath,
9498 .policy = nl80211_policy,
9499 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009500 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009501 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009502 },
9503 {
9504 .cmd = NL80211_CMD_SET_MPATH,
9505 .doit = nl80211_set_mpath,
9506 .policy = nl80211_policy,
9507 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009508 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009509 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009510 },
9511 {
9512 .cmd = NL80211_CMD_NEW_MPATH,
9513 .doit = nl80211_new_mpath,
9514 .policy = nl80211_policy,
9515 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009516 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009517 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009518 },
9519 {
9520 .cmd = NL80211_CMD_DEL_MPATH,
9521 .doit = nl80211_del_mpath,
9522 .policy = nl80211_policy,
9523 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009524 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009525 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009526 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009527 {
9528 .cmd = NL80211_CMD_SET_BSS,
9529 .doit = nl80211_set_bss,
9530 .policy = nl80211_policy,
9531 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009532 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009533 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009534 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009535 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009536 .cmd = NL80211_CMD_GET_REG,
9537 .doit = nl80211_get_reg,
9538 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009539 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009540 /* can be retrieved by unprivileged users */
9541 },
9542 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009543 .cmd = NL80211_CMD_SET_REG,
9544 .doit = nl80211_set_reg,
9545 .policy = nl80211_policy,
9546 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009547 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009548 },
9549 {
9550 .cmd = NL80211_CMD_REQ_SET_REG,
9551 .doit = nl80211_req_set_reg,
9552 .policy = nl80211_policy,
9553 .flags = GENL_ADMIN_PERM,
9554 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009555 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009556 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9557 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009558 .policy = nl80211_policy,
9559 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009560 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009561 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009562 },
9563 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009564 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9565 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009566 .policy = nl80211_policy,
9567 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009568 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009569 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009570 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009571 {
Johannes Berg2a519312009-02-10 21:25:55 +01009572 .cmd = NL80211_CMD_TRIGGER_SCAN,
9573 .doit = nl80211_trigger_scan,
9574 .policy = nl80211_policy,
9575 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009576 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009577 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009578 },
9579 {
9580 .cmd = NL80211_CMD_GET_SCAN,
9581 .policy = nl80211_policy,
9582 .dumpit = nl80211_dump_scan,
9583 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009584 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009585 .cmd = NL80211_CMD_START_SCHED_SCAN,
9586 .doit = nl80211_start_sched_scan,
9587 .policy = nl80211_policy,
9588 .flags = GENL_ADMIN_PERM,
9589 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9590 NL80211_FLAG_NEED_RTNL,
9591 },
9592 {
9593 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9594 .doit = nl80211_stop_sched_scan,
9595 .policy = nl80211_policy,
9596 .flags = GENL_ADMIN_PERM,
9597 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9598 NL80211_FLAG_NEED_RTNL,
9599 },
9600 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009601 .cmd = NL80211_CMD_AUTHENTICATE,
9602 .doit = nl80211_authenticate,
9603 .policy = nl80211_policy,
9604 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009605 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009606 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009607 },
9608 {
9609 .cmd = NL80211_CMD_ASSOCIATE,
9610 .doit = nl80211_associate,
9611 .policy = nl80211_policy,
9612 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009613 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009614 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009615 },
9616 {
9617 .cmd = NL80211_CMD_DEAUTHENTICATE,
9618 .doit = nl80211_deauthenticate,
9619 .policy = nl80211_policy,
9620 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009621 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009622 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009623 },
9624 {
9625 .cmd = NL80211_CMD_DISASSOCIATE,
9626 .doit = nl80211_disassociate,
9627 .policy = nl80211_policy,
9628 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009629 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009630 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009631 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009632 {
9633 .cmd = NL80211_CMD_JOIN_IBSS,
9634 .doit = nl80211_join_ibss,
9635 .policy = nl80211_policy,
9636 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009637 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009638 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009639 },
9640 {
9641 .cmd = NL80211_CMD_LEAVE_IBSS,
9642 .doit = nl80211_leave_ibss,
9643 .policy = nl80211_policy,
9644 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009645 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009646 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009647 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009648#ifdef CONFIG_NL80211_TESTMODE
9649 {
9650 .cmd = NL80211_CMD_TESTMODE,
9651 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009652 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009653 .policy = nl80211_policy,
9654 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009655 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9656 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009657 },
9658#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009659 {
9660 .cmd = NL80211_CMD_CONNECT,
9661 .doit = nl80211_connect,
9662 .policy = nl80211_policy,
9663 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009664 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009665 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009666 },
9667 {
9668 .cmd = NL80211_CMD_DISCONNECT,
9669 .doit = nl80211_disconnect,
9670 .policy = nl80211_policy,
9671 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009672 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009673 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009674 },
Johannes Berg463d0182009-07-14 00:33:35 +02009675 {
9676 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9677 .doit = nl80211_wiphy_netns,
9678 .policy = nl80211_policy,
9679 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009680 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9681 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009682 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009683 {
9684 .cmd = NL80211_CMD_GET_SURVEY,
9685 .policy = nl80211_policy,
9686 .dumpit = nl80211_dump_survey,
9687 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009688 {
9689 .cmd = NL80211_CMD_SET_PMKSA,
9690 .doit = nl80211_setdel_pmksa,
9691 .policy = nl80211_policy,
9692 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009693 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009694 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009695 },
9696 {
9697 .cmd = NL80211_CMD_DEL_PMKSA,
9698 .doit = nl80211_setdel_pmksa,
9699 .policy = nl80211_policy,
9700 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009701 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009702 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009703 },
9704 {
9705 .cmd = NL80211_CMD_FLUSH_PMKSA,
9706 .doit = nl80211_flush_pmksa,
9707 .policy = nl80211_policy,
9708 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009709 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009710 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009711 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009712 {
9713 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9714 .doit = nl80211_remain_on_channel,
9715 .policy = nl80211_policy,
9716 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009717 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009718 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009719 },
9720 {
9721 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9722 .doit = nl80211_cancel_remain_on_channel,
9723 .policy = nl80211_policy,
9724 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009725 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009726 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009727 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009728 {
9729 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9730 .doit = nl80211_set_tx_bitrate_mask,
9731 .policy = nl80211_policy,
9732 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009733 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9734 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009735 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009736 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009737 .cmd = NL80211_CMD_REGISTER_FRAME,
9738 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009739 .policy = nl80211_policy,
9740 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009741 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009742 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009743 },
9744 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009745 .cmd = NL80211_CMD_FRAME,
9746 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009747 .policy = nl80211_policy,
9748 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009749 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009750 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009751 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009752 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009753 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9754 .doit = nl80211_tx_mgmt_cancel_wait,
9755 .policy = nl80211_policy,
9756 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009757 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009758 NL80211_FLAG_NEED_RTNL,
9759 },
9760 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009761 .cmd = NL80211_CMD_SET_POWER_SAVE,
9762 .doit = nl80211_set_power_save,
9763 .policy = nl80211_policy,
9764 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009765 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9766 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009767 },
9768 {
9769 .cmd = NL80211_CMD_GET_POWER_SAVE,
9770 .doit = nl80211_get_power_save,
9771 .policy = nl80211_policy,
9772 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009773 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9774 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009775 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009776 {
9777 .cmd = NL80211_CMD_SET_CQM,
9778 .doit = nl80211_set_cqm,
9779 .policy = nl80211_policy,
9780 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009781 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9782 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009783 },
Johannes Bergf444de02010-05-05 15:25:02 +02009784 {
9785 .cmd = NL80211_CMD_SET_CHANNEL,
9786 .doit = nl80211_set_channel,
9787 .policy = nl80211_policy,
9788 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009789 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9790 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009791 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009792 {
9793 .cmd = NL80211_CMD_SET_WDS_PEER,
9794 .doit = nl80211_set_wds_peer,
9795 .policy = nl80211_policy,
9796 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009797 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9798 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009799 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009800 {
9801 .cmd = NL80211_CMD_JOIN_MESH,
9802 .doit = nl80211_join_mesh,
9803 .policy = nl80211_policy,
9804 .flags = GENL_ADMIN_PERM,
9805 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9806 NL80211_FLAG_NEED_RTNL,
9807 },
9808 {
9809 .cmd = NL80211_CMD_LEAVE_MESH,
9810 .doit = nl80211_leave_mesh,
9811 .policy = nl80211_policy,
9812 .flags = GENL_ADMIN_PERM,
9813 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9814 NL80211_FLAG_NEED_RTNL,
9815 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009816#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009817 {
9818 .cmd = NL80211_CMD_GET_WOWLAN,
9819 .doit = nl80211_get_wowlan,
9820 .policy = nl80211_policy,
9821 /* can be retrieved by unprivileged users */
9822 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9823 NL80211_FLAG_NEED_RTNL,
9824 },
9825 {
9826 .cmd = NL80211_CMD_SET_WOWLAN,
9827 .doit = nl80211_set_wowlan,
9828 .policy = nl80211_policy,
9829 .flags = GENL_ADMIN_PERM,
9830 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9831 NL80211_FLAG_NEED_RTNL,
9832 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009833#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009834 {
9835 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9836 .doit = nl80211_set_rekey_data,
9837 .policy = nl80211_policy,
9838 .flags = GENL_ADMIN_PERM,
9839 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9840 NL80211_FLAG_NEED_RTNL,
9841 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009842 {
9843 .cmd = NL80211_CMD_TDLS_MGMT,
9844 .doit = nl80211_tdls_mgmt,
9845 .policy = nl80211_policy,
9846 .flags = GENL_ADMIN_PERM,
9847 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9848 NL80211_FLAG_NEED_RTNL,
9849 },
9850 {
9851 .cmd = NL80211_CMD_TDLS_OPER,
9852 .doit = nl80211_tdls_oper,
9853 .policy = nl80211_policy,
9854 .flags = GENL_ADMIN_PERM,
9855 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9856 NL80211_FLAG_NEED_RTNL,
9857 },
Johannes Berg28946da2011-11-04 11:18:12 +01009858 {
9859 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9860 .doit = nl80211_register_unexpected_frame,
9861 .policy = nl80211_policy,
9862 .flags = GENL_ADMIN_PERM,
9863 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9864 NL80211_FLAG_NEED_RTNL,
9865 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009866 {
9867 .cmd = NL80211_CMD_PROBE_CLIENT,
9868 .doit = nl80211_probe_client,
9869 .policy = nl80211_policy,
9870 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009871 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009872 NL80211_FLAG_NEED_RTNL,
9873 },
Johannes Berg5e760232011-11-04 11:18:17 +01009874 {
9875 .cmd = NL80211_CMD_REGISTER_BEACONS,
9876 .doit = nl80211_register_beacons,
9877 .policy = nl80211_policy,
9878 .flags = GENL_ADMIN_PERM,
9879 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9880 NL80211_FLAG_NEED_RTNL,
9881 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009882 {
9883 .cmd = NL80211_CMD_SET_NOACK_MAP,
9884 .doit = nl80211_set_noack_map,
9885 .policy = nl80211_policy,
9886 .flags = GENL_ADMIN_PERM,
9887 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9888 NL80211_FLAG_NEED_RTNL,
9889 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009890 {
9891 .cmd = NL80211_CMD_START_P2P_DEVICE,
9892 .doit = nl80211_start_p2p_device,
9893 .policy = nl80211_policy,
9894 .flags = GENL_ADMIN_PERM,
9895 .internal_flags = NL80211_FLAG_NEED_WDEV |
9896 NL80211_FLAG_NEED_RTNL,
9897 },
9898 {
9899 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9900 .doit = nl80211_stop_p2p_device,
9901 .policy = nl80211_policy,
9902 .flags = GENL_ADMIN_PERM,
9903 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9904 NL80211_FLAG_NEED_RTNL,
9905 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009906 {
9907 .cmd = NL80211_CMD_SET_MCAST_RATE,
9908 .doit = nl80211_set_mcast_rate,
9909 .policy = nl80211_policy,
9910 .flags = GENL_ADMIN_PERM,
9911 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9912 NL80211_FLAG_NEED_RTNL,
9913 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309914 {
9915 .cmd = NL80211_CMD_SET_MAC_ACL,
9916 .doit = nl80211_set_mac_acl,
9917 .policy = nl80211_policy,
9918 .flags = GENL_ADMIN_PERM,
9919 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9920 NL80211_FLAG_NEED_RTNL,
9921 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009922 {
9923 .cmd = NL80211_CMD_RADAR_DETECT,
9924 .doit = nl80211_start_radar_detection,
9925 .policy = nl80211_policy,
9926 .flags = GENL_ADMIN_PERM,
9927 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9928 NL80211_FLAG_NEED_RTNL,
9929 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009930 {
9931 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9932 .doit = nl80211_get_protocol_features,
9933 .policy = nl80211_policy,
9934 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009935 {
9936 .cmd = NL80211_CMD_UPDATE_FT_IES,
9937 .doit = nl80211_update_ft_ies,
9938 .policy = nl80211_policy,
9939 .flags = GENL_ADMIN_PERM,
9940 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9941 NL80211_FLAG_NEED_RTNL,
9942 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009943 {
9944 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9945 .doit = nl80211_crit_protocol_start,
9946 .policy = nl80211_policy,
9947 .flags = GENL_ADMIN_PERM,
9948 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9949 NL80211_FLAG_NEED_RTNL,
9950 },
9951 {
9952 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9953 .doit = nl80211_crit_protocol_stop,
9954 .policy = nl80211_policy,
9955 .flags = GENL_ADMIN_PERM,
9956 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9957 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009958 },
9959 {
9960 .cmd = NL80211_CMD_GET_COALESCE,
9961 .doit = nl80211_get_coalesce,
9962 .policy = nl80211_policy,
9963 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9964 NL80211_FLAG_NEED_RTNL,
9965 },
9966 {
9967 .cmd = NL80211_CMD_SET_COALESCE,
9968 .doit = nl80211_set_coalesce,
9969 .policy = nl80211_policy,
9970 .flags = GENL_ADMIN_PERM,
9971 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9972 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009973 },
9974 {
9975 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9976 .doit = nl80211_channel_switch,
9977 .policy = nl80211_policy,
9978 .flags = GENL_ADMIN_PERM,
9979 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9980 NL80211_FLAG_NEED_RTNL,
9981 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009982 {
9983 .cmd = NL80211_CMD_VENDOR,
9984 .doit = nl80211_vendor_cmd,
9985 .policy = nl80211_policy,
9986 .flags = GENL_ADMIN_PERM,
9987 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9988 NL80211_FLAG_NEED_RTNL,
9989 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009990 {
9991 .cmd = NL80211_CMD_SET_QOS_MAP,
9992 .doit = nl80211_set_qos_map,
9993 .policy = nl80211_policy,
9994 .flags = GENL_ADMIN_PERM,
9995 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9996 NL80211_FLAG_NEED_RTNL,
9997 },
Johannes Berg55682962007-09-20 13:09:35 -04009998};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009999
Johannes Berg55682962007-09-20 13:09:35 -040010000/* notification functions */
10001
10002void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
10003{
10004 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010005 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010006
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010008 if (!msg)
10009 return;
10010
Johannes Berg86e8cf92013-06-19 10:57:22 +020010011 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010012 nlmsg_free(msg);
10013 return;
10014 }
10015
Johannes Berg68eb5502013-11-19 15:19:38 +010010016 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010017 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010018}
10019
Johannes Berg362a4152009-05-24 16:43:15 +020010020static int nl80211_add_scan_req(struct sk_buff *msg,
10021 struct cfg80211_registered_device *rdev)
10022{
10023 struct cfg80211_scan_request *req = rdev->scan_req;
10024 struct nlattr *nest;
10025 int i;
10026
10027 if (WARN_ON(!req))
10028 return 0;
10029
10030 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10031 if (!nest)
10032 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010033 for (i = 0; i < req->n_ssids; i++) {
10034 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10035 goto nla_put_failure;
10036 }
Johannes Berg362a4152009-05-24 16:43:15 +020010037 nla_nest_end(msg, nest);
10038
10039 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10040 if (!nest)
10041 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010042 for (i = 0; i < req->n_channels; i++) {
10043 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10044 goto nla_put_failure;
10045 }
Johannes Berg362a4152009-05-24 16:43:15 +020010046 nla_nest_end(msg, nest);
10047
David S. Miller9360ffd2012-03-29 04:41:26 -040010048 if (req->ie &&
10049 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10050 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010051
Johannes Bergae917c92013-10-25 11:05:22 +020010052 if (req->flags &&
10053 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10054 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010055
Johannes Berg362a4152009-05-24 16:43:15 +020010056 return 0;
10057 nla_put_failure:
10058 return -ENOBUFS;
10059}
10060
Johannes Berga538e2d2009-06-16 19:56:42 +020010061static int nl80211_send_scan_msg(struct sk_buff *msg,
10062 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010063 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010064 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010065 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010066{
10067 void *hdr;
10068
Eric W. Biederman15e47302012-09-07 20:12:54 +000010069 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010070 if (!hdr)
10071 return -1;
10072
David S. Miller9360ffd2012-03-29 04:41:26 -040010073 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010074 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10075 wdev->netdev->ifindex)) ||
10076 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010077 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010078
Johannes Berg362a4152009-05-24 16:43:15 +020010079 /* ignore errors and send incomplete event anyway */
10080 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010081
10082 return genlmsg_end(msg, hdr);
10083
10084 nla_put_failure:
10085 genlmsg_cancel(msg, hdr);
10086 return -EMSGSIZE;
10087}
10088
Luciano Coelho807f8a82011-05-11 17:09:35 +030010089static int
10090nl80211_send_sched_scan_msg(struct sk_buff *msg,
10091 struct cfg80211_registered_device *rdev,
10092 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010093 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010094{
10095 void *hdr;
10096
Eric W. Biederman15e47302012-09-07 20:12:54 +000010097 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010098 if (!hdr)
10099 return -1;
10100
David S. Miller9360ffd2012-03-29 04:41:26 -040010101 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10102 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10103 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010104
10105 return genlmsg_end(msg, hdr);
10106
10107 nla_put_failure:
10108 genlmsg_cancel(msg, hdr);
10109 return -EMSGSIZE;
10110}
10111
Johannes Berga538e2d2009-06-16 19:56:42 +020010112void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010113 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010114{
10115 struct sk_buff *msg;
10116
Thomas Graf58050fc2012-06-28 03:57:45 +000010117 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010118 if (!msg)
10119 return;
10120
Johannes Bergfd014282012-06-18 19:17:03 +020010121 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010122 NL80211_CMD_TRIGGER_SCAN) < 0) {
10123 nlmsg_free(msg);
10124 return;
10125 }
10126
Johannes Berg68eb5502013-11-19 15:19:38 +010010127 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010128 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010129}
10130
Johannes Bergf9d15d12014-01-22 11:14:19 +020010131struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10132 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010133{
10134 struct sk_buff *msg;
10135
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010136 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010137 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010138 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010139
Johannes Bergfd014282012-06-18 19:17:03 +020010140 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010141 aborted ? NL80211_CMD_SCAN_ABORTED :
10142 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010143 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010144 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010145 }
10146
Johannes Bergf9d15d12014-01-22 11:14:19 +020010147 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010148}
10149
Johannes Bergf9d15d12014-01-22 11:14:19 +020010150void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10151 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010152{
Johannes Berg2a519312009-02-10 21:25:55 +010010153 if (!msg)
10154 return;
10155
Johannes Berg68eb5502013-11-19 15:19:38 +010010156 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010157 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010158}
10159
Luciano Coelho807f8a82011-05-11 17:09:35 +030010160void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10161 struct net_device *netdev)
10162{
10163 struct sk_buff *msg;
10164
10165 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10166 if (!msg)
10167 return;
10168
10169 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10170 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10171 nlmsg_free(msg);
10172 return;
10173 }
10174
Johannes Berg68eb5502013-11-19 15:19:38 +010010175 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010176 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010177}
10178
10179void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10180 struct net_device *netdev, u32 cmd)
10181{
10182 struct sk_buff *msg;
10183
Thomas Graf58050fc2012-06-28 03:57:45 +000010184 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010185 if (!msg)
10186 return;
10187
10188 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10189 nlmsg_free(msg);
10190 return;
10191 }
10192
Johannes Berg68eb5502013-11-19 15:19:38 +010010193 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010194 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010195}
10196
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010197/*
10198 * This can happen on global regulatory changes or device specific settings
10199 * based on custom world regulatory domains.
10200 */
10201void nl80211_send_reg_change_event(struct regulatory_request *request)
10202{
10203 struct sk_buff *msg;
10204 void *hdr;
10205
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010206 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010207 if (!msg)
10208 return;
10209
10210 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10211 if (!hdr) {
10212 nlmsg_free(msg);
10213 return;
10214 }
10215
10216 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010217 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10218 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010219
David S. Miller9360ffd2012-03-29 04:41:26 -040010220 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10221 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10222 NL80211_REGDOM_TYPE_WORLD))
10223 goto nla_put_failure;
10224 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10225 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10226 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10227 goto nla_put_failure;
10228 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10229 request->intersect) {
10230 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10231 NL80211_REGDOM_TYPE_INTERSECTION))
10232 goto nla_put_failure;
10233 } else {
10234 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10235 NL80211_REGDOM_TYPE_COUNTRY) ||
10236 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10237 request->alpha2))
10238 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010239 }
10240
Johannes Bergf4173762012-12-03 18:23:37 +010010241 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010242 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10243 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010244
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010245 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010246
Johannes Bergbc43b282009-07-25 10:54:13 +020010247 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010248 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010249 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010250 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010251
10252 return;
10253
10254nla_put_failure:
10255 genlmsg_cancel(msg, hdr);
10256 nlmsg_free(msg);
10257}
10258
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010259static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10260 struct net_device *netdev,
10261 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010262 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010263{
10264 struct sk_buff *msg;
10265 void *hdr;
10266
Johannes Berge6d6e342009-07-01 21:26:47 +020010267 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010268 if (!msg)
10269 return;
10270
10271 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10272 if (!hdr) {
10273 nlmsg_free(msg);
10274 return;
10275 }
10276
David S. Miller9360ffd2012-03-29 04:41:26 -040010277 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10278 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10279 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10280 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010281
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010282 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010283
Johannes Berg68eb5502013-11-19 15:19:38 +010010284 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010285 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010286 return;
10287
10288 nla_put_failure:
10289 genlmsg_cancel(msg, hdr);
10290 nlmsg_free(msg);
10291}
10292
10293void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010294 struct net_device *netdev, const u8 *buf,
10295 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010296{
10297 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010298 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010299}
10300
10301void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10302 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010303 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010304{
Johannes Berge6d6e342009-07-01 21:26:47 +020010305 nl80211_send_mlme_event(rdev, netdev, buf, len,
10306 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010307}
10308
Jouni Malinen53b46b82009-03-27 20:53:56 +020010309void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010310 struct net_device *netdev, const u8 *buf,
10311 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010312{
10313 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010314 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010315}
10316
Jouni Malinen53b46b82009-03-27 20:53:56 +020010317void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10318 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010319 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010320{
10321 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010322 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010323}
10324
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010325void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10326 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010327{
Johannes Berg947add32013-02-22 22:05:20 +010010328 struct wireless_dev *wdev = dev->ieee80211_ptr;
10329 struct wiphy *wiphy = wdev->wiphy;
10330 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010331 const struct ieee80211_mgmt *mgmt = (void *)buf;
10332 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010333
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010334 if (WARN_ON(len < 2))
10335 return;
10336
10337 if (ieee80211_is_deauth(mgmt->frame_control))
10338 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10339 else
10340 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10341
10342 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10343 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010344}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010345EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010346
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010347static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10348 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010349 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010350{
10351 struct sk_buff *msg;
10352 void *hdr;
10353
Johannes Berge6d6e342009-07-01 21:26:47 +020010354 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010355 if (!msg)
10356 return;
10357
10358 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10359 if (!hdr) {
10360 nlmsg_free(msg);
10361 return;
10362 }
10363
David S. Miller9360ffd2012-03-29 04:41:26 -040010364 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10365 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10366 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10367 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10368 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010369
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010370 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010371
Johannes Berg68eb5502013-11-19 15:19:38 +010010372 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010373 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010374 return;
10375
10376 nla_put_failure:
10377 genlmsg_cancel(msg, hdr);
10378 nlmsg_free(msg);
10379}
10380
10381void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010382 struct net_device *netdev, const u8 *addr,
10383 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010384{
10385 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010386 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010387}
10388
10389void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010390 struct net_device *netdev, const u8 *addr,
10391 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010392{
Johannes Berge6d6e342009-07-01 21:26:47 +020010393 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10394 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010395}
10396
Samuel Ortizb23aa672009-07-01 21:26:54 +020010397void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10398 struct net_device *netdev, const u8 *bssid,
10399 const u8 *req_ie, size_t req_ie_len,
10400 const u8 *resp_ie, size_t resp_ie_len,
10401 u16 status, gfp_t gfp)
10402{
10403 struct sk_buff *msg;
10404 void *hdr;
10405
Thomas Graf58050fc2012-06-28 03:57:45 +000010406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010407 if (!msg)
10408 return;
10409
10410 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10411 if (!hdr) {
10412 nlmsg_free(msg);
10413 return;
10414 }
10415
David S. Miller9360ffd2012-03-29 04:41:26 -040010416 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10417 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10418 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10419 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10420 (req_ie &&
10421 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10422 (resp_ie &&
10423 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10424 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010425
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010426 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010427
Johannes Berg68eb5502013-11-19 15:19:38 +010010428 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010429 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010430 return;
10431
10432 nla_put_failure:
10433 genlmsg_cancel(msg, hdr);
10434 nlmsg_free(msg);
10435
10436}
10437
10438void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10439 struct net_device *netdev, const u8 *bssid,
10440 const u8 *req_ie, size_t req_ie_len,
10441 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10442{
10443 struct sk_buff *msg;
10444 void *hdr;
10445
Thomas Graf58050fc2012-06-28 03:57:45 +000010446 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010447 if (!msg)
10448 return;
10449
10450 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10451 if (!hdr) {
10452 nlmsg_free(msg);
10453 return;
10454 }
10455
David S. Miller9360ffd2012-03-29 04:41:26 -040010456 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10457 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10458 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10459 (req_ie &&
10460 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10461 (resp_ie &&
10462 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10463 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010464
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010465 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010466
Johannes Berg68eb5502013-11-19 15:19:38 +010010467 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010468 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010469 return;
10470
10471 nla_put_failure:
10472 genlmsg_cancel(msg, hdr);
10473 nlmsg_free(msg);
10474
10475}
10476
10477void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10478 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +020010479 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010480{
10481 struct sk_buff *msg;
10482 void *hdr;
10483
Thomas Graf58050fc2012-06-28 03:57:45 +000010484 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010485 if (!msg)
10486 return;
10487
10488 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10489 if (!hdr) {
10490 nlmsg_free(msg);
10491 return;
10492 }
10493
David S. Miller9360ffd2012-03-29 04:41:26 -040010494 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10495 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10496 (from_ap && reason &&
10497 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10498 (from_ap &&
10499 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10500 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10501 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010502
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010503 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010504
Johannes Berg68eb5502013-11-19 15:19:38 +010010505 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010506 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010507 return;
10508
10509 nla_put_failure:
10510 genlmsg_cancel(msg, hdr);
10511 nlmsg_free(msg);
10512
10513}
10514
Johannes Berg04a773a2009-04-19 21:24:32 +020010515void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10516 struct net_device *netdev, const u8 *bssid,
10517 gfp_t gfp)
10518{
10519 struct sk_buff *msg;
10520 void *hdr;
10521
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010522 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010523 if (!msg)
10524 return;
10525
10526 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10527 if (!hdr) {
10528 nlmsg_free(msg);
10529 return;
10530 }
10531
David S. Miller9360ffd2012-03-29 04:41:26 -040010532 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10533 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10534 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10535 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010536
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010537 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010538
Johannes Berg68eb5502013-11-19 15:19:38 +010010539 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010540 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010541 return;
10542
10543 nla_put_failure:
10544 genlmsg_cancel(msg, hdr);
10545 nlmsg_free(msg);
10546}
10547
Johannes Berg947add32013-02-22 22:05:20 +010010548void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10549 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010550{
Johannes Berg947add32013-02-22 22:05:20 +010010551 struct wireless_dev *wdev = dev->ieee80211_ptr;
10552 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010553 struct sk_buff *msg;
10554 void *hdr;
10555
Johannes Berg947add32013-02-22 22:05:20 +010010556 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10557 return;
10558
10559 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10560
Javier Cardonac93b5e72011-04-07 15:08:34 -070010561 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10562 if (!msg)
10563 return;
10564
10565 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10566 if (!hdr) {
10567 nlmsg_free(msg);
10568 return;
10569 }
10570
David S. Miller9360ffd2012-03-29 04:41:26 -040010571 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010572 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10573 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010574 (ie_len && ie &&
10575 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10576 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010577
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010578 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010579
Johannes Berg68eb5502013-11-19 15:19:38 +010010580 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010581 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010582 return;
10583
10584 nla_put_failure:
10585 genlmsg_cancel(msg, hdr);
10586 nlmsg_free(msg);
10587}
Johannes Berg947add32013-02-22 22:05:20 +010010588EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010589
Jouni Malinena3b8b052009-03-27 21:59:49 +020010590void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10591 struct net_device *netdev, const u8 *addr,
10592 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010593 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010594{
10595 struct sk_buff *msg;
10596 void *hdr;
10597
Johannes Berge6d6e342009-07-01 21:26:47 +020010598 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010599 if (!msg)
10600 return;
10601
10602 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10603 if (!hdr) {
10604 nlmsg_free(msg);
10605 return;
10606 }
10607
David S. Miller9360ffd2012-03-29 04:41:26 -040010608 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10609 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10610 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10611 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10612 (key_id != -1 &&
10613 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10614 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10615 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010616
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010617 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010618
Johannes Berg68eb5502013-11-19 15:19:38 +010010619 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010620 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010621 return;
10622
10623 nla_put_failure:
10624 genlmsg_cancel(msg, hdr);
10625 nlmsg_free(msg);
10626}
10627
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010628void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10629 struct ieee80211_channel *channel_before,
10630 struct ieee80211_channel *channel_after)
10631{
10632 struct sk_buff *msg;
10633 void *hdr;
10634 struct nlattr *nl_freq;
10635
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010636 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010637 if (!msg)
10638 return;
10639
10640 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10641 if (!hdr) {
10642 nlmsg_free(msg);
10643 return;
10644 }
10645
10646 /*
10647 * Since we are applying the beacon hint to a wiphy we know its
10648 * wiphy_idx is valid
10649 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010650 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10651 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010652
10653 /* Before */
10654 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10655 if (!nl_freq)
10656 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010657 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010658 goto nla_put_failure;
10659 nla_nest_end(msg, nl_freq);
10660
10661 /* After */
10662 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10663 if (!nl_freq)
10664 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010665 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010666 goto nla_put_failure;
10667 nla_nest_end(msg, nl_freq);
10668
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010669 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010670
Johannes Berg463d0182009-07-14 00:33:35 +020010671 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010672 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010673 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010674 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010675
10676 return;
10677
10678nla_put_failure:
10679 genlmsg_cancel(msg, hdr);
10680 nlmsg_free(msg);
10681}
10682
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010683static void nl80211_send_remain_on_chan_event(
10684 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010685 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010686 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010687 unsigned int duration, gfp_t gfp)
10688{
10689 struct sk_buff *msg;
10690 void *hdr;
10691
10692 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10693 if (!msg)
10694 return;
10695
10696 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10697 if (!hdr) {
10698 nlmsg_free(msg);
10699 return;
10700 }
10701
David S. Miller9360ffd2012-03-29 04:41:26 -040010702 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010703 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10704 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010705 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010706 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010707 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10708 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010709 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10710 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010711
David S. Miller9360ffd2012-03-29 04:41:26 -040010712 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10713 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10714 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010715
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010716 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010717
Johannes Berg68eb5502013-11-19 15:19:38 +010010718 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010719 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010720 return;
10721
10722 nla_put_failure:
10723 genlmsg_cancel(msg, hdr);
10724 nlmsg_free(msg);
10725}
10726
Johannes Berg947add32013-02-22 22:05:20 +010010727void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10728 struct ieee80211_channel *chan,
10729 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010730{
Johannes Berg947add32013-02-22 22:05:20 +010010731 struct wiphy *wiphy = wdev->wiphy;
10732 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10733
10734 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010735 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010736 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010737 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010738}
Johannes Berg947add32013-02-22 22:05:20 +010010739EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010740
Johannes Berg947add32013-02-22 22:05:20 +010010741void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10742 struct ieee80211_channel *chan,
10743 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010744{
Johannes Berg947add32013-02-22 22:05:20 +010010745 struct wiphy *wiphy = wdev->wiphy;
10746 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10747
10748 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010749 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010750 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010751}
Johannes Berg947add32013-02-22 22:05:20 +010010752EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010753
Johannes Berg947add32013-02-22 22:05:20 +010010754void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10755 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010756{
Johannes Berg947add32013-02-22 22:05:20 +010010757 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10758 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010759 struct sk_buff *msg;
10760
Johannes Berg947add32013-02-22 22:05:20 +010010761 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10762
Thomas Graf58050fc2012-06-28 03:57:45 +000010763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010764 if (!msg)
10765 return;
10766
John W. Linville66266b32012-03-15 13:25:41 -040010767 if (nl80211_send_station(msg, 0, 0, 0,
10768 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010769 nlmsg_free(msg);
10770 return;
10771 }
10772
Johannes Berg68eb5502013-11-19 15:19:38 +010010773 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010774 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010775}
Johannes Berg947add32013-02-22 22:05:20 +010010776EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010777
Johannes Berg947add32013-02-22 22:05:20 +010010778void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010779{
Johannes Berg947add32013-02-22 22:05:20 +010010780 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10781 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010782 struct sk_buff *msg;
10783 void *hdr;
10784
Johannes Berg947add32013-02-22 22:05:20 +010010785 trace_cfg80211_del_sta(dev, mac_addr);
10786
Thomas Graf58050fc2012-06-28 03:57:45 +000010787 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010788 if (!msg)
10789 return;
10790
10791 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10792 if (!hdr) {
10793 nlmsg_free(msg);
10794 return;
10795 }
10796
David S. Miller9360ffd2012-03-29 04:41:26 -040010797 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10798 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10799 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010800
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010801 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010802
Johannes Berg68eb5502013-11-19 15:19:38 +010010803 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010804 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010805 return;
10806
10807 nla_put_failure:
10808 genlmsg_cancel(msg, hdr);
10809 nlmsg_free(msg);
10810}
Johannes Berg947add32013-02-22 22:05:20 +010010811EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010812
Johannes Berg947add32013-02-22 22:05:20 +010010813void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10814 enum nl80211_connect_failed_reason reason,
10815 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010816{
Johannes Berg947add32013-02-22 22:05:20 +010010817 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10818 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010819 struct sk_buff *msg;
10820 void *hdr;
10821
10822 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10823 if (!msg)
10824 return;
10825
10826 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10827 if (!hdr) {
10828 nlmsg_free(msg);
10829 return;
10830 }
10831
10832 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10833 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10834 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10835 goto nla_put_failure;
10836
10837 genlmsg_end(msg, hdr);
10838
Johannes Berg68eb5502013-11-19 15:19:38 +010010839 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010840 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010841 return;
10842
10843 nla_put_failure:
10844 genlmsg_cancel(msg, hdr);
10845 nlmsg_free(msg);
10846}
Johannes Berg947add32013-02-22 22:05:20 +010010847EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010848
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010849static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10850 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010851{
10852 struct wireless_dev *wdev = dev->ieee80211_ptr;
10853 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10854 struct sk_buff *msg;
10855 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010856 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010857
Eric W. Biederman15e47302012-09-07 20:12:54 +000010858 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010859 return false;
10860
10861 msg = nlmsg_new(100, gfp);
10862 if (!msg)
10863 return true;
10864
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010865 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010866 if (!hdr) {
10867 nlmsg_free(msg);
10868 return true;
10869 }
10870
David S. Miller9360ffd2012-03-29 04:41:26 -040010871 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10872 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10873 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10874 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010875
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010876 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010877 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010878 return true;
10879
10880 nla_put_failure:
10881 genlmsg_cancel(msg, hdr);
10882 nlmsg_free(msg);
10883 return true;
10884}
10885
Johannes Berg947add32013-02-22 22:05:20 +010010886bool cfg80211_rx_spurious_frame(struct net_device *dev,
10887 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010888{
Johannes Berg947add32013-02-22 22:05:20 +010010889 struct wireless_dev *wdev = dev->ieee80211_ptr;
10890 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010891
Johannes Berg947add32013-02-22 22:05:20 +010010892 trace_cfg80211_rx_spurious_frame(dev, addr);
10893
10894 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10895 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10896 trace_cfg80211_return_bool(false);
10897 return false;
10898 }
10899 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10900 addr, gfp);
10901 trace_cfg80211_return_bool(ret);
10902 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010903}
Johannes Berg947add32013-02-22 22:05:20 +010010904EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10905
10906bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10907 const u8 *addr, gfp_t gfp)
10908{
10909 struct wireless_dev *wdev = dev->ieee80211_ptr;
10910 bool ret;
10911
10912 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10913
10914 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10915 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10916 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10917 trace_cfg80211_return_bool(false);
10918 return false;
10919 }
10920 ret = __nl80211_unexpected_frame(dev,
10921 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10922 addr, gfp);
10923 trace_cfg80211_return_bool(ret);
10924 return ret;
10925}
10926EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010927
Johannes Berg2e161f72010-08-12 15:38:38 +020010928int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010929 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010930 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010931 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010932{
Johannes Berg71bbc992012-06-15 15:30:18 +020010933 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010934 struct sk_buff *msg;
10935 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010936
10937 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10938 if (!msg)
10939 return -ENOMEM;
10940
Johannes Berg2e161f72010-08-12 15:38:38 +020010941 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010942 if (!hdr) {
10943 nlmsg_free(msg);
10944 return -ENOMEM;
10945 }
10946
David S. Miller9360ffd2012-03-29 04:41:26 -040010947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010948 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10949 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010950 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010951 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10952 (sig_dbm &&
10953 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010954 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10955 (flags &&
10956 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010957 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010958
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010959 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010960
Eric W. Biederman15e47302012-09-07 20:12:54 +000010961 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010962
10963 nla_put_failure:
10964 genlmsg_cancel(msg, hdr);
10965 nlmsg_free(msg);
10966 return -ENOBUFS;
10967}
10968
Johannes Berg947add32013-02-22 22:05:20 +010010969void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10970 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010971{
Johannes Berg947add32013-02-22 22:05:20 +010010972 struct wiphy *wiphy = wdev->wiphy;
10973 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010974 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010975 struct sk_buff *msg;
10976 void *hdr;
10977
Johannes Berg947add32013-02-22 22:05:20 +010010978 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10979
Jouni Malinen026331c2010-02-15 12:53:10 +020010980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10981 if (!msg)
10982 return;
10983
Johannes Berg2e161f72010-08-12 15:38:38 +020010984 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010985 if (!hdr) {
10986 nlmsg_free(msg);
10987 return;
10988 }
10989
David S. Miller9360ffd2012-03-29 04:41:26 -040010990 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010991 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10992 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010993 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010994 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10995 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10996 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10997 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010998
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010999 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011000
Johannes Berg68eb5502013-11-19 15:19:38 +010011001 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011002 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011003 return;
11004
11005 nla_put_failure:
11006 genlmsg_cancel(msg, hdr);
11007 nlmsg_free(msg);
11008}
Johannes Berg947add32013-02-22 22:05:20 +010011009EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011010
Johannes Berg947add32013-02-22 22:05:20 +010011011void cfg80211_cqm_rssi_notify(struct net_device *dev,
11012 enum nl80211_cqm_rssi_threshold_event rssi_event,
11013 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011014{
Johannes Berg947add32013-02-22 22:05:20 +010011015 struct wireless_dev *wdev = dev->ieee80211_ptr;
11016 struct wiphy *wiphy = wdev->wiphy;
11017 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011018 struct sk_buff *msg;
11019 struct nlattr *pinfoattr;
11020 void *hdr;
11021
Johannes Berg947add32013-02-22 22:05:20 +010011022 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11023
Thomas Graf58050fc2012-06-28 03:57:45 +000011024 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011025 if (!msg)
11026 return;
11027
11028 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11029 if (!hdr) {
11030 nlmsg_free(msg);
11031 return;
11032 }
11033
David S. Miller9360ffd2012-03-29 04:41:26 -040011034 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011035 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011036 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011037
11038 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11039 if (!pinfoattr)
11040 goto nla_put_failure;
11041
David S. Miller9360ffd2012-03-29 04:41:26 -040011042 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11043 rssi_event))
11044 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011045
11046 nla_nest_end(msg, pinfoattr);
11047
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011048 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011049
Johannes Berg68eb5502013-11-19 15:19:38 +010011050 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011051 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011052 return;
11053
11054 nla_put_failure:
11055 genlmsg_cancel(msg, hdr);
11056 nlmsg_free(msg);
11057}
Johannes Berg947add32013-02-22 22:05:20 +010011058EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011059
Johannes Berg947add32013-02-22 22:05:20 +010011060static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11061 struct net_device *netdev, const u8 *bssid,
11062 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011063{
11064 struct sk_buff *msg;
11065 struct nlattr *rekey_attr;
11066 void *hdr;
11067
Thomas Graf58050fc2012-06-28 03:57:45 +000011068 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011069 if (!msg)
11070 return;
11071
11072 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11073 if (!hdr) {
11074 nlmsg_free(msg);
11075 return;
11076 }
11077
David S. Miller9360ffd2012-03-29 04:41:26 -040011078 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11079 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11080 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11081 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011082
11083 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11084 if (!rekey_attr)
11085 goto nla_put_failure;
11086
David S. Miller9360ffd2012-03-29 04:41:26 -040011087 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11088 NL80211_REPLAY_CTR_LEN, replay_ctr))
11089 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011090
11091 nla_nest_end(msg, rekey_attr);
11092
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011093 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011094
Johannes Berg68eb5502013-11-19 15:19:38 +010011095 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011096 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011097 return;
11098
11099 nla_put_failure:
11100 genlmsg_cancel(msg, hdr);
11101 nlmsg_free(msg);
11102}
11103
Johannes Berg947add32013-02-22 22:05:20 +010011104void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11105 const u8 *replay_ctr, gfp_t gfp)
11106{
11107 struct wireless_dev *wdev = dev->ieee80211_ptr;
11108 struct wiphy *wiphy = wdev->wiphy;
11109 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11110
11111 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11112 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11113}
11114EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11115
11116static void
11117nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11118 struct net_device *netdev, int index,
11119 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011120{
11121 struct sk_buff *msg;
11122 struct nlattr *attr;
11123 void *hdr;
11124
Thomas Graf58050fc2012-06-28 03:57:45 +000011125 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011126 if (!msg)
11127 return;
11128
11129 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11130 if (!hdr) {
11131 nlmsg_free(msg);
11132 return;
11133 }
11134
David S. Miller9360ffd2012-03-29 04:41:26 -040011135 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11136 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11137 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011138
11139 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11140 if (!attr)
11141 goto nla_put_failure;
11142
David S. Miller9360ffd2012-03-29 04:41:26 -040011143 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11144 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11145 (preauth &&
11146 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11147 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011148
11149 nla_nest_end(msg, attr);
11150
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011151 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011152
Johannes Berg68eb5502013-11-19 15:19:38 +010011153 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011154 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011155 return;
11156
11157 nla_put_failure:
11158 genlmsg_cancel(msg, hdr);
11159 nlmsg_free(msg);
11160}
11161
Johannes Berg947add32013-02-22 22:05:20 +010011162void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11163 const u8 *bssid, bool preauth, gfp_t gfp)
11164{
11165 struct wireless_dev *wdev = dev->ieee80211_ptr;
11166 struct wiphy *wiphy = wdev->wiphy;
11167 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11168
11169 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11170 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11171}
11172EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11173
11174static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11175 struct net_device *netdev,
11176 struct cfg80211_chan_def *chandef,
11177 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011178{
11179 struct sk_buff *msg;
11180 void *hdr;
11181
Thomas Graf58050fc2012-06-28 03:57:45 +000011182 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011183 if (!msg)
11184 return;
11185
11186 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11187 if (!hdr) {
11188 nlmsg_free(msg);
11189 return;
11190 }
11191
Johannes Berg683b6d32012-11-08 21:25:48 +010011192 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11193 goto nla_put_failure;
11194
11195 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011196 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011197
11198 genlmsg_end(msg, hdr);
11199
Johannes Berg68eb5502013-11-19 15:19:38 +010011200 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011201 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011202 return;
11203
11204 nla_put_failure:
11205 genlmsg_cancel(msg, hdr);
11206 nlmsg_free(msg);
11207}
11208
Johannes Berg947add32013-02-22 22:05:20 +010011209void cfg80211_ch_switch_notify(struct net_device *dev,
11210 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011211{
Johannes Berg947add32013-02-22 22:05:20 +010011212 struct wireless_dev *wdev = dev->ieee80211_ptr;
11213 struct wiphy *wiphy = wdev->wiphy;
11214 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11215
Simon Wunderliche487eae2013-11-21 18:19:51 +010011216 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011217
Simon Wunderliche487eae2013-11-21 18:19:51 +010011218 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011219
11220 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011221 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011222 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11223 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011224 return;
Johannes Berg947add32013-02-22 22:05:20 +010011225
Michal Kazior9e0e2962014-01-29 14:22:27 +010011226 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011227 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011228 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011229}
11230EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11231
11232void cfg80211_cqm_txe_notify(struct net_device *dev,
11233 const u8 *peer, u32 num_packets,
11234 u32 rate, u32 intvl, gfp_t gfp)
11235{
11236 struct wireless_dev *wdev = dev->ieee80211_ptr;
11237 struct wiphy *wiphy = wdev->wiphy;
11238 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011239 struct sk_buff *msg;
11240 struct nlattr *pinfoattr;
11241 void *hdr;
11242
11243 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11244 if (!msg)
11245 return;
11246
11247 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11248 if (!hdr) {
11249 nlmsg_free(msg);
11250 return;
11251 }
11252
11253 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011254 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011255 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11256 goto nla_put_failure;
11257
11258 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11259 if (!pinfoattr)
11260 goto nla_put_failure;
11261
11262 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11263 goto nla_put_failure;
11264
11265 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11266 goto nla_put_failure;
11267
11268 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11269 goto nla_put_failure;
11270
11271 nla_nest_end(msg, pinfoattr);
11272
11273 genlmsg_end(msg, hdr);
11274
Johannes Berg68eb5502013-11-19 15:19:38 +010011275 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011276 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011277 return;
11278
11279 nla_put_failure:
11280 genlmsg_cancel(msg, hdr);
11281 nlmsg_free(msg);
11282}
Johannes Berg947add32013-02-22 22:05:20 +010011283EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011284
11285void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011286nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011287 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011288 enum nl80211_radar_event event,
11289 struct net_device *netdev, gfp_t gfp)
11290{
11291 struct sk_buff *msg;
11292 void *hdr;
11293
11294 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11295 if (!msg)
11296 return;
11297
11298 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11299 if (!hdr) {
11300 nlmsg_free(msg);
11301 return;
11302 }
11303
11304 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11305 goto nla_put_failure;
11306
11307 /* NOP and radar events don't need a netdev parameter */
11308 if (netdev) {
11309 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11310
11311 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11312 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11313 goto nla_put_failure;
11314 }
11315
11316 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11317 goto nla_put_failure;
11318
11319 if (nl80211_send_chandef(msg, chandef))
11320 goto nla_put_failure;
11321
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011322 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011323
Johannes Berg68eb5502013-11-19 15:19:38 +010011324 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011325 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011326 return;
11327
11328 nla_put_failure:
11329 genlmsg_cancel(msg, hdr);
11330 nlmsg_free(msg);
11331}
11332
Johannes Berg947add32013-02-22 22:05:20 +010011333void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11334 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011335{
Johannes Berg947add32013-02-22 22:05:20 +010011336 struct wireless_dev *wdev = dev->ieee80211_ptr;
11337 struct wiphy *wiphy = wdev->wiphy;
11338 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011339 struct sk_buff *msg;
11340 struct nlattr *pinfoattr;
11341 void *hdr;
11342
Johannes Berg947add32013-02-22 22:05:20 +010011343 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11344
Thomas Graf58050fc2012-06-28 03:57:45 +000011345 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011346 if (!msg)
11347 return;
11348
11349 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11350 if (!hdr) {
11351 nlmsg_free(msg);
11352 return;
11353 }
11354
David S. Miller9360ffd2012-03-29 04:41:26 -040011355 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011356 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011357 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11358 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011359
11360 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11361 if (!pinfoattr)
11362 goto nla_put_failure;
11363
David S. Miller9360ffd2012-03-29 04:41:26 -040011364 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11365 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011366
11367 nla_nest_end(msg, pinfoattr);
11368
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011369 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011370
Johannes Berg68eb5502013-11-19 15:19:38 +010011371 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011372 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011373 return;
11374
11375 nla_put_failure:
11376 genlmsg_cancel(msg, hdr);
11377 nlmsg_free(msg);
11378}
Johannes Berg947add32013-02-22 22:05:20 +010011379EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011380
Johannes Berg7f6cf312011-11-04 11:18:15 +010011381void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11382 u64 cookie, bool acked, gfp_t gfp)
11383{
11384 struct wireless_dev *wdev = dev->ieee80211_ptr;
11385 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11386 struct sk_buff *msg;
11387 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011388
Beni Lev4ee3e062012-08-27 12:49:39 +030011389 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11390
Thomas Graf58050fc2012-06-28 03:57:45 +000011391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011392
Johannes Berg7f6cf312011-11-04 11:18:15 +010011393 if (!msg)
11394 return;
11395
11396 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11397 if (!hdr) {
11398 nlmsg_free(msg);
11399 return;
11400 }
11401
David S. Miller9360ffd2012-03-29 04:41:26 -040011402 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11403 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11404 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11405 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11406 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11407 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011408
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011409 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011410
Johannes Berg68eb5502013-11-19 15:19:38 +010011411 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011412 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011413 return;
11414
11415 nla_put_failure:
11416 genlmsg_cancel(msg, hdr);
11417 nlmsg_free(msg);
11418}
11419EXPORT_SYMBOL(cfg80211_probe_status);
11420
Johannes Berg5e760232011-11-04 11:18:17 +010011421void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11422 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011423 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010011424{
11425 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11426 struct sk_buff *msg;
11427 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011428 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010011429
Beni Lev4ee3e062012-08-27 12:49:39 +030011430 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11431
Ben Greear37c73b52012-10-26 14:49:25 -070011432 spin_lock_bh(&rdev->beacon_registrations_lock);
11433 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11434 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11435 if (!msg) {
11436 spin_unlock_bh(&rdev->beacon_registrations_lock);
11437 return;
11438 }
Johannes Berg5e760232011-11-04 11:18:17 +010011439
Ben Greear37c73b52012-10-26 14:49:25 -070011440 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11441 if (!hdr)
11442 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010011443
Ben Greear37c73b52012-10-26 14:49:25 -070011444 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11445 (freq &&
11446 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11447 (sig_dbm &&
11448 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11449 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11450 goto nla_put_failure;
11451
11452 genlmsg_end(msg, hdr);
11453
11454 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010011455 }
Ben Greear37c73b52012-10-26 14:49:25 -070011456 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011457 return;
11458
11459 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011460 spin_unlock_bh(&rdev->beacon_registrations_lock);
11461 if (hdr)
11462 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010011463 nlmsg_free(msg);
11464}
11465EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11466
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011467#ifdef CONFIG_PM
11468void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11469 struct cfg80211_wowlan_wakeup *wakeup,
11470 gfp_t gfp)
11471{
11472 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11473 struct sk_buff *msg;
11474 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011475 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011476
11477 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11478
11479 if (wakeup)
11480 size += wakeup->packet_present_len;
11481
11482 msg = nlmsg_new(size, gfp);
11483 if (!msg)
11484 return;
11485
11486 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11487 if (!hdr)
11488 goto free_msg;
11489
11490 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11491 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11492 goto free_msg;
11493
11494 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11495 wdev->netdev->ifindex))
11496 goto free_msg;
11497
11498 if (wakeup) {
11499 struct nlattr *reasons;
11500
11501 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011502 if (!reasons)
11503 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011504
11505 if (wakeup->disconnect &&
11506 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11507 goto free_msg;
11508 if (wakeup->magic_pkt &&
11509 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11510 goto free_msg;
11511 if (wakeup->gtk_rekey_failure &&
11512 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11513 goto free_msg;
11514 if (wakeup->eap_identity_req &&
11515 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11516 goto free_msg;
11517 if (wakeup->four_way_handshake &&
11518 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11519 goto free_msg;
11520 if (wakeup->rfkill_release &&
11521 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11522 goto free_msg;
11523
11524 if (wakeup->pattern_idx >= 0 &&
11525 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11526 wakeup->pattern_idx))
11527 goto free_msg;
11528
Johannes Bergae917c92013-10-25 11:05:22 +020011529 if (wakeup->tcp_match &&
11530 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11531 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011532
Johannes Bergae917c92013-10-25 11:05:22 +020011533 if (wakeup->tcp_connlost &&
11534 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11535 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011536
Johannes Bergae917c92013-10-25 11:05:22 +020011537 if (wakeup->tcp_nomoretokens &&
11538 nla_put_flag(msg,
11539 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11540 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011541
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011542 if (wakeup->packet) {
11543 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11544 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11545
11546 if (!wakeup->packet_80211) {
11547 pkt_attr =
11548 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11549 len_attr =
11550 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11551 }
11552
11553 if (wakeup->packet_len &&
11554 nla_put_u32(msg, len_attr, wakeup->packet_len))
11555 goto free_msg;
11556
11557 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11558 wakeup->packet))
11559 goto free_msg;
11560 }
11561
11562 nla_nest_end(msg, reasons);
11563 }
11564
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011565 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011566
Johannes Berg68eb5502013-11-19 15:19:38 +010011567 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011568 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011569 return;
11570
11571 free_msg:
11572 nlmsg_free(msg);
11573}
11574EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11575#endif
11576
Jouni Malinen3475b092012-11-16 22:49:57 +020011577void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11578 enum nl80211_tdls_operation oper,
11579 u16 reason_code, gfp_t gfp)
11580{
11581 struct wireless_dev *wdev = dev->ieee80211_ptr;
11582 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11583 struct sk_buff *msg;
11584 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011585
11586 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11587 reason_code);
11588
11589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11590 if (!msg)
11591 return;
11592
11593 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11594 if (!hdr) {
11595 nlmsg_free(msg);
11596 return;
11597 }
11598
11599 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11600 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11601 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11602 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11603 (reason_code > 0 &&
11604 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11605 goto nla_put_failure;
11606
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011607 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011608
Johannes Berg68eb5502013-11-19 15:19:38 +010011609 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011610 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011611 return;
11612
11613 nla_put_failure:
11614 genlmsg_cancel(msg, hdr);
11615 nlmsg_free(msg);
11616}
11617EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11618
Jouni Malinen026331c2010-02-15 12:53:10 +020011619static int nl80211_netlink_notify(struct notifier_block * nb,
11620 unsigned long state,
11621 void *_notify)
11622{
11623 struct netlink_notify *notify = _notify;
11624 struct cfg80211_registered_device *rdev;
11625 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011626 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011627
11628 if (state != NETLINK_URELEASE)
11629 return NOTIFY_DONE;
11630
11631 rcu_read_lock();
11632
Johannes Berg5e760232011-11-04 11:18:17 +010011633 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011634 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011635 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011636
11637 spin_lock_bh(&rdev->beacon_registrations_lock);
11638 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11639 list) {
11640 if (reg->nlportid == notify->portid) {
11641 list_del(&reg->list);
11642 kfree(reg);
11643 break;
11644 }
11645 }
11646 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011647 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011648
11649 rcu_read_unlock();
11650
11651 return NOTIFY_DONE;
11652}
11653
11654static struct notifier_block nl80211_netlink_notifier = {
11655 .notifier_call = nl80211_netlink_notify,
11656};
11657
Jouni Malinen355199e2013-02-27 17:14:27 +020011658void cfg80211_ft_event(struct net_device *netdev,
11659 struct cfg80211_ft_event_params *ft_event)
11660{
11661 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11662 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11663 struct sk_buff *msg;
11664 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011665
11666 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11667
11668 if (!ft_event->target_ap)
11669 return;
11670
11671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11672 if (!msg)
11673 return;
11674
11675 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011676 if (!hdr)
11677 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011678
Johannes Bergae917c92013-10-25 11:05:22 +020011679 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11680 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11681 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11682 goto out;
11683
11684 if (ft_event->ies &&
11685 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11686 goto out;
11687 if (ft_event->ric_ies &&
11688 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11689 ft_event->ric_ies))
11690 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011691
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011692 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011693
Johannes Berg68eb5502013-11-19 15:19:38 +010011694 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011695 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011696 return;
11697 out:
11698 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011699}
11700EXPORT_SYMBOL(cfg80211_ft_event);
11701
Arend van Spriel5de17982013-04-18 15:49:00 +020011702void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11703{
11704 struct cfg80211_registered_device *rdev;
11705 struct sk_buff *msg;
11706 void *hdr;
11707 u32 nlportid;
11708
11709 rdev = wiphy_to_dev(wdev->wiphy);
11710 if (!rdev->crit_proto_nlportid)
11711 return;
11712
11713 nlportid = rdev->crit_proto_nlportid;
11714 rdev->crit_proto_nlportid = 0;
11715
11716 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11717 if (!msg)
11718 return;
11719
11720 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11721 if (!hdr)
11722 goto nla_put_failure;
11723
11724 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11725 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11726 goto nla_put_failure;
11727
11728 genlmsg_end(msg, hdr);
11729
11730 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11731 return;
11732
11733 nla_put_failure:
11734 if (hdr)
11735 genlmsg_cancel(msg, hdr);
11736 nlmsg_free(msg);
11737
11738}
11739EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11740
Johannes Berg348baf02014-01-24 14:06:29 +010011741void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11742{
11743 struct wiphy *wiphy = wdev->wiphy;
11744 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11745 struct sk_buff *msg;
11746 void *hdr;
11747
11748 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11749 if (!msg)
11750 return;
11751
11752 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11753 if (!hdr)
11754 goto out;
11755
11756 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11757 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11758 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11759 goto out;
11760
11761 genlmsg_end(msg, hdr);
11762
11763 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11764 NL80211_MCGRP_MLME, GFP_KERNEL);
11765 return;
11766 out:
11767 nlmsg_free(msg);
11768}
11769
Johannes Berg55682962007-09-20 13:09:35 -040011770/* initialisation/exit functions */
11771
11772int nl80211_init(void)
11773{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011774 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011775
Johannes Berg2a94fe42013-11-19 15:19:39 +010011776 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11777 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011778 if (err)
11779 return err;
11780
Jouni Malinen026331c2010-02-15 12:53:10 +020011781 err = netlink_register_notifier(&nl80211_netlink_notifier);
11782 if (err)
11783 goto err_out;
11784
Johannes Berg55682962007-09-20 13:09:35 -040011785 return 0;
11786 err_out:
11787 genl_unregister_family(&nl80211_fam);
11788 return err;
11789}
11790
11791void nl80211_exit(void)
11792{
Jouni Malinen026331c2010-02-15 12:53:10 +020011793 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011794 genl_unregister_family(&nl80211_fam);
11795}