blob: be836098d342dc132b46c339f0fdfdf1dafd5264 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Bergf84f7712013-11-14 17:14:45 +010033static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020034 struct genl_info *info);
Johannes Bergf84f7712013-11-14 17:14:45 +010035static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020036 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070040 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040044 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg2a94fe42013-11-19 15:19:39 +010050/* multicast groups */
51enum nl80211_multicast_groups {
52 NL80211_MCGRP_CONFIG,
53 NL80211_MCGRP_SCAN,
54 NL80211_MCGRP_REGULATORY,
55 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010056 NL80211_MCGRP_VENDOR,
Johannes Berg2a94fe42013-11-19 15:19:39 +010057 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
58};
59
60static const struct genl_multicast_group nl80211_mcgrps[] = {
61 [NL80211_MCGRP_CONFIG] = { .name = "config", },
62 [NL80211_MCGRP_SCAN] = { .name = "scan", },
63 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
64 [NL80211_MCGRP_MLME] = { .name = "mlme", },
Johannes Berg567ffc32013-12-18 14:43:31 +010065 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
Johannes Berg2a94fe42013-11-19 15:19:39 +010066#ifdef CONFIG_NL80211_TESTMODE
67 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
68#endif
69};
70
Johannes Berg89a54e42012-06-15 14:33:17 +020071/* returns ERR_PTR values */
72static struct wireless_dev *
73__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040074{
Johannes Berg89a54e42012-06-15 14:33:17 +020075 struct cfg80211_registered_device *rdev;
76 struct wireless_dev *result = NULL;
77 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
78 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
79 u64 wdev_id;
80 int wiphy_idx = -1;
81 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040082
Johannes Berg5fe231e2013-05-08 21:45:15 +020083 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040084
Johannes Berg89a54e42012-06-15 14:33:17 +020085 if (!have_ifidx && !have_wdev_id)
86 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040087
Johannes Berg89a54e42012-06-15 14:33:17 +020088 if (have_ifidx)
89 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
90 if (have_wdev_id) {
91 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
92 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040093 }
94
Johannes Berg89a54e42012-06-15 14:33:17 +020095 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
96 struct wireless_dev *wdev;
97
98 if (wiphy_net(&rdev->wiphy) != netns)
99 continue;
100
101 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
102 continue;
103
Johannes Berg89a54e42012-06-15 14:33:17 +0200104 list_for_each_entry(wdev, &rdev->wdev_list, list) {
105 if (have_ifidx && wdev->netdev &&
106 wdev->netdev->ifindex == ifidx) {
107 result = wdev;
108 break;
109 }
110 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
111 result = wdev;
112 break;
113 }
114 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200115
116 if (result)
117 break;
118 }
119
120 if (result)
121 return result;
122 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400123}
124
Johannes Berga9455402012-06-15 13:32:49 +0200125static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200126__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200127{
Johannes Berg7fee4772012-06-15 14:09:58 +0200128 struct cfg80211_registered_device *rdev = NULL, *tmp;
129 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200130
Johannes Berg5fe231e2013-05-08 21:45:15 +0200131 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200132
Johannes Berg878d9ec2012-06-15 14:18:32 +0200133 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200134 !attrs[NL80211_ATTR_IFINDEX] &&
135 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200136 return ERR_PTR(-EINVAL);
137
Johannes Berg878d9ec2012-06-15 14:18:32 +0200138 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200139 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200140 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200141
Johannes Berg89a54e42012-06-15 14:33:17 +0200142 if (attrs[NL80211_ATTR_WDEV]) {
143 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
144 struct wireless_dev *wdev;
145 bool found = false;
146
147 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
148 if (tmp) {
149 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200150 list_for_each_entry(wdev, &tmp->wdev_list, list) {
151 if (wdev->identifier != (u32)wdev_id)
152 continue;
153 found = true;
154 break;
155 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200156
157 if (!found)
158 tmp = NULL;
159
160 if (rdev && tmp != rdev)
161 return ERR_PTR(-EINVAL);
162 rdev = tmp;
163 }
164 }
165
Johannes Berg878d9ec2012-06-15 14:18:32 +0200166 if (attrs[NL80211_ATTR_IFINDEX]) {
167 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Ying Xue7f2b8562014-01-15 10:23:45 +0800168 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200169 if (netdev) {
170 if (netdev->ieee80211_ptr)
171 tmp = wiphy_to_dev(
172 netdev->ieee80211_ptr->wiphy);
173 else
174 tmp = NULL;
175
Johannes Berg7fee4772012-06-15 14:09:58 +0200176 /* not wireless device -- return error */
177 if (!tmp)
178 return ERR_PTR(-EINVAL);
179
180 /* mismatch -- return error */
181 if (rdev && tmp != rdev)
182 return ERR_PTR(-EINVAL);
183
184 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200185 }
Johannes Berga9455402012-06-15 13:32:49 +0200186 }
187
Johannes Berg4f7eff12012-06-15 14:14:22 +0200188 if (!rdev)
189 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200190
Johannes Berg4f7eff12012-06-15 14:14:22 +0200191 if (netns != wiphy_net(&rdev->wiphy))
192 return ERR_PTR(-ENODEV);
193
194 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200195}
196
197/*
198 * This function returns a pointer to the driver
199 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200200 *
201 * The result of this can be a PTR_ERR and hence must
202 * be checked with IS_ERR() for errors.
203 */
204static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200205cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200206{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200207 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208}
209
Johannes Berg55682962007-09-20 13:09:35 -0400210/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000211static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400212 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
213 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700214 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200215 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100216
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200217 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530218 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100219 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
220 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
222
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200223 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
224 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
226 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100227 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400228
229 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
230 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
231 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100232
Eliad Pellere007b852011-11-24 18:13:56 +0200233 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
234 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100235
Johannes Bergb9454e82009-07-08 13:29:08 +0200236 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100237 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
238 .len = WLAN_MAX_KEY_LEN },
239 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
240 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
241 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200242 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200243 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100244
245 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
246 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
247 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
248 .len = IEEE80211_MAX_DATA_LEN },
249 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100251 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
252 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
253 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
255 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100256 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100257 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200258 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100259 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800260 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300262
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
264 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
265
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300266 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
267 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
268 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200269 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
270 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100271 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300272
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800273 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700274 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700275
Johannes Berg6c739412011-11-03 09:27:01 +0100276 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200277
278 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
279 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
280 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100281 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
282 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200283
284 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_SSID_LEN },
286 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
287 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200288 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300289 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300290 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300291 [NL80211_ATTR_STA_FLAGS2] = {
292 .len = sizeof(struct nl80211_sta_flag_update),
293 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300294 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300295 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
296 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200297 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
299 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200300 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100301 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100302 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
303 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100304 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
305 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200306 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200307 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
308 .len = IEEE80211_MAX_DATA_LEN },
309 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200310 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200311 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300312 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200313 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300314 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
315 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200316 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900317 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100319 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100320 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100321 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200322 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700323 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300324 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200325 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200326 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300327 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300328 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
329 .len = IEEE80211_MAX_DATA_LEN },
330 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530332 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300333 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530334 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300335 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
336 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
337 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
339 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100340 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200341 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700343 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800344 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
346 .len = NL80211_HT_CAPABILITY_LEN
347 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100348 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530349 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530350 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200351 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700352 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300353 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000354 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700355 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100356 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
357 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530358 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
359 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200360 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
361 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100362 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100363 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
364 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
365 .len = NL80211_VHT_CAPABILITY_LEN,
366 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200367 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
368 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
369 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300370 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200371 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
372 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100379 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100380 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
381 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
382 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800383 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
384 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200385 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
386 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530387 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400388};
389
Johannes Berge31b8212010-10-05 19:39:30 +0200390/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000391static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200392 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200393 [NL80211_KEY_IDX] = { .type = NLA_U8 },
394 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200395 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200396 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
397 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200398 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100399 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
400};
401
402/* policy for the key default flags */
403static const struct nla_policy
404nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
405 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
406 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200407};
408
Johannes Bergff1b6e62011-05-04 15:37:28 +0200409/* policy for WoWLAN attributes */
410static const struct nla_policy
411nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
412 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
413 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200416 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
417 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
418 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100420 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
421};
422
423static const struct nla_policy
424nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
425 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
426 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
427 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
428 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
429 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
430 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
432 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
433 },
434 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
435 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
436 },
437 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
438 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
439 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200440};
441
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700442/* policy for coalesce rule attributes */
443static const struct nla_policy
444nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
445 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
446 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
447 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
448};
449
Johannes Berge5497d72011-07-05 16:35:40 +0200450/* policy for GTK rekey offload attributes */
451static const struct nla_policy
452nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
453 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
454 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
455 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
456};
457
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300458static const struct nla_policy
459nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200460 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300461 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700462 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300463};
464
Johannes Berg97990a02013-04-19 01:02:55 +0200465static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
466 struct netlink_callback *cb,
467 struct cfg80211_registered_device **rdev,
468 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100469{
Johannes Berg67748892010-10-04 21:14:06 +0200470 int err;
471
Johannes Berg67748892010-10-04 21:14:06 +0200472 rtnl_lock();
473
Johannes Berg97990a02013-04-19 01:02:55 +0200474 if (!cb->args[0]) {
475 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
476 nl80211_fam.attrbuf, nl80211_fam.maxattr,
477 nl80211_policy);
478 if (err)
479 goto out_unlock;
480
481 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
482 nl80211_fam.attrbuf);
483 if (IS_ERR(*wdev)) {
484 err = PTR_ERR(*wdev);
485 goto out_unlock;
486 }
487 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200488 /* 0 is the first index - add 1 to parse only once */
489 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200490 cb->args[1] = (*wdev)->identifier;
491 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200492 /* subtract the 1 again here */
493 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200494 struct wireless_dev *tmp;
495
496 if (!wiphy) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
500 *rdev = wiphy_to_dev(wiphy);
501 *wdev = NULL;
502
Johannes Berg97990a02013-04-19 01:02:55 +0200503 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
504 if (tmp->identifier == cb->args[1]) {
505 *wdev = tmp;
506 break;
507 }
508 }
Johannes Berg97990a02013-04-19 01:02:55 +0200509
510 if (!*wdev) {
511 err = -ENODEV;
512 goto out_unlock;
513 }
Johannes Berg67748892010-10-04 21:14:06 +0200514 }
515
Johannes Berg67748892010-10-04 21:14:06 +0200516 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200517 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200518 rtnl_unlock();
519 return err;
520}
521
Johannes Berg97990a02013-04-19 01:02:55 +0200522static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200523{
Johannes Berg67748892010-10-04 21:14:06 +0200524 rtnl_unlock();
525}
526
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100527/* IE validation */
528static bool is_valid_ie_attr(const struct nlattr *attr)
529{
530 const u8 *pos;
531 int len;
532
533 if (!attr)
534 return true;
535
536 pos = nla_data(attr);
537 len = nla_len(attr);
538
539 while (len) {
540 u8 elemlen;
541
542 if (len < 2)
543 return false;
544 len -= 2;
545
546 elemlen = pos[1];
547 if (elemlen > len)
548 return false;
549
550 len -= elemlen;
551 pos += 2 + elemlen;
552 }
553
554 return true;
555}
556
Johannes Berg55682962007-09-20 13:09:35 -0400557/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000558static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400559 int flags, u8 cmd)
560{
561 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000562 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400563}
564
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400565static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100566 struct ieee80211_channel *chan,
567 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400568{
David S. Miller9360ffd2012-03-29 04:41:26 -0400569 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
570 chan->center_freq))
571 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400572
David S. Miller9360ffd2012-03-29 04:41:26 -0400573 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
574 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
575 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200576 if (chan->flags & IEEE80211_CHAN_NO_IR) {
577 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
578 goto nla_put_failure;
579 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
580 goto nla_put_failure;
581 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100582 if (chan->flags & IEEE80211_CHAN_RADAR) {
583 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
584 goto nla_put_failure;
585 if (large) {
586 u32 time;
587
588 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
589
590 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
591 chan->dfs_state))
592 goto nla_put_failure;
593 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
594 time))
595 goto nla_put_failure;
596 }
597 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400598
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100599 if (large) {
600 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
602 goto nla_put_failure;
603 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
604 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
605 goto nla_put_failure;
606 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
607 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
608 goto nla_put_failure;
609 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
610 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
611 goto nla_put_failure;
612 }
613
David S. Miller9360ffd2012-03-29 04:41:26 -0400614 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
615 DBM_TO_MBM(chan->max_power)))
616 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400617
618 return 0;
619
620 nla_put_failure:
621 return -ENOBUFS;
622}
623
Johannes Berg55682962007-09-20 13:09:35 -0400624/* netlink command implementations */
625
Johannes Bergb9454e82009-07-08 13:29:08 +0200626struct key_parse {
627 struct key_params p;
628 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200629 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200630 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100631 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200632};
633
634static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
635{
636 struct nlattr *tb[NL80211_KEY_MAX + 1];
637 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
638 nl80211_key_policy);
639 if (err)
640 return err;
641
642 k->def = !!tb[NL80211_KEY_DEFAULT];
643 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
644
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100645 if (k->def) {
646 k->def_uni = true;
647 k->def_multi = true;
648 }
649 if (k->defmgmt)
650 k->def_multi = true;
651
Johannes Bergb9454e82009-07-08 13:29:08 +0200652 if (tb[NL80211_KEY_IDX])
653 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
654
655 if (tb[NL80211_KEY_DATA]) {
656 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
657 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
658 }
659
660 if (tb[NL80211_KEY_SEQ]) {
661 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
662 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
663 }
664
665 if (tb[NL80211_KEY_CIPHER])
666 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
667
Johannes Berge31b8212010-10-05 19:39:30 +0200668 if (tb[NL80211_KEY_TYPE]) {
669 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
670 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
671 return -EINVAL;
672 }
673
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100674 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
675 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100676 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
677 tb[NL80211_KEY_DEFAULT_TYPES],
678 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100679 if (err)
680 return err;
681
682 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
683 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
684 }
685
Johannes Bergb9454e82009-07-08 13:29:08 +0200686 return 0;
687}
688
689static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
690{
691 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
692 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
693 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
694 }
695
696 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
697 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
698 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
699 }
700
701 if (info->attrs[NL80211_ATTR_KEY_IDX])
702 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
703
704 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
705 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
706
707 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
708 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
709
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100710 if (k->def) {
711 k->def_uni = true;
712 k->def_multi = true;
713 }
714 if (k->defmgmt)
715 k->def_multi = true;
716
Johannes Berge31b8212010-10-05 19:39:30 +0200717 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
718 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
719 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
720 return -EINVAL;
721 }
722
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
724 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
725 int err = nla_parse_nested(
726 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
727 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
728 nl80211_key_default_policy);
729 if (err)
730 return err;
731
732 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
733 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
734 }
735
Johannes Bergb9454e82009-07-08 13:29:08 +0200736 return 0;
737}
738
739static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
740{
741 int err;
742
743 memset(k, 0, sizeof(*k));
744 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200745 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200746
747 if (info->attrs[NL80211_ATTR_KEY])
748 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
749 else
750 err = nl80211_parse_key_old(info, k);
751
752 if (err)
753 return err;
754
755 if (k->def && k->defmgmt)
756 return -EINVAL;
757
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100758 if (k->defmgmt) {
759 if (k->def_uni || !k->def_multi)
760 return -EINVAL;
761 }
762
Johannes Bergb9454e82009-07-08 13:29:08 +0200763 if (k->idx != -1) {
764 if (k->defmgmt) {
765 if (k->idx < 4 || k->idx > 5)
766 return -EINVAL;
767 } else if (k->def) {
768 if (k->idx < 0 || k->idx > 3)
769 return -EINVAL;
770 } else {
771 if (k->idx < 0 || k->idx > 5)
772 return -EINVAL;
773 }
774 }
775
776 return 0;
777}
778
Johannes Bergfffd0932009-07-08 14:22:54 +0200779static struct cfg80211_cached_keys *
780nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530781 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200782{
783 struct key_parse parse;
784 struct nlattr *key;
785 struct cfg80211_cached_keys *result;
786 int rem, err, def = 0;
787
788 result = kzalloc(sizeof(*result), GFP_KERNEL);
789 if (!result)
790 return ERR_PTR(-ENOMEM);
791
792 result->def = -1;
793 result->defmgmt = -1;
794
795 nla_for_each_nested(key, keys, rem) {
796 memset(&parse, 0, sizeof(parse));
797 parse.idx = -1;
798
799 err = nl80211_parse_key_new(key, &parse);
800 if (err)
801 goto error;
802 err = -EINVAL;
803 if (!parse.p.key)
804 goto error;
805 if (parse.idx < 0 || parse.idx > 4)
806 goto error;
807 if (parse.def) {
808 if (def)
809 goto error;
810 def = 1;
811 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100812 if (!parse.def_uni || !parse.def_multi)
813 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200814 } else if (parse.defmgmt)
815 goto error;
816 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200817 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 if (err)
819 goto error;
820 result->params[parse.idx].cipher = parse.p.cipher;
821 result->params[parse.idx].key_len = parse.p.key_len;
822 result->params[parse.idx].key = result->data[parse.idx];
823 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530824
825 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
826 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
827 if (no_ht)
828 *no_ht = true;
829 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200830 }
831
832 return result;
833 error:
834 kfree(result);
835 return ERR_PTR(err);
836}
837
838static int nl80211_key_allowed(struct wireless_dev *wdev)
839{
840 ASSERT_WDEV_LOCK(wdev);
841
Johannes Bergfffd0932009-07-08 14:22:54 +0200842 switch (wdev->iftype) {
843 case NL80211_IFTYPE_AP:
844 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200845 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700846 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200847 break;
848 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200849 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200850 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200851 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200852 return -ENOLINK;
853 break;
854 default:
855 return -EINVAL;
856 }
857
858 return 0;
859}
860
Jouni Malinen664834d2014-01-15 00:01:44 +0200861static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
862 struct nlattr *tb)
863{
864 struct ieee80211_channel *chan;
865
866 if (tb == NULL)
867 return NULL;
868 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
869 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
870 return NULL;
871 return chan;
872}
873
Johannes Berg7527a782011-05-13 10:58:57 +0200874static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
875{
876 struct nlattr *nl_modes = nla_nest_start(msg, attr);
877 int i;
878
879 if (!nl_modes)
880 goto nla_put_failure;
881
882 i = 0;
883 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if ((ifmodes & 1) && nla_put_flag(msg, i))
885 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200886 ifmodes >>= 1;
887 i++;
888 }
889
890 nla_nest_end(msg, nl_modes);
891 return 0;
892
893nla_put_failure:
894 return -ENOBUFS;
895}
896
897static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100898 struct sk_buff *msg,
899 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200900{
901 struct nlattr *nl_combis;
902 int i, j;
903
904 nl_combis = nla_nest_start(msg,
905 NL80211_ATTR_INTERFACE_COMBINATIONS);
906 if (!nl_combis)
907 goto nla_put_failure;
908
909 for (i = 0; i < wiphy->n_iface_combinations; i++) {
910 const struct ieee80211_iface_combination *c;
911 struct nlattr *nl_combi, *nl_limits;
912
913 c = &wiphy->iface_combinations[i];
914
915 nl_combi = nla_nest_start(msg, i + 1);
916 if (!nl_combi)
917 goto nla_put_failure;
918
919 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
920 if (!nl_limits)
921 goto nla_put_failure;
922
923 for (j = 0; j < c->n_limits; j++) {
924 struct nlattr *nl_limit;
925
926 nl_limit = nla_nest_start(msg, j + 1);
927 if (!nl_limit)
928 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400929 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
930 c->limits[j].max))
931 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200932 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
933 c->limits[j].types))
934 goto nla_put_failure;
935 nla_nest_end(msg, nl_limit);
936 }
937
938 nla_nest_end(msg, nl_limits);
939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (c->beacon_int_infra_match &&
941 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
942 goto nla_put_failure;
943 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
944 c->num_different_channels) ||
945 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
946 c->max_interfaces))
947 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100948 if (large &&
949 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
950 c->radar_detect_widths))
951 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200952
953 nla_nest_end(msg, nl_combi);
954 }
955
956 nla_nest_end(msg, nl_combis);
957
958 return 0;
959nla_put_failure:
960 return -ENOBUFS;
961}
962
Johannes Berg3713b4e2013-02-14 16:19:38 +0100963#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100964static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
965 struct sk_buff *msg)
966{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200967 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100968 struct nlattr *nl_tcp;
969
970 if (!tcp)
971 return 0;
972
973 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
974 if (!nl_tcp)
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
978 tcp->data_payload_max))
979 return -ENOBUFS;
980
981 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
982 tcp->data_payload_max))
983 return -ENOBUFS;
984
985 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
986 return -ENOBUFS;
987
988 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
989 sizeof(*tcp->tok), tcp->tok))
990 return -ENOBUFS;
991
992 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
993 tcp->data_interval_max))
994 return -ENOBUFS;
995
996 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
997 tcp->wake_payload_max))
998 return -ENOBUFS;
999
1000 nla_nest_end(msg, nl_tcp);
1001 return 0;
1002}
1003
Johannes Berg3713b4e2013-02-14 16:19:38 +01001004static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001005 struct cfg80211_registered_device *dev,
1006 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001007{
1008 struct nlattr *nl_wowlan;
1009
Johannes Berg964dc9e2013-06-03 17:25:34 +02001010 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001011 return 0;
1012
1013 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1014 if (!nl_wowlan)
1015 return -ENOBUFS;
1016
Johannes Berg964dc9e2013-06-03 17:25:34 +02001017 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001018 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001019 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001020 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001023 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001024 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001025 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001026 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001027 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001028 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001029 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001031 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1033 return -ENOBUFS;
1034
Johannes Berg964dc9e2013-06-03 17:25:34 +02001035 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001036 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001037 .max_patterns = dev->wiphy.wowlan->n_patterns,
1038 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1039 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1040 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041 };
1042
1043 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1044 sizeof(pat), &pat))
1045 return -ENOBUFS;
1046 }
1047
Johannes Bergb56cf722013-02-20 01:02:38 +01001048 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1049 return -ENOBUFS;
1050
Johannes Berg3713b4e2013-02-14 16:19:38 +01001051 nla_nest_end(msg, nl_wowlan);
1052
1053 return 0;
1054}
1055#endif
1056
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001057static int nl80211_send_coalesce(struct sk_buff *msg,
1058 struct cfg80211_registered_device *dev)
1059{
1060 struct nl80211_coalesce_rule_support rule;
1061
1062 if (!dev->wiphy.coalesce)
1063 return 0;
1064
1065 rule.max_rules = dev->wiphy.coalesce->n_rules;
1066 rule.max_delay = dev->wiphy.coalesce->max_delay;
1067 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1068 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1069 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1070 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1071
1072 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1073 return -ENOBUFS;
1074
1075 return 0;
1076}
1077
Johannes Berg3713b4e2013-02-14 16:19:38 +01001078static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1079 struct ieee80211_supported_band *sband)
1080{
1081 struct nlattr *nl_rates, *nl_rate;
1082 struct ieee80211_rate *rate;
1083 int i;
1084
1085 /* add HT info */
1086 if (sband->ht_cap.ht_supported &&
1087 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1088 sizeof(sband->ht_cap.mcs),
1089 &sband->ht_cap.mcs) ||
1090 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1091 sband->ht_cap.cap) ||
1092 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1093 sband->ht_cap.ampdu_factor) ||
1094 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1095 sband->ht_cap.ampdu_density)))
1096 return -ENOBUFS;
1097
1098 /* add VHT info */
1099 if (sband->vht_cap.vht_supported &&
1100 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1101 sizeof(sband->vht_cap.vht_mcs),
1102 &sband->vht_cap.vht_mcs) ||
1103 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1104 sband->vht_cap.cap)))
1105 return -ENOBUFS;
1106
1107 /* add bitrates */
1108 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1109 if (!nl_rates)
1110 return -ENOBUFS;
1111
1112 for (i = 0; i < sband->n_bitrates; i++) {
1113 nl_rate = nla_nest_start(msg, i);
1114 if (!nl_rate)
1115 return -ENOBUFS;
1116
1117 rate = &sband->bitrates[i];
1118 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1119 rate->bitrate))
1120 return -ENOBUFS;
1121 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1122 nla_put_flag(msg,
1123 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1124 return -ENOBUFS;
1125
1126 nla_nest_end(msg, nl_rate);
1127 }
1128
1129 nla_nest_end(msg, nl_rates);
1130
1131 return 0;
1132}
1133
1134static int
1135nl80211_send_mgmt_stypes(struct sk_buff *msg,
1136 const struct ieee80211_txrx_stypes *mgmt_stypes)
1137{
1138 u16 stypes;
1139 struct nlattr *nl_ftypes, *nl_ifs;
1140 enum nl80211_iftype ift;
1141 int i;
1142
1143 if (!mgmt_stypes)
1144 return 0;
1145
1146 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1147 if (!nl_ifs)
1148 return -ENOBUFS;
1149
1150 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1151 nl_ftypes = nla_nest_start(msg, ift);
1152 if (!nl_ftypes)
1153 return -ENOBUFS;
1154 i = 0;
1155 stypes = mgmt_stypes[ift].tx;
1156 while (stypes) {
1157 if ((stypes & 1) &&
1158 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1159 (i << 4) | IEEE80211_FTYPE_MGMT))
1160 return -ENOBUFS;
1161 stypes >>= 1;
1162 i++;
1163 }
1164 nla_nest_end(msg, nl_ftypes);
1165 }
1166
1167 nla_nest_end(msg, nl_ifs);
1168
1169 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1170 if (!nl_ifs)
1171 return -ENOBUFS;
1172
1173 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1174 nl_ftypes = nla_nest_start(msg, ift);
1175 if (!nl_ftypes)
1176 return -ENOBUFS;
1177 i = 0;
1178 stypes = mgmt_stypes[ift].rx;
1179 while (stypes) {
1180 if ((stypes & 1) &&
1181 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1182 (i << 4) | IEEE80211_FTYPE_MGMT))
1183 return -ENOBUFS;
1184 stypes >>= 1;
1185 i++;
1186 }
1187 nla_nest_end(msg, nl_ftypes);
1188 }
1189 nla_nest_end(msg, nl_ifs);
1190
1191 return 0;
1192}
1193
Johannes Berg86e8cf92013-06-19 10:57:22 +02001194struct nl80211_dump_wiphy_state {
1195 s64 filter_wiphy;
1196 long start;
1197 long split_start, band_start, chan_start;
1198 bool split;
1199};
1200
Johannes Berg3713b4e2013-02-14 16:19:38 +01001201static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1202 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001203 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001204{
1205 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001206 struct nlattr *nl_bands, *nl_band;
1207 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001208 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001209 enum ieee80211_band band;
1210 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001211 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001212 const struct ieee80211_txrx_stypes *mgmt_stypes =
1213 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001214 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001215
Eric W. Biederman15e47302012-09-07 20:12:54 +00001216 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001217 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 return -ENOBUFS;
1219
Johannes Berg86e8cf92013-06-19 10:57:22 +02001220 if (WARN_ON(!state))
1221 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001222
David S. Miller9360ffd2012-03-29 04:41:26 -04001223 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001224 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1225 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001226 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001227 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001228 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001229
Johannes Berg86e8cf92013-06-19 10:57:22 +02001230 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001231 case 0:
1232 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1233 dev->wiphy.retry_short) ||
1234 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1235 dev->wiphy.retry_long) ||
1236 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1237 dev->wiphy.frag_threshold) ||
1238 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1239 dev->wiphy.rts_threshold) ||
1240 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1241 dev->wiphy.coverage_class) ||
1242 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1243 dev->wiphy.max_scan_ssids) ||
1244 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1245 dev->wiphy.max_sched_scan_ssids) ||
1246 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1247 dev->wiphy.max_scan_ie_len) ||
1248 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1249 dev->wiphy.max_sched_scan_ie_len) ||
1250 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1251 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001252 goto nla_put_failure;
1253
Johannes Berg3713b4e2013-02-14 16:19:38 +01001254 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1255 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1256 goto nla_put_failure;
1257 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1258 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1259 goto nla_put_failure;
1260 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1261 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1262 goto nla_put_failure;
1263 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1264 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1265 goto nla_put_failure;
1266 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1267 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1268 goto nla_put_failure;
1269 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1270 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001271 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001272 state->split_start++;
1273 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001274 break;
1275 case 1:
1276 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1277 sizeof(u32) * dev->wiphy.n_cipher_suites,
1278 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001279 goto nla_put_failure;
1280
Johannes Berg3713b4e2013-02-14 16:19:38 +01001281 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1282 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001283 goto nla_put_failure;
1284
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1286 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1287 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001288
Johannes Berg3713b4e2013-02-14 16:19:38 +01001289 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1290 dev->wiphy.available_antennas_tx) ||
1291 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1292 dev->wiphy.available_antennas_rx))
1293 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001294
Johannes Berg3713b4e2013-02-14 16:19:38 +01001295 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1296 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1297 dev->wiphy.probe_resp_offload))
1298 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001299
Johannes Berg3713b4e2013-02-14 16:19:38 +01001300 if ((dev->wiphy.available_antennas_tx ||
1301 dev->wiphy.available_antennas_rx) &&
1302 dev->ops->get_antenna) {
1303 u32 tx_ant = 0, rx_ant = 0;
1304 int res;
1305 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1306 if (!res) {
1307 if (nla_put_u32(msg,
1308 NL80211_ATTR_WIPHY_ANTENNA_TX,
1309 tx_ant) ||
1310 nla_put_u32(msg,
1311 NL80211_ATTR_WIPHY_ANTENNA_RX,
1312 rx_ant))
1313 goto nla_put_failure;
1314 }
Johannes Bergee688b002008-01-24 19:38:39 +01001315 }
1316
Johannes Berg86e8cf92013-06-19 10:57:22 +02001317 state->split_start++;
1318 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001319 break;
1320 case 2:
1321 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1322 dev->wiphy.interface_modes))
1323 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001324 state->split_start++;
1325 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001326 break;
1327 case 3:
1328 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1329 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001330 goto nla_put_failure;
1331
Johannes Berg86e8cf92013-06-19 10:57:22 +02001332 for (band = state->band_start;
1333 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001334 struct ieee80211_supported_band *sband;
1335
1336 sband = dev->wiphy.bands[band];
1337
1338 if (!sband)
1339 continue;
1340
1341 nl_band = nla_nest_start(msg, band);
1342 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001343 goto nla_put_failure;
1344
Johannes Berg86e8cf92013-06-19 10:57:22 +02001345 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001346 case 0:
1347 if (nl80211_send_band_rateinfo(msg, sband))
1348 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001349 state->chan_start++;
1350 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 break;
1352 default:
1353 /* add frequencies */
1354 nl_freqs = nla_nest_start(
1355 msg, NL80211_BAND_ATTR_FREQS);
1356 if (!nl_freqs)
1357 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001358
Johannes Berg86e8cf92013-06-19 10:57:22 +02001359 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001360 i < sband->n_channels;
1361 i++) {
1362 nl_freq = nla_nest_start(msg, i);
1363 if (!nl_freq)
1364 goto nla_put_failure;
1365
1366 chan = &sband->channels[i];
1367
Johannes Berg86e8cf92013-06-19 10:57:22 +02001368 if (nl80211_msg_put_channel(
1369 msg, chan,
1370 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 goto nla_put_failure;
1372
1373 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001374 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001375 break;
1376 }
1377 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001378 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001379 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001380 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001381 nla_nest_end(msg, nl_freqs);
1382 }
1383
1384 nla_nest_end(msg, nl_band);
1385
Johannes Berg86e8cf92013-06-19 10:57:22 +02001386 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001387 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001388 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001389 band--;
1390 break;
1391 }
Johannes Bergee688b002008-01-24 19:38:39 +01001392 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001393 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001394
Johannes Berg3713b4e2013-02-14 16:19:38 +01001395 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001396 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001397 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001398 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001399
Johannes Berg3713b4e2013-02-14 16:19:38 +01001400 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001401 if (state->band_start == 0 && state->chan_start == 0)
1402 state->split_start++;
1403 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 break;
1405 case 4:
1406 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1407 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001408 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001409
1410 i = 0;
1411#define CMD(op, n) \
1412 do { \
1413 if (dev->ops->op) { \
1414 i++; \
1415 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1416 goto nla_put_failure; \
1417 } \
1418 } while (0)
1419
1420 CMD(add_virtual_intf, NEW_INTERFACE);
1421 CMD(change_virtual_intf, SET_INTERFACE);
1422 CMD(add_key, NEW_KEY);
1423 CMD(start_ap, START_AP);
1424 CMD(add_station, NEW_STATION);
1425 CMD(add_mpath, NEW_MPATH);
1426 CMD(update_mesh_config, SET_MESH_CONFIG);
1427 CMD(change_bss, SET_BSS);
1428 CMD(auth, AUTHENTICATE);
1429 CMD(assoc, ASSOCIATE);
1430 CMD(deauth, DEAUTHENTICATE);
1431 CMD(disassoc, DISASSOCIATE);
1432 CMD(join_ibss, JOIN_IBSS);
1433 CMD(join_mesh, JOIN_MESH);
1434 CMD(set_pmksa, SET_PMKSA);
1435 CMD(del_pmksa, DEL_PMKSA);
1436 CMD(flush_pmksa, FLUSH_PMKSA);
1437 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1438 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1439 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1440 CMD(mgmt_tx, FRAME);
1441 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1442 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1443 i++;
1444 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1445 goto nla_put_failure;
1446 }
1447 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1448 dev->ops->join_mesh) {
1449 i++;
1450 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1451 goto nla_put_failure;
1452 }
1453 CMD(set_wds_peer, SET_WDS_PEER);
1454 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1455 CMD(tdls_mgmt, TDLS_MGMT);
1456 CMD(tdls_oper, TDLS_OPER);
1457 }
1458 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1459 CMD(sched_scan_start, START_SCHED_SCAN);
1460 CMD(probe_client, PROBE_CLIENT);
1461 CMD(set_noack_map, SET_NOACK_MAP);
1462 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1463 i++;
1464 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1465 goto nla_put_failure;
1466 }
1467 CMD(start_p2p_device, START_P2P_DEVICE);
1468 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001469 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001470 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1471 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001472 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1473 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001474 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001475 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001476
Kalle Valo4745fc02011-11-17 19:06:10 +02001477#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001478 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001479#endif
1480
Johannes Berg8fdc6212009-03-14 09:34:01 +01001481#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001482
Johannes Berg3713b4e2013-02-14 16:19:38 +01001483 if (dev->ops->connect || dev->ops->auth) {
1484 i++;
1485 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001486 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001487 }
1488
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 if (dev->ops->disconnect || dev->ops->deauth) {
1490 i++;
1491 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1492 goto nla_put_failure;
1493 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001494
Johannes Berg3713b4e2013-02-14 16:19:38 +01001495 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001496 state->split_start++;
1497 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001498 break;
1499 case 5:
1500 if (dev->ops->remain_on_channel &&
1501 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1502 nla_put_u32(msg,
1503 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1504 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001505 goto nla_put_failure;
1506
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1508 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1509 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001510
Johannes Berg3713b4e2013-02-14 16:19:38 +01001511 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1512 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001513 state->split_start++;
1514 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001515 break;
1516 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001517#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001518 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001519 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001520 state->split_start++;
1521 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001522 break;
1523#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001525#endif
1526 case 7:
1527 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1528 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001529 goto nla_put_failure;
1530
Johannes Berg86e8cf92013-06-19 10:57:22 +02001531 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1532 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001534
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 state->split_start++;
1536 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001537 break;
1538 case 8:
1539 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1540 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1541 dev->wiphy.ap_sme_capa))
1542 goto nla_put_failure;
1543
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001544 features = dev->wiphy.features;
1545 /*
1546 * We can only add the per-channel limit information if the
1547 * dump is split, otherwise it makes it too big. Therefore
1548 * only advertise it in that case.
1549 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001550 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001551 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1552 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001553 goto nla_put_failure;
1554
1555 if (dev->wiphy.ht_capa_mod_mask &&
1556 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1557 sizeof(*dev->wiphy.ht_capa_mod_mask),
1558 dev->wiphy.ht_capa_mod_mask))
1559 goto nla_put_failure;
1560
1561 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1562 dev->wiphy.max_acl_mac_addrs &&
1563 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1564 dev->wiphy.max_acl_mac_addrs))
1565 goto nla_put_failure;
1566
1567 /*
1568 * Any information below this point is only available to
1569 * applications that can deal with it being split. This
1570 * helps ensure that newly added capabilities don't break
1571 * older tools by overrunning their buffers.
1572 *
1573 * We still increment split_start so that in the split
1574 * case we'll continue with more data in the next round,
1575 * but break unconditionally so unsplit data stops here.
1576 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001577 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001578 break;
1579 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001580 if (dev->wiphy.extended_capabilities &&
1581 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1582 dev->wiphy.extended_capabilities_len,
1583 dev->wiphy.extended_capabilities) ||
1584 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1585 dev->wiphy.extended_capabilities_len,
1586 dev->wiphy.extended_capabilities_mask)))
1587 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001588
Johannes Bergee2aca32013-02-21 17:36:01 +01001589 if (dev->wiphy.vht_capa_mod_mask &&
1590 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1591 sizeof(*dev->wiphy.vht_capa_mod_mask),
1592 dev->wiphy.vht_capa_mod_mask))
1593 goto nla_put_failure;
1594
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001595 state->split_start++;
1596 break;
1597 case 10:
1598 if (nl80211_send_coalesce(msg, dev))
1599 goto nla_put_failure;
1600
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001601 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1602 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1603 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1604 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001605
1606 if (dev->wiphy.max_ap_assoc_sta &&
1607 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1608 dev->wiphy.max_ap_assoc_sta))
1609 goto nla_put_failure;
1610
Johannes Bergad7e7182013-11-13 13:37:47 +01001611 state->split_start++;
1612 break;
1613 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001614 if (dev->wiphy.n_vendor_commands) {
1615 const struct nl80211_vendor_cmd_info *info;
1616 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001617
Johannes Berg567ffc32013-12-18 14:43:31 +01001618 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1619 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001620 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001621
1622 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1623 info = &dev->wiphy.vendor_commands[i].info;
1624 if (nla_put(msg, i + 1, sizeof(*info), info))
1625 goto nla_put_failure;
1626 }
1627 nla_nest_end(msg, nested);
1628 }
1629
1630 if (dev->wiphy.n_vendor_events) {
1631 const struct nl80211_vendor_cmd_info *info;
1632 struct nlattr *nested;
1633
1634 nested = nla_nest_start(msg,
1635 NL80211_ATTR_VENDOR_EVENTS);
1636 if (!nested)
1637 goto nla_put_failure;
1638
1639 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1640 info = &dev->wiphy.vendor_events[i];
1641 if (nla_put(msg, i + 1, sizeof(*info), info))
1642 goto nla_put_failure;
1643 }
1644 nla_nest_end(msg, nested);
1645 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001646
Johannes Berg3713b4e2013-02-14 16:19:38 +01001647 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001648 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001649 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001650 }
Johannes Berg55682962007-09-20 13:09:35 -04001651 return genlmsg_end(msg, hdr);
1652
1653 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001654 genlmsg_cancel(msg, hdr);
1655 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001656}
1657
Johannes Berg86e8cf92013-06-19 10:57:22 +02001658static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1659 struct netlink_callback *cb,
1660 struct nl80211_dump_wiphy_state *state)
1661{
1662 struct nlattr **tb = nl80211_fam.attrbuf;
1663 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1664 tb, nl80211_fam.maxattr, nl80211_policy);
1665 /* ignore parse errors for backward compatibility */
1666 if (ret)
1667 return 0;
1668
1669 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1670 if (tb[NL80211_ATTR_WIPHY])
1671 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1672 if (tb[NL80211_ATTR_WDEV])
1673 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1674 if (tb[NL80211_ATTR_IFINDEX]) {
1675 struct net_device *netdev;
1676 struct cfg80211_registered_device *rdev;
1677 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1678
Ying Xue7f2b8562014-01-15 10:23:45 +08001679 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001680 if (!netdev)
1681 return -ENODEV;
1682 if (netdev->ieee80211_ptr) {
1683 rdev = wiphy_to_dev(
1684 netdev->ieee80211_ptr->wiphy);
1685 state->filter_wiphy = rdev->wiphy_idx;
1686 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001687 }
1688
1689 return 0;
1690}
1691
Johannes Berg55682962007-09-20 13:09:35 -04001692static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1693{
Johannes Berg645e77d2013-03-01 14:03:49 +01001694 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001695 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001696 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001697
Johannes Berg5fe231e2013-05-08 21:45:15 +02001698 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699 if (!state) {
1700 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001701 if (!state) {
1702 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001703 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001704 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001705 state->filter_wiphy = -1;
1706 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1707 if (ret) {
1708 kfree(state);
1709 rtnl_unlock();
1710 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001711 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001712 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001713 }
1714
Johannes Berg79c97e92009-07-07 03:56:12 +02001715 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001716 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1717 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001718 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001719 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001720 if (state->filter_wiphy != -1 &&
1721 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001722 continue;
1723 /* attempt to fit multiple wiphy data chunks into the skb */
1724 do {
1725 ret = nl80211_send_wiphy(dev, skb,
1726 NETLINK_CB(cb->skb).portid,
1727 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001728 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001729 if (ret < 0) {
1730 /*
1731 * If sending the wiphy data didn't fit (ENOBUFS
1732 * or EMSGSIZE returned), this SKB is still
1733 * empty (so it's not too big because another
1734 * wiphy dataset is already in the skb) and
1735 * we've not tried to adjust the dump allocation
1736 * yet ... then adjust the alloc size to be
1737 * bigger, and return 1 but with the empty skb.
1738 * This results in an empty message being RX'ed
1739 * in userspace, but that is ignored.
1740 *
1741 * We can then retry with the larger buffer.
1742 */
1743 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001744 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001745 cb->min_dump_alloc < 4096) {
1746 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001747 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001748 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001749 return 1;
1750 }
1751 idx--;
1752 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001753 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001754 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001755 break;
Johannes Berg55682962007-09-20 13:09:35 -04001756 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001757 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001758
Johannes Berg86e8cf92013-06-19 10:57:22 +02001759 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001760
1761 return skb->len;
1762}
1763
Johannes Berg86e8cf92013-06-19 10:57:22 +02001764static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1765{
1766 kfree((void *)cb->args[0]);
1767 return 0;
1768}
1769
Johannes Berg55682962007-09-20 13:09:35 -04001770static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1771{
1772 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001773 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001774 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001775
Johannes Berg645e77d2013-03-01 14:03:49 +01001776 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001777 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001778 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001779
Johannes Berg3713b4e2013-02-14 16:19:38 +01001780 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001781 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001782 nlmsg_free(msg);
1783 return -ENOBUFS;
1784 }
Johannes Berg55682962007-09-20 13:09:35 -04001785
Johannes Berg134e6372009-07-10 09:51:34 +00001786 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001787}
1788
Jouni Malinen31888482008-10-30 16:59:24 +02001789static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1790 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1791 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1792 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1793 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1794 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1795};
1796
1797static int parse_txq_params(struct nlattr *tb[],
1798 struct ieee80211_txq_params *txq_params)
1799{
Johannes Berga3304b02012-03-28 11:04:24 +02001800 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001801 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1802 !tb[NL80211_TXQ_ATTR_AIFS])
1803 return -EINVAL;
1804
Johannes Berga3304b02012-03-28 11:04:24 +02001805 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001806 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1807 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1808 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1809 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1810
Johannes Berga3304b02012-03-28 11:04:24 +02001811 if (txq_params->ac >= NL80211_NUM_ACS)
1812 return -EINVAL;
1813
Jouni Malinen31888482008-10-30 16:59:24 +02001814 return 0;
1815}
1816
Johannes Bergf444de02010-05-05 15:25:02 +02001817static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1818{
1819 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001820 * You can only set the channel explicitly for WDS interfaces,
1821 * all others have their channel managed via their respective
1822 * "establish a connection" command (connect, join, ...)
1823 *
1824 * For AP/GO and mesh mode, the channel can be set with the
1825 * channel userspace API, but is only stored and passed to the
1826 * low-level driver when the AP starts or the mesh is joined.
1827 * This is for backward compatibility, userspace can also give
1828 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001829 *
1830 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001831 * whatever else is going on, so they have their own special
1832 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001833 */
1834 return !wdev ||
1835 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001836 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001837 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1838 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001839}
1840
Johannes Berg683b6d32012-11-08 21:25:48 +01001841static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1842 struct genl_info *info,
1843 struct cfg80211_chan_def *chandef)
1844{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301845 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001846
1847 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1848 return -EINVAL;
1849
1850 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1851
1852 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001853 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1854 chandef->center_freq1 = control_freq;
1855 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001856
1857 /* Primary channel not allowed */
1858 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1859 return -EINVAL;
1860
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001861 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1862 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001863
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001864 chantype = nla_get_u32(
1865 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1866
1867 switch (chantype) {
1868 case NL80211_CHAN_NO_HT:
1869 case NL80211_CHAN_HT20:
1870 case NL80211_CHAN_HT40PLUS:
1871 case NL80211_CHAN_HT40MINUS:
1872 cfg80211_chandef_create(chandef, chandef->chan,
1873 chantype);
1874 break;
1875 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001876 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001877 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001878 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1879 chandef->width =
1880 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1881 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1882 chandef->center_freq1 =
1883 nla_get_u32(
1884 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1885 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1886 chandef->center_freq2 =
1887 nla_get_u32(
1888 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1889 }
1890
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001891 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001892 return -EINVAL;
1893
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001894 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1895 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001896 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001897
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001898 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1899 chandef->width == NL80211_CHAN_WIDTH_10) &&
1900 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1901 return -EINVAL;
1902
Johannes Berg683b6d32012-11-08 21:25:48 +01001903 return 0;
1904}
1905
Johannes Bergf444de02010-05-05 15:25:02 +02001906static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1907 struct wireless_dev *wdev,
1908 struct genl_info *info)
1909{
Johannes Berg683b6d32012-11-08 21:25:48 +01001910 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001911 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001912 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1913
1914 if (wdev)
1915 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001916
Johannes Bergf444de02010-05-05 15:25:02 +02001917 if (!nl80211_can_set_dev_channel(wdev))
1918 return -EOPNOTSUPP;
1919
Johannes Berg683b6d32012-11-08 21:25:48 +01001920 result = nl80211_parse_chandef(rdev, info, &chandef);
1921 if (result)
1922 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001923
Johannes Berge8c9bd52012-06-06 08:18:22 +02001924 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001925 case NL80211_IFTYPE_AP:
1926 case NL80211_IFTYPE_P2P_GO:
1927 if (wdev->beacon_interval) {
1928 result = -EBUSY;
1929 break;
1930 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001931 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001932 result = -EINVAL;
1933 break;
1934 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001935 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 result = 0;
1937 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001938 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001939 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001940 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001941 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001942 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001943 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001944 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001945 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001946 }
Johannes Bergf444de02010-05-05 15:25:02 +02001947
1948 return result;
1949}
1950
1951static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1952{
Johannes Berg4c476992010-10-04 21:36:35 +02001953 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1954 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001955
Johannes Berg4c476992010-10-04 21:36:35 +02001956 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001957}
1958
Bill Jordane8347eb2010-10-01 13:54:28 -04001959static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1960{
Johannes Berg43b19952010-10-07 13:10:30 +02001961 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1962 struct net_device *dev = info->user_ptr[1];
1963 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001964 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001965
1966 if (!info->attrs[NL80211_ATTR_MAC])
1967 return -EINVAL;
1968
Johannes Berg43b19952010-10-07 13:10:30 +02001969 if (netif_running(dev))
1970 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001971
Johannes Berg43b19952010-10-07 13:10:30 +02001972 if (!rdev->ops->set_wds_peer)
1973 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001974
Johannes Berg43b19952010-10-07 13:10:30 +02001975 if (wdev->iftype != NL80211_IFTYPE_WDS)
1976 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001977
1978 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001979 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001980}
1981
1982
Johannes Berg55682962007-09-20 13:09:35 -04001983static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1984{
1985 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001986 struct net_device *netdev = NULL;
1987 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001988 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001989 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001990 u32 changed;
1991 u8 retry_short = 0, retry_long = 0;
1992 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001993 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001994
Johannes Berg5fe231e2013-05-08 21:45:15 +02001995 ASSERT_RTNL();
1996
Johannes Bergf444de02010-05-05 15:25:02 +02001997 /*
1998 * Try to find the wiphy and netdev. Normally this
1999 * function shouldn't need the netdev, but this is
2000 * done for backward compatibility -- previously
2001 * setting the channel was done per wiphy, but now
2002 * it is per netdev. Previous userland like hostapd
2003 * also passed a netdev to set_wiphy, so that it is
2004 * possible to let that go to the right netdev!
2005 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002006
Johannes Bergf444de02010-05-05 15:25:02 +02002007 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2008 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2009
Ying Xue7f2b8562014-01-15 10:23:45 +08002010 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002011 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002012 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002013 else
Johannes Bergf444de02010-05-05 15:25:02 +02002014 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002015 }
2016
Johannes Bergf444de02010-05-05 15:25:02 +02002017 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002018 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2019 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002020 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002021 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002022 wdev = NULL;
2023 netdev = NULL;
2024 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002025 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002026 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002027
2028 /*
2029 * end workaround code, by now the rdev is available
2030 * and locked, and wdev may or may not be NULL.
2031 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002032
2033 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002034 result = cfg80211_dev_rename(
2035 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002036
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002037 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002038 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002039
Jouni Malinen31888482008-10-30 16:59:24 +02002040 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2041 struct ieee80211_txq_params txq_params;
2042 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2043
Ying Xue7f2b8562014-01-15 10:23:45 +08002044 if (!rdev->ops->set_txq_params)
2045 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002046
Ying Xue7f2b8562014-01-15 10:23:45 +08002047 if (!netdev)
2048 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002049
Johannes Berg133a3ff2011-11-03 14:50:13 +01002050 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002051 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2052 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002053
Ying Xue7f2b8562014-01-15 10:23:45 +08002054 if (!netif_running(netdev))
2055 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002056
Jouni Malinen31888482008-10-30 16:59:24 +02002057 nla_for_each_nested(nl_txq_params,
2058 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2059 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002060 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2061 nla_data(nl_txq_params),
2062 nla_len(nl_txq_params),
2063 txq_params_policy);
2064 if (result)
2065 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002066 result = parse_txq_params(tb, &txq_params);
2067 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002068 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002069
Hila Gonene35e4d22012-06-27 17:19:42 +03002070 result = rdev_set_txq_params(rdev, netdev,
2071 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002072 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002073 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002074 }
2075 }
2076
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002077 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002078 result = __nl80211_set_channel(rdev,
2079 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2080 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002081 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002082 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002083 }
2084
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002085 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002086 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002087 enum nl80211_tx_power_setting type;
2088 int idx, mbm = 0;
2089
Johannes Bergc8442112012-10-24 10:17:18 +02002090 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2091 txp_wdev = NULL;
2092
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 if (!rdev->ops->set_tx_power)
2094 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002095
2096 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2097 type = nla_get_u32(info->attrs[idx]);
2098
2099 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002100 (type != NL80211_TX_POWER_AUTOMATIC))
2101 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002102
2103 if (type != NL80211_TX_POWER_AUTOMATIC) {
2104 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2105 mbm = nla_get_u32(info->attrs[idx]);
2106 }
2107
Johannes Bergc8442112012-10-24 10:17:18 +02002108 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002109 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002110 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002111 }
2112
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002113 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2114 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2115 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002116 if ((!rdev->wiphy.available_antennas_tx &&
2117 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002118 !rdev->ops->set_antenna)
2119 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002120
2121 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2122 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2123
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002124 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002125 * available antenna masks, except for the "all" mask */
2126 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002127 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2128 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002129
Bruno Randolf7f531e02010-12-16 11:30:22 +09002130 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2131 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002132
Hila Gonene35e4d22012-06-27 17:19:42 +03002133 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002134 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002135 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002136 }
2137
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002138 changed = 0;
2139
2140 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2141 retry_short = nla_get_u8(
2142 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002143 if (retry_short == 0)
2144 return -EINVAL;
2145
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002146 changed |= WIPHY_PARAM_RETRY_SHORT;
2147 }
2148
2149 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2150 retry_long = nla_get_u8(
2151 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002152 if (retry_long == 0)
2153 return -EINVAL;
2154
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002155 changed |= WIPHY_PARAM_RETRY_LONG;
2156 }
2157
2158 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2159 frag_threshold = nla_get_u32(
2160 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002161 if (frag_threshold < 256)
2162 return -EINVAL;
2163
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002164 if (frag_threshold != (u32) -1) {
2165 /*
2166 * Fragments (apart from the last one) are required to
2167 * have even length. Make the fragmentation code
2168 * simpler by stripping LSB should someone try to use
2169 * odd threshold value.
2170 */
2171 frag_threshold &= ~0x1;
2172 }
2173 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2174 }
2175
2176 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2177 rts_threshold = nla_get_u32(
2178 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2179 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2180 }
2181
Lukáš Turek81077e82009-12-21 22:50:47 +01002182 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2183 coverage_class = nla_get_u8(
2184 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2185 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2186 }
2187
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002188 if (changed) {
2189 u8 old_retry_short, old_retry_long;
2190 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002191 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002192
Ying Xue7f2b8562014-01-15 10:23:45 +08002193 if (!rdev->ops->set_wiphy_params)
2194 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002195
2196 old_retry_short = rdev->wiphy.retry_short;
2197 old_retry_long = rdev->wiphy.retry_long;
2198 old_frag_threshold = rdev->wiphy.frag_threshold;
2199 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002200 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002201
2202 if (changed & WIPHY_PARAM_RETRY_SHORT)
2203 rdev->wiphy.retry_short = retry_short;
2204 if (changed & WIPHY_PARAM_RETRY_LONG)
2205 rdev->wiphy.retry_long = retry_long;
2206 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2207 rdev->wiphy.frag_threshold = frag_threshold;
2208 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2209 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002210 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2211 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002212
Hila Gonene35e4d22012-06-27 17:19:42 +03002213 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002214 if (result) {
2215 rdev->wiphy.retry_short = old_retry_short;
2216 rdev->wiphy.retry_long = old_retry_long;
2217 rdev->wiphy.frag_threshold = old_frag_threshold;
2218 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002219 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002220 }
2221 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002222 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002223}
2224
Johannes Berg71bbc992012-06-15 15:30:18 +02002225static inline u64 wdev_id(struct wireless_dev *wdev)
2226{
2227 return (u64)wdev->identifier |
2228 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2229}
Johannes Berg55682962007-09-20 13:09:35 -04002230
Johannes Berg683b6d32012-11-08 21:25:48 +01002231static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002232 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002233{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002234 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002235
Johannes Berg683b6d32012-11-08 21:25:48 +01002236 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2237 chandef->chan->center_freq))
2238 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002239 switch (chandef->width) {
2240 case NL80211_CHAN_WIDTH_20_NOHT:
2241 case NL80211_CHAN_WIDTH_20:
2242 case NL80211_CHAN_WIDTH_40:
2243 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2244 cfg80211_get_chandef_type(chandef)))
2245 return -ENOBUFS;
2246 break;
2247 default:
2248 break;
2249 }
2250 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2251 return -ENOBUFS;
2252 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2253 return -ENOBUFS;
2254 if (chandef->center_freq2 &&
2255 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002256 return -ENOBUFS;
2257 return 0;
2258}
2259
Eric W. Biederman15e47302012-09-07 20:12:54 +00002260static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002261 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002262 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002263{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002264 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002265 void *hdr;
2266
Eric W. Biederman15e47302012-09-07 20:12:54 +00002267 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002268 if (!hdr)
2269 return -1;
2270
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002271 if (dev &&
2272 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002273 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002274 goto nla_put_failure;
2275
2276 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2277 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002278 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002279 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002280 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2281 rdev->devlist_generation ^
2282 (cfg80211_rdev_list_generation << 2)))
2283 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002284
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002285 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002286 int ret;
2287 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002288
Johannes Berg683b6d32012-11-08 21:25:48 +01002289 ret = rdev_get_channel(rdev, wdev, &chandef);
2290 if (ret == 0) {
2291 if (nl80211_send_chandef(msg, &chandef))
2292 goto nla_put_failure;
2293 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002294 }
2295
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002296 if (wdev->ssid_len) {
2297 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2298 goto nla_put_failure;
2299 }
2300
Johannes Berg55682962007-09-20 13:09:35 -04002301 return genlmsg_end(msg, hdr);
2302
2303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002304 genlmsg_cancel(msg, hdr);
2305 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002306}
2307
2308static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2309{
2310 int wp_idx = 0;
2311 int if_idx = 0;
2312 int wp_start = cb->args[0];
2313 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002314 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002315 struct wireless_dev *wdev;
2316
Johannes Berg5fe231e2013-05-08 21:45:15 +02002317 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002318 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2319 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002320 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002321 if (wp_idx < wp_start) {
2322 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002323 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002324 }
Johannes Berg55682962007-09-20 13:09:35 -04002325 if_idx = 0;
2326
Johannes Berg89a54e42012-06-15 14:33:17 +02002327 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002328 if (if_idx < if_start) {
2329 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002330 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002331 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002332 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002333 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002334 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 goto out;
2336 }
2337 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002338 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339
2340 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002341 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002343 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002344
2345 cb->args[0] = wp_idx;
2346 cb->args[1] = if_idx;
2347
2348 return skb->len;
2349}
2350
2351static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2352{
2353 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002354 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002355 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002356
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002358 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002359 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002360
Eric W. Biederman15e47302012-09-07 20:12:54 +00002361 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002362 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002363 nlmsg_free(msg);
2364 return -ENOBUFS;
2365 }
Johannes Berg55682962007-09-20 13:09:35 -04002366
Johannes Berg134e6372009-07-10 09:51:34 +00002367 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002368}
2369
Michael Wu66f7ac52008-01-31 19:48:22 +01002370static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2371 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2372 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2373 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2374 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2375 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002376 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002377};
2378
2379static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2380{
2381 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2382 int flag;
2383
2384 *mntrflags = 0;
2385
2386 if (!nla)
2387 return -EINVAL;
2388
2389 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2390 nla, mntr_flags_policy))
2391 return -EINVAL;
2392
2393 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2394 if (flags[flag])
2395 *mntrflags |= (1<<flag);
2396
2397 return 0;
2398}
2399
Johannes Berg9bc383d2009-11-19 11:55:19 +01002400static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002401 struct net_device *netdev, u8 use_4addr,
2402 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002403{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002404 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002405 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002406 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002407 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002408 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002409
2410 switch (iftype) {
2411 case NL80211_IFTYPE_AP_VLAN:
2412 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2413 return 0;
2414 break;
2415 case NL80211_IFTYPE_STATION:
2416 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2417 return 0;
2418 break;
2419 default:
2420 break;
2421 }
2422
2423 return -EOPNOTSUPP;
2424}
2425
Johannes Berg55682962007-09-20 13:09:35 -04002426static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2427{
Johannes Berg4c476992010-10-04 21:36:35 +02002428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002429 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002430 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002431 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002433 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002434 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002435
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002436 memset(&params, 0, sizeof(params));
2437
Johannes Berg04a773a2009-04-19 21:24:32 +02002438 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002439
Johannes Berg723b0382008-09-16 20:22:09 +02002440 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002441 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002443 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002444 if (ntype > NL80211_IFTYPE_MAX)
2445 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002446 }
2447
Johannes Berg92ffe052008-09-16 20:39:36 +02002448 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002449 struct wireless_dev *wdev = dev->ieee80211_ptr;
2450
Johannes Berg4c476992010-10-04 21:36:35 +02002451 if (ntype != NL80211_IFTYPE_MESH_POINT)
2452 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002453 if (netif_running(dev))
2454 return -EBUSY;
2455
2456 wdev_lock(wdev);
2457 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2458 IEEE80211_MAX_MESH_ID_LEN);
2459 wdev->mesh_id_up_len =
2460 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2461 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2462 wdev->mesh_id_up_len);
2463 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002464 }
2465
Felix Fietkau8b787642009-11-10 18:53:10 +01002466 if (info->attrs[NL80211_ATTR_4ADDR]) {
2467 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2468 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002469 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002470 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002471 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002472 } else {
2473 params.use_4addr = -1;
2474 }
2475
Johannes Berg92ffe052008-09-16 20:39:36 +02002476 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002477 if (ntype != NL80211_IFTYPE_MONITOR)
2478 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002479 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2480 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002483
2484 flags = &_flags;
2485 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002486 }
Johannes Berg3b858752009-03-12 09:55:09 +01002487
Luciano Coelho18003292013-08-29 13:26:57 +03002488 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002489 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2490 return -EOPNOTSUPP;
2491
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002492 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002493 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002494 else
2495 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002496
Johannes Berg9bc383d2009-11-19 11:55:19 +01002497 if (!err && params.use_4addr != -1)
2498 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2499
Johannes Berg55682962007-09-20 13:09:35 -04002500 return err;
2501}
2502
2503static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2504{
Johannes Berg4c476992010-10-04 21:36:35 +02002505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002506 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002507 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002508 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002509 int err;
2510 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002511 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002512
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002513 memset(&params, 0, sizeof(params));
2514
Johannes Berg55682962007-09-20 13:09:35 -04002515 if (!info->attrs[NL80211_ATTR_IFNAME])
2516 return -EINVAL;
2517
2518 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2519 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2520 if (type > NL80211_IFTYPE_MAX)
2521 return -EINVAL;
2522 }
2523
Johannes Berg79c97e92009-07-07 03:56:12 +02002524 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002525 !(rdev->wiphy.interface_modes & (1 << type)))
2526 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002527
Arend van Spriel1c18f142013-01-08 10:17:27 +01002528 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2529 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2530 ETH_ALEN);
2531 if (!is_valid_ether_addr(params.macaddr))
2532 return -EADDRNOTAVAIL;
2533 }
2534
Johannes Berg9bc383d2009-11-19 11:55:19 +01002535 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002536 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002537 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002538 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002539 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002540 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002541
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002542 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2543 if (!msg)
2544 return -ENOMEM;
2545
Michael Wu66f7ac52008-01-31 19:48:22 +01002546 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2547 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2548 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002549
Luciano Coelho18003292013-08-29 13:26:57 +03002550 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002551 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2552 return -EOPNOTSUPP;
2553
Hila Gonene35e4d22012-06-27 17:19:42 +03002554 wdev = rdev_add_virtual_intf(rdev,
2555 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2556 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002557 if (IS_ERR(wdev)) {
2558 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002559 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002560 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002561
Johannes Berg98104fde2012-06-16 00:19:54 +02002562 switch (type) {
2563 case NL80211_IFTYPE_MESH_POINT:
2564 if (!info->attrs[NL80211_ATTR_MESH_ID])
2565 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002566 wdev_lock(wdev);
2567 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2568 IEEE80211_MAX_MESH_ID_LEN);
2569 wdev->mesh_id_up_len =
2570 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2571 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2572 wdev->mesh_id_up_len);
2573 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002574 break;
2575 case NL80211_IFTYPE_P2P_DEVICE:
2576 /*
2577 * P2P Device doesn't have a netdev, so doesn't go
2578 * through the netdev notifier and must be added here
2579 */
2580 mutex_init(&wdev->mtx);
2581 INIT_LIST_HEAD(&wdev->event_list);
2582 spin_lock_init(&wdev->event_lock);
2583 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2584 spin_lock_init(&wdev->mgmt_registrations_lock);
2585
Johannes Berg98104fde2012-06-16 00:19:54 +02002586 wdev->identifier = ++rdev->wdev_id;
2587 list_add_rcu(&wdev->list, &rdev->wdev_list);
2588 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002589 break;
2590 default:
2591 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002592 }
2593
Eric W. Biederman15e47302012-09-07 20:12:54 +00002594 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002595 rdev, wdev) < 0) {
2596 nlmsg_free(msg);
2597 return -ENOBUFS;
2598 }
2599
2600 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002601}
2602
2603static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2604{
Johannes Berg4c476992010-10-04 21:36:35 +02002605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002606 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002607
Johannes Berg4c476992010-10-04 21:36:35 +02002608 if (!rdev->ops->del_virtual_intf)
2609 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002610
Johannes Berg84efbb82012-06-16 00:00:26 +02002611 /*
2612 * If we remove a wireless device without a netdev then clear
2613 * user_ptr[1] so that nl80211_post_doit won't dereference it
2614 * to check if it needs to do dev_put(). Otherwise it crashes
2615 * since the wdev has been freed, unlike with a netdev where
2616 * we need the dev_put() for the netdev to really be freed.
2617 */
2618 if (!wdev->netdev)
2619 info->user_ptr[1] = NULL;
2620
Hila Gonene35e4d22012-06-27 17:19:42 +03002621 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002622}
2623
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002624static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2625{
2626 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2627 struct net_device *dev = info->user_ptr[1];
2628 u16 noack_map;
2629
2630 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2631 return -EINVAL;
2632
2633 if (!rdev->ops->set_noack_map)
2634 return -EOPNOTSUPP;
2635
2636 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2637
Hila Gonene35e4d22012-06-27 17:19:42 +03002638 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002639}
2640
Johannes Berg41ade002007-12-19 02:03:29 +01002641struct get_key_cookie {
2642 struct sk_buff *msg;
2643 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002644 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002645};
2646
2647static void get_key_callback(void *c, struct key_params *params)
2648{
Johannes Bergb9454e82009-07-08 13:29:08 +02002649 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002650 struct get_key_cookie *cookie = c;
2651
David S. Miller9360ffd2012-03-29 04:41:26 -04002652 if ((params->key &&
2653 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2654 params->key_len, params->key)) ||
2655 (params->seq &&
2656 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2657 params->seq_len, params->seq)) ||
2658 (params->cipher &&
2659 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2660 params->cipher)))
2661 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002662
Johannes Bergb9454e82009-07-08 13:29:08 +02002663 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2664 if (!key)
2665 goto nla_put_failure;
2666
David S. Miller9360ffd2012-03-29 04:41:26 -04002667 if ((params->key &&
2668 nla_put(cookie->msg, NL80211_KEY_DATA,
2669 params->key_len, params->key)) ||
2670 (params->seq &&
2671 nla_put(cookie->msg, NL80211_KEY_SEQ,
2672 params->seq_len, params->seq)) ||
2673 (params->cipher &&
2674 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2675 params->cipher)))
2676 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002677
David S. Miller9360ffd2012-03-29 04:41:26 -04002678 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2679 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002680
2681 nla_nest_end(cookie->msg, key);
2682
Johannes Berg41ade002007-12-19 02:03:29 +01002683 return;
2684 nla_put_failure:
2685 cookie->error = 1;
2686}
2687
2688static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2689{
Johannes Berg4c476992010-10-04 21:36:35 +02002690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002691 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002692 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002693 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002694 const u8 *mac_addr = NULL;
2695 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002696 struct get_key_cookie cookie = {
2697 .error = 0,
2698 };
2699 void *hdr;
2700 struct sk_buff *msg;
2701
2702 if (info->attrs[NL80211_ATTR_KEY_IDX])
2703 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2704
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002705 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002706 return -EINVAL;
2707
2708 if (info->attrs[NL80211_ATTR_MAC])
2709 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2710
Johannes Berge31b8212010-10-05 19:39:30 +02002711 pairwise = !!mac_addr;
2712 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2713 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2714 if (kt >= NUM_NL80211_KEYTYPES)
2715 return -EINVAL;
2716 if (kt != NL80211_KEYTYPE_GROUP &&
2717 kt != NL80211_KEYTYPE_PAIRWISE)
2718 return -EINVAL;
2719 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2720 }
2721
Johannes Berg4c476992010-10-04 21:36:35 +02002722 if (!rdev->ops->get_key)
2723 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002724
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002726 if (!msg)
2727 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002728
Eric W. Biederman15e47302012-09-07 20:12:54 +00002729 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002730 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002731 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002732 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002733
2734 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002735 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002736
David S. Miller9360ffd2012-03-29 04:41:26 -04002737 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2738 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2739 goto nla_put_failure;
2740 if (mac_addr &&
2741 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2742 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002743
Johannes Berge31b8212010-10-05 19:39:30 +02002744 if (pairwise && mac_addr &&
2745 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2746 return -ENOENT;
2747
Hila Gonene35e4d22012-06-27 17:19:42 +03002748 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2749 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002752 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002753
2754 if (cookie.error)
2755 goto nla_put_failure;
2756
2757 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002758 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002759
2760 nla_put_failure:
2761 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002762 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002763 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002764 return err;
2765}
2766
2767static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2768{
Johannes Berg4c476992010-10-04 21:36:35 +02002769 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002770 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002771 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002772 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002773
Johannes Bergb9454e82009-07-08 13:29:08 +02002774 err = nl80211_parse_key(info, &key);
2775 if (err)
2776 return err;
2777
2778 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002779 return -EINVAL;
2780
Johannes Bergb9454e82009-07-08 13:29:08 +02002781 /* only support setting default key */
2782 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002783 return -EINVAL;
2784
Johannes Bergfffd0932009-07-08 14:22:54 +02002785 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002786
2787 if (key.def) {
2788 if (!rdev->ops->set_default_key) {
2789 err = -EOPNOTSUPP;
2790 goto out;
2791 }
2792
2793 err = nl80211_key_allowed(dev->ieee80211_ptr);
2794 if (err)
2795 goto out;
2796
Hila Gonene35e4d22012-06-27 17:19:42 +03002797 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002798 key.def_uni, key.def_multi);
2799
2800 if (err)
2801 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002802
Johannes Berg3d23e342009-09-29 23:27:28 +02002803#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002804 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002805#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002806 } else {
2807 if (key.def_uni || !key.def_multi) {
2808 err = -EINVAL;
2809 goto out;
2810 }
2811
2812 if (!rdev->ops->set_default_mgmt_key) {
2813 err = -EOPNOTSUPP;
2814 goto out;
2815 }
2816
2817 err = nl80211_key_allowed(dev->ieee80211_ptr);
2818 if (err)
2819 goto out;
2820
Hila Gonene35e4d22012-06-27 17:19:42 +03002821 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002822 if (err)
2823 goto out;
2824
2825#ifdef CONFIG_CFG80211_WEXT
2826 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2827#endif
2828 }
2829
2830 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002831 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002832
Johannes Berg41ade002007-12-19 02:03:29 +01002833 return err;
2834}
2835
2836static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2837{
Johannes Berg4c476992010-10-04 21:36:35 +02002838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002839 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002840 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002841 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002842 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002843
Johannes Bergb9454e82009-07-08 13:29:08 +02002844 err = nl80211_parse_key(info, &key);
2845 if (err)
2846 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002847
Johannes Bergb9454e82009-07-08 13:29:08 +02002848 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002849 return -EINVAL;
2850
Johannes Berg41ade002007-12-19 02:03:29 +01002851 if (info->attrs[NL80211_ATTR_MAC])
2852 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2853
Johannes Berge31b8212010-10-05 19:39:30 +02002854 if (key.type == -1) {
2855 if (mac_addr)
2856 key.type = NL80211_KEYTYPE_PAIRWISE;
2857 else
2858 key.type = NL80211_KEYTYPE_GROUP;
2859 }
2860
2861 /* for now */
2862 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2863 key.type != NL80211_KEYTYPE_GROUP)
2864 return -EINVAL;
2865
Johannes Berg4c476992010-10-04 21:36:35 +02002866 if (!rdev->ops->add_key)
2867 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002868
Johannes Berge31b8212010-10-05 19:39:30 +02002869 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2870 key.type == NL80211_KEYTYPE_PAIRWISE,
2871 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002872 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002873
2874 wdev_lock(dev->ieee80211_ptr);
2875 err = nl80211_key_allowed(dev->ieee80211_ptr);
2876 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002877 err = rdev_add_key(rdev, dev, key.idx,
2878 key.type == NL80211_KEYTYPE_PAIRWISE,
2879 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002880 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002881
Johannes Berg41ade002007-12-19 02:03:29 +01002882 return err;
2883}
2884
2885static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2886{
Johannes Berg4c476992010-10-04 21:36:35 +02002887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002888 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002889 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002890 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002891 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002892
Johannes Bergb9454e82009-07-08 13:29:08 +02002893 err = nl80211_parse_key(info, &key);
2894 if (err)
2895 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002896
2897 if (info->attrs[NL80211_ATTR_MAC])
2898 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2899
Johannes Berge31b8212010-10-05 19:39:30 +02002900 if (key.type == -1) {
2901 if (mac_addr)
2902 key.type = NL80211_KEYTYPE_PAIRWISE;
2903 else
2904 key.type = NL80211_KEYTYPE_GROUP;
2905 }
2906
2907 /* for now */
2908 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2909 key.type != NL80211_KEYTYPE_GROUP)
2910 return -EINVAL;
2911
Johannes Berg4c476992010-10-04 21:36:35 +02002912 if (!rdev->ops->del_key)
2913 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002914
Johannes Bergfffd0932009-07-08 14:22:54 +02002915 wdev_lock(dev->ieee80211_ptr);
2916 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002917
2918 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2919 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2920 err = -ENOENT;
2921
Johannes Bergfffd0932009-07-08 14:22:54 +02002922 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002923 err = rdev_del_key(rdev, dev, key.idx,
2924 key.type == NL80211_KEYTYPE_PAIRWISE,
2925 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002926
Johannes Berg3d23e342009-09-29 23:27:28 +02002927#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002928 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002929 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002930 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002931 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002932 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2933 }
2934#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002935 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002936
Johannes Berg41ade002007-12-19 02:03:29 +01002937 return err;
2938}
2939
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302940/* This function returns an error or the number of nested attributes */
2941static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2942{
2943 struct nlattr *attr;
2944 int n_entries = 0, tmp;
2945
2946 nla_for_each_nested(attr, nl_attr, tmp) {
2947 if (nla_len(attr) != ETH_ALEN)
2948 return -EINVAL;
2949
2950 n_entries++;
2951 }
2952
2953 return n_entries;
2954}
2955
2956/*
2957 * This function parses ACL information and allocates memory for ACL data.
2958 * On successful return, the calling function is responsible to free the
2959 * ACL buffer returned by this function.
2960 */
2961static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2962 struct genl_info *info)
2963{
2964 enum nl80211_acl_policy acl_policy;
2965 struct nlattr *attr;
2966 struct cfg80211_acl_data *acl;
2967 int i = 0, n_entries, tmp;
2968
2969 if (!wiphy->max_acl_mac_addrs)
2970 return ERR_PTR(-EOPNOTSUPP);
2971
2972 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2973 return ERR_PTR(-EINVAL);
2974
2975 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2976 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2977 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2978 return ERR_PTR(-EINVAL);
2979
2980 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2981 return ERR_PTR(-EINVAL);
2982
2983 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2984 if (n_entries < 0)
2985 return ERR_PTR(n_entries);
2986
2987 if (n_entries > wiphy->max_acl_mac_addrs)
2988 return ERR_PTR(-ENOTSUPP);
2989
2990 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2991 GFP_KERNEL);
2992 if (!acl)
2993 return ERR_PTR(-ENOMEM);
2994
2995 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2996 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2997 i++;
2998 }
2999
3000 acl->n_acl_entries = n_entries;
3001 acl->acl_policy = acl_policy;
3002
3003 return acl;
3004}
3005
3006static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3007{
3008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3009 struct net_device *dev = info->user_ptr[1];
3010 struct cfg80211_acl_data *acl;
3011 int err;
3012
3013 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3014 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3015 return -EOPNOTSUPP;
3016
3017 if (!dev->ieee80211_ptr->beacon_interval)
3018 return -EINVAL;
3019
3020 acl = parse_acl_data(&rdev->wiphy, info);
3021 if (IS_ERR(acl))
3022 return PTR_ERR(acl);
3023
3024 err = rdev_set_mac_acl(rdev, dev, acl);
3025
3026 kfree(acl);
3027
3028 return err;
3029}
3030
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003031static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003032 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003033{
Johannes Berg88600202012-02-13 15:17:18 +01003034 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003035
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003036 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3037 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3038 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3039 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003040 return -EINVAL;
3041
Johannes Berg88600202012-02-13 15:17:18 +01003042 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003043
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003044 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3045 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3046 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003047 if (!bcn->head_len)
3048 return -EINVAL;
3049 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003050 }
3051
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003052 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3053 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3054 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003055 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003056 }
3057
Johannes Berg4c476992010-10-04 21:36:35 +02003058 if (!haveinfo)
3059 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003061 if (attrs[NL80211_ATTR_IE]) {
3062 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3063 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003064 }
3065
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003066 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003067 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003068 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003069 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003070 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003071 }
3072
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003073 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003074 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003075 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003076 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003077 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003078 }
3079
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003080 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3081 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3082 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003083 }
3084
Johannes Berg88600202012-02-13 15:17:18 +01003085 return 0;
3086}
3087
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003088static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3089 struct cfg80211_ap_settings *params)
3090{
3091 struct wireless_dev *wdev;
3092 bool ret = false;
3093
Johannes Berg89a54e42012-06-15 14:33:17 +02003094 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003095 if (wdev->iftype != NL80211_IFTYPE_AP &&
3096 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3097 continue;
3098
Johannes Berg683b6d32012-11-08 21:25:48 +01003099 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003100 continue;
3101
Johannes Berg683b6d32012-11-08 21:25:48 +01003102 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003103 ret = true;
3104 break;
3105 }
3106
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003107 return ret;
3108}
3109
Jouni Malinene39e5b52012-09-30 19:29:39 +03003110static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3111 enum nl80211_auth_type auth_type,
3112 enum nl80211_commands cmd)
3113{
3114 if (auth_type > NL80211_AUTHTYPE_MAX)
3115 return false;
3116
3117 switch (cmd) {
3118 case NL80211_CMD_AUTHENTICATE:
3119 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3120 auth_type == NL80211_AUTHTYPE_SAE)
3121 return false;
3122 return true;
3123 case NL80211_CMD_CONNECT:
3124 case NL80211_CMD_START_AP:
3125 /* SAE not supported yet */
3126 if (auth_type == NL80211_AUTHTYPE_SAE)
3127 return false;
3128 return true;
3129 default:
3130 return false;
3131 }
3132}
3133
Johannes Berg88600202012-02-13 15:17:18 +01003134static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3135{
3136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3137 struct net_device *dev = info->user_ptr[1];
3138 struct wireless_dev *wdev = dev->ieee80211_ptr;
3139 struct cfg80211_ap_settings params;
3140 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003141 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003142
3143 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3144 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3145 return -EOPNOTSUPP;
3146
3147 if (!rdev->ops->start_ap)
3148 return -EOPNOTSUPP;
3149
3150 if (wdev->beacon_interval)
3151 return -EALREADY;
3152
3153 memset(&params, 0, sizeof(params));
3154
3155 /* these are required for START_AP */
3156 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3157 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3158 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3159 return -EINVAL;
3160
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003161 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003162 if (err)
3163 return err;
3164
3165 params.beacon_interval =
3166 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3167 params.dtim_period =
3168 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3169
3170 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3171 if (err)
3172 return err;
3173
3174 /*
3175 * In theory, some of these attributes should be required here
3176 * but since they were not used when the command was originally
3177 * added, keep them optional for old user space programs to let
3178 * them continue to work with drivers that do not need the
3179 * additional information -- drivers must check!
3180 */
3181 if (info->attrs[NL80211_ATTR_SSID]) {
3182 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3183 params.ssid_len =
3184 nla_len(info->attrs[NL80211_ATTR_SSID]);
3185 if (params.ssid_len == 0 ||
3186 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3187 return -EINVAL;
3188 }
3189
3190 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3191 params.hidden_ssid = nla_get_u32(
3192 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3193 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3194 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3195 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3196 return -EINVAL;
3197 }
3198
3199 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3200
3201 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3202 params.auth_type = nla_get_u32(
3203 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003204 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3205 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003206 return -EINVAL;
3207 } else
3208 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3209
3210 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3211 NL80211_MAX_NR_CIPHER_SUITES);
3212 if (err)
3213 return err;
3214
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303215 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3216 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3217 return -EOPNOTSUPP;
3218 params.inactivity_timeout = nla_get_u16(
3219 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3220 }
3221
Johannes Berg53cabad2012-11-14 15:17:28 +01003222 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3223 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3224 return -EINVAL;
3225 params.p2p_ctwindow =
3226 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3227 if (params.p2p_ctwindow > 127)
3228 return -EINVAL;
3229 if (params.p2p_ctwindow != 0 &&
3230 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3231 return -EINVAL;
3232 }
3233
3234 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3235 u8 tmp;
3236
3237 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3238 return -EINVAL;
3239 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3240 if (tmp > 1)
3241 return -EINVAL;
3242 params.p2p_opp_ps = tmp;
3243 if (params.p2p_opp_ps != 0 &&
3244 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3245 return -EINVAL;
3246 }
3247
Johannes Bergaa430da2012-05-16 23:50:18 +02003248 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003249 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3250 if (err)
3251 return err;
3252 } else if (wdev->preset_chandef.chan) {
3253 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003254 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003255 return -EINVAL;
3256
Johannes Berg683b6d32012-11-08 21:25:48 +01003257 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003258 return -EINVAL;
3259
Simon Wunderlich04f39042013-02-08 18:16:19 +01003260 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3261 if (err < 0)
3262 return err;
3263 if (err) {
3264 radar_detect_width = BIT(params.chandef.width);
3265 params.radar_required = true;
3266 }
3267
Simon Wunderlich04f39042013-02-08 18:16:19 +01003268 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3269 params.chandef.chan,
3270 CHAN_MODE_SHARED,
3271 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003272 if (err)
3273 return err;
3274
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303275 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3276 params.acl = parse_acl_data(&rdev->wiphy, info);
3277 if (IS_ERR(params.acl))
3278 return PTR_ERR(params.acl);
3279 }
3280
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003281 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003282 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003283 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003284 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003285 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003286 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003287 wdev->ssid_len = params.ssid_len;
3288 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003289 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003290 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303291
3292 kfree(params.acl);
3293
Johannes Berg56d18932011-05-09 18:41:15 +02003294 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003295}
3296
Johannes Berg88600202012-02-13 15:17:18 +01003297static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3298{
3299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3300 struct net_device *dev = info->user_ptr[1];
3301 struct wireless_dev *wdev = dev->ieee80211_ptr;
3302 struct cfg80211_beacon_data params;
3303 int err;
3304
3305 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3306 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3307 return -EOPNOTSUPP;
3308
3309 if (!rdev->ops->change_beacon)
3310 return -EOPNOTSUPP;
3311
3312 if (!wdev->beacon_interval)
3313 return -EINVAL;
3314
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003315 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003316 if (err)
3317 return err;
3318
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003319 wdev_lock(wdev);
3320 err = rdev_change_beacon(rdev, dev, &params);
3321 wdev_unlock(wdev);
3322
3323 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003324}
3325
3326static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003327{
Johannes Berg4c476992010-10-04 21:36:35 +02003328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3329 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003330
Michal Kazior60771782012-06-29 12:46:56 +02003331 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003332}
3333
Johannes Berg5727ef12007-12-19 02:03:34 +01003334static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3335 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3336 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3337 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003338 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003339 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003340 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003341};
3342
Johannes Bergeccb8e82009-05-11 21:57:56 +03003343static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003344 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003345 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003346{
3347 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003348 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003349 int flag;
3350
Johannes Bergeccb8e82009-05-11 21:57:56 +03003351 /*
3352 * Try parsing the new attribute first so userspace
3353 * can specify both for older kernels.
3354 */
3355 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3356 if (nla) {
3357 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003358
Johannes Bergeccb8e82009-05-11 21:57:56 +03003359 sta_flags = nla_data(nla);
3360 params->sta_flags_mask = sta_flags->mask;
3361 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003362 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003363 if ((params->sta_flags_mask |
3364 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3365 return -EINVAL;
3366 return 0;
3367 }
3368
3369 /* if present, parse the old attribute */
3370
3371 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003372 if (!nla)
3373 return 0;
3374
3375 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3376 nla, sta_flags_policy))
3377 return -EINVAL;
3378
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003379 /*
3380 * Only allow certain flags for interface types so that
3381 * other attributes are silently ignored. Remember that
3382 * this is backward compatibility code with old userspace
3383 * and shouldn't be hit in other cases anyway.
3384 */
3385 switch (iftype) {
3386 case NL80211_IFTYPE_AP:
3387 case NL80211_IFTYPE_AP_VLAN:
3388 case NL80211_IFTYPE_P2P_GO:
3389 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3390 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3391 BIT(NL80211_STA_FLAG_WME) |
3392 BIT(NL80211_STA_FLAG_MFP);
3393 break;
3394 case NL80211_IFTYPE_P2P_CLIENT:
3395 case NL80211_IFTYPE_STATION:
3396 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3397 BIT(NL80211_STA_FLAG_TDLS_PEER);
3398 break;
3399 case NL80211_IFTYPE_MESH_POINT:
3400 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3401 BIT(NL80211_STA_FLAG_MFP) |
3402 BIT(NL80211_STA_FLAG_AUTHORIZED);
3403 default:
3404 return -EINVAL;
3405 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003406
Johannes Berg3383b5a2012-05-10 20:14:43 +02003407 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3408 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003409 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003410
Johannes Berg3383b5a2012-05-10 20:14:43 +02003411 /* no longer support new API additions in old API */
3412 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3413 return -EINVAL;
3414 }
3415 }
3416
Johannes Berg5727ef12007-12-19 02:03:34 +01003417 return 0;
3418}
3419
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003420static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3421 int attr)
3422{
3423 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003424 u32 bitrate;
3425 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003426
3427 rate = nla_nest_start(msg, attr);
3428 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003429 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003430
3431 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3432 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003433 /* report 16-bit bitrate only if we can */
3434 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003435 if (bitrate > 0 &&
3436 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3437 return false;
3438 if (bitrate_compat > 0 &&
3439 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3440 return false;
3441
3442 if (info->flags & RATE_INFO_FLAGS_MCS) {
3443 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3444 return false;
3445 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3446 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3447 return false;
3448 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3449 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3450 return false;
3451 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3452 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3453 return false;
3454 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3455 return false;
3456 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3457 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3458 return false;
3459 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3460 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3461 return false;
3462 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3463 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3464 return false;
3465 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3466 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3467 return false;
3468 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3469 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3470 return false;
3471 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003472
3473 nla_nest_end(msg, rate);
3474 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003475}
3476
Felix Fietkau119363c2013-04-22 16:29:30 +02003477static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3478 int id)
3479{
3480 void *attr;
3481 int i = 0;
3482
3483 if (!mask)
3484 return true;
3485
3486 attr = nla_nest_start(msg, id);
3487 if (!attr)
3488 return false;
3489
3490 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3491 if (!(mask & BIT(i)))
3492 continue;
3493
3494 if (nla_put_u8(msg, i, signal[i]))
3495 return false;
3496 }
3497
3498 nla_nest_end(msg, attr);
3499
3500 return true;
3501}
3502
Eric W. Biederman15e47302012-09-07 20:12:54 +00003503static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003504 int flags,
3505 struct cfg80211_registered_device *rdev,
3506 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003507 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003508{
3509 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003510 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003511
Eric W. Biederman15e47302012-09-07 20:12:54 +00003512 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003513 if (!hdr)
3514 return -1;
3515
David S. Miller9360ffd2012-03-29 04:41:26 -04003516 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3517 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3518 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3519 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003520
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003521 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3522 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003523 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003524 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3525 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3526 sinfo->connected_time))
3527 goto nla_put_failure;
3528 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3529 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3530 sinfo->inactive_time))
3531 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003532 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3533 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003534 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003535 (u32)sinfo->rx_bytes))
3536 goto nla_put_failure;
3537 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003538 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003539 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3540 (u32)sinfo->tx_bytes))
3541 goto nla_put_failure;
3542 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3543 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003544 sinfo->rx_bytes))
3545 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003546 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3547 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003548 sinfo->tx_bytes))
3549 goto nla_put_failure;
3550 if ((sinfo->filled & STATION_INFO_LLID) &&
3551 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3552 goto nla_put_failure;
3553 if ((sinfo->filled & STATION_INFO_PLID) &&
3554 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3555 goto nla_put_failure;
3556 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3557 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3558 sinfo->plink_state))
3559 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003560 switch (rdev->wiphy.signal_type) {
3561 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003562 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3563 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3564 sinfo->signal))
3565 goto nla_put_failure;
3566 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3567 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3568 sinfo->signal_avg))
3569 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003570 break;
3571 default:
3572 break;
3573 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003574 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3575 if (!nl80211_put_signal(msg, sinfo->chains,
3576 sinfo->chain_signal,
3577 NL80211_STA_INFO_CHAIN_SIGNAL))
3578 goto nla_put_failure;
3579 }
3580 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3581 if (!nl80211_put_signal(msg, sinfo->chains,
3582 sinfo->chain_signal_avg,
3583 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3584 goto nla_put_failure;
3585 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003586 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003587 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3588 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003589 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003590 }
3591 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3592 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3593 NL80211_STA_INFO_RX_BITRATE))
3594 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003595 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003596 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3597 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3598 sinfo->rx_packets))
3599 goto nla_put_failure;
3600 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3601 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3602 sinfo->tx_packets))
3603 goto nla_put_failure;
3604 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3605 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3606 sinfo->tx_retries))
3607 goto nla_put_failure;
3608 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3609 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3610 sinfo->tx_failed))
3611 goto nla_put_failure;
3612 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3613 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3614 sinfo->beacon_loss_count))
3615 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003616 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3617 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3618 sinfo->local_pm))
3619 goto nla_put_failure;
3620 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3621 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3622 sinfo->peer_pm))
3623 goto nla_put_failure;
3624 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3625 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3626 sinfo->nonpeer_pm))
3627 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003628 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3629 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3630 if (!bss_param)
3631 goto nla_put_failure;
3632
David S. Miller9360ffd2012-03-29 04:41:26 -04003633 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3634 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3635 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3636 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3637 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3638 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3639 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3640 sinfo->bss_param.dtim_period) ||
3641 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3642 sinfo->bss_param.beacon_interval))
3643 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003644
3645 nla_nest_end(msg, bss_param);
3646 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003647 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3648 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3649 sizeof(struct nl80211_sta_flag_update),
3650 &sinfo->sta_flags))
3651 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003652 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3653 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3654 sinfo->t_offset))
3655 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003656 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003657
David S. Miller9360ffd2012-03-29 04:41:26 -04003658 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3659 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3660 sinfo->assoc_req_ies))
3661 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003662
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003663 return genlmsg_end(msg, hdr);
3664
3665 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003666 genlmsg_cancel(msg, hdr);
3667 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003668}
3669
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003670static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003671 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003672{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003673 struct station_info sinfo;
3674 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003675 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003676 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003677 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003678 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003679
Johannes Berg97990a02013-04-19 01:02:55 +02003680 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003681 if (err)
3682 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003683
Johannes Berg97990a02013-04-19 01:02:55 +02003684 if (!wdev->netdev) {
3685 err = -EINVAL;
3686 goto out_err;
3687 }
3688
Johannes Bergbba95fe2008-07-29 13:22:51 +02003689 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003690 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003691 goto out_err;
3692 }
3693
Johannes Bergbba95fe2008-07-29 13:22:51 +02003694 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003695 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003696 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003697 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003698 if (err == -ENOENT)
3699 break;
3700 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003701 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003702
3703 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003704 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003705 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003706 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003707 &sinfo) < 0)
3708 goto out;
3709
3710 sta_idx++;
3711 }
3712
3713
3714 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003715 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003716 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003717 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003718 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003719
3720 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003721}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003722
Johannes Berg5727ef12007-12-19 02:03:34 +01003723static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3724{
Johannes Berg4c476992010-10-04 21:36:35 +02003725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3726 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003727 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003728 struct sk_buff *msg;
3729 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003730 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003731
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003732 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003733
3734 if (!info->attrs[NL80211_ATTR_MAC])
3735 return -EINVAL;
3736
3737 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3738
Johannes Berg4c476992010-10-04 21:36:35 +02003739 if (!rdev->ops->get_station)
3740 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003741
Hila Gonene35e4d22012-06-27 17:19:42 +03003742 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003743 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003744 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003745
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003747 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003748 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003749
Eric W. Biederman15e47302012-09-07 20:12:54 +00003750 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003751 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003752 nlmsg_free(msg);
3753 return -ENOBUFS;
3754 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003755
Johannes Berg4c476992010-10-04 21:36:35 +02003756 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003757}
3758
Johannes Berg77ee7c82013-02-15 00:48:33 +01003759int cfg80211_check_station_change(struct wiphy *wiphy,
3760 struct station_parameters *params,
3761 enum cfg80211_station_type statype)
3762{
3763 if (params->listen_interval != -1)
3764 return -EINVAL;
3765 if (params->aid)
3766 return -EINVAL;
3767
3768 /* When you run into this, adjust the code below for the new flag */
3769 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3770
3771 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003772 case CFG80211_STA_MESH_PEER_KERNEL:
3773 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003774 /*
3775 * No ignoring the TDLS flag here -- the userspace mesh
3776 * code doesn't have the bug of including TDLS in the
3777 * mask everywhere.
3778 */
3779 if (params->sta_flags_mask &
3780 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3781 BIT(NL80211_STA_FLAG_MFP) |
3782 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3783 return -EINVAL;
3784 break;
3785 case CFG80211_STA_TDLS_PEER_SETUP:
3786 case CFG80211_STA_TDLS_PEER_ACTIVE:
3787 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3788 return -EINVAL;
3789 /* ignore since it can't change */
3790 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3791 break;
3792 default:
3793 /* disallow mesh-specific things */
3794 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3795 return -EINVAL;
3796 if (params->local_pm)
3797 return -EINVAL;
3798 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3799 return -EINVAL;
3800 }
3801
3802 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3803 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3804 /* TDLS can't be set, ... */
3805 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3806 return -EINVAL;
3807 /*
3808 * ... but don't bother the driver with it. This works around
3809 * a hostapd/wpa_supplicant issue -- it always includes the
3810 * TLDS_PEER flag in the mask even for AP mode.
3811 */
3812 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3813 }
3814
3815 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3816 /* reject other things that can't change */
3817 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3818 return -EINVAL;
3819 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3820 return -EINVAL;
3821 if (params->supported_rates)
3822 return -EINVAL;
3823 if (params->ext_capab || params->ht_capa || params->vht_capa)
3824 return -EINVAL;
3825 }
3826
3827 if (statype != CFG80211_STA_AP_CLIENT) {
3828 if (params->vlan)
3829 return -EINVAL;
3830 }
3831
3832 switch (statype) {
3833 case CFG80211_STA_AP_MLME_CLIENT:
3834 /* Use this only for authorizing/unauthorizing a station */
3835 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3836 return -EOPNOTSUPP;
3837 break;
3838 case CFG80211_STA_AP_CLIENT:
3839 /* accept only the listed bits */
3840 if (params->sta_flags_mask &
3841 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3842 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3843 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3844 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3845 BIT(NL80211_STA_FLAG_WME) |
3846 BIT(NL80211_STA_FLAG_MFP)))
3847 return -EINVAL;
3848
3849 /* but authenticated/associated only if driver handles it */
3850 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3851 params->sta_flags_mask &
3852 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3853 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3854 return -EINVAL;
3855 break;
3856 case CFG80211_STA_IBSS:
3857 case CFG80211_STA_AP_STA:
3858 /* reject any changes other than AUTHORIZED */
3859 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3860 return -EINVAL;
3861 break;
3862 case CFG80211_STA_TDLS_PEER_SETUP:
3863 /* reject any changes other than AUTHORIZED or WME */
3864 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3865 BIT(NL80211_STA_FLAG_WME)))
3866 return -EINVAL;
3867 /* force (at least) rates when authorizing */
3868 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3869 !params->supported_rates)
3870 return -EINVAL;
3871 break;
3872 case CFG80211_STA_TDLS_PEER_ACTIVE:
3873 /* reject any changes */
3874 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003875 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003876 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3877 return -EINVAL;
3878 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003879 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003880 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3881 return -EINVAL;
3882 break;
3883 }
3884
3885 return 0;
3886}
3887EXPORT_SYMBOL(cfg80211_check_station_change);
3888
Johannes Berg5727ef12007-12-19 02:03:34 +01003889/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003890 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003891 */
Johannes Berg80b99892011-11-18 16:23:01 +01003892static struct net_device *get_vlan(struct genl_info *info,
3893 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003894{
Johannes Berg463d0182009-07-14 00:33:35 +02003895 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003896 struct net_device *v;
3897 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003898
Johannes Berg80b99892011-11-18 16:23:01 +01003899 if (!vlanattr)
3900 return NULL;
3901
3902 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3903 if (!v)
3904 return ERR_PTR(-ENODEV);
3905
3906 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3907 ret = -EINVAL;
3908 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003909 }
Johannes Berg80b99892011-11-18 16:23:01 +01003910
Johannes Berg77ee7c82013-02-15 00:48:33 +01003911 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3912 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3913 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3914 ret = -EINVAL;
3915 goto error;
3916 }
3917
Johannes Berg80b99892011-11-18 16:23:01 +01003918 if (!netif_running(v)) {
3919 ret = -ENETDOWN;
3920 goto error;
3921 }
3922
3923 return v;
3924 error:
3925 dev_put(v);
3926 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003927}
3928
Johannes Berg94e860f2014-01-20 23:58:15 +01003929static const struct nla_policy
3930nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003931 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3932 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3933};
3934
Johannes Bergff276692013-02-15 00:09:01 +01003935static int nl80211_parse_sta_wme(struct genl_info *info,
3936 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003937{
Jouni Malinendf881292013-02-14 21:10:54 +02003938 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3939 struct nlattr *nla;
3940 int err;
3941
Jouni Malinendf881292013-02-14 21:10:54 +02003942 /* parse WME attributes if present */
3943 if (!info->attrs[NL80211_ATTR_STA_WME])
3944 return 0;
3945
3946 nla = info->attrs[NL80211_ATTR_STA_WME];
3947 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3948 nl80211_sta_wme_policy);
3949 if (err)
3950 return err;
3951
3952 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3953 params->uapsd_queues = nla_get_u8(
3954 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3955 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3956 return -EINVAL;
3957
3958 if (tb[NL80211_STA_WME_MAX_SP])
3959 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3960
3961 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3962 return -EINVAL;
3963
3964 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3965
3966 return 0;
3967}
3968
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303969static int nl80211_parse_sta_channel_info(struct genl_info *info,
3970 struct station_parameters *params)
3971{
3972 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3973 params->supported_channels =
3974 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3975 params->supported_channels_len =
3976 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3977 /*
3978 * Need to include at least one (first channel, number of
3979 * channels) tuple for each subband, and must have proper
3980 * tuples for the rest of the data as well.
3981 */
3982 if (params->supported_channels_len < 2)
3983 return -EINVAL;
3984 if (params->supported_channels_len % 2)
3985 return -EINVAL;
3986 }
3987
3988 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3989 params->supported_oper_classes =
3990 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3991 params->supported_oper_classes_len =
3992 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3993 /*
3994 * The value of the Length field of the Supported Operating
3995 * Classes element is between 2 and 253.
3996 */
3997 if (params->supported_oper_classes_len < 2 ||
3998 params->supported_oper_classes_len > 253)
3999 return -EINVAL;
4000 }
4001 return 0;
4002}
4003
Johannes Bergff276692013-02-15 00:09:01 +01004004static int nl80211_set_station_tdls(struct genl_info *info,
4005 struct station_parameters *params)
4006{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304007 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004008 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004009 if (info->attrs[NL80211_ATTR_PEER_AID])
4010 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004011 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4012 params->ht_capa =
4013 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4014 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4015 params->vht_capa =
4016 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4017
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304018 err = nl80211_parse_sta_channel_info(info, params);
4019 if (err)
4020 return err;
4021
Johannes Bergff276692013-02-15 00:09:01 +01004022 return nl80211_parse_sta_wme(info, params);
4023}
4024
Johannes Berg5727ef12007-12-19 02:03:34 +01004025static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4026{
Johannes Berg4c476992010-10-04 21:36:35 +02004027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004028 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004029 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004030 u8 *mac_addr;
4031 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004032
4033 memset(&params, 0, sizeof(params));
4034
4035 params.listen_interval = -1;
4036
Johannes Berg77ee7c82013-02-15 00:48:33 +01004037 if (!rdev->ops->change_station)
4038 return -EOPNOTSUPP;
4039
Johannes Berg5727ef12007-12-19 02:03:34 +01004040 if (info->attrs[NL80211_ATTR_STA_AID])
4041 return -EINVAL;
4042
4043 if (!info->attrs[NL80211_ATTR_MAC])
4044 return -EINVAL;
4045
4046 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4047
4048 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4049 params.supported_rates =
4050 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4051 params.supported_rates_len =
4052 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4053 }
4054
Jouni Malinen9d62a982013-02-14 21:10:13 +02004055 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4056 params.capability =
4057 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4058 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4059 }
4060
4061 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4062 params.ext_capab =
4063 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4064 params.ext_capab_len =
4065 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4066 }
4067
Jouni Malinendf881292013-02-14 21:10:54 +02004068 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004069 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004070
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004071 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004072 return -EINVAL;
4073
Johannes Bergf8bacc22013-02-14 23:27:01 +01004074 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004075 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004076 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4077 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4078 return -EINVAL;
4079 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004080
Johannes Bergf8bacc22013-02-14 23:27:01 +01004081 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004082 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004083 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4084 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4085 return -EINVAL;
4086 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4087 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004088
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004089 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4090 enum nl80211_mesh_power_mode pm = nla_get_u32(
4091 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4092
4093 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4094 pm > NL80211_MESH_POWER_MAX)
4095 return -EINVAL;
4096
4097 params.local_pm = pm;
4098 }
4099
Johannes Berg77ee7c82013-02-15 00:48:33 +01004100 /* Include parameters for TDLS peer (will check later) */
4101 err = nl80211_set_station_tdls(info, &params);
4102 if (err)
4103 return err;
4104
4105 params.vlan = get_vlan(info, rdev);
4106 if (IS_ERR(params.vlan))
4107 return PTR_ERR(params.vlan);
4108
Johannes Berga97f4422009-06-18 17:23:43 +02004109 switch (dev->ieee80211_ptr->iftype) {
4110 case NL80211_IFTYPE_AP:
4111 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004112 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004113 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004114 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004115 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004116 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004117 break;
4118 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004119 err = -EOPNOTSUPP;
4120 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004121 }
4122
Johannes Berg77ee7c82013-02-15 00:48:33 +01004123 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004124 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004125
Johannes Berg77ee7c82013-02-15 00:48:33 +01004126 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004127 if (params.vlan)
4128 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004129
Johannes Berg5727ef12007-12-19 02:03:34 +01004130 return err;
4131}
4132
4133static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4134{
Johannes Berg4c476992010-10-04 21:36:35 +02004135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004136 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004137 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004138 struct station_parameters params;
4139 u8 *mac_addr = NULL;
4140
4141 memset(&params, 0, sizeof(params));
4142
Johannes Berg984c3112013-02-14 23:43:25 +01004143 if (!rdev->ops->add_station)
4144 return -EOPNOTSUPP;
4145
Johannes Berg5727ef12007-12-19 02:03:34 +01004146 if (!info->attrs[NL80211_ATTR_MAC])
4147 return -EINVAL;
4148
Johannes Berg5727ef12007-12-19 02:03:34 +01004149 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4150 return -EINVAL;
4151
4152 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4153 return -EINVAL;
4154
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004155 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4156 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004157 return -EINVAL;
4158
Johannes Berg5727ef12007-12-19 02:03:34 +01004159 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4160 params.supported_rates =
4161 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4162 params.supported_rates_len =
4163 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4164 params.listen_interval =
4165 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004166
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004167 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004168 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004169 else
4170 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004171 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4172 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004173
Jouni Malinen9d62a982013-02-14 21:10:13 +02004174 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4175 params.capability =
4176 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4177 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4178 }
4179
4180 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4181 params.ext_capab =
4182 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4183 params.ext_capab_len =
4184 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4185 }
4186
Jouni Malinen36aedc902008-08-25 11:58:58 +03004187 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4188 params.ht_capa =
4189 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004190
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004191 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4192 params.vht_capa =
4193 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4194
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004195 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4196 params.opmode_notif_used = true;
4197 params.opmode_notif =
4198 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4199 }
4200
Johannes Bergf8bacc22013-02-14 23:27:01 +01004201 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004202 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004203 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4204 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4205 return -EINVAL;
4206 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004207
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304208 err = nl80211_parse_sta_channel_info(info, &params);
4209 if (err)
4210 return err;
4211
Johannes Bergff276692013-02-15 00:09:01 +01004212 err = nl80211_parse_sta_wme(info, &params);
4213 if (err)
4214 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004215
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004216 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004217 return -EINVAL;
4218
Johannes Berg77ee7c82013-02-15 00:48:33 +01004219 /* When you run into this, adjust the code below for the new flag */
4220 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4221
Johannes Bergbdd90d52011-12-14 12:20:27 +01004222 switch (dev->ieee80211_ptr->iftype) {
4223 case NL80211_IFTYPE_AP:
4224 case NL80211_IFTYPE_AP_VLAN:
4225 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004226 /* ignore WME attributes if iface/sta is not capable */
4227 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4228 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4229 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004230
Johannes Bergbdd90d52011-12-14 12:20:27 +01004231 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004232 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4233 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004234 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004235 /* but don't bother the driver with it */
4236 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004237
Johannes Bergd582cff2012-10-26 17:53:44 +02004238 /* allow authenticated/associated only if driver handles it */
4239 if (!(rdev->wiphy.features &
4240 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4241 params.sta_flags_mask &
4242 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4243 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4244 return -EINVAL;
4245
Johannes Bergbdd90d52011-12-14 12:20:27 +01004246 /* must be last in here for error handling */
4247 params.vlan = get_vlan(info, rdev);
4248 if (IS_ERR(params.vlan))
4249 return PTR_ERR(params.vlan);
4250 break;
4251 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004252 /* ignore uAPSD data */
4253 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4254
Johannes Bergd582cff2012-10-26 17:53:44 +02004255 /* associated is disallowed */
4256 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4257 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004258 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004259 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4260 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004261 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004262 break;
4263 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004264 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004265 /* ignore uAPSD data */
4266 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4267
Johannes Berg77ee7c82013-02-15 00:48:33 +01004268 /* these are disallowed */
4269 if (params.sta_flags_mask &
4270 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4271 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004272 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004273 /* Only TDLS peers can be added */
4274 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4275 return -EINVAL;
4276 /* Can only add if TDLS ... */
4277 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4278 return -EOPNOTSUPP;
4279 /* ... with external setup is supported */
4280 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4281 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004282 /*
4283 * Older wpa_supplicant versions always mark the TDLS peer
4284 * as authorized, but it shouldn't yet be.
4285 */
4286 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004287 break;
4288 default:
4289 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004290 }
4291
Johannes Bergbdd90d52011-12-14 12:20:27 +01004292 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004293
Hila Gonene35e4d22012-06-27 17:19:42 +03004294 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004295
Johannes Berg5727ef12007-12-19 02:03:34 +01004296 if (params.vlan)
4297 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004298 return err;
4299}
4300
4301static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4302{
Johannes Berg4c476992010-10-04 21:36:35 +02004303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4304 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004305 u8 *mac_addr = NULL;
4306
4307 if (info->attrs[NL80211_ATTR_MAC])
4308 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4309
Johannes Berge80cf852009-05-11 14:43:13 +02004310 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004311 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004312 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4314 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004315
Johannes Berg4c476992010-10-04 21:36:35 +02004316 if (!rdev->ops->del_station)
4317 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004318
Hila Gonene35e4d22012-06-27 17:19:42 +03004319 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004320}
4321
Eric W. Biederman15e47302012-09-07 20:12:54 +00004322static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004323 int flags, struct net_device *dev,
4324 u8 *dst, u8 *next_hop,
4325 struct mpath_info *pinfo)
4326{
4327 void *hdr;
4328 struct nlattr *pinfoattr;
4329
Eric W. Biederman15e47302012-09-07 20:12:54 +00004330 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331 if (!hdr)
4332 return -1;
4333
David S. Miller9360ffd2012-03-29 04:41:26 -04004334 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4335 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4336 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4337 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4338 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004339
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004340 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4341 if (!pinfoattr)
4342 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004343 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4344 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4345 pinfo->frame_qlen))
4346 goto nla_put_failure;
4347 if (((pinfo->filled & MPATH_INFO_SN) &&
4348 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4349 ((pinfo->filled & MPATH_INFO_METRIC) &&
4350 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4351 pinfo->metric)) ||
4352 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4353 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4354 pinfo->exptime)) ||
4355 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4356 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4357 pinfo->flags)) ||
4358 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4359 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4360 pinfo->discovery_timeout)) ||
4361 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4362 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4363 pinfo->discovery_retries)))
4364 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004365
4366 nla_nest_end(msg, pinfoattr);
4367
4368 return genlmsg_end(msg, hdr);
4369
4370 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004371 genlmsg_cancel(msg, hdr);
4372 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004373}
4374
4375static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004376 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004377{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004378 struct mpath_info pinfo;
4379 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004380 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004381 u8 dst[ETH_ALEN];
4382 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004383 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004384 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004385
Johannes Berg97990a02013-04-19 01:02:55 +02004386 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004387 if (err)
4388 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004389
4390 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004391 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004392 goto out_err;
4393 }
4394
Johannes Berg97990a02013-04-19 01:02:55 +02004395 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004396 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004397 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004398 }
4399
Johannes Bergbba95fe2008-07-29 13:22:51 +02004400 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004401 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4402 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004403 if (err == -ENOENT)
4404 break;
4405 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004406 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004407
Eric W. Biederman15e47302012-09-07 20:12:54 +00004408 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004409 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004410 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004411 &pinfo) < 0)
4412 goto out;
4413
4414 path_idx++;
4415 }
4416
4417
4418 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004419 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004420 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004421 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004422 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004423 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004424}
4425
4426static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4427{
Johannes Berg4c476992010-10-04 21:36:35 +02004428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004429 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004430 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004431 struct mpath_info pinfo;
4432 struct sk_buff *msg;
4433 u8 *dst = NULL;
4434 u8 next_hop[ETH_ALEN];
4435
4436 memset(&pinfo, 0, sizeof(pinfo));
4437
4438 if (!info->attrs[NL80211_ATTR_MAC])
4439 return -EINVAL;
4440
4441 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4442
Johannes Berg4c476992010-10-04 21:36:35 +02004443 if (!rdev->ops->get_mpath)
4444 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004445
Johannes Berg4c476992010-10-04 21:36:35 +02004446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4447 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004448
Hila Gonene35e4d22012-06-27 17:19:42 +03004449 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004450 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004451 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004452
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004453 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004454 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004455 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004456
Eric W. Biederman15e47302012-09-07 20:12:54 +00004457 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004458 dev, dst, next_hop, &pinfo) < 0) {
4459 nlmsg_free(msg);
4460 return -ENOBUFS;
4461 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004464}
4465
4466static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4467{
Johannes Berg4c476992010-10-04 21:36:35 +02004468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4469 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470 u8 *dst = NULL;
4471 u8 *next_hop = NULL;
4472
4473 if (!info->attrs[NL80211_ATTR_MAC])
4474 return -EINVAL;
4475
4476 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4477 return -EINVAL;
4478
4479 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4480 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4481
Johannes Berg4c476992010-10-04 21:36:35 +02004482 if (!rdev->ops->change_mpath)
4483 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004484
Johannes Berg4c476992010-10-04 21:36:35 +02004485 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4486 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487
Hila Gonene35e4d22012-06-27 17:19:42 +03004488 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004489}
Johannes Berg4c476992010-10-04 21:36:35 +02004490
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4494 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004495 u8 *dst = NULL;
4496 u8 *next_hop = NULL;
4497
4498 if (!info->attrs[NL80211_ATTR_MAC])
4499 return -EINVAL;
4500
4501 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4502 return -EINVAL;
4503
4504 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4505 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4506
Johannes Berg4c476992010-10-04 21:36:35 +02004507 if (!rdev->ops->add_mpath)
4508 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004509
Johannes Berg4c476992010-10-04 21:36:35 +02004510 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4511 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004512
Hila Gonene35e4d22012-06-27 17:19:42 +03004513 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004514}
4515
4516static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4517{
Johannes Berg4c476992010-10-04 21:36:35 +02004518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4519 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004520 u8 *dst = NULL;
4521
4522 if (info->attrs[NL80211_ATTR_MAC])
4523 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4524
Johannes Berg4c476992010-10-04 21:36:35 +02004525 if (!rdev->ops->del_mpath)
4526 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004527
Hila Gonene35e4d22012-06-27 17:19:42 +03004528 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004529}
4530
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004531static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4532{
Johannes Berg4c476992010-10-04 21:36:35 +02004533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4534 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004535 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004536 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004537 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004538
4539 memset(&params, 0, sizeof(params));
4540 /* default to not changing parameters */
4541 params.use_cts_prot = -1;
4542 params.use_short_preamble = -1;
4543 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004544 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004545 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004546 params.p2p_ctwindow = -1;
4547 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004548
4549 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4550 params.use_cts_prot =
4551 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4552 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4553 params.use_short_preamble =
4554 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4555 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4556 params.use_short_slot_time =
4557 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004558 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4559 params.basic_rates =
4560 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4561 params.basic_rates_len =
4562 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4563 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004564 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4565 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004566 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4567 params.ht_opmode =
4568 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004569
Johannes Berg53cabad2012-11-14 15:17:28 +01004570 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4571 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4572 return -EINVAL;
4573 params.p2p_ctwindow =
4574 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4575 if (params.p2p_ctwindow < 0)
4576 return -EINVAL;
4577 if (params.p2p_ctwindow != 0 &&
4578 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4579 return -EINVAL;
4580 }
4581
4582 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4583 u8 tmp;
4584
4585 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4586 return -EINVAL;
4587 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4588 if (tmp > 1)
4589 return -EINVAL;
4590 params.p2p_opp_ps = tmp;
4591 if (params.p2p_opp_ps &&
4592 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4593 return -EINVAL;
4594 }
4595
Johannes Berg4c476992010-10-04 21:36:35 +02004596 if (!rdev->ops->change_bss)
4597 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004598
Johannes Berg074ac8d2010-09-16 14:58:22 +02004599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004600 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4601 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004602
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004603 wdev_lock(wdev);
4604 err = rdev_change_bss(rdev, dev, &params);
4605 wdev_unlock(wdev);
4606
4607 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004608}
4609
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004610static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004611 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4612 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4613 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4614 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4615 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4616 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4617};
4618
4619static int parse_reg_rule(struct nlattr *tb[],
4620 struct ieee80211_reg_rule *reg_rule)
4621{
4622 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4623 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4624
4625 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4626 return -EINVAL;
4627 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4628 return -EINVAL;
4629 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4630 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004631 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4632 return -EINVAL;
4633
4634 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4635
4636 freq_range->start_freq_khz =
4637 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4638 freq_range->end_freq_khz =
4639 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzic97524822014-01-30 09:52:20 +01004640 if (tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4641 freq_range->max_bandwidth_khz =
4642 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004643
4644 power_rule->max_eirp =
4645 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4646
4647 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4648 power_rule->max_antenna_gain =
4649 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4650
4651 return 0;
4652}
4653
4654static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4655{
4656 int r;
4657 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004658 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004659
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004660 /*
4661 * You should only get this when cfg80211 hasn't yet initialized
4662 * completely when built-in to the kernel right between the time
4663 * window between nl80211_init() and regulatory_init(), if that is
4664 * even possible.
4665 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004666 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004667 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004668
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004669 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4670 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004671
4672 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4673
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004674 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4675 user_reg_hint_type =
4676 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4677 else
4678 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4679
4680 switch (user_reg_hint_type) {
4681 case NL80211_USER_REG_HINT_USER:
4682 case NL80211_USER_REG_HINT_CELL_BASE:
4683 break;
4684 default:
4685 return -EINVAL;
4686 }
4687
4688 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004689
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004690 return r;
4691}
4692
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004693static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004694 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004695{
Johannes Berg4c476992010-10-04 21:36:35 +02004696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004697 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004698 struct wireless_dev *wdev = dev->ieee80211_ptr;
4699 struct mesh_config cur_params;
4700 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004701 void *hdr;
4702 struct nlattr *pinfoattr;
4703 struct sk_buff *msg;
4704
Johannes Berg29cbe682010-12-03 09:20:44 +01004705 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4706 return -EOPNOTSUPP;
4707
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004708 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004709 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004710
Johannes Berg29cbe682010-12-03 09:20:44 +01004711 wdev_lock(wdev);
4712 /* If not connected, get default parameters */
4713 if (!wdev->mesh_id_len)
4714 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4715 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004716 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004717 wdev_unlock(wdev);
4718
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004719 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004720 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004721
4722 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004724 if (!msg)
4725 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004726 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004727 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004728 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004729 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004730 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004731 if (!pinfoattr)
4732 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004733 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4734 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4735 cur_params.dot11MeshRetryTimeout) ||
4736 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4737 cur_params.dot11MeshConfirmTimeout) ||
4738 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4739 cur_params.dot11MeshHoldingTimeout) ||
4740 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4741 cur_params.dot11MeshMaxPeerLinks) ||
4742 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4743 cur_params.dot11MeshMaxRetries) ||
4744 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4745 cur_params.dot11MeshTTL) ||
4746 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4747 cur_params.element_ttl) ||
4748 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4749 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004750 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4751 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004752 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4753 cur_params.dot11MeshHWMPmaxPREQretries) ||
4754 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4755 cur_params.path_refresh_time) ||
4756 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4757 cur_params.min_discovery_timeout) ||
4758 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4759 cur_params.dot11MeshHWMPactivePathTimeout) ||
4760 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4761 cur_params.dot11MeshHWMPpreqMinInterval) ||
4762 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4763 cur_params.dot11MeshHWMPperrMinInterval) ||
4764 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4765 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4766 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4767 cur_params.dot11MeshHWMPRootMode) ||
4768 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4769 cur_params.dot11MeshHWMPRannInterval) ||
4770 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4771 cur_params.dot11MeshGateAnnouncementProtocol) ||
4772 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4773 cur_params.dot11MeshForwarding) ||
4774 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004775 cur_params.rssi_threshold) ||
4776 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004777 cur_params.ht_opmode) ||
4778 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4779 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4780 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004781 cur_params.dot11MeshHWMProotInterval) ||
4782 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004783 cur_params.dot11MeshHWMPconfirmationInterval) ||
4784 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4785 cur_params.power_mode) ||
4786 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004787 cur_params.dot11MeshAwakeWindowDuration) ||
4788 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4789 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004790 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004791 nla_nest_end(msg, pinfoattr);
4792 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004793 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004794
Johannes Berg3b858752009-03-12 09:55:09 +01004795 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004796 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004797 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004798 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004799 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004800}
4801
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004802static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004803 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4804 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4805 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4806 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4807 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4808 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004809 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004810 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004811 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004812 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4813 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4814 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4815 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4816 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004817 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004818 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004819 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004820 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004821 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004822 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004823 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4824 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004825 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4826 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004827 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004828 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4829 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004830 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004831};
4832
Javier Cardonac80d5452010-12-16 17:37:49 -08004833static const struct nla_policy
4834 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004835 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004836 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4837 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004838 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004839 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004840 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004841 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004842 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004843 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004844};
4845
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004846static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004847 struct mesh_config *cfg,
4848 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004849{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004850 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004851 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004852
Marco Porschea54fba2013-01-07 16:04:48 +01004853#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4854do { \
4855 if (tb[attr]) { \
4856 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4857 return -EINVAL; \
4858 cfg->param = fn(tb[attr]); \
4859 mask |= (1 << (attr - 1)); \
4860 } \
4861} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004862
4863
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004864 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004865 return -EINVAL;
4866 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004867 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004868 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004869 return -EINVAL;
4870
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004871 /* This makes sure that there aren't more than 32 mesh config
4872 * parameters (otherwise our bitfield scheme would not work.) */
4873 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4874
4875 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004876 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004877 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4878 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004880 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4881 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004883 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4884 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004886 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4887 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004889 mask, NL80211_MESHCONF_MAX_RETRIES,
4890 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004892 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004893 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004894 mask, NL80211_MESHCONF_ELEMENT_TTL,
4895 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004896 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004897 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4898 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004899 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4900 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004901 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4902 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004903 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004904 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4905 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004907 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4908 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004910 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4911 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004912 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4913 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004914 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4915 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004917 1, 65535, mask,
4918 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004919 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004920 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004921 1, 65535, mask,
4922 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004923 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004924 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004925 dot11MeshHWMPnetDiameterTraversalTime,
4926 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004927 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4928 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4930 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4931 nla_get_u8);
4932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4933 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004934 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004935 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004936 dot11MeshGateAnnouncementProtocol, 0, 1,
4937 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004938 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004939 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004940 mask, NL80211_MESHCONF_FORWARDING,
4941 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004942 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004943 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004944 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004945 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004946 mask, NL80211_MESHCONF_HT_OPMODE,
4947 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004948 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004949 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004950 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4951 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004952 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004953 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4954 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004955 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004956 dot11MeshHWMPconfirmationInterval,
4957 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004958 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4959 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004960 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4961 NL80211_MESH_POWER_ACTIVE,
4962 NL80211_MESH_POWER_MAX,
4963 mask, NL80211_MESHCONF_POWER_MODE,
4964 nla_get_u32);
4965 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4966 0, 65535, mask,
4967 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004968 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4969 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4970 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004971 if (mask_out)
4972 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004973
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004974 return 0;
4975
4976#undef FILL_IN_MESH_PARAM_IF_SET
4977}
4978
Javier Cardonac80d5452010-12-16 17:37:49 -08004979static int nl80211_parse_mesh_setup(struct genl_info *info,
4980 struct mesh_setup *setup)
4981{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004983 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4984
4985 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4986 return -EINVAL;
4987 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4988 info->attrs[NL80211_ATTR_MESH_SETUP],
4989 nl80211_mesh_setup_params_policy))
4990 return -EINVAL;
4991
Javier Cardonad299a1f2012-03-31 11:31:33 -07004992 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4993 setup->sync_method =
4994 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4995 IEEE80211_SYNC_METHOD_VENDOR :
4996 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4997
Javier Cardonac80d5452010-12-16 17:37:49 -08004998 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4999 setup->path_sel_proto =
5000 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5001 IEEE80211_PATH_PROTOCOL_VENDOR :
5002 IEEE80211_PATH_PROTOCOL_HWMP;
5003
5004 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5005 setup->path_metric =
5006 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5007 IEEE80211_PATH_METRIC_VENDOR :
5008 IEEE80211_PATH_METRIC_AIRTIME;
5009
Javier Cardona581a8b02011-04-07 15:08:27 -07005010
5011 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005012 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005013 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005014 if (!is_valid_ie_attr(ieattr))
5015 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005016 setup->ie = nla_data(ieattr);
5017 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005018 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005019 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5020 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5021 return -EINVAL;
5022 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005023 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5024 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005025 if (setup->is_secure)
5026 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005027
Colleen Twitty6e16d902013-05-08 11:45:59 -07005028 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5029 if (!setup->user_mpm)
5030 return -EINVAL;
5031 setup->auth_id =
5032 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5033 }
5034
Javier Cardonac80d5452010-12-16 17:37:49 -08005035 return 0;
5036}
5037
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005038static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005039 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005040{
5041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5042 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005043 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005044 struct mesh_config cfg;
5045 u32 mask;
5046 int err;
5047
Johannes Berg29cbe682010-12-03 09:20:44 +01005048 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5049 return -EOPNOTSUPP;
5050
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005051 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005052 return -EOPNOTSUPP;
5053
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005054 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005055 if (err)
5056 return err;
5057
Johannes Berg29cbe682010-12-03 09:20:44 +01005058 wdev_lock(wdev);
5059 if (!wdev->mesh_id_len)
5060 err = -ENOLINK;
5061
5062 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005063 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005064
5065 wdev_unlock(wdev);
5066
5067 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005068}
5069
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005070static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5071{
Johannes Berg458f4f92012-12-06 15:47:38 +01005072 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005073 struct sk_buff *msg;
5074 void *hdr = NULL;
5075 struct nlattr *nl_reg_rules;
5076 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005077
5078 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005079 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005080
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005081 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005082 if (!msg)
5083 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005084
Eric W. Biederman15e47302012-09-07 20:12:54 +00005085 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005086 NL80211_CMD_GET_REG);
5087 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005088 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005089
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005090 if (reg_last_request_cell_base() &&
5091 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5092 NL80211_USER_REG_HINT_CELL_BASE))
5093 goto nla_put_failure;
5094
Johannes Berg458f4f92012-12-06 15:47:38 +01005095 rcu_read_lock();
5096 regdom = rcu_dereference(cfg80211_regdomain);
5097
5098 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5099 (regdom->dfs_region &&
5100 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5101 goto nla_put_failure_rcu;
5102
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005103 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5104 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005105 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005106
Johannes Berg458f4f92012-12-06 15:47:38 +01005107 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005108 struct nlattr *nl_reg_rule;
5109 const struct ieee80211_reg_rule *reg_rule;
5110 const struct ieee80211_freq_range *freq_range;
5111 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005112 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005113
Johannes Berg458f4f92012-12-06 15:47:38 +01005114 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005115 freq_range = &reg_rule->freq_range;
5116 power_rule = &reg_rule->power_rule;
5117
5118 nl_reg_rule = nla_nest_start(msg, i);
5119 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005120 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005121
Janusz Dziedzic97524822014-01-30 09:52:20 +01005122 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5123 if (!max_bandwidth_khz)
5124 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5125 reg_rule);
5126
David S. Miller9360ffd2012-03-29 04:41:26 -04005127 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5128 reg_rule->flags) ||
5129 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5130 freq_range->start_freq_khz) ||
5131 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5132 freq_range->end_freq_khz) ||
5133 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005134 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005135 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5136 power_rule->max_antenna_gain) ||
5137 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5138 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005139 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005140
5141 nla_nest_end(msg, nl_reg_rule);
5142 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005143 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005144
5145 nla_nest_end(msg, nl_reg_rules);
5146
5147 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005148 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005149
Johannes Berg458f4f92012-12-06 15:47:38 +01005150nla_put_failure_rcu:
5151 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005152nla_put_failure:
5153 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005154put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005155 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005156 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005157}
5158
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005159static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5160{
5161 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5162 struct nlattr *nl_reg_rule;
5163 char *alpha2 = NULL;
5164 int rem_reg_rules = 0, r = 0;
5165 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005166 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005167 struct ieee80211_regdomain *rd = NULL;
5168
5169 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5170 return -EINVAL;
5171
5172 if (!info->attrs[NL80211_ATTR_REG_RULES])
5173 return -EINVAL;
5174
5175 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5176
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005177 if (info->attrs[NL80211_ATTR_DFS_REGION])
5178 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5179
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005180 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005181 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005182 num_rules++;
5183 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005184 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005185 }
5186
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005187 if (!reg_is_valid_request(alpha2))
5188 return -EINVAL;
5189
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005190 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005191 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005192
5193 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005194 if (!rd)
5195 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005196
5197 rd->n_reg_rules = num_rules;
5198 rd->alpha2[0] = alpha2[0];
5199 rd->alpha2[1] = alpha2[1];
5200
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005201 /*
5202 * Disable DFS master mode if the DFS region was
5203 * not supported or known on this kernel.
5204 */
5205 if (reg_supported_dfs_region(dfs_region))
5206 rd->dfs_region = dfs_region;
5207
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005208 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005209 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005210 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5211 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5212 reg_rule_policy);
5213 if (r)
5214 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005215 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5216 if (r)
5217 goto bad_reg;
5218
5219 rule_idx++;
5220
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005221 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5222 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005223 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005224 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005225 }
5226
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005227 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005228 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005229 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005230
Johannes Bergd2372b32008-10-24 20:32:20 +02005231 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005232 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005233 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005234}
5235
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005236static int validate_scan_freqs(struct nlattr *freqs)
5237{
5238 struct nlattr *attr1, *attr2;
5239 int n_channels = 0, tmp1, tmp2;
5240
5241 nla_for_each_nested(attr1, freqs, tmp1) {
5242 n_channels++;
5243 /*
5244 * Some hardware has a limited channel list for
5245 * scanning, and it is pretty much nonsensical
5246 * to scan for a channel twice, so disallow that
5247 * and don't require drivers to check that the
5248 * channel list they get isn't longer than what
5249 * they can scan, as long as they can scan all
5250 * the channels they registered at once.
5251 */
5252 nla_for_each_nested(attr2, freqs, tmp2)
5253 if (attr1 != attr2 &&
5254 nla_get_u32(attr1) == nla_get_u32(attr2))
5255 return 0;
5256 }
5257
5258 return n_channels;
5259}
5260
Johannes Berg2a519312009-02-10 21:25:55 +01005261static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5262{
Johannes Berg4c476992010-10-04 21:36:35 +02005263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005264 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005265 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005266 struct nlattr *attr;
5267 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005268 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005269 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005270
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005271 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5272 return -EINVAL;
5273
Johannes Berg79c97e92009-07-07 03:56:12 +02005274 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005275
Johannes Berg4c476992010-10-04 21:36:35 +02005276 if (!rdev->ops->scan)
5277 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005278
Johannes Bergf9d15d12014-01-22 11:14:19 +02005279 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005280 err = -EBUSY;
5281 goto unlock;
5282 }
Johannes Berg2a519312009-02-10 21:25:55 +01005283
5284 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005285 n_channels = validate_scan_freqs(
5286 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005287 if (!n_channels) {
5288 err = -EINVAL;
5289 goto unlock;
5290 }
Johannes Berg2a519312009-02-10 21:25:55 +01005291 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005292 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005293 }
5294
5295 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5296 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5297 n_ssids++;
5298
Johannes Bergf9f47522013-03-19 15:04:07 +01005299 if (n_ssids > wiphy->max_scan_ssids) {
5300 err = -EINVAL;
5301 goto unlock;
5302 }
Johannes Berg2a519312009-02-10 21:25:55 +01005303
Jouni Malinen70692ad2009-02-16 19:39:13 +02005304 if (info->attrs[NL80211_ATTR_IE])
5305 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5306 else
5307 ie_len = 0;
5308
Johannes Bergf9f47522013-03-19 15:04:07 +01005309 if (ie_len > wiphy->max_scan_ie_len) {
5310 err = -EINVAL;
5311 goto unlock;
5312 }
Johannes Berg18a83652009-03-31 12:12:05 +02005313
Johannes Berg2a519312009-02-10 21:25:55 +01005314 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005315 + sizeof(*request->ssids) * n_ssids
5316 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005317 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005318 if (!request) {
5319 err = -ENOMEM;
5320 goto unlock;
5321 }
Johannes Berg2a519312009-02-10 21:25:55 +01005322
Johannes Berg2a519312009-02-10 21:25:55 +01005323 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005324 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005325 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005326 if (ie_len) {
5327 if (request->ssids)
5328 request->ie = (void *)(request->ssids + n_ssids);
5329 else
5330 request->ie = (void *)(request->channels + n_channels);
5331 }
Johannes Berg2a519312009-02-10 21:25:55 +01005332
Johannes Berg584991d2009-11-02 13:32:03 +01005333 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005334 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5335 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005336 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005337 struct ieee80211_channel *chan;
5338
5339 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5340
5341 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005342 err = -EINVAL;
5343 goto out_free;
5344 }
Johannes Berg584991d2009-11-02 13:32:03 +01005345
5346 /* ignore disabled channels */
5347 if (chan->flags & IEEE80211_CHAN_DISABLED)
5348 continue;
5349
5350 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005351 i++;
5352 }
5353 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005354 enum ieee80211_band band;
5355
Johannes Berg2a519312009-02-10 21:25:55 +01005356 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005357 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5358 int j;
5359 if (!wiphy->bands[band])
5360 continue;
5361 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005362 struct ieee80211_channel *chan;
5363
5364 chan = &wiphy->bands[band]->channels[j];
5365
5366 if (chan->flags & IEEE80211_CHAN_DISABLED)
5367 continue;
5368
5369 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005370 i++;
5371 }
5372 }
5373 }
5374
Johannes Berg584991d2009-11-02 13:32:03 +01005375 if (!i) {
5376 err = -EINVAL;
5377 goto out_free;
5378 }
5379
5380 request->n_channels = i;
5381
Johannes Berg2a519312009-02-10 21:25:55 +01005382 i = 0;
5383 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5384 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005385 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005386 err = -EINVAL;
5387 goto out_free;
5388 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005389 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005390 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005391 i++;
5392 }
5393 }
5394
Jouni Malinen70692ad2009-02-16 19:39:13 +02005395 if (info->attrs[NL80211_ATTR_IE]) {
5396 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005397 memcpy((void *)request->ie,
5398 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005399 request->ie_len);
5400 }
5401
Johannes Berg34850ab2011-07-18 18:08:35 +02005402 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005403 if (wiphy->bands[i])
5404 request->rates[i] =
5405 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005406
5407 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5408 nla_for_each_nested(attr,
5409 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5410 tmp) {
5411 enum ieee80211_band band = nla_type(attr);
5412
Dan Carpenter84404622011-07-29 11:52:18 +03005413 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005414 err = -EINVAL;
5415 goto out_free;
5416 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005417
5418 if (!wiphy->bands[band])
5419 continue;
5420
Johannes Berg34850ab2011-07-18 18:08:35 +02005421 err = ieee80211_get_ratemask(wiphy->bands[band],
5422 nla_data(attr),
5423 nla_len(attr),
5424 &request->rates[band]);
5425 if (err)
5426 goto out_free;
5427 }
5428 }
5429
Sam Leffler46856bb2012-10-11 21:03:32 -07005430 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005431 request->flags = nla_get_u32(
5432 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005433 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5434 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005435 err = -EOPNOTSUPP;
5436 goto out_free;
5437 }
5438 }
Sam Lefflered4737712012-10-11 21:03:31 -07005439
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305440 request->no_cck =
5441 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5442
Johannes Bergfd014282012-06-18 19:17:03 +02005443 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005444 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005445 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005446
Johannes Berg79c97e92009-07-07 03:56:12 +02005447 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005448 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005449
Johannes Berg463d0182009-07-14 00:33:35 +02005450 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005451 nl80211_send_scan_start(rdev, wdev);
5452 if (wdev->netdev)
5453 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005454 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005455 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005456 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005457 kfree(request);
5458 }
Johannes Berg3b858752009-03-12 09:55:09 +01005459
Johannes Bergf9f47522013-03-19 15:04:07 +01005460 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005461 return err;
5462}
5463
Luciano Coelho807f8a82011-05-11 17:09:35 +03005464static int nl80211_start_sched_scan(struct sk_buff *skb,
5465 struct genl_info *info)
5466{
5467 struct cfg80211_sched_scan_request *request;
5468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5469 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005470 struct nlattr *attr;
5471 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005472 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005473 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005474 enum ieee80211_band band;
5475 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005476 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005477 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005478
5479 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5480 !rdev->ops->sched_scan_start)
5481 return -EOPNOTSUPP;
5482
5483 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5484 return -EINVAL;
5485
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005486 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5487 return -EINVAL;
5488
5489 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5490 if (interval == 0)
5491 return -EINVAL;
5492
Luciano Coelho807f8a82011-05-11 17:09:35 +03005493 wiphy = &rdev->wiphy;
5494
5495 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5496 n_channels = validate_scan_freqs(
5497 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5498 if (!n_channels)
5499 return -EINVAL;
5500 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005501 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005502 }
5503
5504 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5505 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5506 tmp)
5507 n_ssids++;
5508
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005509 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005510 return -EINVAL;
5511
Johannes Bergea73cbc2014-01-24 10:53:53 +01005512 /*
5513 * First, count the number of 'real' matchsets. Due to an issue with
5514 * the old implementation, matchsets containing only the RSSI attribute
5515 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5516 * RSSI for all matchsets, rather than their own matchset for reporting
5517 * all APs with a strong RSSI. This is needed to be compatible with
5518 * older userspace that treated a matchset with only the RSSI as the
5519 * global RSSI for all other matchsets - if there are other matchsets.
5520 */
5521 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005522 nla_for_each_nested(attr,
5523 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005524 tmp) {
5525 struct nlattr *rssi;
5526
5527 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5528 nla_data(attr), nla_len(attr),
5529 nl80211_match_policy);
5530 if (err)
5531 return err;
5532 /* add other standalone attributes here */
5533 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5534 n_match_sets++;
5535 continue;
5536 }
5537 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5538 if (rssi)
5539 default_match_rssi = nla_get_s32(rssi);
5540 }
5541 }
5542
5543 /* However, if there's no other matchset, add the RSSI one */
5544 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5545 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005546
5547 if (n_match_sets > wiphy->max_match_sets)
5548 return -EINVAL;
5549
Luciano Coelho807f8a82011-05-11 17:09:35 +03005550 if (info->attrs[NL80211_ATTR_IE])
5551 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5552 else
5553 ie_len = 0;
5554
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005555 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005556 return -EINVAL;
5557
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005558 if (rdev->sched_scan_req) {
5559 err = -EINPROGRESS;
5560 goto out;
5561 }
5562
Luciano Coelho807f8a82011-05-11 17:09:35 +03005563 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005564 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005565 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005566 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005567 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005568 if (!request) {
5569 err = -ENOMEM;
5570 goto out;
5571 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005572
5573 if (n_ssids)
5574 request->ssids = (void *)&request->channels[n_channels];
5575 request->n_ssids = n_ssids;
5576 if (ie_len) {
5577 if (request->ssids)
5578 request->ie = (void *)(request->ssids + n_ssids);
5579 else
5580 request->ie = (void *)(request->channels + n_channels);
5581 }
5582
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005583 if (n_match_sets) {
5584 if (request->ie)
5585 request->match_sets = (void *)(request->ie + ie_len);
5586 else if (request->ssids)
5587 request->match_sets =
5588 (void *)(request->ssids + n_ssids);
5589 else
5590 request->match_sets =
5591 (void *)(request->channels + n_channels);
5592 }
5593 request->n_match_sets = n_match_sets;
5594
Luciano Coelho807f8a82011-05-11 17:09:35 +03005595 i = 0;
5596 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5597 /* user specified, bail out if channel not found */
5598 nla_for_each_nested(attr,
5599 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5600 tmp) {
5601 struct ieee80211_channel *chan;
5602
5603 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5604
5605 if (!chan) {
5606 err = -EINVAL;
5607 goto out_free;
5608 }
5609
5610 /* ignore disabled channels */
5611 if (chan->flags & IEEE80211_CHAN_DISABLED)
5612 continue;
5613
5614 request->channels[i] = chan;
5615 i++;
5616 }
5617 } else {
5618 /* all channels */
5619 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5620 int j;
5621 if (!wiphy->bands[band])
5622 continue;
5623 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5624 struct ieee80211_channel *chan;
5625
5626 chan = &wiphy->bands[band]->channels[j];
5627
5628 if (chan->flags & IEEE80211_CHAN_DISABLED)
5629 continue;
5630
5631 request->channels[i] = chan;
5632 i++;
5633 }
5634 }
5635 }
5636
5637 if (!i) {
5638 err = -EINVAL;
5639 goto out_free;
5640 }
5641
5642 request->n_channels = i;
5643
5644 i = 0;
5645 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5646 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5647 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005648 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005649 err = -EINVAL;
5650 goto out_free;
5651 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005652 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005653 memcpy(request->ssids[i].ssid, nla_data(attr),
5654 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005655 i++;
5656 }
5657 }
5658
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005659 i = 0;
5660 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5661 nla_for_each_nested(attr,
5662 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5663 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005664 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005665
Johannes Bergae811e22014-01-24 10:17:47 +01005666 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5667 nla_data(attr), nla_len(attr),
5668 nl80211_match_policy);
5669 if (err)
5670 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005671 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005672 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005673 if (WARN_ON(i >= n_match_sets)) {
5674 /* this indicates a programming error,
5675 * the loop above should have verified
5676 * things properly
5677 */
5678 err = -EINVAL;
5679 goto out_free;
5680 }
5681
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005682 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5683 err = -EINVAL;
5684 goto out_free;
5685 }
5686 memcpy(request->match_sets[i].ssid.ssid,
5687 nla_data(ssid), nla_len(ssid));
5688 request->match_sets[i].ssid.ssid_len =
5689 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005690 /* special attribute - old implemenation w/a */
5691 request->match_sets[i].rssi_thold =
5692 default_match_rssi;
5693 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5694 if (rssi)
5695 request->match_sets[i].rssi_thold =
5696 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005697 }
5698 i++;
5699 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005700
5701 /* there was no other matchset, so the RSSI one is alone */
5702 if (i == 0)
5703 request->match_sets[0].rssi_thold = default_match_rssi;
5704
5705 request->min_rssi_thold = INT_MAX;
5706 for (i = 0; i < n_match_sets; i++)
5707 request->min_rssi_thold =
5708 min(request->match_sets[i].rssi_thold,
5709 request->min_rssi_thold);
5710 } else {
5711 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005712 }
5713
Johannes Berg9900e482014-02-04 21:01:25 +01005714 if (ie_len) {
5715 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005716 memcpy((void *)request->ie,
5717 nla_data(info->attrs[NL80211_ATTR_IE]),
5718 request->ie_len);
5719 }
5720
Sam Leffler46856bb2012-10-11 21:03:32 -07005721 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005722 request->flags = nla_get_u32(
5723 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005724 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5725 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005726 err = -EOPNOTSUPP;
5727 goto out_free;
5728 }
5729 }
Sam Lefflered4737712012-10-11 21:03:31 -07005730
Luciano Coelho807f8a82011-05-11 17:09:35 +03005731 request->dev = dev;
5732 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005733 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005734 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005735
Hila Gonene35e4d22012-06-27 17:19:42 +03005736 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005737 if (!err) {
5738 rdev->sched_scan_req = request;
5739 nl80211_send_sched_scan(rdev, dev,
5740 NL80211_CMD_START_SCHED_SCAN);
5741 goto out;
5742 }
5743
5744out_free:
5745 kfree(request);
5746out:
5747 return err;
5748}
5749
5750static int nl80211_stop_sched_scan(struct sk_buff *skb,
5751 struct genl_info *info)
5752{
5753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5754
5755 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5756 !rdev->ops->sched_scan_stop)
5757 return -EOPNOTSUPP;
5758
Johannes Berg5fe231e2013-05-08 21:45:15 +02005759 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005760}
5761
Simon Wunderlich04f39042013-02-08 18:16:19 +01005762static int nl80211_start_radar_detection(struct sk_buff *skb,
5763 struct genl_info *info)
5764{
5765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5766 struct net_device *dev = info->user_ptr[1];
5767 struct wireless_dev *wdev = dev->ieee80211_ptr;
5768 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005769 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005770 int err;
5771
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005772 dfs_region = reg_get_dfs_region(wdev->wiphy);
5773 if (dfs_region == NL80211_DFS_UNSET)
5774 return -EINVAL;
5775
Simon Wunderlich04f39042013-02-08 18:16:19 +01005776 err = nl80211_parse_chandef(rdev, info, &chandef);
5777 if (err)
5778 return err;
5779
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005780 if (netif_carrier_ok(dev))
5781 return -EBUSY;
5782
Simon Wunderlich04f39042013-02-08 18:16:19 +01005783 if (wdev->cac_started)
5784 return -EBUSY;
5785
5786 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5787 if (err < 0)
5788 return err;
5789
5790 if (err == 0)
5791 return -EINVAL;
5792
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005793 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005794 return -EINVAL;
5795
5796 if (!rdev->ops->start_radar_detection)
5797 return -EOPNOTSUPP;
5798
Simon Wunderlich04f39042013-02-08 18:16:19 +01005799 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5800 chandef.chan, CHAN_MODE_SHARED,
5801 BIT(chandef.width));
5802 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005803 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005804
5805 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5806 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005807 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005808 wdev->cac_started = true;
5809 wdev->cac_start_time = jiffies;
5810 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005811 return err;
5812}
5813
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005814static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5815{
5816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5817 struct net_device *dev = info->user_ptr[1];
5818 struct wireless_dev *wdev = dev->ieee80211_ptr;
5819 struct cfg80211_csa_settings params;
5820 /* csa_attrs is defined static to avoid waste of stack size - this
5821 * function is called under RTNL lock, so this should not be a problem.
5822 */
5823 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5824 u8 radar_detect_width = 0;
5825 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005826 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005827
5828 if (!rdev->ops->channel_switch ||
5829 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5830 return -EOPNOTSUPP;
5831
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005832 switch (dev->ieee80211_ptr->iftype) {
5833 case NL80211_IFTYPE_AP:
5834 case NL80211_IFTYPE_P2P_GO:
5835 need_new_beacon = true;
5836
5837 /* useless if AP is not running */
5838 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005839 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005840 break;
5841 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005842 if (!wdev->ssid_len)
5843 return -ENOTCONN;
5844 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005845 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005846 if (!wdev->mesh_id_len)
5847 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005848 break;
5849 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005850 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005851 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005852
5853 memset(&params, 0, sizeof(params));
5854
5855 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5856 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5857 return -EINVAL;
5858
5859 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005860 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005861 return -EINVAL;
5862
5863 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5864
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005865 if (!need_new_beacon)
5866 goto skip_beacons;
5867
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005868 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5869 if (err)
5870 return err;
5871
5872 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5873 info->attrs[NL80211_ATTR_CSA_IES],
5874 nl80211_policy);
5875 if (err)
5876 return err;
5877
5878 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5879 if (err)
5880 return err;
5881
5882 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5883 return -EINVAL;
5884
5885 params.counter_offset_beacon =
5886 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5887 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5888 return -EINVAL;
5889
5890 /* sanity check - counters should be the same */
5891 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5892 params.count)
5893 return -EINVAL;
5894
5895 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5896 params.counter_offset_presp =
5897 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5898 if (params.counter_offset_presp >=
5899 params.beacon_csa.probe_resp_len)
5900 return -EINVAL;
5901
5902 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5903 params.count)
5904 return -EINVAL;
5905 }
5906
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005907skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005908 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5909 if (err)
5910 return err;
5911
5912 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5913 return -EINVAL;
5914
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005915 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
Simon Wunderlich5336fa82013-10-07 18:41:05 +02005916 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5917 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005918 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5919 &params.chandef);
5920 if (err < 0) {
5921 return err;
5922 } else if (err) {
5923 radar_detect_width = BIT(params.chandef.width);
5924 params.radar_required = true;
5925 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005926 }
5927
5928 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5929 params.chandef.chan,
5930 CHAN_MODE_SHARED,
5931 radar_detect_width);
5932 if (err)
5933 return err;
5934
5935 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5936 params.block_tx = true;
5937
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005938 wdev_lock(wdev);
5939 err = rdev_channel_switch(rdev, dev, &params);
5940 wdev_unlock(wdev);
5941
5942 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005943}
5944
Johannes Berg9720bb32011-06-21 09:45:33 +02005945static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5946 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005947 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005948 struct wireless_dev *wdev,
5949 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005950{
Johannes Berg48ab9052009-07-10 18:42:31 +02005951 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005952 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005953 void *hdr;
5954 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005955 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005956
5957 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005958
Eric W. Biederman15e47302012-09-07 20:12:54 +00005959 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005960 NL80211_CMD_NEW_SCAN_RESULTS);
5961 if (!hdr)
5962 return -1;
5963
Johannes Berg9720bb32011-06-21 09:45:33 +02005964 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5965
Johannes Berg97990a02013-04-19 01:02:55 +02005966 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5967 goto nla_put_failure;
5968 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005969 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5970 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005971 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5972 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005973
5974 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5975 if (!bss)
5976 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005977 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005978 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005979 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005980
5981 rcu_read_lock();
5982 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005983 if (ies) {
5984 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5985 goto fail_unlock_rcu;
5986 tsf = true;
5987 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5988 ies->len, ies->data))
5989 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005990 }
5991 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005992 if (ies) {
5993 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5994 goto fail_unlock_rcu;
5995 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5996 ies->len, ies->data))
5997 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005998 }
5999 rcu_read_unlock();
6000
David S. Miller9360ffd2012-03-29 04:41:26 -04006001 if (res->beacon_interval &&
6002 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6003 goto nla_put_failure;
6004 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6005 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006006 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006007 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6008 jiffies_to_msecs(jiffies - intbss->ts)))
6009 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006010
Johannes Berg77965c92009-02-18 18:45:06 +01006011 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006012 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006013 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6014 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006015 break;
6016 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006017 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6018 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006019 break;
6020 default:
6021 break;
6022 }
6023
Johannes Berg48ab9052009-07-10 18:42:31 +02006024 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006025 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006026 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006027 if (intbss == wdev->current_bss &&
6028 nla_put_u32(msg, NL80211_BSS_STATUS,
6029 NL80211_BSS_STATUS_ASSOCIATED))
6030 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006031 break;
6032 case NL80211_IFTYPE_ADHOC:
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_IBSS_JOINED))
6036 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006037 break;
6038 default:
6039 break;
6040 }
6041
Johannes Berg2a519312009-02-10 21:25:55 +01006042 nla_nest_end(msg, bss);
6043
6044 return genlmsg_end(msg, hdr);
6045
Johannes Berg8cef2c92013-02-05 16:54:31 +01006046 fail_unlock_rcu:
6047 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006048 nla_put_failure:
6049 genlmsg_cancel(msg, hdr);
6050 return -EMSGSIZE;
6051}
6052
Johannes Berg97990a02013-04-19 01:02:55 +02006053static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006054{
Johannes Berg48ab9052009-07-10 18:42:31 +02006055 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006056 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006057 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006058 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006059 int err;
6060
Johannes Berg97990a02013-04-19 01:02:55 +02006061 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006062 if (err)
6063 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006064
Johannes Berg48ab9052009-07-10 18:42:31 +02006065 wdev_lock(wdev);
6066 spin_lock_bh(&rdev->bss_lock);
6067 cfg80211_bss_expire(rdev);
6068
Johannes Berg9720bb32011-06-21 09:45:33 +02006069 cb->seq = rdev->bss_generation;
6070
Johannes Berg48ab9052009-07-10 18:42:31 +02006071 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006072 if (++idx <= start)
6073 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006074 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006075 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006076 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006077 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006078 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006079 }
6080 }
6081
Johannes Berg48ab9052009-07-10 18:42:31 +02006082 spin_unlock_bh(&rdev->bss_lock);
6083 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006084
Johannes Berg97990a02013-04-19 01:02:55 +02006085 cb->args[2] = idx;
6086 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006087
Johannes Berg67748892010-10-04 21:14:06 +02006088 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006089}
6090
Eric W. Biederman15e47302012-09-07 20:12:54 +00006091static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006092 int flags, struct net_device *dev,
6093 struct survey_info *survey)
6094{
6095 void *hdr;
6096 struct nlattr *infoattr;
6097
Eric W. Biederman15e47302012-09-07 20:12:54 +00006098 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006099 NL80211_CMD_NEW_SURVEY_RESULTS);
6100 if (!hdr)
6101 return -ENOMEM;
6102
David S. Miller9360ffd2012-03-29 04:41:26 -04006103 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6104 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006105
6106 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6107 if (!infoattr)
6108 goto nla_put_failure;
6109
David S. Miller9360ffd2012-03-29 04:41:26 -04006110 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6111 survey->channel->center_freq))
6112 goto nla_put_failure;
6113
6114 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6115 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6116 goto nla_put_failure;
6117 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6118 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6119 goto nla_put_failure;
6120 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6121 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6122 survey->channel_time))
6123 goto nla_put_failure;
6124 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6125 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6126 survey->channel_time_busy))
6127 goto nla_put_failure;
6128 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6129 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6130 survey->channel_time_ext_busy))
6131 goto nla_put_failure;
6132 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6133 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6134 survey->channel_time_rx))
6135 goto nla_put_failure;
6136 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6137 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6138 survey->channel_time_tx))
6139 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006140
6141 nla_nest_end(msg, infoattr);
6142
6143 return genlmsg_end(msg, hdr);
6144
6145 nla_put_failure:
6146 genlmsg_cancel(msg, hdr);
6147 return -EMSGSIZE;
6148}
6149
6150static int nl80211_dump_survey(struct sk_buff *skb,
6151 struct netlink_callback *cb)
6152{
6153 struct survey_info survey;
6154 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006155 struct wireless_dev *wdev;
6156 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006157 int res;
6158
Johannes Berg97990a02013-04-19 01:02:55 +02006159 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006160 if (res)
6161 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006162
Johannes Berg97990a02013-04-19 01:02:55 +02006163 if (!wdev->netdev) {
6164 res = -EINVAL;
6165 goto out_err;
6166 }
6167
Holger Schurig61fa7132009-11-11 12:25:40 +01006168 if (!dev->ops->dump_survey) {
6169 res = -EOPNOTSUPP;
6170 goto out_err;
6171 }
6172
6173 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006174 struct ieee80211_channel *chan;
6175
Johannes Berg97990a02013-04-19 01:02:55 +02006176 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006177 if (res == -ENOENT)
6178 break;
6179 if (res)
6180 goto out_err;
6181
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006182 /* Survey without a channel doesn't make sense */
6183 if (!survey.channel) {
6184 res = -EINVAL;
6185 goto out;
6186 }
6187
6188 chan = ieee80211_get_channel(&dev->wiphy,
6189 survey.channel->center_freq);
6190 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6191 survey_idx++;
6192 continue;
6193 }
6194
Holger Schurig61fa7132009-11-11 12:25:40 +01006195 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006196 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006197 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006198 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006199 goto out;
6200 survey_idx++;
6201 }
6202
6203 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006204 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006205 res = skb->len;
6206 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006207 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006208 return res;
6209}
6210
Samuel Ortizb23aa672009-07-01 21:26:54 +02006211static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6212{
6213 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6214 NL80211_WPA_VERSION_2));
6215}
6216
Jouni Malinen636a5d32009-03-19 13:39:22 +02006217static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6218{
Johannes Berg4c476992010-10-04 21:36:35 +02006219 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6220 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006221 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006222 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6223 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006224 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006225 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006226 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006227
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006228 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6229 return -EINVAL;
6230
6231 if (!info->attrs[NL80211_ATTR_MAC])
6232 return -EINVAL;
6233
Jouni Malinen17780922009-03-27 20:52:47 +02006234 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6235 return -EINVAL;
6236
Johannes Berg19957bb2009-07-02 17:20:43 +02006237 if (!info->attrs[NL80211_ATTR_SSID])
6238 return -EINVAL;
6239
6240 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6241 return -EINVAL;
6242
Johannes Bergfffd0932009-07-08 14:22:54 +02006243 err = nl80211_parse_key(info, &key);
6244 if (err)
6245 return err;
6246
6247 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006248 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6249 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006250 if (!key.p.key || !key.p.key_len)
6251 return -EINVAL;
6252 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6253 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6254 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6255 key.p.key_len != WLAN_KEY_LEN_WEP104))
6256 return -EINVAL;
6257 if (key.idx > 4)
6258 return -EINVAL;
6259 } else {
6260 key.p.key_len = 0;
6261 key.p.key = NULL;
6262 }
6263
Johannes Bergafea0b72010-08-10 09:46:42 +02006264 if (key.idx >= 0) {
6265 int i;
6266 bool ok = false;
6267 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6268 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6269 ok = true;
6270 break;
6271 }
6272 }
Johannes Berg4c476992010-10-04 21:36:35 +02006273 if (!ok)
6274 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006275 }
6276
Johannes Berg4c476992010-10-04 21:36:35 +02006277 if (!rdev->ops->auth)
6278 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006279
Johannes Berg074ac8d2010-09-16 14:58:22 +02006280 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006281 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6282 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006283
Johannes Berg19957bb2009-07-02 17:20:43 +02006284 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006285 chan = nl80211_get_valid_chan(&rdev->wiphy,
6286 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6287 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006288 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006289
Johannes Berg19957bb2009-07-02 17:20:43 +02006290 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6291 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6292
6293 if (info->attrs[NL80211_ATTR_IE]) {
6294 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6295 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6296 }
6297
6298 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006299 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006300 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006301
Jouni Malinene39e5b52012-09-30 19:29:39 +03006302 if (auth_type == NL80211_AUTHTYPE_SAE &&
6303 !info->attrs[NL80211_ATTR_SAE_DATA])
6304 return -EINVAL;
6305
6306 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6307 if (auth_type != NL80211_AUTHTYPE_SAE)
6308 return -EINVAL;
6309 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6310 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6311 /* need to include at least Auth Transaction and Status Code */
6312 if (sae_data_len < 4)
6313 return -EINVAL;
6314 }
6315
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006316 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6317
Johannes Berg95de8172012-01-20 13:55:25 +01006318 /*
6319 * Since we no longer track auth state, ignore
6320 * requests to only change local state.
6321 */
6322 if (local_state_change)
6323 return 0;
6324
Johannes Berg91bf9b22013-05-15 17:44:01 +02006325 wdev_lock(dev->ieee80211_ptr);
6326 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6327 ssid, ssid_len, ie, ie_len,
6328 key.p.key, key.p.key_len, key.idx,
6329 sae_data, sae_data_len);
6330 wdev_unlock(dev->ieee80211_ptr);
6331 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006332}
6333
Johannes Bergc0692b82010-08-27 14:26:53 +03006334static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6335 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006336 struct cfg80211_crypto_settings *settings,
6337 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006338{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006339 memset(settings, 0, sizeof(*settings));
6340
Samuel Ortizb23aa672009-07-01 21:26:54 +02006341 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6342
Johannes Bergc0692b82010-08-27 14:26:53 +03006343 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6344 u16 proto;
6345 proto = nla_get_u16(
6346 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6347 settings->control_port_ethertype = cpu_to_be16(proto);
6348 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6349 proto != ETH_P_PAE)
6350 return -EINVAL;
6351 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6352 settings->control_port_no_encrypt = true;
6353 } else
6354 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6355
Samuel Ortizb23aa672009-07-01 21:26:54 +02006356 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6357 void *data;
6358 int len, i;
6359
6360 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6361 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6362 settings->n_ciphers_pairwise = len / sizeof(u32);
6363
6364 if (len % sizeof(u32))
6365 return -EINVAL;
6366
Johannes Berg3dc27d22009-07-02 21:36:37 +02006367 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006368 return -EINVAL;
6369
6370 memcpy(settings->ciphers_pairwise, data, len);
6371
6372 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006373 if (!cfg80211_supported_cipher_suite(
6374 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006375 settings->ciphers_pairwise[i]))
6376 return -EINVAL;
6377 }
6378
6379 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6380 settings->cipher_group =
6381 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006382 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6383 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006384 return -EINVAL;
6385 }
6386
6387 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6388 settings->wpa_versions =
6389 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6390 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6391 return -EINVAL;
6392 }
6393
6394 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6395 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006396 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006397
6398 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6399 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6400 settings->n_akm_suites = len / sizeof(u32);
6401
6402 if (len % sizeof(u32))
6403 return -EINVAL;
6404
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006405 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6406 return -EINVAL;
6407
Samuel Ortizb23aa672009-07-01 21:26:54 +02006408 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006409 }
6410
6411 return 0;
6412}
6413
Jouni Malinen636a5d32009-03-19 13:39:22 +02006414static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6415{
Johannes Berg4c476992010-10-04 21:36:35 +02006416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6417 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006418 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006419 struct cfg80211_assoc_request req = {};
6420 const u8 *bssid, *ssid;
6421 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006422
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006423 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6424 return -EINVAL;
6425
6426 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006427 !info->attrs[NL80211_ATTR_SSID] ||
6428 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006429 return -EINVAL;
6430
Johannes Berg4c476992010-10-04 21:36:35 +02006431 if (!rdev->ops->assoc)
6432 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006433
Johannes Berg074ac8d2010-09-16 14:58:22 +02006434 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006435 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6436 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006437
Johannes Berg19957bb2009-07-02 17:20:43 +02006438 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006439
Jouni Malinen664834d2014-01-15 00:01:44 +02006440 chan = nl80211_get_valid_chan(&rdev->wiphy,
6441 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6442 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006443 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006444
Johannes Berg19957bb2009-07-02 17:20:43 +02006445 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6446 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006447
6448 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006449 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6450 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006451 }
6452
Jouni Malinendc6382c2009-05-06 22:09:37 +03006453 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006454 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006455 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006456 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006457 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006458 else if (mfp != NL80211_MFP_NO)
6459 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006460 }
6461
Johannes Berg3e5d7642009-07-07 14:37:26 +02006462 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006463 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006464
Ben Greear7e7c8922011-11-18 11:31:59 -08006465 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006466 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006467
6468 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006469 memcpy(&req.ht_capa_mask,
6470 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6471 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006472
6473 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006474 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006475 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006476 memcpy(&req.ht_capa,
6477 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6478 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006479 }
6480
Johannes Bergee2aca32013-02-21 17:36:01 +01006481 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006482 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006483
6484 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006485 memcpy(&req.vht_capa_mask,
6486 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6487 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006488
6489 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006490 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006491 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006492 memcpy(&req.vht_capa,
6493 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6494 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006495 }
6496
Johannes Bergf62fab72013-02-21 20:09:09 +01006497 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006498 if (!err) {
6499 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006500 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6501 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006502 wdev_unlock(dev->ieee80211_ptr);
6503 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006504
Jouni Malinen636a5d32009-03-19 13:39:22 +02006505 return err;
6506}
6507
6508static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6509{
Johannes Berg4c476992010-10-04 21:36:35 +02006510 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6511 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006512 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006513 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006514 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006515 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006516
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006517 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6518 return -EINVAL;
6519
6520 if (!info->attrs[NL80211_ATTR_MAC])
6521 return -EINVAL;
6522
6523 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6524 return -EINVAL;
6525
Johannes Berg4c476992010-10-04 21:36:35 +02006526 if (!rdev->ops->deauth)
6527 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006528
Johannes Berg074ac8d2010-09-16 14:58:22 +02006529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006530 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6531 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006532
Johannes Berg19957bb2009-07-02 17:20:43 +02006533 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006534
Johannes Berg19957bb2009-07-02 17:20:43 +02006535 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6536 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006537 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006538 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006539 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006540
6541 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006542 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6543 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006544 }
6545
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006546 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6547
Johannes Berg91bf9b22013-05-15 17:44:01 +02006548 wdev_lock(dev->ieee80211_ptr);
6549 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6550 local_state_change);
6551 wdev_unlock(dev->ieee80211_ptr);
6552 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006553}
6554
6555static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6556{
Johannes Berg4c476992010-10-04 21:36:35 +02006557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6558 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006559 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006560 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006561 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006562 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006563
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006564 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6565 return -EINVAL;
6566
6567 if (!info->attrs[NL80211_ATTR_MAC])
6568 return -EINVAL;
6569
6570 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6571 return -EINVAL;
6572
Johannes Berg4c476992010-10-04 21:36:35 +02006573 if (!rdev->ops->disassoc)
6574 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006575
Johannes Berg074ac8d2010-09-16 14:58:22 +02006576 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006577 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6578 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006579
Johannes Berg19957bb2009-07-02 17:20:43 +02006580 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006581
Johannes Berg19957bb2009-07-02 17:20:43 +02006582 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6583 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006584 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006585 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006586 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006587
6588 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006589 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6590 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006591 }
6592
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006593 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6594
Johannes Berg91bf9b22013-05-15 17:44:01 +02006595 wdev_lock(dev->ieee80211_ptr);
6596 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6597 local_state_change);
6598 wdev_unlock(dev->ieee80211_ptr);
6599 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006600}
6601
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006602static bool
6603nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6604 int mcast_rate[IEEE80211_NUM_BANDS],
6605 int rateval)
6606{
6607 struct wiphy *wiphy = &rdev->wiphy;
6608 bool found = false;
6609 int band, i;
6610
6611 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6612 struct ieee80211_supported_band *sband;
6613
6614 sband = wiphy->bands[band];
6615 if (!sband)
6616 continue;
6617
6618 for (i = 0; i < sband->n_bitrates; i++) {
6619 if (sband->bitrates[i].bitrate == rateval) {
6620 mcast_rate[band] = i + 1;
6621 found = true;
6622 break;
6623 }
6624 }
6625 }
6626
6627 return found;
6628}
6629
Johannes Berg04a773a2009-04-19 21:24:32 +02006630static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6631{
Johannes Berg4c476992010-10-04 21:36:35 +02006632 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6633 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006634 struct cfg80211_ibss_params ibss;
6635 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006636 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006637 int err;
6638
Johannes Berg8e30bc52009-04-22 17:45:38 +02006639 memset(&ibss, 0, sizeof(ibss));
6640
Johannes Berg04a773a2009-04-19 21:24:32 +02006641 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6642 return -EINVAL;
6643
Johannes Berg683b6d32012-11-08 21:25:48 +01006644 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006645 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6646 return -EINVAL;
6647
Johannes Berg8e30bc52009-04-22 17:45:38 +02006648 ibss.beacon_interval = 100;
6649
6650 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6651 ibss.beacon_interval =
6652 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6653 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6654 return -EINVAL;
6655 }
6656
Johannes Berg4c476992010-10-04 21:36:35 +02006657 if (!rdev->ops->join_ibss)
6658 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006659
Johannes Berg4c476992010-10-04 21:36:35 +02006660 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6661 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006662
Johannes Berg79c97e92009-07-07 03:56:12 +02006663 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006664
Johannes Berg39193492011-09-16 13:45:25 +02006665 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006666 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006667
6668 if (!is_valid_ether_addr(ibss.bssid))
6669 return -EINVAL;
6670 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006671 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6672 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6673
6674 if (info->attrs[NL80211_ATTR_IE]) {
6675 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6676 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6677 }
6678
Johannes Berg683b6d32012-11-08 21:25:48 +01006679 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6680 if (err)
6681 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006682
Johannes Berg683b6d32012-11-08 21:25:48 +01006683 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006684 return -EINVAL;
6685
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006686 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006687 case NL80211_CHAN_WIDTH_5:
6688 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006689 case NL80211_CHAN_WIDTH_20_NOHT:
6690 break;
6691 case NL80211_CHAN_WIDTH_20:
6692 case NL80211_CHAN_WIDTH_40:
6693 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6694 break;
6695 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006696 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006697 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006698
Johannes Berg04a773a2009-04-19 21:24:32 +02006699 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006700 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006701
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006702 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6703 u8 *rates =
6704 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6705 int n_rates =
6706 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6707 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006708 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006709
Johannes Berg34850ab2011-07-18 18:08:35 +02006710 err = ieee80211_get_ratemask(sband, rates, n_rates,
6711 &ibss.basic_rates);
6712 if (err)
6713 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006714 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006715
Simon Wunderlich803768f2013-06-28 10:39:58 +02006716 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6717 memcpy(&ibss.ht_capa_mask,
6718 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6719 sizeof(ibss.ht_capa_mask));
6720
6721 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6722 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6723 return -EINVAL;
6724 memcpy(&ibss.ht_capa,
6725 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6726 sizeof(ibss.ht_capa));
6727 }
6728
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006729 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6730 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6731 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6732 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006733
Johannes Berg4c476992010-10-04 21:36:35 +02006734 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306735 bool no_ht = false;
6736
Johannes Berg4c476992010-10-04 21:36:35 +02006737 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306738 info->attrs[NL80211_ATTR_KEYS],
6739 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006740 if (IS_ERR(connkeys))
6741 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306742
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006743 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6744 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306745 kfree(connkeys);
6746 return -EINVAL;
6747 }
Johannes Berg4c476992010-10-04 21:36:35 +02006748 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006749
Antonio Quartulli267335d2012-01-31 20:25:47 +01006750 ibss.control_port =
6751 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6752
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006753 ibss.userspace_handles_dfs =
6754 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6755
Johannes Berg4c476992010-10-04 21:36:35 +02006756 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006757 if (err)
6758 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006759 return err;
6760}
6761
6762static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6763{
Johannes Berg4c476992010-10-04 21:36:35 +02006764 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6765 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006766
Johannes Berg4c476992010-10-04 21:36:35 +02006767 if (!rdev->ops->leave_ibss)
6768 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006769
Johannes Berg4c476992010-10-04 21:36:35 +02006770 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6771 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006772
Johannes Berg4c476992010-10-04 21:36:35 +02006773 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006774}
6775
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006776static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6777{
6778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6779 struct net_device *dev = info->user_ptr[1];
6780 int mcast_rate[IEEE80211_NUM_BANDS];
6781 u32 nla_rate;
6782 int err;
6783
6784 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6785 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6786 return -EOPNOTSUPP;
6787
6788 if (!rdev->ops->set_mcast_rate)
6789 return -EOPNOTSUPP;
6790
6791 memset(mcast_rate, 0, sizeof(mcast_rate));
6792
6793 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6794 return -EINVAL;
6795
6796 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6797 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6798 return -EINVAL;
6799
6800 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6801
6802 return err;
6803}
6804
Johannes Bergad7e7182013-11-13 13:37:47 +01006805static struct sk_buff *
6806__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6807 int approxlen, u32 portid, u32 seq,
6808 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006809 enum nl80211_attrs attr,
6810 const struct nl80211_vendor_cmd_info *info,
6811 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006812{
6813 struct sk_buff *skb;
6814 void *hdr;
6815 struct nlattr *data;
6816
6817 skb = nlmsg_new(approxlen + 100, gfp);
6818 if (!skb)
6819 return NULL;
6820
6821 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6822 if (!hdr) {
6823 kfree_skb(skb);
6824 return NULL;
6825 }
6826
6827 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6828 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006829
6830 if (info) {
6831 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6832 info->vendor_id))
6833 goto nla_put_failure;
6834 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6835 info->subcmd))
6836 goto nla_put_failure;
6837 }
6838
Johannes Bergad7e7182013-11-13 13:37:47 +01006839 data = nla_nest_start(skb, attr);
6840
6841 ((void **)skb->cb)[0] = rdev;
6842 ((void **)skb->cb)[1] = hdr;
6843 ((void **)skb->cb)[2] = data;
6844
6845 return skb;
6846
6847 nla_put_failure:
6848 kfree_skb(skb);
6849 return NULL;
6850}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006851
Johannes Berge03ad6e2014-01-01 17:22:30 +01006852struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6853 enum nl80211_commands cmd,
6854 enum nl80211_attrs attr,
6855 int vendor_event_idx,
6856 int approxlen, gfp_t gfp)
6857{
6858 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6859 const struct nl80211_vendor_cmd_info *info;
6860
6861 switch (cmd) {
6862 case NL80211_CMD_TESTMODE:
6863 if (WARN_ON(vendor_event_idx != -1))
6864 return NULL;
6865 info = NULL;
6866 break;
6867 case NL80211_CMD_VENDOR:
6868 if (WARN_ON(vendor_event_idx < 0 ||
6869 vendor_event_idx >= wiphy->n_vendor_events))
6870 return NULL;
6871 info = &wiphy->vendor_events[vendor_event_idx];
6872 break;
6873 default:
6874 WARN_ON(1);
6875 return NULL;
6876 }
6877
6878 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6879 cmd, attr, info, gfp);
6880}
6881EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6882
6883void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6884{
6885 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6886 void *hdr = ((void **)skb->cb)[1];
6887 struct nlattr *data = ((void **)skb->cb)[2];
6888 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6889
6890 nla_nest_end(skb, data);
6891 genlmsg_end(skb, hdr);
6892
6893 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6894 mcgrp = NL80211_MCGRP_VENDOR;
6895
6896 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6897 mcgrp, gfp);
6898}
6899EXPORT_SYMBOL(__cfg80211_send_event_skb);
6900
Johannes Bergaff89a92009-07-01 21:26:51 +02006901#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006902static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6903{
Johannes Berg4c476992010-10-04 21:36:35 +02006904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006905 struct wireless_dev *wdev =
6906 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006907 int err;
6908
David Spinadelfc73f112013-07-31 18:04:15 +03006909 if (!rdev->ops->testmode_cmd)
6910 return -EOPNOTSUPP;
6911
6912 if (IS_ERR(wdev)) {
6913 err = PTR_ERR(wdev);
6914 if (err != -EINVAL)
6915 return err;
6916 wdev = NULL;
6917 } else if (wdev->wiphy != &rdev->wiphy) {
6918 return -EINVAL;
6919 }
6920
Johannes Bergaff89a92009-07-01 21:26:51 +02006921 if (!info->attrs[NL80211_ATTR_TESTDATA])
6922 return -EINVAL;
6923
Johannes Bergad7e7182013-11-13 13:37:47 +01006924 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006925 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006926 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6927 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006928 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006929
Johannes Bergaff89a92009-07-01 21:26:51 +02006930 return err;
6931}
6932
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006933static int nl80211_testmode_dump(struct sk_buff *skb,
6934 struct netlink_callback *cb)
6935{
Johannes Berg00918d32011-12-13 17:22:05 +01006936 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006937 int err;
6938 long phy_idx;
6939 void *data = NULL;
6940 int data_len = 0;
6941
Johannes Berg5fe231e2013-05-08 21:45:15 +02006942 rtnl_lock();
6943
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006944 if (cb->args[0]) {
6945 /*
6946 * 0 is a valid index, but not valid for args[0],
6947 * so we need to offset by 1.
6948 */
6949 phy_idx = cb->args[0] - 1;
6950 } else {
6951 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6952 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6953 nl80211_policy);
6954 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006955 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006956
Johannes Berg2bd7e352012-06-15 14:23:16 +02006957 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6958 nl80211_fam.attrbuf);
6959 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006960 err = PTR_ERR(rdev);
6961 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006962 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006963 phy_idx = rdev->wiphy_idx;
6964 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006965
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006966 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6967 cb->args[1] =
6968 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6969 }
6970
6971 if (cb->args[1]) {
6972 data = nla_data((void *)cb->args[1]);
6973 data_len = nla_len((void *)cb->args[1]);
6974 }
6975
Johannes Berg00918d32011-12-13 17:22:05 +01006976 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6977 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006978 err = -ENOENT;
6979 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006980 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006981
Johannes Berg00918d32011-12-13 17:22:05 +01006982 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006983 err = -EOPNOTSUPP;
6984 goto out_err;
6985 }
6986
6987 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006988 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006989 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6990 NL80211_CMD_TESTMODE);
6991 struct nlattr *tmdata;
6992
Dan Carpentercb35fba2013-08-14 14:50:01 +03006993 if (!hdr)
6994 break;
6995
David S. Miller9360ffd2012-03-29 04:41:26 -04006996 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006997 genlmsg_cancel(skb, hdr);
6998 break;
6999 }
7000
7001 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7002 if (!tmdata) {
7003 genlmsg_cancel(skb, hdr);
7004 break;
7005 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007006 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007007 nla_nest_end(skb, tmdata);
7008
7009 if (err == -ENOBUFS || err == -ENOENT) {
7010 genlmsg_cancel(skb, hdr);
7011 break;
7012 } else if (err) {
7013 genlmsg_cancel(skb, hdr);
7014 goto out_err;
7015 }
7016
7017 genlmsg_end(skb, hdr);
7018 }
7019
7020 err = skb->len;
7021 /* see above */
7022 cb->args[0] = phy_idx + 1;
7023 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007024 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007025 return err;
7026}
Johannes Bergaff89a92009-07-01 21:26:51 +02007027#endif
7028
Samuel Ortizb23aa672009-07-01 21:26:54 +02007029static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7030{
Johannes Berg4c476992010-10-04 21:36:35 +02007031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7032 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007033 struct cfg80211_connect_params connect;
7034 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007035 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007036 int err;
7037
7038 memset(&connect, 0, sizeof(connect));
7039
7040 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7041 return -EINVAL;
7042
7043 if (!info->attrs[NL80211_ATTR_SSID] ||
7044 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7045 return -EINVAL;
7046
7047 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7048 connect.auth_type =
7049 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007050 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7051 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007052 return -EINVAL;
7053 } else
7054 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7055
7056 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7057
Johannes Bergc0692b82010-08-27 14:26:53 +03007058 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007059 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007060 if (err)
7061 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007062
Johannes Berg074ac8d2010-09-16 14:58:22 +02007063 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007064 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7065 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007066
Johannes Berg79c97e92009-07-07 03:56:12 +02007067 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007068
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307069 connect.bg_scan_period = -1;
7070 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7071 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7072 connect.bg_scan_period =
7073 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7074 }
7075
Samuel Ortizb23aa672009-07-01 21:26:54 +02007076 if (info->attrs[NL80211_ATTR_MAC])
7077 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007078 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7079 connect.bssid_hint =
7080 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007081 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7082 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7083
7084 if (info->attrs[NL80211_ATTR_IE]) {
7085 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7086 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7087 }
7088
Jouni Malinencee00a92013-01-15 17:15:57 +02007089 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7090 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7091 if (connect.mfp != NL80211_MFP_REQUIRED &&
7092 connect.mfp != NL80211_MFP_NO)
7093 return -EINVAL;
7094 } else {
7095 connect.mfp = NL80211_MFP_NO;
7096 }
7097
Samuel Ortizb23aa672009-07-01 21:26:54 +02007098 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007099 connect.channel = nl80211_get_valid_chan(
7100 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7101 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007102 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007103 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007104 connect.channel_hint = nl80211_get_valid_chan(
7105 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7106 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007107 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007108 }
7109
Johannes Bergfffd0932009-07-08 14:22:54 +02007110 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7111 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307112 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007113 if (IS_ERR(connkeys))
7114 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007115 }
7116
Ben Greear7e7c8922011-11-18 11:31:59 -08007117 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7118 connect.flags |= ASSOC_REQ_DISABLE_HT;
7119
7120 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7121 memcpy(&connect.ht_capa_mask,
7122 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7123 sizeof(connect.ht_capa_mask));
7124
7125 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007126 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7127 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007128 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007129 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007130 memcpy(&connect.ht_capa,
7131 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7132 sizeof(connect.ht_capa));
7133 }
7134
Johannes Bergee2aca32013-02-21 17:36:01 +01007135 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7136 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7137
7138 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7139 memcpy(&connect.vht_capa_mask,
7140 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7141 sizeof(connect.vht_capa_mask));
7142
7143 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7144 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7145 kfree(connkeys);
7146 return -EINVAL;
7147 }
7148 memcpy(&connect.vht_capa,
7149 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7150 sizeof(connect.vht_capa));
7151 }
7152
Johannes Berg83739b02013-05-15 17:44:01 +02007153 wdev_lock(dev->ieee80211_ptr);
7154 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7155 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007156 if (err)
7157 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007158 return err;
7159}
7160
7161static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7162{
Johannes Berg4c476992010-10-04 21:36:35 +02007163 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7164 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007165 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007166 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007167
7168 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7169 reason = WLAN_REASON_DEAUTH_LEAVING;
7170 else
7171 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7172
7173 if (reason == 0)
7174 return -EINVAL;
7175
Johannes Berg074ac8d2010-09-16 14:58:22 +02007176 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007177 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7178 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007179
Johannes Berg83739b02013-05-15 17:44:01 +02007180 wdev_lock(dev->ieee80211_ptr);
7181 ret = cfg80211_disconnect(rdev, dev, reason, true);
7182 wdev_unlock(dev->ieee80211_ptr);
7183 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007184}
7185
Johannes Berg463d0182009-07-14 00:33:35 +02007186static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7187{
Johannes Berg4c476992010-10-04 21:36:35 +02007188 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007189 struct net *net;
7190 int err;
7191 u32 pid;
7192
7193 if (!info->attrs[NL80211_ATTR_PID])
7194 return -EINVAL;
7195
7196 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7197
Johannes Berg463d0182009-07-14 00:33:35 +02007198 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007199 if (IS_ERR(net))
7200 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007201
7202 err = 0;
7203
7204 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007205 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7206 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007207
Johannes Berg463d0182009-07-14 00:33:35 +02007208 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007209 return err;
7210}
7211
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007212static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7213{
Johannes Berg4c476992010-10-04 21:36:35 +02007214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007215 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7216 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007217 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007218 struct cfg80211_pmksa pmksa;
7219
7220 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7221
7222 if (!info->attrs[NL80211_ATTR_MAC])
7223 return -EINVAL;
7224
7225 if (!info->attrs[NL80211_ATTR_PMKID])
7226 return -EINVAL;
7227
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007228 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7229 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7230
Johannes Berg074ac8d2010-09-16 14:58:22 +02007231 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007232 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7233 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007234
7235 switch (info->genlhdr->cmd) {
7236 case NL80211_CMD_SET_PMKSA:
7237 rdev_ops = rdev->ops->set_pmksa;
7238 break;
7239 case NL80211_CMD_DEL_PMKSA:
7240 rdev_ops = rdev->ops->del_pmksa;
7241 break;
7242 default:
7243 WARN_ON(1);
7244 break;
7245 }
7246
Johannes Berg4c476992010-10-04 21:36:35 +02007247 if (!rdev_ops)
7248 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007249
Johannes Berg4c476992010-10-04 21:36:35 +02007250 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007251}
7252
7253static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7254{
Johannes Berg4c476992010-10-04 21:36:35 +02007255 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7256 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007257
Johannes Berg074ac8d2010-09-16 14:58:22 +02007258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007259 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7260 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007261
Johannes Berg4c476992010-10-04 21:36:35 +02007262 if (!rdev->ops->flush_pmksa)
7263 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007264
Hila Gonene35e4d22012-06-27 17:19:42 +03007265 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007266}
7267
Arik Nemtsov109086c2011-09-28 14:12:50 +03007268static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7269{
7270 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7271 struct net_device *dev = info->user_ptr[1];
7272 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307273 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007274 u16 status_code;
7275 u8 *peer;
7276
7277 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7278 !rdev->ops->tdls_mgmt)
7279 return -EOPNOTSUPP;
7280
7281 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7282 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7283 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7284 !info->attrs[NL80211_ATTR_IE] ||
7285 !info->attrs[NL80211_ATTR_MAC])
7286 return -EINVAL;
7287
7288 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7289 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7290 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7291 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307292 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7293 peer_capability =
7294 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007295
Hila Gonene35e4d22012-06-27 17:19:42 +03007296 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307297 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007298 nla_data(info->attrs[NL80211_ATTR_IE]),
7299 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007300}
7301
7302static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7303{
7304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7305 struct net_device *dev = info->user_ptr[1];
7306 enum nl80211_tdls_operation operation;
7307 u8 *peer;
7308
7309 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7310 !rdev->ops->tdls_oper)
7311 return -EOPNOTSUPP;
7312
7313 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7314 !info->attrs[NL80211_ATTR_MAC])
7315 return -EINVAL;
7316
7317 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7318 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7319
Hila Gonene35e4d22012-06-27 17:19:42 +03007320 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007321}
7322
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007323static int nl80211_remain_on_channel(struct sk_buff *skb,
7324 struct genl_info *info)
7325{
Johannes Berg4c476992010-10-04 21:36:35 +02007326 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007327 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007328 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007329 struct sk_buff *msg;
7330 void *hdr;
7331 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007332 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007333 int err;
7334
7335 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7336 !info->attrs[NL80211_ATTR_DURATION])
7337 return -EINVAL;
7338
7339 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7340
Johannes Berg7c4ef712011-11-18 15:33:48 +01007341 if (!rdev->ops->remain_on_channel ||
7342 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007343 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007344
Johannes Bergebf348f2012-06-01 12:50:54 +02007345 /*
7346 * We should be on that channel for at least a minimum amount of
7347 * time (10ms) but no longer than the driver supports.
7348 */
7349 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7350 duration > rdev->wiphy.max_remain_on_channel_duration)
7351 return -EINVAL;
7352
Johannes Berg683b6d32012-11-08 21:25:48 +01007353 err = nl80211_parse_chandef(rdev, info, &chandef);
7354 if (err)
7355 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007356
7357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007358 if (!msg)
7359 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007360
Eric W. Biederman15e47302012-09-07 20:12:54 +00007361 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007362 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007363 if (!hdr) {
7364 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007365 goto free_msg;
7366 }
7367
Johannes Berg683b6d32012-11-08 21:25:48 +01007368 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7369 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007370
7371 if (err)
7372 goto free_msg;
7373
David S. Miller9360ffd2012-03-29 04:41:26 -04007374 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7375 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007376
7377 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007378
7379 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007380
7381 nla_put_failure:
7382 err = -ENOBUFS;
7383 free_msg:
7384 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007385 return err;
7386}
7387
7388static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7389 struct genl_info *info)
7390{
Johannes Berg4c476992010-10-04 21:36:35 +02007391 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007392 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007393 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007394
7395 if (!info->attrs[NL80211_ATTR_COOKIE])
7396 return -EINVAL;
7397
Johannes Berg4c476992010-10-04 21:36:35 +02007398 if (!rdev->ops->cancel_remain_on_channel)
7399 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007400
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007401 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7402
Hila Gonene35e4d22012-06-27 17:19:42 +03007403 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007404}
7405
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007406static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7407 u8 *rates, u8 rates_len)
7408{
7409 u8 i;
7410 u32 mask = 0;
7411
7412 for (i = 0; i < rates_len; i++) {
7413 int rate = (rates[i] & 0x7f) * 5;
7414 int ridx;
7415 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7416 struct ieee80211_rate *srate =
7417 &sband->bitrates[ridx];
7418 if (rate == srate->bitrate) {
7419 mask |= 1 << ridx;
7420 break;
7421 }
7422 }
7423 if (ridx == sband->n_bitrates)
7424 return 0; /* rate not found */
7425 }
7426
7427 return mask;
7428}
7429
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007430static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7431 u8 *rates, u8 rates_len,
7432 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7433{
7434 u8 i;
7435
7436 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7437
7438 for (i = 0; i < rates_len; i++) {
7439 int ridx, rbit;
7440
7441 ridx = rates[i] / 8;
7442 rbit = BIT(rates[i] % 8);
7443
7444 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007445 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007446 return false;
7447
7448 /* check availability */
7449 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7450 mcs[ridx] |= rbit;
7451 else
7452 return false;
7453 }
7454
7455 return true;
7456}
7457
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007458static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7459{
7460 u16 mcs_mask = 0;
7461
7462 switch (vht_mcs_map) {
7463 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7464 break;
7465 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7466 mcs_mask = 0x00FF;
7467 break;
7468 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7469 mcs_mask = 0x01FF;
7470 break;
7471 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7472 mcs_mask = 0x03FF;
7473 break;
7474 default:
7475 break;
7476 }
7477
7478 return mcs_mask;
7479}
7480
7481static void vht_build_mcs_mask(u16 vht_mcs_map,
7482 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7483{
7484 u8 nss;
7485
7486 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7487 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7488 vht_mcs_map >>= 2;
7489 }
7490}
7491
7492static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7493 struct nl80211_txrate_vht *txrate,
7494 u16 mcs[NL80211_VHT_NSS_MAX])
7495{
7496 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7497 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7498 u8 i;
7499
7500 if (!sband->vht_cap.vht_supported)
7501 return false;
7502
7503 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7504
7505 /* Build vht_mcs_mask from VHT capabilities */
7506 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7507
7508 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7509 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7510 mcs[i] = txrate->mcs[i];
7511 else
7512 return false;
7513 }
7514
7515 return true;
7516}
7517
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007518static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007519 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7520 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007521 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7522 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007523 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007524 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007525};
7526
7527static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7528 struct genl_info *info)
7529{
7530 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007531 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007532 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007533 int rem, i;
7534 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007535 struct nlattr *tx_rates;
7536 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007537 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007538
Johannes Berg4c476992010-10-04 21:36:35 +02007539 if (!rdev->ops->set_bitrate_mask)
7540 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007541
7542 memset(&mask, 0, sizeof(mask));
7543 /* Default to all rates enabled */
7544 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7545 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007546
7547 if (!sband)
7548 continue;
7549
7550 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007551 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007552 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007553 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007554
7555 if (!sband->vht_cap.vht_supported)
7556 continue;
7557
7558 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7559 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007560 }
7561
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007562 /* if no rates are given set it back to the defaults */
7563 if (!info->attrs[NL80211_ATTR_TX_RATES])
7564 goto out;
7565
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007566 /*
7567 * The nested attribute uses enum nl80211_band as the index. This maps
7568 * directly to the enum ieee80211_band values used in cfg80211.
7569 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007570 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007571 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007572 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007573 int err;
7574
Johannes Berg4c476992010-10-04 21:36:35 +02007575 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7576 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007577 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007578 if (sband == NULL)
7579 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007580 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7581 nla_len(tx_rates), nl80211_txattr_policy);
7582 if (err)
7583 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007584 if (tb[NL80211_TXRATE_LEGACY]) {
7585 mask.control[band].legacy = rateset_to_mask(
7586 sband,
7587 nla_data(tb[NL80211_TXRATE_LEGACY]),
7588 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307589 if ((mask.control[band].legacy == 0) &&
7590 nla_len(tb[NL80211_TXRATE_LEGACY]))
7591 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007592 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007593 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007594 if (!ht_rateset_to_mask(
7595 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007596 nla_data(tb[NL80211_TXRATE_HT]),
7597 nla_len(tb[NL80211_TXRATE_HT]),
7598 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007599 return -EINVAL;
7600 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007601 if (tb[NL80211_TXRATE_VHT]) {
7602 if (!vht_set_mcs_mask(
7603 sband,
7604 nla_data(tb[NL80211_TXRATE_VHT]),
7605 mask.control[band].vht_mcs))
7606 return -EINVAL;
7607 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007608 if (tb[NL80211_TXRATE_GI]) {
7609 mask.control[band].gi =
7610 nla_get_u8(tb[NL80211_TXRATE_GI]);
7611 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7612 return -EINVAL;
7613 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007614
7615 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007616 /* don't allow empty legacy rates if HT or VHT
7617 * are not even supported.
7618 */
7619 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7620 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007621 return -EINVAL;
7622
7623 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007624 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007625 goto out;
7626
7627 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7628 if (mask.control[band].vht_mcs[i])
7629 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007630
7631 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007632 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007633 }
7634 }
7635
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007636out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007637 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007638}
7639
Johannes Berg2e161f72010-08-12 15:38:38 +02007640static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007641{
Johannes Berg4c476992010-10-04 21:36:35 +02007642 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007643 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007644 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007645
7646 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7647 return -EINVAL;
7648
Johannes Berg2e161f72010-08-12 15:38:38 +02007649 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7650 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007651
Johannes Berg71bbc992012-06-15 15:30:18 +02007652 switch (wdev->iftype) {
7653 case NL80211_IFTYPE_STATION:
7654 case NL80211_IFTYPE_ADHOC:
7655 case NL80211_IFTYPE_P2P_CLIENT:
7656 case NL80211_IFTYPE_AP:
7657 case NL80211_IFTYPE_AP_VLAN:
7658 case NL80211_IFTYPE_MESH_POINT:
7659 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007660 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007661 break;
7662 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007663 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007664 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007665
7666 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007667 if (!rdev->ops->mgmt_tx)
7668 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007669
Eric W. Biederman15e47302012-09-07 20:12:54 +00007670 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007671 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7672 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007673}
7674
Johannes Berg2e161f72010-08-12 15:38:38 +02007675static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007676{
Johannes Berg4c476992010-10-04 21:36:35 +02007677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007678 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007679 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007680 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007681 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007682 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007683 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007684 struct cfg80211_mgmt_tx_params params = {
7685 .dont_wait_for_ack =
7686 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7687 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007688
Johannes Berg683b6d32012-11-08 21:25:48 +01007689 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007690 return -EINVAL;
7691
Johannes Berg4c476992010-10-04 21:36:35 +02007692 if (!rdev->ops->mgmt_tx)
7693 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007694
Johannes Berg71bbc992012-06-15 15:30:18 +02007695 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007696 case NL80211_IFTYPE_P2P_DEVICE:
7697 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7698 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007699 case NL80211_IFTYPE_STATION:
7700 case NL80211_IFTYPE_ADHOC:
7701 case NL80211_IFTYPE_P2P_CLIENT:
7702 case NL80211_IFTYPE_AP:
7703 case NL80211_IFTYPE_AP_VLAN:
7704 case NL80211_IFTYPE_MESH_POINT:
7705 case NL80211_IFTYPE_P2P_GO:
7706 break;
7707 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007708 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007709 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007710
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007711 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007712 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007713 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007714 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007715
7716 /*
7717 * We should wait on the channel for at least a minimum amount
7718 * of time (10ms) but no longer than the driver supports.
7719 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007720 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7721 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007722 return -EINVAL;
7723
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007724 }
7725
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007726 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007727
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007728 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007729 return -EINVAL;
7730
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007731 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307732
Antonio Quartulliea141b752013-06-11 14:20:03 +02007733 /* get the channel if any has been specified, otherwise pass NULL to
7734 * the driver. The latter will use the current one
7735 */
7736 chandef.chan = NULL;
7737 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7738 err = nl80211_parse_chandef(rdev, info, &chandef);
7739 if (err)
7740 return err;
7741 }
7742
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007743 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007744 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007745
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007746 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7748 if (!msg)
7749 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007750
Eric W. Biederman15e47302012-09-07 20:12:54 +00007751 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007752 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007753 if (!hdr) {
7754 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007755 goto free_msg;
7756 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007757 }
Johannes Berge247bd902011-11-04 11:18:21 +01007758
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007759 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7760 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7761 params.chan = chandef.chan;
7762 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007763 if (err)
7764 goto free_msg;
7765
Johannes Berge247bd902011-11-04 11:18:21 +01007766 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007767 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7768 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007769
Johannes Berge247bd902011-11-04 11:18:21 +01007770 genlmsg_end(msg, hdr);
7771 return genlmsg_reply(msg, info);
7772 }
7773
7774 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007775
7776 nla_put_failure:
7777 err = -ENOBUFS;
7778 free_msg:
7779 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007780 return err;
7781}
7782
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007783static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7784{
7785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007786 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007787 u64 cookie;
7788
7789 if (!info->attrs[NL80211_ATTR_COOKIE])
7790 return -EINVAL;
7791
7792 if (!rdev->ops->mgmt_tx_cancel_wait)
7793 return -EOPNOTSUPP;
7794
Johannes Berg71bbc992012-06-15 15:30:18 +02007795 switch (wdev->iftype) {
7796 case NL80211_IFTYPE_STATION:
7797 case NL80211_IFTYPE_ADHOC:
7798 case NL80211_IFTYPE_P2P_CLIENT:
7799 case NL80211_IFTYPE_AP:
7800 case NL80211_IFTYPE_AP_VLAN:
7801 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007802 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007803 break;
7804 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007805 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007806 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007807
7808 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7809
Hila Gonene35e4d22012-06-27 17:19:42 +03007810 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007811}
7812
Kalle Valoffb9eb32010-02-17 17:58:10 +02007813static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7814{
Johannes Berg4c476992010-10-04 21:36:35 +02007815 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007816 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007817 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007818 u8 ps_state;
7819 bool state;
7820 int err;
7821
Johannes Berg4c476992010-10-04 21:36:35 +02007822 if (!info->attrs[NL80211_ATTR_PS_STATE])
7823 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007824
7825 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7826
Johannes Berg4c476992010-10-04 21:36:35 +02007827 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7828 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007829
7830 wdev = dev->ieee80211_ptr;
7831
Johannes Berg4c476992010-10-04 21:36:35 +02007832 if (!rdev->ops->set_power_mgmt)
7833 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007834
7835 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7836
7837 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007838 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007839
Hila Gonene35e4d22012-06-27 17:19:42 +03007840 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007841 if (!err)
7842 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007843 return err;
7844}
7845
7846static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7847{
Johannes Berg4c476992010-10-04 21:36:35 +02007848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007849 enum nl80211_ps_state ps_state;
7850 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007851 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007852 struct sk_buff *msg;
7853 void *hdr;
7854 int err;
7855
Kalle Valoffb9eb32010-02-17 17:58:10 +02007856 wdev = dev->ieee80211_ptr;
7857
Johannes Berg4c476992010-10-04 21:36:35 +02007858 if (!rdev->ops->set_power_mgmt)
7859 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007860
7861 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007862 if (!msg)
7863 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007864
Eric W. Biederman15e47302012-09-07 20:12:54 +00007865 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007866 NL80211_CMD_GET_POWER_SAVE);
7867 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007868 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007869 goto free_msg;
7870 }
7871
7872 if (wdev->ps)
7873 ps_state = NL80211_PS_ENABLED;
7874 else
7875 ps_state = NL80211_PS_DISABLED;
7876
David S. Miller9360ffd2012-03-29 04:41:26 -04007877 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7878 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007879
7880 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007881 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007882
Johannes Berg4c476992010-10-04 21:36:35 +02007883 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007884 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007885 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007886 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007887 return err;
7888}
7889
Johannes Berg94e860f2014-01-20 23:58:15 +01007890static const struct nla_policy
7891nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007892 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7893 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7894 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007895 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7896 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7897 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007898};
7899
Thomas Pedersen84f10702012-07-12 16:17:33 -07007900static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007901 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007902{
7903 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007904 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007905 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007906
Johannes Bergd9d8b012012-11-26 12:51:52 +01007907 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007908 return -EINVAL;
7909
Thomas Pedersen84f10702012-07-12 16:17:33 -07007910 if (!rdev->ops->set_cqm_txe_config)
7911 return -EOPNOTSUPP;
7912
7913 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7914 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7915 return -EOPNOTSUPP;
7916
Hila Gonene35e4d22012-06-27 17:19:42 +03007917 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007918}
7919
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007920static int nl80211_set_cqm_rssi(struct genl_info *info,
7921 s32 threshold, u32 hysteresis)
7922{
Johannes Berg4c476992010-10-04 21:36:35 +02007923 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007924 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007925 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007926
7927 if (threshold > 0)
7928 return -EINVAL;
7929
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007930 /* disabling - hysteresis should also be zero then */
7931 if (threshold == 0)
7932 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007933
Johannes Berg4c476992010-10-04 21:36:35 +02007934 if (!rdev->ops->set_cqm_rssi_config)
7935 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007936
Johannes Berg074ac8d2010-09-16 14:58:22 +02007937 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007938 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7939 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007940
Hila Gonene35e4d22012-06-27 17:19:42 +03007941 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007942}
7943
7944static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7945{
7946 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7947 struct nlattr *cqm;
7948 int err;
7949
7950 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007951 if (!cqm)
7952 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007953
7954 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7955 nl80211_attr_cqm_policy);
7956 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007957 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007958
7959 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7960 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007961 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7962 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007963
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007964 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7965 }
7966
7967 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7968 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7969 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7970 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7971 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7972 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7973
7974 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7975 }
7976
7977 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007978}
7979
Johannes Berg29cbe682010-12-03 09:20:44 +01007980static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7981{
7982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7983 struct net_device *dev = info->user_ptr[1];
7984 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007985 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007986 int err;
7987
7988 /* start with default */
7989 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007990 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007991
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007992 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007993 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007994 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007995 if (err)
7996 return err;
7997 }
7998
7999 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8000 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8001 return -EINVAL;
8002
Javier Cardonac80d5452010-12-16 17:37:49 -08008003 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8004 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8005
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008006 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8007 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8008 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8009 return -EINVAL;
8010
Marco Porsch9bdbf042013-01-07 16:04:51 +01008011 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8012 setup.beacon_interval =
8013 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8014 if (setup.beacon_interval < 10 ||
8015 setup.beacon_interval > 10000)
8016 return -EINVAL;
8017 }
8018
8019 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8020 setup.dtim_period =
8021 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8022 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8023 return -EINVAL;
8024 }
8025
Javier Cardonac80d5452010-12-16 17:37:49 -08008026 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8027 /* parse additional setup parameters if given */
8028 err = nl80211_parse_mesh_setup(info, &setup);
8029 if (err)
8030 return err;
8031 }
8032
Thomas Pedersend37bb182013-03-04 13:06:13 -08008033 if (setup.user_mpm)
8034 cfg.auto_open_plinks = false;
8035
Johannes Bergcc1d2802012-05-16 23:50:20 +02008036 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008037 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8038 if (err)
8039 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008040 } else {
8041 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008042 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008043 }
8044
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008045 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8046 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8047 int n_rates =
8048 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8049 struct ieee80211_supported_band *sband;
8050
8051 if (!setup.chandef.chan)
8052 return -EINVAL;
8053
8054 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8055
8056 err = ieee80211_get_ratemask(sband, rates, n_rates,
8057 &setup.basic_rates);
8058 if (err)
8059 return err;
8060 }
8061
Javier Cardonac80d5452010-12-16 17:37:49 -08008062 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008063}
8064
8065static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8066{
8067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8068 struct net_device *dev = info->user_ptr[1];
8069
8070 return cfg80211_leave_mesh(rdev, dev);
8071}
8072
Johannes Bergdfb89c52012-06-27 09:23:48 +02008073#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008074static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8075 struct cfg80211_registered_device *rdev)
8076{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008077 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008078 struct nlattr *nl_pats, *nl_pat;
8079 int i, pat_len;
8080
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008081 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008082 return 0;
8083
8084 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8085 if (!nl_pats)
8086 return -ENOBUFS;
8087
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008088 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008089 nl_pat = nla_nest_start(msg, i + 1);
8090 if (!nl_pat)
8091 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008092 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008093 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008094 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008095 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8096 wowlan->patterns[i].pattern) ||
8097 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008098 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008099 return -ENOBUFS;
8100 nla_nest_end(msg, nl_pat);
8101 }
8102 nla_nest_end(msg, nl_pats);
8103
8104 return 0;
8105}
8106
Johannes Berg2a0e0472013-01-23 22:57:40 +01008107static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8108 struct cfg80211_wowlan_tcp *tcp)
8109{
8110 struct nlattr *nl_tcp;
8111
8112 if (!tcp)
8113 return 0;
8114
8115 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8116 if (!nl_tcp)
8117 return -ENOBUFS;
8118
8119 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8120 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8121 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8122 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8123 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8124 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8125 tcp->payload_len, tcp->payload) ||
8126 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8127 tcp->data_interval) ||
8128 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8129 tcp->wake_len, tcp->wake_data) ||
8130 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8131 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8132 return -ENOBUFS;
8133
8134 if (tcp->payload_seq.len &&
8135 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8136 sizeof(tcp->payload_seq), &tcp->payload_seq))
8137 return -ENOBUFS;
8138
8139 if (tcp->payload_tok.len &&
8140 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8141 sizeof(tcp->payload_tok) + tcp->tokens_size,
8142 &tcp->payload_tok))
8143 return -ENOBUFS;
8144
Johannes Berge248ad32013-05-16 10:24:28 +02008145 nla_nest_end(msg, nl_tcp);
8146
Johannes Berg2a0e0472013-01-23 22:57:40 +01008147 return 0;
8148}
8149
Johannes Bergff1b6e62011-05-04 15:37:28 +02008150static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8151{
8152 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8153 struct sk_buff *msg;
8154 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008155 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008156
Johannes Berg964dc9e2013-06-03 17:25:34 +02008157 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008158 return -EOPNOTSUPP;
8159
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008160 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008161 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008162 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8163 rdev->wiphy.wowlan_config->tcp->payload_len +
8164 rdev->wiphy.wowlan_config->tcp->wake_len +
8165 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008166 }
8167
8168 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008169 if (!msg)
8170 return -ENOMEM;
8171
Eric W. Biederman15e47302012-09-07 20:12:54 +00008172 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008173 NL80211_CMD_GET_WOWLAN);
8174 if (!hdr)
8175 goto nla_put_failure;
8176
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008177 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008178 struct nlattr *nl_wowlan;
8179
8180 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8181 if (!nl_wowlan)
8182 goto nla_put_failure;
8183
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008184 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008185 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008186 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008187 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008188 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008189 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008190 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008191 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008192 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008193 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008194 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008195 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008196 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008197 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8198 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008199
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008200 if (nl80211_send_wowlan_patterns(msg, rdev))
8201 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008202
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008203 if (nl80211_send_wowlan_tcp(msg,
8204 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008205 goto nla_put_failure;
8206
Johannes Bergff1b6e62011-05-04 15:37:28 +02008207 nla_nest_end(msg, nl_wowlan);
8208 }
8209
8210 genlmsg_end(msg, hdr);
8211 return genlmsg_reply(msg, info);
8212
8213nla_put_failure:
8214 nlmsg_free(msg);
8215 return -ENOBUFS;
8216}
8217
Johannes Berg2a0e0472013-01-23 22:57:40 +01008218static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8219 struct nlattr *attr,
8220 struct cfg80211_wowlan *trig)
8221{
8222 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8223 struct cfg80211_wowlan_tcp *cfg;
8224 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8225 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8226 u32 size;
8227 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8228 int err, port;
8229
Johannes Berg964dc9e2013-06-03 17:25:34 +02008230 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008231 return -EINVAL;
8232
8233 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8234 nla_data(attr), nla_len(attr),
8235 nl80211_wowlan_tcp_policy);
8236 if (err)
8237 return err;
8238
8239 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8240 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8241 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8242 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8243 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8244 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8245 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8246 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8247 return -EINVAL;
8248
8249 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008250 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008251 return -EINVAL;
8252
8253 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008254 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008255 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008256 return -EINVAL;
8257
8258 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008259 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008260 return -EINVAL;
8261
8262 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8263 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8264 return -EINVAL;
8265
8266 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8267 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8268
8269 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8270 tokens_size = tokln - sizeof(*tok);
8271
8272 if (!tok->len || tokens_size % tok->len)
8273 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008274 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008275 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008276 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008277 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008278 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008279 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008280 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008281 return -EINVAL;
8282 if (tok->offset + tok->len > data_size)
8283 return -EINVAL;
8284 }
8285
8286 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8287 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008288 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008289 return -EINVAL;
8290 if (seq->len == 0 || seq->len > 4)
8291 return -EINVAL;
8292 if (seq->len + seq->offset > data_size)
8293 return -EINVAL;
8294 }
8295
8296 size = sizeof(*cfg);
8297 size += data_size;
8298 size += wake_size + wake_mask_size;
8299 size += tokens_size;
8300
8301 cfg = kzalloc(size, GFP_KERNEL);
8302 if (!cfg)
8303 return -ENOMEM;
8304 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8305 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8306 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8307 ETH_ALEN);
8308 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8309 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8310 else
8311 port = 0;
8312#ifdef CONFIG_INET
8313 /* allocate a socket and port for it and use it */
8314 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8315 IPPROTO_TCP, &cfg->sock, 1);
8316 if (err) {
8317 kfree(cfg);
8318 return err;
8319 }
8320 if (inet_csk_get_port(cfg->sock->sk, port)) {
8321 sock_release(cfg->sock);
8322 kfree(cfg);
8323 return -EADDRINUSE;
8324 }
8325 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8326#else
8327 if (!port) {
8328 kfree(cfg);
8329 return -EINVAL;
8330 }
8331 cfg->src_port = port;
8332#endif
8333
8334 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8335 cfg->payload_len = data_size;
8336 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8337 memcpy((void *)cfg->payload,
8338 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8339 data_size);
8340 if (seq)
8341 cfg->payload_seq = *seq;
8342 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8343 cfg->wake_len = wake_size;
8344 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8345 memcpy((void *)cfg->wake_data,
8346 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8347 wake_size);
8348 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8349 data_size + wake_size;
8350 memcpy((void *)cfg->wake_mask,
8351 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8352 wake_mask_size);
8353 if (tok) {
8354 cfg->tokens_size = tokens_size;
8355 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8356 }
8357
8358 trig->tcp = cfg;
8359
8360 return 0;
8361}
8362
Johannes Bergff1b6e62011-05-04 15:37:28 +02008363static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8364{
8365 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8366 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008367 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008368 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008369 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008370 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008371 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008372
Johannes Berg964dc9e2013-06-03 17:25:34 +02008373 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008374 return -EOPNOTSUPP;
8375
Johannes Bergae33bd82012-07-12 16:25:02 +02008376 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8377 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008378 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008379 goto set_wakeup;
8380 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008381
8382 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8383 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8384 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8385 nl80211_wowlan_policy);
8386 if (err)
8387 return err;
8388
8389 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8390 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8391 return -EINVAL;
8392 new_triggers.any = true;
8393 }
8394
8395 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8396 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8397 return -EINVAL;
8398 new_triggers.disconnect = true;
8399 }
8400
8401 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8402 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8403 return -EINVAL;
8404 new_triggers.magic_pkt = true;
8405 }
8406
Johannes Berg77dbbb12011-07-13 10:48:55 +02008407 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8408 return -EINVAL;
8409
8410 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8411 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8412 return -EINVAL;
8413 new_triggers.gtk_rekey_failure = true;
8414 }
8415
8416 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8417 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8418 return -EINVAL;
8419 new_triggers.eap_identity_req = true;
8420 }
8421
8422 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8423 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8424 return -EINVAL;
8425 new_triggers.four_way_handshake = true;
8426 }
8427
8428 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8429 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8430 return -EINVAL;
8431 new_triggers.rfkill_release = true;
8432 }
8433
Johannes Bergff1b6e62011-05-04 15:37:28 +02008434 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8435 struct nlattr *pat;
8436 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008437 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008438 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008439
8440 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8441 rem)
8442 n_patterns++;
8443 if (n_patterns > wowlan->n_patterns)
8444 return -EINVAL;
8445
8446 new_triggers.patterns = kcalloc(n_patterns,
8447 sizeof(new_triggers.patterns[0]),
8448 GFP_KERNEL);
8449 if (!new_triggers.patterns)
8450 return -ENOMEM;
8451
8452 new_triggers.n_patterns = n_patterns;
8453 i = 0;
8454
8455 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8456 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008457 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8458 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008459 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008460 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8461 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008462 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008463 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008464 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008465 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008466 goto error;
8467 if (pat_len > wowlan->pattern_max_len ||
8468 pat_len < wowlan->pattern_min_len)
8469 goto error;
8470
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008471 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008472 pkt_offset = 0;
8473 else
8474 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008475 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008476 if (pkt_offset > wowlan->max_pkt_offset)
8477 goto error;
8478 new_triggers.patterns[i].pkt_offset = pkt_offset;
8479
Johannes Bergff1b6e62011-05-04 15:37:28 +02008480 new_triggers.patterns[i].mask =
8481 kmalloc(mask_len + pat_len, GFP_KERNEL);
8482 if (!new_triggers.patterns[i].mask) {
8483 err = -ENOMEM;
8484 goto error;
8485 }
8486 new_triggers.patterns[i].pattern =
8487 new_triggers.patterns[i].mask + mask_len;
8488 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008489 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008490 mask_len);
8491 new_triggers.patterns[i].pattern_len = pat_len;
8492 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008493 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008494 pat_len);
8495 i++;
8496 }
8497 }
8498
Johannes Berg2a0e0472013-01-23 22:57:40 +01008499 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8500 err = nl80211_parse_wowlan_tcp(
8501 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8502 &new_triggers);
8503 if (err)
8504 goto error;
8505 }
8506
Johannes Bergae33bd82012-07-12 16:25:02 +02008507 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8508 if (!ntrig) {
8509 err = -ENOMEM;
8510 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008511 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008512 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008513 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008514
Johannes Bergae33bd82012-07-12 16:25:02 +02008515 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008516 if (rdev->ops->set_wakeup &&
8517 prev_enabled != !!rdev->wiphy.wowlan_config)
8518 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008519
Johannes Bergff1b6e62011-05-04 15:37:28 +02008520 return 0;
8521 error:
8522 for (i = 0; i < new_triggers.n_patterns; i++)
8523 kfree(new_triggers.patterns[i].mask);
8524 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008525 if (new_triggers.tcp && new_triggers.tcp->sock)
8526 sock_release(new_triggers.tcp->sock);
8527 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008528 return err;
8529}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008530#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008531
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008532static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8533 struct cfg80211_registered_device *rdev)
8534{
8535 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8536 int i, j, pat_len;
8537 struct cfg80211_coalesce_rules *rule;
8538
8539 if (!rdev->coalesce->n_rules)
8540 return 0;
8541
8542 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8543 if (!nl_rules)
8544 return -ENOBUFS;
8545
8546 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8547 nl_rule = nla_nest_start(msg, i + 1);
8548 if (!nl_rule)
8549 return -ENOBUFS;
8550
8551 rule = &rdev->coalesce->rules[i];
8552 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8553 rule->delay))
8554 return -ENOBUFS;
8555
8556 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8557 rule->condition))
8558 return -ENOBUFS;
8559
8560 nl_pats = nla_nest_start(msg,
8561 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8562 if (!nl_pats)
8563 return -ENOBUFS;
8564
8565 for (j = 0; j < rule->n_patterns; j++) {
8566 nl_pat = nla_nest_start(msg, j + 1);
8567 if (!nl_pat)
8568 return -ENOBUFS;
8569 pat_len = rule->patterns[j].pattern_len;
8570 if (nla_put(msg, NL80211_PKTPAT_MASK,
8571 DIV_ROUND_UP(pat_len, 8),
8572 rule->patterns[j].mask) ||
8573 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8574 rule->patterns[j].pattern) ||
8575 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8576 rule->patterns[j].pkt_offset))
8577 return -ENOBUFS;
8578 nla_nest_end(msg, nl_pat);
8579 }
8580 nla_nest_end(msg, nl_pats);
8581 nla_nest_end(msg, nl_rule);
8582 }
8583 nla_nest_end(msg, nl_rules);
8584
8585 return 0;
8586}
8587
8588static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8589{
8590 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8591 struct sk_buff *msg;
8592 void *hdr;
8593
8594 if (!rdev->wiphy.coalesce)
8595 return -EOPNOTSUPP;
8596
8597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8598 if (!msg)
8599 return -ENOMEM;
8600
8601 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8602 NL80211_CMD_GET_COALESCE);
8603 if (!hdr)
8604 goto nla_put_failure;
8605
8606 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8607 goto nla_put_failure;
8608
8609 genlmsg_end(msg, hdr);
8610 return genlmsg_reply(msg, info);
8611
8612nla_put_failure:
8613 nlmsg_free(msg);
8614 return -ENOBUFS;
8615}
8616
8617void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8618{
8619 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8620 int i, j;
8621 struct cfg80211_coalesce_rules *rule;
8622
8623 if (!coalesce)
8624 return;
8625
8626 for (i = 0; i < coalesce->n_rules; i++) {
8627 rule = &coalesce->rules[i];
8628 for (j = 0; j < rule->n_patterns; j++)
8629 kfree(rule->patterns[j].mask);
8630 kfree(rule->patterns);
8631 }
8632 kfree(coalesce->rules);
8633 kfree(coalesce);
8634 rdev->coalesce = NULL;
8635}
8636
8637static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8638 struct nlattr *rule,
8639 struct cfg80211_coalesce_rules *new_rule)
8640{
8641 int err, i;
8642 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8643 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8644 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8645 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8646
8647 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8648 nla_len(rule), nl80211_coalesce_policy);
8649 if (err)
8650 return err;
8651
8652 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8653 new_rule->delay =
8654 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8655 if (new_rule->delay > coalesce->max_delay)
8656 return -EINVAL;
8657
8658 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8659 new_rule->condition =
8660 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8661 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8662 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8663 return -EINVAL;
8664
8665 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8666 return -EINVAL;
8667
8668 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8669 rem)
8670 n_patterns++;
8671 if (n_patterns > coalesce->n_patterns)
8672 return -EINVAL;
8673
8674 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8675 GFP_KERNEL);
8676 if (!new_rule->patterns)
8677 return -ENOMEM;
8678
8679 new_rule->n_patterns = n_patterns;
8680 i = 0;
8681
8682 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8683 rem) {
8684 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8685 nla_len(pat), NULL);
8686 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8687 !pat_tb[NL80211_PKTPAT_PATTERN])
8688 return -EINVAL;
8689 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8690 mask_len = DIV_ROUND_UP(pat_len, 8);
8691 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8692 return -EINVAL;
8693 if (pat_len > coalesce->pattern_max_len ||
8694 pat_len < coalesce->pattern_min_len)
8695 return -EINVAL;
8696
8697 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8698 pkt_offset = 0;
8699 else
8700 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8701 if (pkt_offset > coalesce->max_pkt_offset)
8702 return -EINVAL;
8703 new_rule->patterns[i].pkt_offset = pkt_offset;
8704
8705 new_rule->patterns[i].mask =
8706 kmalloc(mask_len + pat_len, GFP_KERNEL);
8707 if (!new_rule->patterns[i].mask)
8708 return -ENOMEM;
8709 new_rule->patterns[i].pattern =
8710 new_rule->patterns[i].mask + mask_len;
8711 memcpy(new_rule->patterns[i].mask,
8712 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8713 new_rule->patterns[i].pattern_len = pat_len;
8714 memcpy(new_rule->patterns[i].pattern,
8715 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8716 i++;
8717 }
8718
8719 return 0;
8720}
8721
8722static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8723{
8724 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8725 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8726 struct cfg80211_coalesce new_coalesce = {};
8727 struct cfg80211_coalesce *n_coalesce;
8728 int err, rem_rule, n_rules = 0, i, j;
8729 struct nlattr *rule;
8730 struct cfg80211_coalesce_rules *tmp_rule;
8731
8732 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8733 return -EOPNOTSUPP;
8734
8735 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8736 cfg80211_rdev_free_coalesce(rdev);
8737 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8738 return 0;
8739 }
8740
8741 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8742 rem_rule)
8743 n_rules++;
8744 if (n_rules > coalesce->n_rules)
8745 return -EINVAL;
8746
8747 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8748 GFP_KERNEL);
8749 if (!new_coalesce.rules)
8750 return -ENOMEM;
8751
8752 new_coalesce.n_rules = n_rules;
8753 i = 0;
8754
8755 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8756 rem_rule) {
8757 err = nl80211_parse_coalesce_rule(rdev, rule,
8758 &new_coalesce.rules[i]);
8759 if (err)
8760 goto error;
8761
8762 i++;
8763 }
8764
8765 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8766 if (err)
8767 goto error;
8768
8769 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8770 if (!n_coalesce) {
8771 err = -ENOMEM;
8772 goto error;
8773 }
8774 cfg80211_rdev_free_coalesce(rdev);
8775 rdev->coalesce = n_coalesce;
8776
8777 return 0;
8778error:
8779 for (i = 0; i < new_coalesce.n_rules; i++) {
8780 tmp_rule = &new_coalesce.rules[i];
8781 for (j = 0; j < tmp_rule->n_patterns; j++)
8782 kfree(tmp_rule->patterns[j].mask);
8783 kfree(tmp_rule->patterns);
8784 }
8785 kfree(new_coalesce.rules);
8786
8787 return err;
8788}
8789
Johannes Berge5497d72011-07-05 16:35:40 +02008790static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8791{
8792 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8793 struct net_device *dev = info->user_ptr[1];
8794 struct wireless_dev *wdev = dev->ieee80211_ptr;
8795 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8796 struct cfg80211_gtk_rekey_data rekey_data;
8797 int err;
8798
8799 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8800 return -EINVAL;
8801
8802 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8803 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8804 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8805 nl80211_rekey_policy);
8806 if (err)
8807 return err;
8808
8809 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8810 return -ERANGE;
8811 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8812 return -ERANGE;
8813 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8814 return -ERANGE;
8815
8816 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8817 NL80211_KEK_LEN);
8818 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8819 NL80211_KCK_LEN);
8820 memcpy(rekey_data.replay_ctr,
8821 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8822 NL80211_REPLAY_CTR_LEN);
8823
8824 wdev_lock(wdev);
8825 if (!wdev->current_bss) {
8826 err = -ENOTCONN;
8827 goto out;
8828 }
8829
8830 if (!rdev->ops->set_rekey_data) {
8831 err = -EOPNOTSUPP;
8832 goto out;
8833 }
8834
Hila Gonene35e4d22012-06-27 17:19:42 +03008835 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008836 out:
8837 wdev_unlock(wdev);
8838 return err;
8839}
8840
Johannes Berg28946da2011-11-04 11:18:12 +01008841static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8842 struct genl_info *info)
8843{
8844 struct net_device *dev = info->user_ptr[1];
8845 struct wireless_dev *wdev = dev->ieee80211_ptr;
8846
8847 if (wdev->iftype != NL80211_IFTYPE_AP &&
8848 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8849 return -EINVAL;
8850
Eric W. Biederman15e47302012-09-07 20:12:54 +00008851 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008852 return -EBUSY;
8853
Eric W. Biederman15e47302012-09-07 20:12:54 +00008854 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008855 return 0;
8856}
8857
Johannes Berg7f6cf312011-11-04 11:18:15 +01008858static int nl80211_probe_client(struct sk_buff *skb,
8859 struct genl_info *info)
8860{
8861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8862 struct net_device *dev = info->user_ptr[1];
8863 struct wireless_dev *wdev = dev->ieee80211_ptr;
8864 struct sk_buff *msg;
8865 void *hdr;
8866 const u8 *addr;
8867 u64 cookie;
8868 int err;
8869
8870 if (wdev->iftype != NL80211_IFTYPE_AP &&
8871 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8872 return -EOPNOTSUPP;
8873
8874 if (!info->attrs[NL80211_ATTR_MAC])
8875 return -EINVAL;
8876
8877 if (!rdev->ops->probe_client)
8878 return -EOPNOTSUPP;
8879
8880 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8881 if (!msg)
8882 return -ENOMEM;
8883
Eric W. Biederman15e47302012-09-07 20:12:54 +00008884 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008885 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008886 if (!hdr) {
8887 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008888 goto free_msg;
8889 }
8890
8891 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8892
Hila Gonene35e4d22012-06-27 17:19:42 +03008893 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008894 if (err)
8895 goto free_msg;
8896
David S. Miller9360ffd2012-03-29 04:41:26 -04008897 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8898 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008899
8900 genlmsg_end(msg, hdr);
8901
8902 return genlmsg_reply(msg, info);
8903
8904 nla_put_failure:
8905 err = -ENOBUFS;
8906 free_msg:
8907 nlmsg_free(msg);
8908 return err;
8909}
8910
Johannes Berg5e7602302011-11-04 11:18:17 +01008911static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8912{
8913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008914 struct cfg80211_beacon_registration *reg, *nreg;
8915 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008916
8917 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8918 return -EOPNOTSUPP;
8919
Ben Greear37c73b52012-10-26 14:49:25 -07008920 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8921 if (!nreg)
8922 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008923
Ben Greear37c73b52012-10-26 14:49:25 -07008924 /* First, check if already registered. */
8925 spin_lock_bh(&rdev->beacon_registrations_lock);
8926 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8927 if (reg->nlportid == info->snd_portid) {
8928 rv = -EALREADY;
8929 goto out_err;
8930 }
8931 }
8932 /* Add it to the list */
8933 nreg->nlportid = info->snd_portid;
8934 list_add(&nreg->list, &rdev->beacon_registrations);
8935
8936 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008937
8938 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008939out_err:
8940 spin_unlock_bh(&rdev->beacon_registrations_lock);
8941 kfree(nreg);
8942 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008943}
8944
Johannes Berg98104fde2012-06-16 00:19:54 +02008945static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8946{
8947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8948 struct wireless_dev *wdev = info->user_ptr[1];
8949 int err;
8950
8951 if (!rdev->ops->start_p2p_device)
8952 return -EOPNOTSUPP;
8953
8954 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8955 return -EOPNOTSUPP;
8956
8957 if (wdev->p2p_started)
8958 return 0;
8959
Johannes Berg98104fde2012-06-16 00:19:54 +02008960 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008961 if (err)
8962 return err;
8963
Johannes Bergeeb126e2012-10-23 15:16:50 +02008964 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008965 if (err)
8966 return err;
8967
8968 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008969 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008970
8971 return 0;
8972}
8973
8974static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8975{
8976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8977 struct wireless_dev *wdev = info->user_ptr[1];
8978
8979 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8980 return -EOPNOTSUPP;
8981
8982 if (!rdev->ops->stop_p2p_device)
8983 return -EOPNOTSUPP;
8984
Johannes Bergf9f47522013-03-19 15:04:07 +01008985 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008986
8987 return 0;
8988}
8989
Johannes Berg3713b4e2013-02-14 16:19:38 +01008990static int nl80211_get_protocol_features(struct sk_buff *skb,
8991 struct genl_info *info)
8992{
8993 void *hdr;
8994 struct sk_buff *msg;
8995
8996 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8997 if (!msg)
8998 return -ENOMEM;
8999
9000 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9001 NL80211_CMD_GET_PROTOCOL_FEATURES);
9002 if (!hdr)
9003 goto nla_put_failure;
9004
9005 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9006 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9007 goto nla_put_failure;
9008
9009 genlmsg_end(msg, hdr);
9010 return genlmsg_reply(msg, info);
9011
9012 nla_put_failure:
9013 kfree_skb(msg);
9014 return -ENOBUFS;
9015}
9016
Jouni Malinen355199e2013-02-27 17:14:27 +02009017static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9018{
9019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9020 struct cfg80211_update_ft_ies_params ft_params;
9021 struct net_device *dev = info->user_ptr[1];
9022
9023 if (!rdev->ops->update_ft_ies)
9024 return -EOPNOTSUPP;
9025
9026 if (!info->attrs[NL80211_ATTR_MDID] ||
9027 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9028 return -EINVAL;
9029
9030 memset(&ft_params, 0, sizeof(ft_params));
9031 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9032 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9033 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9034
9035 return rdev_update_ft_ies(rdev, dev, &ft_params);
9036}
9037
Arend van Spriel5de17982013-04-18 15:49:00 +02009038static int nl80211_crit_protocol_start(struct sk_buff *skb,
9039 struct genl_info *info)
9040{
9041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9042 struct wireless_dev *wdev = info->user_ptr[1];
9043 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9044 u16 duration;
9045 int ret;
9046
9047 if (!rdev->ops->crit_proto_start)
9048 return -EOPNOTSUPP;
9049
9050 if (WARN_ON(!rdev->ops->crit_proto_stop))
9051 return -EINVAL;
9052
9053 if (rdev->crit_proto_nlportid)
9054 return -EBUSY;
9055
9056 /* determine protocol if provided */
9057 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9058 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9059
9060 if (proto >= NUM_NL80211_CRIT_PROTO)
9061 return -EINVAL;
9062
9063 /* timeout must be provided */
9064 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9065 return -EINVAL;
9066
9067 duration =
9068 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9069
9070 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9071 return -ERANGE;
9072
9073 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9074 if (!ret)
9075 rdev->crit_proto_nlportid = info->snd_portid;
9076
9077 return ret;
9078}
9079
9080static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9081 struct genl_info *info)
9082{
9083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9084 struct wireless_dev *wdev = info->user_ptr[1];
9085
9086 if (!rdev->ops->crit_proto_stop)
9087 return -EOPNOTSUPP;
9088
9089 if (rdev->crit_proto_nlportid) {
9090 rdev->crit_proto_nlportid = 0;
9091 rdev_crit_proto_stop(rdev, wdev);
9092 }
9093 return 0;
9094}
9095
Johannes Bergad7e7182013-11-13 13:37:47 +01009096static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9097{
9098 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9099 struct wireless_dev *wdev =
9100 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9101 int i, err;
9102 u32 vid, subcmd;
9103
9104 if (!rdev->wiphy.vendor_commands)
9105 return -EOPNOTSUPP;
9106
9107 if (IS_ERR(wdev)) {
9108 err = PTR_ERR(wdev);
9109 if (err != -EINVAL)
9110 return err;
9111 wdev = NULL;
9112 } else if (wdev->wiphy != &rdev->wiphy) {
9113 return -EINVAL;
9114 }
9115
9116 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9117 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9118 return -EINVAL;
9119
9120 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9121 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9122 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9123 const struct wiphy_vendor_command *vcmd;
9124 void *data = NULL;
9125 int len = 0;
9126
9127 vcmd = &rdev->wiphy.vendor_commands[i];
9128
9129 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9130 continue;
9131
9132 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9133 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9134 if (!wdev)
9135 return -EINVAL;
9136 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9137 !wdev->netdev)
9138 return -EINVAL;
9139
9140 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9141 if (wdev->netdev &&
9142 !netif_running(wdev->netdev))
9143 return -ENETDOWN;
9144 if (!wdev->netdev && !wdev->p2p_started)
9145 return -ENETDOWN;
9146 }
9147 } else {
9148 wdev = NULL;
9149 }
9150
9151 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9152 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9153 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9154 }
9155
9156 rdev->cur_cmd_info = info;
9157 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9158 data, len);
9159 rdev->cur_cmd_info = NULL;
9160 return err;
9161 }
9162
9163 return -EOPNOTSUPP;
9164}
9165
9166struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9167 enum nl80211_commands cmd,
9168 enum nl80211_attrs attr,
9169 int approxlen)
9170{
9171 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9172
9173 if (WARN_ON(!rdev->cur_cmd_info))
9174 return NULL;
9175
9176 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9177 rdev->cur_cmd_info->snd_portid,
9178 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009179 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009180}
9181EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9182
9183int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9184{
9185 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9186 void *hdr = ((void **)skb->cb)[1];
9187 struct nlattr *data = ((void **)skb->cb)[2];
9188
9189 if (WARN_ON(!rdev->cur_cmd_info)) {
9190 kfree_skb(skb);
9191 return -EINVAL;
9192 }
9193
9194 nla_nest_end(skb, data);
9195 genlmsg_end(skb, hdr);
9196 return genlmsg_reply(skb, rdev->cur_cmd_info);
9197}
9198EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9199
9200
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009201static int nl80211_set_qos_map(struct sk_buff *skb,
9202 struct genl_info *info)
9203{
9204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9205 struct cfg80211_qos_map *qos_map = NULL;
9206 struct net_device *dev = info->user_ptr[1];
9207 u8 *pos, len, num_des, des_len, des;
9208 int ret;
9209
9210 if (!rdev->ops->set_qos_map)
9211 return -EOPNOTSUPP;
9212
9213 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9214 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9215 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9216
9217 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9218 len > IEEE80211_QOS_MAP_LEN_MAX)
9219 return -EINVAL;
9220
9221 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9222 if (!qos_map)
9223 return -ENOMEM;
9224
9225 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9226 if (num_des) {
9227 des_len = num_des *
9228 sizeof(struct cfg80211_dscp_exception);
9229 memcpy(qos_map->dscp_exception, pos, des_len);
9230 qos_map->num_des = num_des;
9231 for (des = 0; des < num_des; des++) {
9232 if (qos_map->dscp_exception[des].up > 7) {
9233 kfree(qos_map);
9234 return -EINVAL;
9235 }
9236 }
9237 pos += des_len;
9238 }
9239 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9240 }
9241
9242 wdev_lock(dev->ieee80211_ptr);
9243 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9244 if (!ret)
9245 ret = rdev_set_qos_map(rdev, dev, qos_map);
9246 wdev_unlock(dev->ieee80211_ptr);
9247
9248 kfree(qos_map);
9249 return ret;
9250}
9251
Johannes Berg4c476992010-10-04 21:36:35 +02009252#define NL80211_FLAG_NEED_WIPHY 0x01
9253#define NL80211_FLAG_NEED_NETDEV 0x02
9254#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009255#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9256#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9257 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009258#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009259/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009260#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9261 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009262
Johannes Bergf84f7712013-11-14 17:14:45 +01009263static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009264 struct genl_info *info)
9265{
9266 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009267 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009268 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009269 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9270
9271 if (rtnl)
9272 rtnl_lock();
9273
9274 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009275 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009276 if (IS_ERR(rdev)) {
9277 if (rtnl)
9278 rtnl_unlock();
9279 return PTR_ERR(rdev);
9280 }
9281 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009282 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9283 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009284 ASSERT_RTNL();
9285
Johannes Berg89a54e42012-06-15 14:33:17 +02009286 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9287 info->attrs);
9288 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009289 if (rtnl)
9290 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009291 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009292 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009293
Johannes Berg89a54e42012-06-15 14:33:17 +02009294 dev = wdev->netdev;
9295 rdev = wiphy_to_dev(wdev->wiphy);
9296
Johannes Berg1bf614e2012-06-15 15:23:36 +02009297 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9298 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009299 if (rtnl)
9300 rtnl_unlock();
9301 return -EINVAL;
9302 }
9303
9304 info->user_ptr[1] = dev;
9305 } else {
9306 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009307 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009308
Johannes Berg1bf614e2012-06-15 15:23:36 +02009309 if (dev) {
9310 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9311 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009312 if (rtnl)
9313 rtnl_unlock();
9314 return -ENETDOWN;
9315 }
9316
9317 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009318 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9319 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009320 if (rtnl)
9321 rtnl_unlock();
9322 return -ENETDOWN;
9323 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009324 }
9325
Johannes Berg4c476992010-10-04 21:36:35 +02009326 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009327 }
9328
9329 return 0;
9330}
9331
Johannes Bergf84f7712013-11-14 17:14:45 +01009332static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009333 struct genl_info *info)
9334{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009335 if (info->user_ptr[1]) {
9336 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9337 struct wireless_dev *wdev = info->user_ptr[1];
9338
9339 if (wdev->netdev)
9340 dev_put(wdev->netdev);
9341 } else {
9342 dev_put(info->user_ptr[1]);
9343 }
9344 }
Johannes Berg4c476992010-10-04 21:36:35 +02009345 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9346 rtnl_unlock();
9347}
9348
Johannes Berg4534de82013-11-14 17:14:46 +01009349static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009350 {
9351 .cmd = NL80211_CMD_GET_WIPHY,
9352 .doit = nl80211_get_wiphy,
9353 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009354 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009355 .policy = nl80211_policy,
9356 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009357 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9358 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009359 },
9360 {
9361 .cmd = NL80211_CMD_SET_WIPHY,
9362 .doit = nl80211_set_wiphy,
9363 .policy = nl80211_policy,
9364 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009365 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009366 },
9367 {
9368 .cmd = NL80211_CMD_GET_INTERFACE,
9369 .doit = nl80211_get_interface,
9370 .dumpit = nl80211_dump_interface,
9371 .policy = nl80211_policy,
9372 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009373 .internal_flags = NL80211_FLAG_NEED_WDEV |
9374 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009375 },
9376 {
9377 .cmd = NL80211_CMD_SET_INTERFACE,
9378 .doit = nl80211_set_interface,
9379 .policy = nl80211_policy,
9380 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009381 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9382 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009383 },
9384 {
9385 .cmd = NL80211_CMD_NEW_INTERFACE,
9386 .doit = nl80211_new_interface,
9387 .policy = nl80211_policy,
9388 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009389 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9390 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009391 },
9392 {
9393 .cmd = NL80211_CMD_DEL_INTERFACE,
9394 .doit = nl80211_del_interface,
9395 .policy = nl80211_policy,
9396 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009397 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009398 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009399 },
Johannes Berg41ade002007-12-19 02:03:29 +01009400 {
9401 .cmd = NL80211_CMD_GET_KEY,
9402 .doit = nl80211_get_key,
9403 .policy = nl80211_policy,
9404 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009405 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009406 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009407 },
9408 {
9409 .cmd = NL80211_CMD_SET_KEY,
9410 .doit = nl80211_set_key,
9411 .policy = nl80211_policy,
9412 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009413 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009414 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009415 },
9416 {
9417 .cmd = NL80211_CMD_NEW_KEY,
9418 .doit = nl80211_new_key,
9419 .policy = nl80211_policy,
9420 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009421 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009422 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009423 },
9424 {
9425 .cmd = NL80211_CMD_DEL_KEY,
9426 .doit = nl80211_del_key,
9427 .policy = nl80211_policy,
9428 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009429 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009430 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009431 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009432 {
9433 .cmd = NL80211_CMD_SET_BEACON,
9434 .policy = nl80211_policy,
9435 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009436 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009437 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009438 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009439 },
9440 {
Johannes Berg88600202012-02-13 15:17:18 +01009441 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009442 .policy = nl80211_policy,
9443 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009444 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009445 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009446 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009447 },
9448 {
Johannes Berg88600202012-02-13 15:17:18 +01009449 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009450 .policy = nl80211_policy,
9451 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009452 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009453 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009454 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009455 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009456 {
9457 .cmd = NL80211_CMD_GET_STATION,
9458 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009459 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009460 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009461 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9462 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009463 },
9464 {
9465 .cmd = NL80211_CMD_SET_STATION,
9466 .doit = nl80211_set_station,
9467 .policy = nl80211_policy,
9468 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009470 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009471 },
9472 {
9473 .cmd = NL80211_CMD_NEW_STATION,
9474 .doit = nl80211_new_station,
9475 .policy = nl80211_policy,
9476 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009478 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009479 },
9480 {
9481 .cmd = NL80211_CMD_DEL_STATION,
9482 .doit = nl80211_del_station,
9483 .policy = nl80211_policy,
9484 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009486 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009487 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009488 {
9489 .cmd = NL80211_CMD_GET_MPATH,
9490 .doit = nl80211_get_mpath,
9491 .dumpit = nl80211_dump_mpath,
9492 .policy = nl80211_policy,
9493 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009495 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009496 },
9497 {
9498 .cmd = NL80211_CMD_SET_MPATH,
9499 .doit = nl80211_set_mpath,
9500 .policy = nl80211_policy,
9501 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009503 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009504 },
9505 {
9506 .cmd = NL80211_CMD_NEW_MPATH,
9507 .doit = nl80211_new_mpath,
9508 .policy = nl80211_policy,
9509 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009511 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009512 },
9513 {
9514 .cmd = NL80211_CMD_DEL_MPATH,
9515 .doit = nl80211_del_mpath,
9516 .policy = nl80211_policy,
9517 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009519 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009520 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009521 {
9522 .cmd = NL80211_CMD_SET_BSS,
9523 .doit = nl80211_set_bss,
9524 .policy = nl80211_policy,
9525 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009527 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009528 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009529 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009530 .cmd = NL80211_CMD_GET_REG,
9531 .doit = nl80211_get_reg,
9532 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009533 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009534 /* can be retrieved by unprivileged users */
9535 },
9536 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009537 .cmd = NL80211_CMD_SET_REG,
9538 .doit = nl80211_set_reg,
9539 .policy = nl80211_policy,
9540 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009541 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009542 },
9543 {
9544 .cmd = NL80211_CMD_REQ_SET_REG,
9545 .doit = nl80211_req_set_reg,
9546 .policy = nl80211_policy,
9547 .flags = GENL_ADMIN_PERM,
9548 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009549 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009550 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9551 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009552 .policy = nl80211_policy,
9553 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009554 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009555 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009556 },
9557 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009558 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9559 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009560 .policy = nl80211_policy,
9561 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009562 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009563 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009564 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009565 {
Johannes Berg2a519312009-02-10 21:25:55 +01009566 .cmd = NL80211_CMD_TRIGGER_SCAN,
9567 .doit = nl80211_trigger_scan,
9568 .policy = nl80211_policy,
9569 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009570 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009571 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009572 },
9573 {
9574 .cmd = NL80211_CMD_GET_SCAN,
9575 .policy = nl80211_policy,
9576 .dumpit = nl80211_dump_scan,
9577 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009578 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009579 .cmd = NL80211_CMD_START_SCHED_SCAN,
9580 .doit = nl80211_start_sched_scan,
9581 .policy = nl80211_policy,
9582 .flags = GENL_ADMIN_PERM,
9583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9584 NL80211_FLAG_NEED_RTNL,
9585 },
9586 {
9587 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9588 .doit = nl80211_stop_sched_scan,
9589 .policy = nl80211_policy,
9590 .flags = GENL_ADMIN_PERM,
9591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9592 NL80211_FLAG_NEED_RTNL,
9593 },
9594 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009595 .cmd = NL80211_CMD_AUTHENTICATE,
9596 .doit = nl80211_authenticate,
9597 .policy = nl80211_policy,
9598 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009600 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009601 },
9602 {
9603 .cmd = NL80211_CMD_ASSOCIATE,
9604 .doit = nl80211_associate,
9605 .policy = nl80211_policy,
9606 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009607 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009608 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009609 },
9610 {
9611 .cmd = NL80211_CMD_DEAUTHENTICATE,
9612 .doit = nl80211_deauthenticate,
9613 .policy = nl80211_policy,
9614 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009615 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009616 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009617 },
9618 {
9619 .cmd = NL80211_CMD_DISASSOCIATE,
9620 .doit = nl80211_disassociate,
9621 .policy = nl80211_policy,
9622 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009623 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009624 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009625 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009626 {
9627 .cmd = NL80211_CMD_JOIN_IBSS,
9628 .doit = nl80211_join_ibss,
9629 .policy = nl80211_policy,
9630 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009631 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009632 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009633 },
9634 {
9635 .cmd = NL80211_CMD_LEAVE_IBSS,
9636 .doit = nl80211_leave_ibss,
9637 .policy = nl80211_policy,
9638 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009639 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009640 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009641 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009642#ifdef CONFIG_NL80211_TESTMODE
9643 {
9644 .cmd = NL80211_CMD_TESTMODE,
9645 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009646 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009647 .policy = nl80211_policy,
9648 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009649 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9650 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009651 },
9652#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009653 {
9654 .cmd = NL80211_CMD_CONNECT,
9655 .doit = nl80211_connect,
9656 .policy = nl80211_policy,
9657 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009658 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009659 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009660 },
9661 {
9662 .cmd = NL80211_CMD_DISCONNECT,
9663 .doit = nl80211_disconnect,
9664 .policy = nl80211_policy,
9665 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009666 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009667 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009668 },
Johannes Berg463d0182009-07-14 00:33:35 +02009669 {
9670 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9671 .doit = nl80211_wiphy_netns,
9672 .policy = nl80211_policy,
9673 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009674 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9675 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009676 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009677 {
9678 .cmd = NL80211_CMD_GET_SURVEY,
9679 .policy = nl80211_policy,
9680 .dumpit = nl80211_dump_survey,
9681 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009682 {
9683 .cmd = NL80211_CMD_SET_PMKSA,
9684 .doit = nl80211_setdel_pmksa,
9685 .policy = nl80211_policy,
9686 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009687 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009688 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009689 },
9690 {
9691 .cmd = NL80211_CMD_DEL_PMKSA,
9692 .doit = nl80211_setdel_pmksa,
9693 .policy = nl80211_policy,
9694 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009695 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009696 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009697 },
9698 {
9699 .cmd = NL80211_CMD_FLUSH_PMKSA,
9700 .doit = nl80211_flush_pmksa,
9701 .policy = nl80211_policy,
9702 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009703 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009704 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009705 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009706 {
9707 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9708 .doit = nl80211_remain_on_channel,
9709 .policy = nl80211_policy,
9710 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009711 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009712 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009713 },
9714 {
9715 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9716 .doit = nl80211_cancel_remain_on_channel,
9717 .policy = nl80211_policy,
9718 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009719 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009720 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009721 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009722 {
9723 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9724 .doit = nl80211_set_tx_bitrate_mask,
9725 .policy = nl80211_policy,
9726 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009727 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9728 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009729 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009730 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009731 .cmd = NL80211_CMD_REGISTER_FRAME,
9732 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009733 .policy = nl80211_policy,
9734 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009735 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009736 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009737 },
9738 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009739 .cmd = NL80211_CMD_FRAME,
9740 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009741 .policy = nl80211_policy,
9742 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009743 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009744 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009745 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009746 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009747 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9748 .doit = nl80211_tx_mgmt_cancel_wait,
9749 .policy = nl80211_policy,
9750 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009751 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009752 NL80211_FLAG_NEED_RTNL,
9753 },
9754 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009755 .cmd = NL80211_CMD_SET_POWER_SAVE,
9756 .doit = nl80211_set_power_save,
9757 .policy = nl80211_policy,
9758 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009759 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9760 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009761 },
9762 {
9763 .cmd = NL80211_CMD_GET_POWER_SAVE,
9764 .doit = nl80211_get_power_save,
9765 .policy = nl80211_policy,
9766 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009767 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9768 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009769 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009770 {
9771 .cmd = NL80211_CMD_SET_CQM,
9772 .doit = nl80211_set_cqm,
9773 .policy = nl80211_policy,
9774 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009775 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9776 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009777 },
Johannes Bergf444de02010-05-05 15:25:02 +02009778 {
9779 .cmd = NL80211_CMD_SET_CHANNEL,
9780 .doit = nl80211_set_channel,
9781 .policy = nl80211_policy,
9782 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009783 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9784 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009785 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009786 {
9787 .cmd = NL80211_CMD_SET_WDS_PEER,
9788 .doit = nl80211_set_wds_peer,
9789 .policy = nl80211_policy,
9790 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009791 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9792 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009793 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009794 {
9795 .cmd = NL80211_CMD_JOIN_MESH,
9796 .doit = nl80211_join_mesh,
9797 .policy = nl80211_policy,
9798 .flags = GENL_ADMIN_PERM,
9799 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9800 NL80211_FLAG_NEED_RTNL,
9801 },
9802 {
9803 .cmd = NL80211_CMD_LEAVE_MESH,
9804 .doit = nl80211_leave_mesh,
9805 .policy = nl80211_policy,
9806 .flags = GENL_ADMIN_PERM,
9807 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9808 NL80211_FLAG_NEED_RTNL,
9809 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009810#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009811 {
9812 .cmd = NL80211_CMD_GET_WOWLAN,
9813 .doit = nl80211_get_wowlan,
9814 .policy = nl80211_policy,
9815 /* can be retrieved by unprivileged users */
9816 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9817 NL80211_FLAG_NEED_RTNL,
9818 },
9819 {
9820 .cmd = NL80211_CMD_SET_WOWLAN,
9821 .doit = nl80211_set_wowlan,
9822 .policy = nl80211_policy,
9823 .flags = GENL_ADMIN_PERM,
9824 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9825 NL80211_FLAG_NEED_RTNL,
9826 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009827#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009828 {
9829 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9830 .doit = nl80211_set_rekey_data,
9831 .policy = nl80211_policy,
9832 .flags = GENL_ADMIN_PERM,
9833 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9834 NL80211_FLAG_NEED_RTNL,
9835 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009836 {
9837 .cmd = NL80211_CMD_TDLS_MGMT,
9838 .doit = nl80211_tdls_mgmt,
9839 .policy = nl80211_policy,
9840 .flags = GENL_ADMIN_PERM,
9841 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9842 NL80211_FLAG_NEED_RTNL,
9843 },
9844 {
9845 .cmd = NL80211_CMD_TDLS_OPER,
9846 .doit = nl80211_tdls_oper,
9847 .policy = nl80211_policy,
9848 .flags = GENL_ADMIN_PERM,
9849 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9850 NL80211_FLAG_NEED_RTNL,
9851 },
Johannes Berg28946da2011-11-04 11:18:12 +01009852 {
9853 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9854 .doit = nl80211_register_unexpected_frame,
9855 .policy = nl80211_policy,
9856 .flags = GENL_ADMIN_PERM,
9857 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9858 NL80211_FLAG_NEED_RTNL,
9859 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009860 {
9861 .cmd = NL80211_CMD_PROBE_CLIENT,
9862 .doit = nl80211_probe_client,
9863 .policy = nl80211_policy,
9864 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009866 NL80211_FLAG_NEED_RTNL,
9867 },
Johannes Berg5e7602302011-11-04 11:18:17 +01009868 {
9869 .cmd = NL80211_CMD_REGISTER_BEACONS,
9870 .doit = nl80211_register_beacons,
9871 .policy = nl80211_policy,
9872 .flags = GENL_ADMIN_PERM,
9873 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9874 NL80211_FLAG_NEED_RTNL,
9875 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009876 {
9877 .cmd = NL80211_CMD_SET_NOACK_MAP,
9878 .doit = nl80211_set_noack_map,
9879 .policy = nl80211_policy,
9880 .flags = GENL_ADMIN_PERM,
9881 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9882 NL80211_FLAG_NEED_RTNL,
9883 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009884 {
9885 .cmd = NL80211_CMD_START_P2P_DEVICE,
9886 .doit = nl80211_start_p2p_device,
9887 .policy = nl80211_policy,
9888 .flags = GENL_ADMIN_PERM,
9889 .internal_flags = NL80211_FLAG_NEED_WDEV |
9890 NL80211_FLAG_NEED_RTNL,
9891 },
9892 {
9893 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9894 .doit = nl80211_stop_p2p_device,
9895 .policy = nl80211_policy,
9896 .flags = GENL_ADMIN_PERM,
9897 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9898 NL80211_FLAG_NEED_RTNL,
9899 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009900 {
9901 .cmd = NL80211_CMD_SET_MCAST_RATE,
9902 .doit = nl80211_set_mcast_rate,
9903 .policy = nl80211_policy,
9904 .flags = GENL_ADMIN_PERM,
9905 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9906 NL80211_FLAG_NEED_RTNL,
9907 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309908 {
9909 .cmd = NL80211_CMD_SET_MAC_ACL,
9910 .doit = nl80211_set_mac_acl,
9911 .policy = nl80211_policy,
9912 .flags = GENL_ADMIN_PERM,
9913 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9914 NL80211_FLAG_NEED_RTNL,
9915 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009916 {
9917 .cmd = NL80211_CMD_RADAR_DETECT,
9918 .doit = nl80211_start_radar_detection,
9919 .policy = nl80211_policy,
9920 .flags = GENL_ADMIN_PERM,
9921 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9922 NL80211_FLAG_NEED_RTNL,
9923 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009924 {
9925 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9926 .doit = nl80211_get_protocol_features,
9927 .policy = nl80211_policy,
9928 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009929 {
9930 .cmd = NL80211_CMD_UPDATE_FT_IES,
9931 .doit = nl80211_update_ft_ies,
9932 .policy = nl80211_policy,
9933 .flags = GENL_ADMIN_PERM,
9934 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9935 NL80211_FLAG_NEED_RTNL,
9936 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009937 {
9938 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9939 .doit = nl80211_crit_protocol_start,
9940 .policy = nl80211_policy,
9941 .flags = GENL_ADMIN_PERM,
9942 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9943 NL80211_FLAG_NEED_RTNL,
9944 },
9945 {
9946 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9947 .doit = nl80211_crit_protocol_stop,
9948 .policy = nl80211_policy,
9949 .flags = GENL_ADMIN_PERM,
9950 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9951 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009952 },
9953 {
9954 .cmd = NL80211_CMD_GET_COALESCE,
9955 .doit = nl80211_get_coalesce,
9956 .policy = nl80211_policy,
9957 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9958 NL80211_FLAG_NEED_RTNL,
9959 },
9960 {
9961 .cmd = NL80211_CMD_SET_COALESCE,
9962 .doit = nl80211_set_coalesce,
9963 .policy = nl80211_policy,
9964 .flags = GENL_ADMIN_PERM,
9965 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9966 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009967 },
9968 {
9969 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9970 .doit = nl80211_channel_switch,
9971 .policy = nl80211_policy,
9972 .flags = GENL_ADMIN_PERM,
9973 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9974 NL80211_FLAG_NEED_RTNL,
9975 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009976 {
9977 .cmd = NL80211_CMD_VENDOR,
9978 .doit = nl80211_vendor_cmd,
9979 .policy = nl80211_policy,
9980 .flags = GENL_ADMIN_PERM,
9981 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9982 NL80211_FLAG_NEED_RTNL,
9983 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009984 {
9985 .cmd = NL80211_CMD_SET_QOS_MAP,
9986 .doit = nl80211_set_qos_map,
9987 .policy = nl80211_policy,
9988 .flags = GENL_ADMIN_PERM,
9989 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9990 NL80211_FLAG_NEED_RTNL,
9991 },
Johannes Berg55682962007-09-20 13:09:35 -04009992};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009993
Johannes Berg55682962007-09-20 13:09:35 -04009994/* notification functions */
9995
9996void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9997{
9998 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +02009999 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010000
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010001 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010002 if (!msg)
10003 return;
10004
Johannes Berg86e8cf92013-06-19 10:57:22 +020010005 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010006 nlmsg_free(msg);
10007 return;
10008 }
10009
Johannes Berg68eb5502013-11-19 15:19:38 +010010010 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010011 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010012}
10013
Johannes Berg362a4152009-05-24 16:43:15 +020010014static int nl80211_add_scan_req(struct sk_buff *msg,
10015 struct cfg80211_registered_device *rdev)
10016{
10017 struct cfg80211_scan_request *req = rdev->scan_req;
10018 struct nlattr *nest;
10019 int i;
10020
10021 if (WARN_ON(!req))
10022 return 0;
10023
10024 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10025 if (!nest)
10026 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010027 for (i = 0; i < req->n_ssids; i++) {
10028 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10029 goto nla_put_failure;
10030 }
Johannes Berg362a4152009-05-24 16:43:15 +020010031 nla_nest_end(msg, nest);
10032
10033 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10034 if (!nest)
10035 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010036 for (i = 0; i < req->n_channels; i++) {
10037 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10038 goto nla_put_failure;
10039 }
Johannes Berg362a4152009-05-24 16:43:15 +020010040 nla_nest_end(msg, nest);
10041
David S. Miller9360ffd2012-03-29 04:41:26 -040010042 if (req->ie &&
10043 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10044 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010045
Johannes Bergae917c92013-10-25 11:05:22 +020010046 if (req->flags &&
10047 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10048 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010049
Johannes Berg362a4152009-05-24 16:43:15 +020010050 return 0;
10051 nla_put_failure:
10052 return -ENOBUFS;
10053}
10054
Johannes Berga538e2d2009-06-16 19:56:42 +020010055static int nl80211_send_scan_msg(struct sk_buff *msg,
10056 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010057 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010058 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010059 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010060{
10061 void *hdr;
10062
Eric W. Biederman15e47302012-09-07 20:12:54 +000010063 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010064 if (!hdr)
10065 return -1;
10066
David S. Miller9360ffd2012-03-29 04:41:26 -040010067 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010068 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10069 wdev->netdev->ifindex)) ||
10070 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010071 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010072
Johannes Berg362a4152009-05-24 16:43:15 +020010073 /* ignore errors and send incomplete event anyway */
10074 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010075
10076 return genlmsg_end(msg, hdr);
10077
10078 nla_put_failure:
10079 genlmsg_cancel(msg, hdr);
10080 return -EMSGSIZE;
10081}
10082
Luciano Coelho807f8a82011-05-11 17:09:35 +030010083static int
10084nl80211_send_sched_scan_msg(struct sk_buff *msg,
10085 struct cfg80211_registered_device *rdev,
10086 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010087 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010088{
10089 void *hdr;
10090
Eric W. Biederman15e47302012-09-07 20:12:54 +000010091 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010092 if (!hdr)
10093 return -1;
10094
David S. Miller9360ffd2012-03-29 04:41:26 -040010095 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10096 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10097 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010098
10099 return genlmsg_end(msg, hdr);
10100
10101 nla_put_failure:
10102 genlmsg_cancel(msg, hdr);
10103 return -EMSGSIZE;
10104}
10105
Johannes Berga538e2d2009-06-16 19:56:42 +020010106void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010107 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010108{
10109 struct sk_buff *msg;
10110
Thomas Graf58050fc2012-06-28 03:57:45 +000010111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010112 if (!msg)
10113 return;
10114
Johannes Bergfd014282012-06-18 19:17:03 +020010115 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010116 NL80211_CMD_TRIGGER_SCAN) < 0) {
10117 nlmsg_free(msg);
10118 return;
10119 }
10120
Johannes Berg68eb5502013-11-19 15:19:38 +010010121 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010122 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010123}
10124
Johannes Bergf9d15d12014-01-22 11:14:19 +020010125struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10126 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010127{
10128 struct sk_buff *msg;
10129
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010130 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010131 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010132 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010133
Johannes Bergfd014282012-06-18 19:17:03 +020010134 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010135 aborted ? NL80211_CMD_SCAN_ABORTED :
10136 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010137 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010138 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010139 }
10140
Johannes Bergf9d15d12014-01-22 11:14:19 +020010141 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010142}
10143
Johannes Bergf9d15d12014-01-22 11:14:19 +020010144void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10145 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010146{
Johannes Berg2a519312009-02-10 21:25:55 +010010147 if (!msg)
10148 return;
10149
Johannes Berg68eb5502013-11-19 15:19:38 +010010150 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010151 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010152}
10153
Luciano Coelho807f8a82011-05-11 17:09:35 +030010154void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10155 struct net_device *netdev)
10156{
10157 struct sk_buff *msg;
10158
10159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10160 if (!msg)
10161 return;
10162
10163 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10164 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10165 nlmsg_free(msg);
10166 return;
10167 }
10168
Johannes Berg68eb5502013-11-19 15:19:38 +010010169 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010170 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010171}
10172
10173void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10174 struct net_device *netdev, u32 cmd)
10175{
10176 struct sk_buff *msg;
10177
Thomas Graf58050fc2012-06-28 03:57:45 +000010178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010179 if (!msg)
10180 return;
10181
10182 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10183 nlmsg_free(msg);
10184 return;
10185 }
10186
Johannes Berg68eb5502013-11-19 15:19:38 +010010187 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010188 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010189}
10190
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010191/*
10192 * This can happen on global regulatory changes or device specific settings
10193 * based on custom world regulatory domains.
10194 */
10195void nl80211_send_reg_change_event(struct regulatory_request *request)
10196{
10197 struct sk_buff *msg;
10198 void *hdr;
10199
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010200 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010201 if (!msg)
10202 return;
10203
10204 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10205 if (!hdr) {
10206 nlmsg_free(msg);
10207 return;
10208 }
10209
10210 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010211 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10212 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010213
David S. Miller9360ffd2012-03-29 04:41:26 -040010214 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10215 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10216 NL80211_REGDOM_TYPE_WORLD))
10217 goto nla_put_failure;
10218 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10219 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10220 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10221 goto nla_put_failure;
10222 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10223 request->intersect) {
10224 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10225 NL80211_REGDOM_TYPE_INTERSECTION))
10226 goto nla_put_failure;
10227 } else {
10228 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10229 NL80211_REGDOM_TYPE_COUNTRY) ||
10230 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10231 request->alpha2))
10232 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010233 }
10234
Johannes Bergf4173762012-12-03 18:23:37 +010010235 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010236 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10237 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010238
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010239 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010240
Johannes Bergbc43b282009-07-25 10:54:13 +020010241 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010242 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010243 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010244 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010245
10246 return;
10247
10248nla_put_failure:
10249 genlmsg_cancel(msg, hdr);
10250 nlmsg_free(msg);
10251}
10252
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010253static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10254 struct net_device *netdev,
10255 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010256 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010257{
10258 struct sk_buff *msg;
10259 void *hdr;
10260
Johannes Berge6d6e342009-07-01 21:26:47 +020010261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010262 if (!msg)
10263 return;
10264
10265 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10266 if (!hdr) {
10267 nlmsg_free(msg);
10268 return;
10269 }
10270
David S. Miller9360ffd2012-03-29 04:41:26 -040010271 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10272 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10273 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10274 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010275
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010276 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010277
Johannes Berg68eb5502013-11-19 15:19:38 +010010278 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010279 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010280 return;
10281
10282 nla_put_failure:
10283 genlmsg_cancel(msg, hdr);
10284 nlmsg_free(msg);
10285}
10286
10287void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010288 struct net_device *netdev, const u8 *buf,
10289 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010290{
10291 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010292 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010293}
10294
10295void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10296 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010297 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010298{
Johannes Berge6d6e342009-07-01 21:26:47 +020010299 nl80211_send_mlme_event(rdev, netdev, buf, len,
10300 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010301}
10302
Jouni Malinen53b46b82009-03-27 20:53:56 +020010303void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010304 struct net_device *netdev, const u8 *buf,
10305 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010306{
10307 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010308 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010309}
10310
Jouni Malinen53b46b82009-03-27 20:53:56 +020010311void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10312 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010313 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010314{
10315 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010316 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010317}
10318
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010319void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10320 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010321{
Johannes Berg947add32013-02-22 22:05:20 +010010322 struct wireless_dev *wdev = dev->ieee80211_ptr;
10323 struct wiphy *wiphy = wdev->wiphy;
10324 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010325 const struct ieee80211_mgmt *mgmt = (void *)buf;
10326 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010327
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010328 if (WARN_ON(len < 2))
10329 return;
10330
10331 if (ieee80211_is_deauth(mgmt->frame_control))
10332 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10333 else
10334 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10335
10336 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10337 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010338}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010339EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010340
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010341static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10342 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010343 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010344{
10345 struct sk_buff *msg;
10346 void *hdr;
10347
Johannes Berge6d6e342009-07-01 21:26:47 +020010348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010349 if (!msg)
10350 return;
10351
10352 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10353 if (!hdr) {
10354 nlmsg_free(msg);
10355 return;
10356 }
10357
David S. Miller9360ffd2012-03-29 04:41:26 -040010358 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10359 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10360 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10361 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10362 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010363
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010364 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010365
Johannes Berg68eb5502013-11-19 15:19:38 +010010366 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010367 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010368 return;
10369
10370 nla_put_failure:
10371 genlmsg_cancel(msg, hdr);
10372 nlmsg_free(msg);
10373}
10374
10375void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010376 struct net_device *netdev, const u8 *addr,
10377 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010378{
10379 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010380 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010381}
10382
10383void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010384 struct net_device *netdev, const u8 *addr,
10385 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010386{
Johannes Berge6d6e342009-07-01 21:26:47 +020010387 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10388 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010389}
10390
Samuel Ortizb23aa672009-07-01 21:26:54 +020010391void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10392 struct net_device *netdev, const u8 *bssid,
10393 const u8 *req_ie, size_t req_ie_len,
10394 const u8 *resp_ie, size_t resp_ie_len,
10395 u16 status, gfp_t gfp)
10396{
10397 struct sk_buff *msg;
10398 void *hdr;
10399
Thomas Graf58050fc2012-06-28 03:57:45 +000010400 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010401 if (!msg)
10402 return;
10403
10404 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10405 if (!hdr) {
10406 nlmsg_free(msg);
10407 return;
10408 }
10409
David S. Miller9360ffd2012-03-29 04:41:26 -040010410 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10411 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10412 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10413 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10414 (req_ie &&
10415 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10416 (resp_ie &&
10417 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10418 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010419
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010420 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010421
Johannes Berg68eb5502013-11-19 15:19:38 +010010422 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010423 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010424 return;
10425
10426 nla_put_failure:
10427 genlmsg_cancel(msg, hdr);
10428 nlmsg_free(msg);
10429
10430}
10431
10432void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10433 struct net_device *netdev, const u8 *bssid,
10434 const u8 *req_ie, size_t req_ie_len,
10435 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10436{
10437 struct sk_buff *msg;
10438 void *hdr;
10439
Thomas Graf58050fc2012-06-28 03:57:45 +000010440 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010441 if (!msg)
10442 return;
10443
10444 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10445 if (!hdr) {
10446 nlmsg_free(msg);
10447 return;
10448 }
10449
David S. Miller9360ffd2012-03-29 04:41:26 -040010450 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10451 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10452 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10453 (req_ie &&
10454 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10455 (resp_ie &&
10456 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10457 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010458
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010459 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010460
Johannes Berg68eb5502013-11-19 15:19:38 +010010461 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010462 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010463 return;
10464
10465 nla_put_failure:
10466 genlmsg_cancel(msg, hdr);
10467 nlmsg_free(msg);
10468
10469}
10470
10471void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10472 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010473 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010474{
10475 struct sk_buff *msg;
10476 void *hdr;
10477
Thomas Graf58050fc2012-06-28 03:57:45 +000010478 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010479 if (!msg)
10480 return;
10481
10482 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10483 if (!hdr) {
10484 nlmsg_free(msg);
10485 return;
10486 }
10487
David S. Miller9360ffd2012-03-29 04:41:26 -040010488 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10489 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10490 (from_ap && reason &&
10491 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10492 (from_ap &&
10493 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10494 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10495 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010496
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010497 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010498
Johannes Berg68eb5502013-11-19 15:19:38 +010010499 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010500 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010501 return;
10502
10503 nla_put_failure:
10504 genlmsg_cancel(msg, hdr);
10505 nlmsg_free(msg);
10506
10507}
10508
Johannes Berg04a773a2009-04-19 21:24:32 +020010509void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10510 struct net_device *netdev, const u8 *bssid,
10511 gfp_t gfp)
10512{
10513 struct sk_buff *msg;
10514 void *hdr;
10515
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010516 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010517 if (!msg)
10518 return;
10519
10520 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10521 if (!hdr) {
10522 nlmsg_free(msg);
10523 return;
10524 }
10525
David S. Miller9360ffd2012-03-29 04:41:26 -040010526 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10527 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10528 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10529 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010530
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010531 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010532
Johannes Berg68eb5502013-11-19 15:19:38 +010010533 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010534 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010535 return;
10536
10537 nla_put_failure:
10538 genlmsg_cancel(msg, hdr);
10539 nlmsg_free(msg);
10540}
10541
Johannes Berg947add32013-02-22 22:05:20 +010010542void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10543 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010544{
Johannes Berg947add32013-02-22 22:05:20 +010010545 struct wireless_dev *wdev = dev->ieee80211_ptr;
10546 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010547 struct sk_buff *msg;
10548 void *hdr;
10549
Johannes Berg947add32013-02-22 22:05:20 +010010550 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10551 return;
10552
10553 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10554
Javier Cardonac93b5e72011-04-07 15:08:34 -070010555 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10556 if (!msg)
10557 return;
10558
10559 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10560 if (!hdr) {
10561 nlmsg_free(msg);
10562 return;
10563 }
10564
David S. Miller9360ffd2012-03-29 04:41:26 -040010565 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010566 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10567 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010568 (ie_len && ie &&
10569 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10570 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010571
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010572 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010573
Johannes Berg68eb5502013-11-19 15:19:38 +010010574 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010575 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010576 return;
10577
10578 nla_put_failure:
10579 genlmsg_cancel(msg, hdr);
10580 nlmsg_free(msg);
10581}
Johannes Berg947add32013-02-22 22:05:20 +010010582EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010583
Jouni Malinena3b8b052009-03-27 21:59:49 +020010584void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10585 struct net_device *netdev, const u8 *addr,
10586 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010587 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010588{
10589 struct sk_buff *msg;
10590 void *hdr;
10591
Johannes Berge6d6e342009-07-01 21:26:47 +020010592 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010593 if (!msg)
10594 return;
10595
10596 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10597 if (!hdr) {
10598 nlmsg_free(msg);
10599 return;
10600 }
10601
David S. Miller9360ffd2012-03-29 04:41:26 -040010602 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10603 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10604 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10605 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10606 (key_id != -1 &&
10607 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10608 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10609 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010610
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010611 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010612
Johannes Berg68eb5502013-11-19 15:19:38 +010010613 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010614 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010615 return;
10616
10617 nla_put_failure:
10618 genlmsg_cancel(msg, hdr);
10619 nlmsg_free(msg);
10620}
10621
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010622void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10623 struct ieee80211_channel *channel_before,
10624 struct ieee80211_channel *channel_after)
10625{
10626 struct sk_buff *msg;
10627 void *hdr;
10628 struct nlattr *nl_freq;
10629
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010631 if (!msg)
10632 return;
10633
10634 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10635 if (!hdr) {
10636 nlmsg_free(msg);
10637 return;
10638 }
10639
10640 /*
10641 * Since we are applying the beacon hint to a wiphy we know its
10642 * wiphy_idx is valid
10643 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010644 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10645 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010646
10647 /* Before */
10648 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10649 if (!nl_freq)
10650 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010651 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010652 goto nla_put_failure;
10653 nla_nest_end(msg, nl_freq);
10654
10655 /* After */
10656 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10657 if (!nl_freq)
10658 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010659 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010660 goto nla_put_failure;
10661 nla_nest_end(msg, nl_freq);
10662
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010663 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010664
Johannes Berg463d0182009-07-14 00:33:35 +020010665 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010666 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010667 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010668 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010669
10670 return;
10671
10672nla_put_failure:
10673 genlmsg_cancel(msg, hdr);
10674 nlmsg_free(msg);
10675}
10676
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010677static void nl80211_send_remain_on_chan_event(
10678 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010679 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010680 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010681 unsigned int duration, gfp_t gfp)
10682{
10683 struct sk_buff *msg;
10684 void *hdr;
10685
10686 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10687 if (!msg)
10688 return;
10689
10690 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10691 if (!hdr) {
10692 nlmsg_free(msg);
10693 return;
10694 }
10695
David S. Miller9360ffd2012-03-29 04:41:26 -040010696 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010697 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10698 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010699 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010700 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010701 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10702 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010703 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10704 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010705
David S. Miller9360ffd2012-03-29 04:41:26 -040010706 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10707 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10708 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010709
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010710 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010711
Johannes Berg68eb5502013-11-19 15:19:38 +010010712 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010713 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010714 return;
10715
10716 nla_put_failure:
10717 genlmsg_cancel(msg, hdr);
10718 nlmsg_free(msg);
10719}
10720
Johannes Berg947add32013-02-22 22:05:20 +010010721void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10722 struct ieee80211_channel *chan,
10723 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010724{
Johannes Berg947add32013-02-22 22:05:20 +010010725 struct wiphy *wiphy = wdev->wiphy;
10726 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10727
10728 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010729 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010730 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010731 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010732}
Johannes Berg947add32013-02-22 22:05:20 +010010733EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010734
Johannes Berg947add32013-02-22 22:05:20 +010010735void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10736 struct ieee80211_channel *chan,
10737 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010738{
Johannes Berg947add32013-02-22 22:05:20 +010010739 struct wiphy *wiphy = wdev->wiphy;
10740 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10741
10742 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010743 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010744 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010745}
Johannes Berg947add32013-02-22 22:05:20 +010010746EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010747
Johannes Berg947add32013-02-22 22:05:20 +010010748void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10749 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010750{
Johannes Berg947add32013-02-22 22:05:20 +010010751 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10752 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010753 struct sk_buff *msg;
10754
Johannes Berg947add32013-02-22 22:05:20 +010010755 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10756
Thomas Graf58050fc2012-06-28 03:57:45 +000010757 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010758 if (!msg)
10759 return;
10760
John W. Linville66266b32012-03-15 13:25:41 -040010761 if (nl80211_send_station(msg, 0, 0, 0,
10762 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010763 nlmsg_free(msg);
10764 return;
10765 }
10766
Johannes Berg68eb5502013-11-19 15:19:38 +010010767 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010768 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010769}
Johannes Berg947add32013-02-22 22:05:20 +010010770EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010771
Johannes Berg947add32013-02-22 22:05:20 +010010772void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010773{
Johannes Berg947add32013-02-22 22:05:20 +010010774 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10775 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010776 struct sk_buff *msg;
10777 void *hdr;
10778
Johannes Berg947add32013-02-22 22:05:20 +010010779 trace_cfg80211_del_sta(dev, mac_addr);
10780
Thomas Graf58050fc2012-06-28 03:57:45 +000010781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010782 if (!msg)
10783 return;
10784
10785 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10786 if (!hdr) {
10787 nlmsg_free(msg);
10788 return;
10789 }
10790
David S. Miller9360ffd2012-03-29 04:41:26 -040010791 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10792 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10793 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010794
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010795 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010796
Johannes Berg68eb5502013-11-19 15:19:38 +010010797 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010798 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010799 return;
10800
10801 nla_put_failure:
10802 genlmsg_cancel(msg, hdr);
10803 nlmsg_free(msg);
10804}
Johannes Berg947add32013-02-22 22:05:20 +010010805EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010806
Johannes Berg947add32013-02-22 22:05:20 +010010807void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10808 enum nl80211_connect_failed_reason reason,
10809 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010810{
Johannes Berg947add32013-02-22 22:05:20 +010010811 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10812 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010813 struct sk_buff *msg;
10814 void *hdr;
10815
10816 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10817 if (!msg)
10818 return;
10819
10820 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10821 if (!hdr) {
10822 nlmsg_free(msg);
10823 return;
10824 }
10825
10826 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10827 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10828 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10829 goto nla_put_failure;
10830
10831 genlmsg_end(msg, hdr);
10832
Johannes Berg68eb5502013-11-19 15:19:38 +010010833 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010834 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010835 return;
10836
10837 nla_put_failure:
10838 genlmsg_cancel(msg, hdr);
10839 nlmsg_free(msg);
10840}
Johannes Berg947add32013-02-22 22:05:20 +010010841EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010842
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010843static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10844 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010845{
10846 struct wireless_dev *wdev = dev->ieee80211_ptr;
10847 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10848 struct sk_buff *msg;
10849 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010850 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010851
Eric W. Biederman15e47302012-09-07 20:12:54 +000010852 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010853 return false;
10854
10855 msg = nlmsg_new(100, gfp);
10856 if (!msg)
10857 return true;
10858
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010859 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010860 if (!hdr) {
10861 nlmsg_free(msg);
10862 return true;
10863 }
10864
David S. Miller9360ffd2012-03-29 04:41:26 -040010865 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10866 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10867 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10868 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010869
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010870 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010871 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010872 return true;
10873
10874 nla_put_failure:
10875 genlmsg_cancel(msg, hdr);
10876 nlmsg_free(msg);
10877 return true;
10878}
10879
Johannes Berg947add32013-02-22 22:05:20 +010010880bool cfg80211_rx_spurious_frame(struct net_device *dev,
10881 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010882{
Johannes Berg947add32013-02-22 22:05:20 +010010883 struct wireless_dev *wdev = dev->ieee80211_ptr;
10884 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010885
Johannes Berg947add32013-02-22 22:05:20 +010010886 trace_cfg80211_rx_spurious_frame(dev, addr);
10887
10888 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10889 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10890 trace_cfg80211_return_bool(false);
10891 return false;
10892 }
10893 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10894 addr, gfp);
10895 trace_cfg80211_return_bool(ret);
10896 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010897}
Johannes Berg947add32013-02-22 22:05:20 +010010898EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10899
10900bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10901 const u8 *addr, gfp_t gfp)
10902{
10903 struct wireless_dev *wdev = dev->ieee80211_ptr;
10904 bool ret;
10905
10906 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10907
10908 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10909 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10910 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10911 trace_cfg80211_return_bool(false);
10912 return false;
10913 }
10914 ret = __nl80211_unexpected_frame(dev,
10915 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10916 addr, gfp);
10917 trace_cfg80211_return_bool(ret);
10918 return ret;
10919}
10920EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010921
Johannes Berg2e161f72010-08-12 15:38:38 +020010922int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010923 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010924 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010925 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010926{
Johannes Berg71bbc992012-06-15 15:30:18 +020010927 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010928 struct sk_buff *msg;
10929 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010930
10931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10932 if (!msg)
10933 return -ENOMEM;
10934
Johannes Berg2e161f72010-08-12 15:38:38 +020010935 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010936 if (!hdr) {
10937 nlmsg_free(msg);
10938 return -ENOMEM;
10939 }
10940
David S. Miller9360ffd2012-03-29 04:41:26 -040010941 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010942 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10943 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010944 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010945 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10946 (sig_dbm &&
10947 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010948 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10949 (flags &&
10950 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010951 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010952
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010953 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010954
Eric W. Biederman15e47302012-09-07 20:12:54 +000010955 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010956
10957 nla_put_failure:
10958 genlmsg_cancel(msg, hdr);
10959 nlmsg_free(msg);
10960 return -ENOBUFS;
10961}
10962
Johannes Berg947add32013-02-22 22:05:20 +010010963void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10964 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010965{
Johannes Berg947add32013-02-22 22:05:20 +010010966 struct wiphy *wiphy = wdev->wiphy;
10967 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010968 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010969 struct sk_buff *msg;
10970 void *hdr;
10971
Johannes Berg947add32013-02-22 22:05:20 +010010972 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10973
Jouni Malinen026331c2010-02-15 12:53:10 +020010974 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10975 if (!msg)
10976 return;
10977
Johannes Berg2e161f72010-08-12 15:38:38 +020010978 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010979 if (!hdr) {
10980 nlmsg_free(msg);
10981 return;
10982 }
10983
David S. Miller9360ffd2012-03-29 04:41:26 -040010984 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010985 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10986 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010987 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010988 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10989 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10990 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10991 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010992
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010993 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010994
Johannes Berg68eb5502013-11-19 15:19:38 +010010995 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010996 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010997 return;
10998
10999 nla_put_failure:
11000 genlmsg_cancel(msg, hdr);
11001 nlmsg_free(msg);
11002}
Johannes Berg947add32013-02-22 22:05:20 +010011003EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011004
Johannes Berg947add32013-02-22 22:05:20 +010011005void cfg80211_cqm_rssi_notify(struct net_device *dev,
11006 enum nl80211_cqm_rssi_threshold_event rssi_event,
11007 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011008{
Johannes Berg947add32013-02-22 22:05:20 +010011009 struct wireless_dev *wdev = dev->ieee80211_ptr;
11010 struct wiphy *wiphy = wdev->wiphy;
11011 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011012 struct sk_buff *msg;
11013 struct nlattr *pinfoattr;
11014 void *hdr;
11015
Johannes Berg947add32013-02-22 22:05:20 +010011016 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11017
Thomas Graf58050fc2012-06-28 03:57:45 +000011018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011019 if (!msg)
11020 return;
11021
11022 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11023 if (!hdr) {
11024 nlmsg_free(msg);
11025 return;
11026 }
11027
David S. Miller9360ffd2012-03-29 04:41:26 -040011028 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011029 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011030 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011031
11032 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11033 if (!pinfoattr)
11034 goto nla_put_failure;
11035
David S. Miller9360ffd2012-03-29 04:41:26 -040011036 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11037 rssi_event))
11038 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011039
11040 nla_nest_end(msg, pinfoattr);
11041
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011042 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011043
Johannes Berg68eb5502013-11-19 15:19:38 +010011044 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011045 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011046 return;
11047
11048 nla_put_failure:
11049 genlmsg_cancel(msg, hdr);
11050 nlmsg_free(msg);
11051}
Johannes Berg947add32013-02-22 22:05:20 +010011052EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011053
Johannes Berg947add32013-02-22 22:05:20 +010011054static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11055 struct net_device *netdev, const u8 *bssid,
11056 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011057{
11058 struct sk_buff *msg;
11059 struct nlattr *rekey_attr;
11060 void *hdr;
11061
Thomas Graf58050fc2012-06-28 03:57:45 +000011062 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011063 if (!msg)
11064 return;
11065
11066 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11067 if (!hdr) {
11068 nlmsg_free(msg);
11069 return;
11070 }
11071
David S. Miller9360ffd2012-03-29 04:41:26 -040011072 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11073 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11074 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11075 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011076
11077 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11078 if (!rekey_attr)
11079 goto nla_put_failure;
11080
David S. Miller9360ffd2012-03-29 04:41:26 -040011081 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11082 NL80211_REPLAY_CTR_LEN, replay_ctr))
11083 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011084
11085 nla_nest_end(msg, rekey_attr);
11086
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011087 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011088
Johannes Berg68eb5502013-11-19 15:19:38 +010011089 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011090 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011091 return;
11092
11093 nla_put_failure:
11094 genlmsg_cancel(msg, hdr);
11095 nlmsg_free(msg);
11096}
11097
Johannes Berg947add32013-02-22 22:05:20 +010011098void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11099 const u8 *replay_ctr, gfp_t gfp)
11100{
11101 struct wireless_dev *wdev = dev->ieee80211_ptr;
11102 struct wiphy *wiphy = wdev->wiphy;
11103 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11104
11105 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11106 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11107}
11108EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11109
11110static void
11111nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11112 struct net_device *netdev, int index,
11113 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011114{
11115 struct sk_buff *msg;
11116 struct nlattr *attr;
11117 void *hdr;
11118
Thomas Graf58050fc2012-06-28 03:57:45 +000011119 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011120 if (!msg)
11121 return;
11122
11123 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11124 if (!hdr) {
11125 nlmsg_free(msg);
11126 return;
11127 }
11128
David S. Miller9360ffd2012-03-29 04:41:26 -040011129 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11130 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11131 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011132
11133 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11134 if (!attr)
11135 goto nla_put_failure;
11136
David S. Miller9360ffd2012-03-29 04:41:26 -040011137 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11138 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11139 (preauth &&
11140 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11141 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011142
11143 nla_nest_end(msg, attr);
11144
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011145 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011146
Johannes Berg68eb5502013-11-19 15:19:38 +010011147 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011148 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011149 return;
11150
11151 nla_put_failure:
11152 genlmsg_cancel(msg, hdr);
11153 nlmsg_free(msg);
11154}
11155
Johannes Berg947add32013-02-22 22:05:20 +010011156void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11157 const u8 *bssid, bool preauth, gfp_t gfp)
11158{
11159 struct wireless_dev *wdev = dev->ieee80211_ptr;
11160 struct wiphy *wiphy = wdev->wiphy;
11161 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11162
11163 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11164 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11165}
11166EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11167
11168static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11169 struct net_device *netdev,
11170 struct cfg80211_chan_def *chandef,
11171 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011172{
11173 struct sk_buff *msg;
11174 void *hdr;
11175
Thomas Graf58050fc2012-06-28 03:57:45 +000011176 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011177 if (!msg)
11178 return;
11179
11180 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11181 if (!hdr) {
11182 nlmsg_free(msg);
11183 return;
11184 }
11185
Johannes Berg683b6d32012-11-08 21:25:48 +010011186 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11187 goto nla_put_failure;
11188
11189 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011190 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011191
11192 genlmsg_end(msg, hdr);
11193
Johannes Berg68eb5502013-11-19 15:19:38 +010011194 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011195 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011196 return;
11197
11198 nla_put_failure:
11199 genlmsg_cancel(msg, hdr);
11200 nlmsg_free(msg);
11201}
11202
Johannes Berg947add32013-02-22 22:05:20 +010011203void cfg80211_ch_switch_notify(struct net_device *dev,
11204 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011205{
Johannes Berg947add32013-02-22 22:05:20 +010011206 struct wireless_dev *wdev = dev->ieee80211_ptr;
11207 struct wiphy *wiphy = wdev->wiphy;
11208 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11209
Simon Wunderliche487eae2013-11-21 18:19:51 +010011210 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011211
Simon Wunderliche487eae2013-11-21 18:19:51 +010011212 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011213
11214 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011215 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011216 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11217 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011218 return;
Johannes Berg947add32013-02-22 22:05:20 +010011219
Michal Kazior9e0e2962014-01-29 14:22:27 +010011220 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011221 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011222 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011223}
11224EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11225
11226void cfg80211_cqm_txe_notify(struct net_device *dev,
11227 const u8 *peer, u32 num_packets,
11228 u32 rate, u32 intvl, gfp_t gfp)
11229{
11230 struct wireless_dev *wdev = dev->ieee80211_ptr;
11231 struct wiphy *wiphy = wdev->wiphy;
11232 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011233 struct sk_buff *msg;
11234 struct nlattr *pinfoattr;
11235 void *hdr;
11236
11237 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11238 if (!msg)
11239 return;
11240
11241 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11242 if (!hdr) {
11243 nlmsg_free(msg);
11244 return;
11245 }
11246
11247 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011248 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011249 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11250 goto nla_put_failure;
11251
11252 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11253 if (!pinfoattr)
11254 goto nla_put_failure;
11255
11256 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11257 goto nla_put_failure;
11258
11259 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11260 goto nla_put_failure;
11261
11262 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11263 goto nla_put_failure;
11264
11265 nla_nest_end(msg, pinfoattr);
11266
11267 genlmsg_end(msg, hdr);
11268
Johannes Berg68eb5502013-11-19 15:19:38 +010011269 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011270 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011271 return;
11272
11273 nla_put_failure:
11274 genlmsg_cancel(msg, hdr);
11275 nlmsg_free(msg);
11276}
Johannes Berg947add32013-02-22 22:05:20 +010011277EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011278
11279void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011280nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011281 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011282 enum nl80211_radar_event event,
11283 struct net_device *netdev, gfp_t gfp)
11284{
11285 struct sk_buff *msg;
11286 void *hdr;
11287
11288 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11289 if (!msg)
11290 return;
11291
11292 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11293 if (!hdr) {
11294 nlmsg_free(msg);
11295 return;
11296 }
11297
11298 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11299 goto nla_put_failure;
11300
11301 /* NOP and radar events don't need a netdev parameter */
11302 if (netdev) {
11303 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11304
11305 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11306 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11307 goto nla_put_failure;
11308 }
11309
11310 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11311 goto nla_put_failure;
11312
11313 if (nl80211_send_chandef(msg, chandef))
11314 goto nla_put_failure;
11315
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011316 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011317
Johannes Berg68eb5502013-11-19 15:19:38 +010011318 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011319 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011320 return;
11321
11322 nla_put_failure:
11323 genlmsg_cancel(msg, hdr);
11324 nlmsg_free(msg);
11325}
11326
Johannes Berg947add32013-02-22 22:05:20 +010011327void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11328 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011329{
Johannes Berg947add32013-02-22 22:05:20 +010011330 struct wireless_dev *wdev = dev->ieee80211_ptr;
11331 struct wiphy *wiphy = wdev->wiphy;
11332 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011333 struct sk_buff *msg;
11334 struct nlattr *pinfoattr;
11335 void *hdr;
11336
Johannes Berg947add32013-02-22 22:05:20 +010011337 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11338
Thomas Graf58050fc2012-06-28 03:57:45 +000011339 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011340 if (!msg)
11341 return;
11342
11343 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11344 if (!hdr) {
11345 nlmsg_free(msg);
11346 return;
11347 }
11348
David S. Miller9360ffd2012-03-29 04:41:26 -040011349 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011350 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011351 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11352 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011353
11354 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11355 if (!pinfoattr)
11356 goto nla_put_failure;
11357
David S. Miller9360ffd2012-03-29 04:41:26 -040011358 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11359 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011360
11361 nla_nest_end(msg, pinfoattr);
11362
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011363 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011364
Johannes Berg68eb5502013-11-19 15:19:38 +010011365 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011366 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011367 return;
11368
11369 nla_put_failure:
11370 genlmsg_cancel(msg, hdr);
11371 nlmsg_free(msg);
11372}
Johannes Berg947add32013-02-22 22:05:20 +010011373EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011374
Johannes Berg7f6cf312011-11-04 11:18:15 +010011375void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11376 u64 cookie, bool acked, gfp_t gfp)
11377{
11378 struct wireless_dev *wdev = dev->ieee80211_ptr;
11379 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11380 struct sk_buff *msg;
11381 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011382
Beni Lev4ee3e062012-08-27 12:49:39 +030011383 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11384
Thomas Graf58050fc2012-06-28 03:57:45 +000011385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011386
Johannes Berg7f6cf312011-11-04 11:18:15 +010011387 if (!msg)
11388 return;
11389
11390 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11391 if (!hdr) {
11392 nlmsg_free(msg);
11393 return;
11394 }
11395
David S. Miller9360ffd2012-03-29 04:41:26 -040011396 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11397 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11398 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11399 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11400 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11401 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011402
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011403 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011404
Johannes Berg68eb5502013-11-19 15:19:38 +010011405 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011406 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011407 return;
11408
11409 nla_put_failure:
11410 genlmsg_cancel(msg, hdr);
11411 nlmsg_free(msg);
11412}
11413EXPORT_SYMBOL(cfg80211_probe_status);
11414
Johannes Berg5e7602302011-11-04 11:18:17 +010011415void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11416 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011417 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010011418{
11419 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11420 struct sk_buff *msg;
11421 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011422 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010011423
Beni Lev4ee3e062012-08-27 12:49:39 +030011424 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11425
Ben Greear37c73b52012-10-26 14:49:25 -070011426 spin_lock_bh(&rdev->beacon_registrations_lock);
11427 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11428 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11429 if (!msg) {
11430 spin_unlock_bh(&rdev->beacon_registrations_lock);
11431 return;
11432 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011433
Ben Greear37c73b52012-10-26 14:49:25 -070011434 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11435 if (!hdr)
11436 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010011437
Ben Greear37c73b52012-10-26 14:49:25 -070011438 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11439 (freq &&
11440 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11441 (sig_dbm &&
11442 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11443 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11444 goto nla_put_failure;
11445
11446 genlmsg_end(msg, hdr);
11447
11448 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010011449 }
Ben Greear37c73b52012-10-26 14:49:25 -070011450 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011451 return;
11452
11453 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011454 spin_unlock_bh(&rdev->beacon_registrations_lock);
11455 if (hdr)
11456 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010011457 nlmsg_free(msg);
11458}
11459EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11460
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011461#ifdef CONFIG_PM
11462void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11463 struct cfg80211_wowlan_wakeup *wakeup,
11464 gfp_t gfp)
11465{
11466 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11467 struct sk_buff *msg;
11468 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011469 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011470
11471 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11472
11473 if (wakeup)
11474 size += wakeup->packet_present_len;
11475
11476 msg = nlmsg_new(size, gfp);
11477 if (!msg)
11478 return;
11479
11480 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11481 if (!hdr)
11482 goto free_msg;
11483
11484 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11485 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11486 goto free_msg;
11487
11488 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11489 wdev->netdev->ifindex))
11490 goto free_msg;
11491
11492 if (wakeup) {
11493 struct nlattr *reasons;
11494
11495 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011496 if (!reasons)
11497 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011498
11499 if (wakeup->disconnect &&
11500 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11501 goto free_msg;
11502 if (wakeup->magic_pkt &&
11503 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11504 goto free_msg;
11505 if (wakeup->gtk_rekey_failure &&
11506 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11507 goto free_msg;
11508 if (wakeup->eap_identity_req &&
11509 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11510 goto free_msg;
11511 if (wakeup->four_way_handshake &&
11512 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11513 goto free_msg;
11514 if (wakeup->rfkill_release &&
11515 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11516 goto free_msg;
11517
11518 if (wakeup->pattern_idx >= 0 &&
11519 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11520 wakeup->pattern_idx))
11521 goto free_msg;
11522
Johannes Bergae917c92013-10-25 11:05:22 +020011523 if (wakeup->tcp_match &&
11524 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11525 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011526
Johannes Bergae917c92013-10-25 11:05:22 +020011527 if (wakeup->tcp_connlost &&
11528 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11529 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011530
Johannes Bergae917c92013-10-25 11:05:22 +020011531 if (wakeup->tcp_nomoretokens &&
11532 nla_put_flag(msg,
11533 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11534 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011535
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011536 if (wakeup->packet) {
11537 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11538 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11539
11540 if (!wakeup->packet_80211) {
11541 pkt_attr =
11542 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11543 len_attr =
11544 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11545 }
11546
11547 if (wakeup->packet_len &&
11548 nla_put_u32(msg, len_attr, wakeup->packet_len))
11549 goto free_msg;
11550
11551 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11552 wakeup->packet))
11553 goto free_msg;
11554 }
11555
11556 nla_nest_end(msg, reasons);
11557 }
11558
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011559 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011560
Johannes Berg68eb5502013-11-19 15:19:38 +010011561 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011562 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011563 return;
11564
11565 free_msg:
11566 nlmsg_free(msg);
11567}
11568EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11569#endif
11570
Jouni Malinen3475b092012-11-16 22:49:57 +020011571void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11572 enum nl80211_tdls_operation oper,
11573 u16 reason_code, gfp_t gfp)
11574{
11575 struct wireless_dev *wdev = dev->ieee80211_ptr;
11576 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11577 struct sk_buff *msg;
11578 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011579
11580 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11581 reason_code);
11582
11583 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11584 if (!msg)
11585 return;
11586
11587 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11588 if (!hdr) {
11589 nlmsg_free(msg);
11590 return;
11591 }
11592
11593 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11594 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11595 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11596 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11597 (reason_code > 0 &&
11598 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11599 goto nla_put_failure;
11600
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011601 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011602
Johannes Berg68eb5502013-11-19 15:19:38 +010011603 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011604 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011605 return;
11606
11607 nla_put_failure:
11608 genlmsg_cancel(msg, hdr);
11609 nlmsg_free(msg);
11610}
11611EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11612
Jouni Malinen026331c2010-02-15 12:53:10 +020011613static int nl80211_netlink_notify(struct notifier_block * nb,
11614 unsigned long state,
11615 void *_notify)
11616{
11617 struct netlink_notify *notify = _notify;
11618 struct cfg80211_registered_device *rdev;
11619 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011620 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011621
11622 if (state != NETLINK_URELEASE)
11623 return NOTIFY_DONE;
11624
11625 rcu_read_lock();
11626
Johannes Berg5e7602302011-11-04 11:18:17 +010011627 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011628 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011629 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011630
11631 spin_lock_bh(&rdev->beacon_registrations_lock);
11632 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11633 list) {
11634 if (reg->nlportid == notify->portid) {
11635 list_del(&reg->list);
11636 kfree(reg);
11637 break;
11638 }
11639 }
11640 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011641 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011642
11643 rcu_read_unlock();
11644
11645 return NOTIFY_DONE;
11646}
11647
11648static struct notifier_block nl80211_netlink_notifier = {
11649 .notifier_call = nl80211_netlink_notify,
11650};
11651
Jouni Malinen355199e2013-02-27 17:14:27 +020011652void cfg80211_ft_event(struct net_device *netdev,
11653 struct cfg80211_ft_event_params *ft_event)
11654{
11655 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11656 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11657 struct sk_buff *msg;
11658 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011659
11660 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11661
11662 if (!ft_event->target_ap)
11663 return;
11664
11665 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11666 if (!msg)
11667 return;
11668
11669 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011670 if (!hdr)
11671 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011672
Johannes Bergae917c92013-10-25 11:05:22 +020011673 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11674 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11675 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11676 goto out;
11677
11678 if (ft_event->ies &&
11679 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11680 goto out;
11681 if (ft_event->ric_ies &&
11682 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11683 ft_event->ric_ies))
11684 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011685
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011686 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011687
Johannes Berg68eb5502013-11-19 15:19:38 +010011688 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011689 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011690 return;
11691 out:
11692 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011693}
11694EXPORT_SYMBOL(cfg80211_ft_event);
11695
Arend van Spriel5de17982013-04-18 15:49:00 +020011696void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11697{
11698 struct cfg80211_registered_device *rdev;
11699 struct sk_buff *msg;
11700 void *hdr;
11701 u32 nlportid;
11702
11703 rdev = wiphy_to_dev(wdev->wiphy);
11704 if (!rdev->crit_proto_nlportid)
11705 return;
11706
11707 nlportid = rdev->crit_proto_nlportid;
11708 rdev->crit_proto_nlportid = 0;
11709
11710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11711 if (!msg)
11712 return;
11713
11714 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11715 if (!hdr)
11716 goto nla_put_failure;
11717
11718 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11719 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11720 goto nla_put_failure;
11721
11722 genlmsg_end(msg, hdr);
11723
11724 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11725 return;
11726
11727 nla_put_failure:
11728 if (hdr)
11729 genlmsg_cancel(msg, hdr);
11730 nlmsg_free(msg);
11731
11732}
11733EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11734
Johannes Berg348baf02014-01-24 14:06:29 +010011735void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11736{
11737 struct wiphy *wiphy = wdev->wiphy;
11738 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11739 struct sk_buff *msg;
11740 void *hdr;
11741
11742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11743 if (!msg)
11744 return;
11745
11746 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11747 if (!hdr)
11748 goto out;
11749
11750 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11751 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11752 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11753 goto out;
11754
11755 genlmsg_end(msg, hdr);
11756
11757 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11758 NL80211_MCGRP_MLME, GFP_KERNEL);
11759 return;
11760 out:
11761 nlmsg_free(msg);
11762}
11763
Johannes Berg55682962007-09-20 13:09:35 -040011764/* initialisation/exit functions */
11765
11766int nl80211_init(void)
11767{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011768 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011769
Johannes Berg2a94fe42013-11-19 15:19:39 +010011770 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11771 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011772 if (err)
11773 return err;
11774
Jouni Malinen026331c2010-02-15 12:53:10 +020011775 err = netlink_register_notifier(&nl80211_netlink_notifier);
11776 if (err)
11777 goto err_out;
11778
Johannes Berg55682962007-09-20 13:09:35 -040011779 return 0;
11780 err_out:
11781 genl_unregister_family(&nl80211_fam);
11782 return err;
11783}
11784
11785void nl80211_exit(void)
11786{
Jouni Malinen026331c2010-02-15 12:53:10 +020011787 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011788 genl_unregister_family(&nl80211_fam);
11789}