blob: 052c1bf8ffaceb92d3f117231a46fca78ed30216 [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;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100596 if (nla_put_u32(msg,
597 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
598 chan->dfs_cac_ms))
599 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100600 }
601 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400602
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100603 if (large) {
604 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
605 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
606 goto nla_put_failure;
607 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
608 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
609 goto nla_put_failure;
610 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
611 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
612 goto nla_put_failure;
613 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
614 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
615 goto nla_put_failure;
616 }
617
David S. Miller9360ffd2012-03-29 04:41:26 -0400618 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
619 DBM_TO_MBM(chan->max_power)))
620 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400621
622 return 0;
623
624 nla_put_failure:
625 return -ENOBUFS;
626}
627
Johannes Berg55682962007-09-20 13:09:35 -0400628/* netlink command implementations */
629
Johannes Bergb9454e82009-07-08 13:29:08 +0200630struct key_parse {
631 struct key_params p;
632 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200633 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200634 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100635 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200636};
637
638static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
639{
640 struct nlattr *tb[NL80211_KEY_MAX + 1];
641 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
642 nl80211_key_policy);
643 if (err)
644 return err;
645
646 k->def = !!tb[NL80211_KEY_DEFAULT];
647 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
648
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100649 if (k->def) {
650 k->def_uni = true;
651 k->def_multi = true;
652 }
653 if (k->defmgmt)
654 k->def_multi = true;
655
Johannes Bergb9454e82009-07-08 13:29:08 +0200656 if (tb[NL80211_KEY_IDX])
657 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
658
659 if (tb[NL80211_KEY_DATA]) {
660 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
661 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
662 }
663
664 if (tb[NL80211_KEY_SEQ]) {
665 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
666 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
667 }
668
669 if (tb[NL80211_KEY_CIPHER])
670 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
671
Johannes Berge31b8212010-10-05 19:39:30 +0200672 if (tb[NL80211_KEY_TYPE]) {
673 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
674 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
675 return -EINVAL;
676 }
677
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100678 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
679 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100680 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
681 tb[NL80211_KEY_DEFAULT_TYPES],
682 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100683 if (err)
684 return err;
685
686 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
687 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
688 }
689
Johannes Bergb9454e82009-07-08 13:29:08 +0200690 return 0;
691}
692
693static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
694{
695 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
696 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
697 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
698 }
699
700 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
701 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
702 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
703 }
704
705 if (info->attrs[NL80211_ATTR_KEY_IDX])
706 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
707
708 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
709 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
710
711 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
712 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
713
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (k->def) {
715 k->def_uni = true;
716 k->def_multi = true;
717 }
718 if (k->defmgmt)
719 k->def_multi = true;
720
Johannes Berge31b8212010-10-05 19:39:30 +0200721 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
722 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
723 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
724 return -EINVAL;
725 }
726
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100727 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
728 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
729 int err = nla_parse_nested(
730 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
731 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
732 nl80211_key_default_policy);
733 if (err)
734 return err;
735
736 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
737 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
738 }
739
Johannes Bergb9454e82009-07-08 13:29:08 +0200740 return 0;
741}
742
743static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
744{
745 int err;
746
747 memset(k, 0, sizeof(*k));
748 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200749 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200750
751 if (info->attrs[NL80211_ATTR_KEY])
752 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
753 else
754 err = nl80211_parse_key_old(info, k);
755
756 if (err)
757 return err;
758
759 if (k->def && k->defmgmt)
760 return -EINVAL;
761
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100762 if (k->defmgmt) {
763 if (k->def_uni || !k->def_multi)
764 return -EINVAL;
765 }
766
Johannes Bergb9454e82009-07-08 13:29:08 +0200767 if (k->idx != -1) {
768 if (k->defmgmt) {
769 if (k->idx < 4 || k->idx > 5)
770 return -EINVAL;
771 } else if (k->def) {
772 if (k->idx < 0 || k->idx > 3)
773 return -EINVAL;
774 } else {
775 if (k->idx < 0 || k->idx > 5)
776 return -EINVAL;
777 }
778 }
779
780 return 0;
781}
782
Johannes Bergfffd0932009-07-08 14:22:54 +0200783static struct cfg80211_cached_keys *
784nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530785 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200786{
787 struct key_parse parse;
788 struct nlattr *key;
789 struct cfg80211_cached_keys *result;
790 int rem, err, def = 0;
791
792 result = kzalloc(sizeof(*result), GFP_KERNEL);
793 if (!result)
794 return ERR_PTR(-ENOMEM);
795
796 result->def = -1;
797 result->defmgmt = -1;
798
799 nla_for_each_nested(key, keys, rem) {
800 memset(&parse, 0, sizeof(parse));
801 parse.idx = -1;
802
803 err = nl80211_parse_key_new(key, &parse);
804 if (err)
805 goto error;
806 err = -EINVAL;
807 if (!parse.p.key)
808 goto error;
809 if (parse.idx < 0 || parse.idx > 4)
810 goto error;
811 if (parse.def) {
812 if (def)
813 goto error;
814 def = 1;
815 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100816 if (!parse.def_uni || !parse.def_multi)
817 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 } else if (parse.defmgmt)
819 goto error;
820 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200821 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200822 if (err)
823 goto error;
824 result->params[parse.idx].cipher = parse.p.cipher;
825 result->params[parse.idx].key_len = parse.p.key_len;
826 result->params[parse.idx].key = result->data[parse.idx];
827 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530828
829 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
830 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
831 if (no_ht)
832 *no_ht = true;
833 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200834 }
835
836 return result;
837 error:
838 kfree(result);
839 return ERR_PTR(err);
840}
841
842static int nl80211_key_allowed(struct wireless_dev *wdev)
843{
844 ASSERT_WDEV_LOCK(wdev);
845
Johannes Bergfffd0932009-07-08 14:22:54 +0200846 switch (wdev->iftype) {
847 case NL80211_IFTYPE_AP:
848 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200849 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700850 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200851 break;
852 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200854 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200855 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200856 return -ENOLINK;
857 break;
858 default:
859 return -EINVAL;
860 }
861
862 return 0;
863}
864
Jouni Malinen664834d2014-01-15 00:01:44 +0200865static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
866 struct nlattr *tb)
867{
868 struct ieee80211_channel *chan;
869
870 if (tb == NULL)
871 return NULL;
872 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
873 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
874 return NULL;
875 return chan;
876}
877
Johannes Berg7527a782011-05-13 10:58:57 +0200878static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
879{
880 struct nlattr *nl_modes = nla_nest_start(msg, attr);
881 int i;
882
883 if (!nl_modes)
884 goto nla_put_failure;
885
886 i = 0;
887 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400888 if ((ifmodes & 1) && nla_put_flag(msg, i))
889 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200890 ifmodes >>= 1;
891 i++;
892 }
893
894 nla_nest_end(msg, nl_modes);
895 return 0;
896
897nla_put_failure:
898 return -ENOBUFS;
899}
900
901static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100902 struct sk_buff *msg,
903 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200904{
905 struct nlattr *nl_combis;
906 int i, j;
907
908 nl_combis = nla_nest_start(msg,
909 NL80211_ATTR_INTERFACE_COMBINATIONS);
910 if (!nl_combis)
911 goto nla_put_failure;
912
913 for (i = 0; i < wiphy->n_iface_combinations; i++) {
914 const struct ieee80211_iface_combination *c;
915 struct nlattr *nl_combi, *nl_limits;
916
917 c = &wiphy->iface_combinations[i];
918
919 nl_combi = nla_nest_start(msg, i + 1);
920 if (!nl_combi)
921 goto nla_put_failure;
922
923 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
924 if (!nl_limits)
925 goto nla_put_failure;
926
927 for (j = 0; j < c->n_limits; j++) {
928 struct nlattr *nl_limit;
929
930 nl_limit = nla_nest_start(msg, j + 1);
931 if (!nl_limit)
932 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400933 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
934 c->limits[j].max))
935 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200936 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
937 c->limits[j].types))
938 goto nla_put_failure;
939 nla_nest_end(msg, nl_limit);
940 }
941
942 nla_nest_end(msg, nl_limits);
943
David S. Miller9360ffd2012-03-29 04:41:26 -0400944 if (c->beacon_int_infra_match &&
945 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
946 goto nla_put_failure;
947 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
948 c->num_different_channels) ||
949 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
950 c->max_interfaces))
951 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100952 if (large &&
953 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
954 c->radar_detect_widths))
955 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200956
957 nla_nest_end(msg, nl_combi);
958 }
959
960 nla_nest_end(msg, nl_combis);
961
962 return 0;
963nla_put_failure:
964 return -ENOBUFS;
965}
966
Johannes Berg3713b4e2013-02-14 16:19:38 +0100967#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100968static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
969 struct sk_buff *msg)
970{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200971 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100972 struct nlattr *nl_tcp;
973
974 if (!tcp)
975 return 0;
976
977 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
978 if (!nl_tcp)
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 (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
986 tcp->data_payload_max))
987 return -ENOBUFS;
988
989 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
990 return -ENOBUFS;
991
992 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
993 sizeof(*tcp->tok), tcp->tok))
994 return -ENOBUFS;
995
996 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
997 tcp->data_interval_max))
998 return -ENOBUFS;
999
1000 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1001 tcp->wake_payload_max))
1002 return -ENOBUFS;
1003
1004 nla_nest_end(msg, nl_tcp);
1005 return 0;
1006}
1007
Johannes Berg3713b4e2013-02-14 16:19:38 +01001008static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001009 struct cfg80211_registered_device *dev,
1010 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001011{
1012 struct nlattr *nl_wowlan;
1013
Johannes Berg964dc9e2013-06-03 17:25:34 +02001014 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001015 return 0;
1016
1017 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1018 if (!nl_wowlan)
1019 return -ENOBUFS;
1020
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001023 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001024 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001025 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001026 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001027 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001028 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001029 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001030 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001031 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001033 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001034 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001035 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001036 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1037 return -ENOBUFS;
1038
Johannes Berg964dc9e2013-06-03 17:25:34 +02001039 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001040 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001041 .max_patterns = dev->wiphy.wowlan->n_patterns,
1042 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1043 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1044 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001045 };
1046
1047 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1048 sizeof(pat), &pat))
1049 return -ENOBUFS;
1050 }
1051
Johannes Bergb56cf722013-02-20 01:02:38 +01001052 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1053 return -ENOBUFS;
1054
Johannes Berg3713b4e2013-02-14 16:19:38 +01001055 nla_nest_end(msg, nl_wowlan);
1056
1057 return 0;
1058}
1059#endif
1060
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001061static int nl80211_send_coalesce(struct sk_buff *msg,
1062 struct cfg80211_registered_device *dev)
1063{
1064 struct nl80211_coalesce_rule_support rule;
1065
1066 if (!dev->wiphy.coalesce)
1067 return 0;
1068
1069 rule.max_rules = dev->wiphy.coalesce->n_rules;
1070 rule.max_delay = dev->wiphy.coalesce->max_delay;
1071 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1072 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1073 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1074 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1075
1076 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1077 return -ENOBUFS;
1078
1079 return 0;
1080}
1081
Johannes Berg3713b4e2013-02-14 16:19:38 +01001082static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1083 struct ieee80211_supported_band *sband)
1084{
1085 struct nlattr *nl_rates, *nl_rate;
1086 struct ieee80211_rate *rate;
1087 int i;
1088
1089 /* add HT info */
1090 if (sband->ht_cap.ht_supported &&
1091 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1092 sizeof(sband->ht_cap.mcs),
1093 &sband->ht_cap.mcs) ||
1094 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1095 sband->ht_cap.cap) ||
1096 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1097 sband->ht_cap.ampdu_factor) ||
1098 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1099 sband->ht_cap.ampdu_density)))
1100 return -ENOBUFS;
1101
1102 /* add VHT info */
1103 if (sband->vht_cap.vht_supported &&
1104 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1105 sizeof(sband->vht_cap.vht_mcs),
1106 &sband->vht_cap.vht_mcs) ||
1107 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1108 sband->vht_cap.cap)))
1109 return -ENOBUFS;
1110
1111 /* add bitrates */
1112 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1113 if (!nl_rates)
1114 return -ENOBUFS;
1115
1116 for (i = 0; i < sband->n_bitrates; i++) {
1117 nl_rate = nla_nest_start(msg, i);
1118 if (!nl_rate)
1119 return -ENOBUFS;
1120
1121 rate = &sband->bitrates[i];
1122 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1123 rate->bitrate))
1124 return -ENOBUFS;
1125 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1126 nla_put_flag(msg,
1127 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1128 return -ENOBUFS;
1129
1130 nla_nest_end(msg, nl_rate);
1131 }
1132
1133 nla_nest_end(msg, nl_rates);
1134
1135 return 0;
1136}
1137
1138static int
1139nl80211_send_mgmt_stypes(struct sk_buff *msg,
1140 const struct ieee80211_txrx_stypes *mgmt_stypes)
1141{
1142 u16 stypes;
1143 struct nlattr *nl_ftypes, *nl_ifs;
1144 enum nl80211_iftype ift;
1145 int i;
1146
1147 if (!mgmt_stypes)
1148 return 0;
1149
1150 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1151 if (!nl_ifs)
1152 return -ENOBUFS;
1153
1154 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1155 nl_ftypes = nla_nest_start(msg, ift);
1156 if (!nl_ftypes)
1157 return -ENOBUFS;
1158 i = 0;
1159 stypes = mgmt_stypes[ift].tx;
1160 while (stypes) {
1161 if ((stypes & 1) &&
1162 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1163 (i << 4) | IEEE80211_FTYPE_MGMT))
1164 return -ENOBUFS;
1165 stypes >>= 1;
1166 i++;
1167 }
1168 nla_nest_end(msg, nl_ftypes);
1169 }
1170
1171 nla_nest_end(msg, nl_ifs);
1172
1173 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1174 if (!nl_ifs)
1175 return -ENOBUFS;
1176
1177 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1178 nl_ftypes = nla_nest_start(msg, ift);
1179 if (!nl_ftypes)
1180 return -ENOBUFS;
1181 i = 0;
1182 stypes = mgmt_stypes[ift].rx;
1183 while (stypes) {
1184 if ((stypes & 1) &&
1185 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1186 (i << 4) | IEEE80211_FTYPE_MGMT))
1187 return -ENOBUFS;
1188 stypes >>= 1;
1189 i++;
1190 }
1191 nla_nest_end(msg, nl_ftypes);
1192 }
1193 nla_nest_end(msg, nl_ifs);
1194
1195 return 0;
1196}
1197
Johannes Berg86e8cf92013-06-19 10:57:22 +02001198struct nl80211_dump_wiphy_state {
1199 s64 filter_wiphy;
1200 long start;
1201 long split_start, band_start, chan_start;
1202 bool split;
1203};
1204
Johannes Berg3713b4e2013-02-14 16:19:38 +01001205static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1206 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001207 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001208{
1209 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001210 struct nlattr *nl_bands, *nl_band;
1211 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001212 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001213 enum ieee80211_band band;
1214 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001215 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001216 const struct ieee80211_txrx_stypes *mgmt_stypes =
1217 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001218 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001219
Eric W. Biederman15e47302012-09-07 20:12:54 +00001220 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001221 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001222 return -ENOBUFS;
1223
Johannes Berg86e8cf92013-06-19 10:57:22 +02001224 if (WARN_ON(!state))
1225 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001226
David S. Miller9360ffd2012-03-29 04:41:26 -04001227 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001228 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1229 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001231 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001232 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001233
Johannes Berg86e8cf92013-06-19 10:57:22 +02001234 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001235 case 0:
1236 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1237 dev->wiphy.retry_short) ||
1238 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1239 dev->wiphy.retry_long) ||
1240 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1241 dev->wiphy.frag_threshold) ||
1242 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1243 dev->wiphy.rts_threshold) ||
1244 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1245 dev->wiphy.coverage_class) ||
1246 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1247 dev->wiphy.max_scan_ssids) ||
1248 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1249 dev->wiphy.max_sched_scan_ssids) ||
1250 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1251 dev->wiphy.max_scan_ie_len) ||
1252 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1253 dev->wiphy.max_sched_scan_ie_len) ||
1254 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1255 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001256 goto nla_put_failure;
1257
Johannes Berg3713b4e2013-02-14 16:19:38 +01001258 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1259 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1260 goto nla_put_failure;
1261 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1262 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1263 goto nla_put_failure;
1264 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1265 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1266 goto nla_put_failure;
1267 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1268 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1269 goto nla_put_failure;
1270 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1271 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1272 goto nla_put_failure;
1273 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1274 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001275 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001276 state->split_start++;
1277 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001278 break;
1279 case 1:
1280 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1281 sizeof(u32) * dev->wiphy.n_cipher_suites,
1282 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001283 goto nla_put_failure;
1284
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1286 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001287 goto nla_put_failure;
1288
Johannes Berg3713b4e2013-02-14 16:19:38 +01001289 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1290 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1291 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001292
Johannes Berg3713b4e2013-02-14 16:19:38 +01001293 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1294 dev->wiphy.available_antennas_tx) ||
1295 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1296 dev->wiphy.available_antennas_rx))
1297 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001298
Johannes Berg3713b4e2013-02-14 16:19:38 +01001299 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1300 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1301 dev->wiphy.probe_resp_offload))
1302 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001303
Johannes Berg3713b4e2013-02-14 16:19:38 +01001304 if ((dev->wiphy.available_antennas_tx ||
1305 dev->wiphy.available_antennas_rx) &&
1306 dev->ops->get_antenna) {
1307 u32 tx_ant = 0, rx_ant = 0;
1308 int res;
1309 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1310 if (!res) {
1311 if (nla_put_u32(msg,
1312 NL80211_ATTR_WIPHY_ANTENNA_TX,
1313 tx_ant) ||
1314 nla_put_u32(msg,
1315 NL80211_ATTR_WIPHY_ANTENNA_RX,
1316 rx_ant))
1317 goto nla_put_failure;
1318 }
Johannes Bergee688b002008-01-24 19:38:39 +01001319 }
1320
Johannes Berg86e8cf92013-06-19 10:57:22 +02001321 state->split_start++;
1322 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001323 break;
1324 case 2:
1325 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1326 dev->wiphy.interface_modes))
1327 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001328 state->split_start++;
1329 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330 break;
1331 case 3:
1332 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1333 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001334 goto nla_put_failure;
1335
Johannes Berg86e8cf92013-06-19 10:57:22 +02001336 for (band = state->band_start;
1337 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001338 struct ieee80211_supported_band *sband;
1339
1340 sband = dev->wiphy.bands[band];
1341
1342 if (!sband)
1343 continue;
1344
1345 nl_band = nla_nest_start(msg, band);
1346 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001347 goto nla_put_failure;
1348
Johannes Berg86e8cf92013-06-19 10:57:22 +02001349 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001350 case 0:
1351 if (nl80211_send_band_rateinfo(msg, sband))
1352 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001353 state->chan_start++;
1354 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001355 break;
1356 default:
1357 /* add frequencies */
1358 nl_freqs = nla_nest_start(
1359 msg, NL80211_BAND_ATTR_FREQS);
1360 if (!nl_freqs)
1361 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001362
Johannes Berg86e8cf92013-06-19 10:57:22 +02001363 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001364 i < sband->n_channels;
1365 i++) {
1366 nl_freq = nla_nest_start(msg, i);
1367 if (!nl_freq)
1368 goto nla_put_failure;
1369
1370 chan = &sband->channels[i];
1371
Johannes Berg86e8cf92013-06-19 10:57:22 +02001372 if (nl80211_msg_put_channel(
1373 msg, chan,
1374 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001375 goto nla_put_failure;
1376
1377 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001378 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001379 break;
1380 }
1381 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001382 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001383 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001384 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001385 nla_nest_end(msg, nl_freqs);
1386 }
1387
1388 nla_nest_end(msg, nl_band);
1389
Johannes Berg86e8cf92013-06-19 10:57:22 +02001390 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001391 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001392 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001393 band--;
1394 break;
1395 }
Johannes Bergee688b002008-01-24 19:38:39 +01001396 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001397 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001398
Johannes Berg3713b4e2013-02-14 16:19:38 +01001399 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001400 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001401 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001402 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001403
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001405 if (state->band_start == 0 && state->chan_start == 0)
1406 state->split_start++;
1407 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001408 break;
1409 case 4:
1410 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1411 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001412 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001413
1414 i = 0;
1415#define CMD(op, n) \
1416 do { \
1417 if (dev->ops->op) { \
1418 i++; \
1419 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1420 goto nla_put_failure; \
1421 } \
1422 } while (0)
1423
1424 CMD(add_virtual_intf, NEW_INTERFACE);
1425 CMD(change_virtual_intf, SET_INTERFACE);
1426 CMD(add_key, NEW_KEY);
1427 CMD(start_ap, START_AP);
1428 CMD(add_station, NEW_STATION);
1429 CMD(add_mpath, NEW_MPATH);
1430 CMD(update_mesh_config, SET_MESH_CONFIG);
1431 CMD(change_bss, SET_BSS);
1432 CMD(auth, AUTHENTICATE);
1433 CMD(assoc, ASSOCIATE);
1434 CMD(deauth, DEAUTHENTICATE);
1435 CMD(disassoc, DISASSOCIATE);
1436 CMD(join_ibss, JOIN_IBSS);
1437 CMD(join_mesh, JOIN_MESH);
1438 CMD(set_pmksa, SET_PMKSA);
1439 CMD(del_pmksa, DEL_PMKSA);
1440 CMD(flush_pmksa, FLUSH_PMKSA);
1441 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1442 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1443 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1444 CMD(mgmt_tx, FRAME);
1445 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1446 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1447 i++;
1448 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1449 goto nla_put_failure;
1450 }
1451 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1452 dev->ops->join_mesh) {
1453 i++;
1454 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1455 goto nla_put_failure;
1456 }
1457 CMD(set_wds_peer, SET_WDS_PEER);
1458 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1459 CMD(tdls_mgmt, TDLS_MGMT);
1460 CMD(tdls_oper, TDLS_OPER);
1461 }
1462 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1463 CMD(sched_scan_start, START_SCHED_SCAN);
1464 CMD(probe_client, PROBE_CLIENT);
1465 CMD(set_noack_map, SET_NOACK_MAP);
1466 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1467 i++;
1468 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1469 goto nla_put_failure;
1470 }
1471 CMD(start_p2p_device, START_P2P_DEVICE);
1472 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001473 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001474 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1475 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001476 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1477 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001478 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001479 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001480
Kalle Valo4745fc02011-11-17 19:06:10 +02001481#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001482 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001483#endif
1484
Johannes Berg8fdc6212009-03-14 09:34:01 +01001485#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001486
Johannes Berg3713b4e2013-02-14 16:19:38 +01001487 if (dev->ops->connect || dev->ops->auth) {
1488 i++;
1489 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001490 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001491 }
1492
Johannes Berg3713b4e2013-02-14 16:19:38 +01001493 if (dev->ops->disconnect || dev->ops->deauth) {
1494 i++;
1495 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1496 goto nla_put_failure;
1497 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001498
Johannes Berg3713b4e2013-02-14 16:19:38 +01001499 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001500 state->split_start++;
1501 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001502 break;
1503 case 5:
1504 if (dev->ops->remain_on_channel &&
1505 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1506 nla_put_u32(msg,
1507 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1508 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001509 goto nla_put_failure;
1510
Johannes Berg3713b4e2013-02-14 16:19:38 +01001511 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1512 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1513 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001514
Johannes Berg3713b4e2013-02-14 16:19:38 +01001515 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1516 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001517 state->split_start++;
1518 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001519 break;
1520 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001521#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001522 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001523 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
1525 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001526 break;
1527#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001528 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001529#endif
1530 case 7:
1531 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1532 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001533 goto nla_put_failure;
1534
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1536 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001537 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001538
Johannes Berg86e8cf92013-06-19 10:57:22 +02001539 state->split_start++;
1540 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001541 break;
1542 case 8:
1543 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1544 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1545 dev->wiphy.ap_sme_capa))
1546 goto nla_put_failure;
1547
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001548 features = dev->wiphy.features;
1549 /*
1550 * We can only add the per-channel limit information if the
1551 * dump is split, otherwise it makes it too big. Therefore
1552 * only advertise it in that case.
1553 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001554 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001555 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1556 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001557 goto nla_put_failure;
1558
1559 if (dev->wiphy.ht_capa_mod_mask &&
1560 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1561 sizeof(*dev->wiphy.ht_capa_mod_mask),
1562 dev->wiphy.ht_capa_mod_mask))
1563 goto nla_put_failure;
1564
1565 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1566 dev->wiphy.max_acl_mac_addrs &&
1567 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1568 dev->wiphy.max_acl_mac_addrs))
1569 goto nla_put_failure;
1570
1571 /*
1572 * Any information below this point is only available to
1573 * applications that can deal with it being split. This
1574 * helps ensure that newly added capabilities don't break
1575 * older tools by overrunning their buffers.
1576 *
1577 * We still increment split_start so that in the split
1578 * case we'll continue with more data in the next round,
1579 * but break unconditionally so unsplit data stops here.
1580 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001581 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001582 break;
1583 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001584 if (dev->wiphy.extended_capabilities &&
1585 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1586 dev->wiphy.extended_capabilities_len,
1587 dev->wiphy.extended_capabilities) ||
1588 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1589 dev->wiphy.extended_capabilities_len,
1590 dev->wiphy.extended_capabilities_mask)))
1591 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001592
Johannes Bergee2aca32013-02-21 17:36:01 +01001593 if (dev->wiphy.vht_capa_mod_mask &&
1594 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1595 sizeof(*dev->wiphy.vht_capa_mod_mask),
1596 dev->wiphy.vht_capa_mod_mask))
1597 goto nla_put_failure;
1598
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001599 state->split_start++;
1600 break;
1601 case 10:
1602 if (nl80211_send_coalesce(msg, dev))
1603 goto nla_put_failure;
1604
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001605 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1606 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1607 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1608 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001609
1610 if (dev->wiphy.max_ap_assoc_sta &&
1611 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1612 dev->wiphy.max_ap_assoc_sta))
1613 goto nla_put_failure;
1614
Johannes Bergad7e7182013-11-13 13:37:47 +01001615 state->split_start++;
1616 break;
1617 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001618 if (dev->wiphy.n_vendor_commands) {
1619 const struct nl80211_vendor_cmd_info *info;
1620 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001621
Johannes Berg567ffc32013-12-18 14:43:31 +01001622 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1623 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001624 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001625
1626 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1627 info = &dev->wiphy.vendor_commands[i].info;
1628 if (nla_put(msg, i + 1, sizeof(*info), info))
1629 goto nla_put_failure;
1630 }
1631 nla_nest_end(msg, nested);
1632 }
1633
1634 if (dev->wiphy.n_vendor_events) {
1635 const struct nl80211_vendor_cmd_info *info;
1636 struct nlattr *nested;
1637
1638 nested = nla_nest_start(msg,
1639 NL80211_ATTR_VENDOR_EVENTS);
1640 if (!nested)
1641 goto nla_put_failure;
1642
1643 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1644 info = &dev->wiphy.vendor_events[i];
1645 if (nla_put(msg, i + 1, sizeof(*info), info))
1646 goto nla_put_failure;
1647 }
1648 nla_nest_end(msg, nested);
1649 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001650
Johannes Berg3713b4e2013-02-14 16:19:38 +01001651 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001652 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001653 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001654 }
Johannes Berg55682962007-09-20 13:09:35 -04001655 return genlmsg_end(msg, hdr);
1656
1657 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001658 genlmsg_cancel(msg, hdr);
1659 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001660}
1661
Johannes Berg86e8cf92013-06-19 10:57:22 +02001662static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1663 struct netlink_callback *cb,
1664 struct nl80211_dump_wiphy_state *state)
1665{
1666 struct nlattr **tb = nl80211_fam.attrbuf;
1667 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1668 tb, nl80211_fam.maxattr, nl80211_policy);
1669 /* ignore parse errors for backward compatibility */
1670 if (ret)
1671 return 0;
1672
1673 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1674 if (tb[NL80211_ATTR_WIPHY])
1675 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1676 if (tb[NL80211_ATTR_WDEV])
1677 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1678 if (tb[NL80211_ATTR_IFINDEX]) {
1679 struct net_device *netdev;
1680 struct cfg80211_registered_device *rdev;
1681 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1682
Ying Xue7f2b8562014-01-15 10:23:45 +08001683 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001684 if (!netdev)
1685 return -ENODEV;
1686 if (netdev->ieee80211_ptr) {
1687 rdev = wiphy_to_dev(
1688 netdev->ieee80211_ptr->wiphy);
1689 state->filter_wiphy = rdev->wiphy_idx;
1690 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001691 }
1692
1693 return 0;
1694}
1695
Johannes Berg55682962007-09-20 13:09:35 -04001696static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1697{
Johannes Berg645e77d2013-03-01 14:03:49 +01001698 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001700 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001701
Johannes Berg5fe231e2013-05-08 21:45:15 +02001702 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001703 if (!state) {
1704 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001705 if (!state) {
1706 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001707 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001708 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001709 state->filter_wiphy = -1;
1710 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1711 if (ret) {
1712 kfree(state);
1713 rtnl_unlock();
1714 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001717 }
1718
Johannes Berg79c97e92009-07-07 03:56:12 +02001719 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001720 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1721 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001722 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001723 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001724 if (state->filter_wiphy != -1 &&
1725 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001726 continue;
1727 /* attempt to fit multiple wiphy data chunks into the skb */
1728 do {
1729 ret = nl80211_send_wiphy(dev, skb,
1730 NETLINK_CB(cb->skb).portid,
1731 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001732 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001733 if (ret < 0) {
1734 /*
1735 * If sending the wiphy data didn't fit (ENOBUFS
1736 * or EMSGSIZE returned), this SKB is still
1737 * empty (so it's not too big because another
1738 * wiphy dataset is already in the skb) and
1739 * we've not tried to adjust the dump allocation
1740 * yet ... then adjust the alloc size to be
1741 * bigger, and return 1 but with the empty skb.
1742 * This results in an empty message being RX'ed
1743 * in userspace, but that is ignored.
1744 *
1745 * We can then retry with the larger buffer.
1746 */
1747 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001748 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001749 cb->min_dump_alloc < 4096) {
1750 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001751 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001752 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001753 return 1;
1754 }
1755 idx--;
1756 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001757 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001758 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001759 break;
Johannes Berg55682962007-09-20 13:09:35 -04001760 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001761 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001762
Johannes Berg86e8cf92013-06-19 10:57:22 +02001763 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001764
1765 return skb->len;
1766}
1767
Johannes Berg86e8cf92013-06-19 10:57:22 +02001768static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1769{
1770 kfree((void *)cb->args[0]);
1771 return 0;
1772}
1773
Johannes Berg55682962007-09-20 13:09:35 -04001774static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1775{
1776 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001777 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001778 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001779
Johannes Berg645e77d2013-03-01 14:03:49 +01001780 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001781 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001782 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001783
Johannes Berg3713b4e2013-02-14 16:19:38 +01001784 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001785 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001786 nlmsg_free(msg);
1787 return -ENOBUFS;
1788 }
Johannes Berg55682962007-09-20 13:09:35 -04001789
Johannes Berg134e6372009-07-10 09:51:34 +00001790 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001791}
1792
Jouni Malinen31888482008-10-30 16:59:24 +02001793static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1794 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1795 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1796 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1797 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1798 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1799};
1800
1801static int parse_txq_params(struct nlattr *tb[],
1802 struct ieee80211_txq_params *txq_params)
1803{
Johannes Berga3304b02012-03-28 11:04:24 +02001804 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001805 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1806 !tb[NL80211_TXQ_ATTR_AIFS])
1807 return -EINVAL;
1808
Johannes Berga3304b02012-03-28 11:04:24 +02001809 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001810 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1811 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1812 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1813 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1814
Johannes Berga3304b02012-03-28 11:04:24 +02001815 if (txq_params->ac >= NL80211_NUM_ACS)
1816 return -EINVAL;
1817
Jouni Malinen31888482008-10-30 16:59:24 +02001818 return 0;
1819}
1820
Johannes Bergf444de02010-05-05 15:25:02 +02001821static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1822{
1823 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001824 * You can only set the channel explicitly for WDS interfaces,
1825 * all others have their channel managed via their respective
1826 * "establish a connection" command (connect, join, ...)
1827 *
1828 * For AP/GO and mesh mode, the channel can be set with the
1829 * channel userspace API, but is only stored and passed to the
1830 * low-level driver when the AP starts or the mesh is joined.
1831 * This is for backward compatibility, userspace can also give
1832 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001833 *
1834 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001835 * whatever else is going on, so they have their own special
1836 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001837 */
1838 return !wdev ||
1839 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001840 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001841 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1842 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001843}
1844
Johannes Berg683b6d32012-11-08 21:25:48 +01001845static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1846 struct genl_info *info,
1847 struct cfg80211_chan_def *chandef)
1848{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301849 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001850
1851 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1852 return -EINVAL;
1853
1854 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1855
1856 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001857 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1858 chandef->center_freq1 = control_freq;
1859 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001860
1861 /* Primary channel not allowed */
1862 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1863 return -EINVAL;
1864
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001865 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1866 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001867
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001868 chantype = nla_get_u32(
1869 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1870
1871 switch (chantype) {
1872 case NL80211_CHAN_NO_HT:
1873 case NL80211_CHAN_HT20:
1874 case NL80211_CHAN_HT40PLUS:
1875 case NL80211_CHAN_HT40MINUS:
1876 cfg80211_chandef_create(chandef, chandef->chan,
1877 chantype);
1878 break;
1879 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001880 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001881 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001882 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1883 chandef->width =
1884 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1885 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1886 chandef->center_freq1 =
1887 nla_get_u32(
1888 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1889 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1890 chandef->center_freq2 =
1891 nla_get_u32(
1892 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1893 }
1894
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001895 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001896 return -EINVAL;
1897
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001898 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1899 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001900 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001901
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001902 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1903 chandef->width == NL80211_CHAN_WIDTH_10) &&
1904 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1905 return -EINVAL;
1906
Johannes Berg683b6d32012-11-08 21:25:48 +01001907 return 0;
1908}
1909
Johannes Bergf444de02010-05-05 15:25:02 +02001910static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1911 struct wireless_dev *wdev,
1912 struct genl_info *info)
1913{
Johannes Berg683b6d32012-11-08 21:25:48 +01001914 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001915 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001916 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1917
1918 if (wdev)
1919 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001920
Johannes Bergf444de02010-05-05 15:25:02 +02001921 if (!nl80211_can_set_dev_channel(wdev))
1922 return -EOPNOTSUPP;
1923
Johannes Berg683b6d32012-11-08 21:25:48 +01001924 result = nl80211_parse_chandef(rdev, info, &chandef);
1925 if (result)
1926 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001927
Johannes Berge8c9bd52012-06-06 08:18:22 +02001928 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001929 case NL80211_IFTYPE_AP:
1930 case NL80211_IFTYPE_P2P_GO:
1931 if (wdev->beacon_interval) {
1932 result = -EBUSY;
1933 break;
1934 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001935 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 result = -EINVAL;
1937 break;
1938 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001939 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001940 result = 0;
1941 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001942 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001943 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001944 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001945 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001946 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001947 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001948 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001949 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001950 }
Johannes Bergf444de02010-05-05 15:25:02 +02001951
1952 return result;
1953}
1954
1955static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1956{
Johannes Berg4c476992010-10-04 21:36:35 +02001957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1958 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001959
Johannes Berg4c476992010-10-04 21:36:35 +02001960 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001961}
1962
Bill Jordane8347eb2010-10-01 13:54:28 -04001963static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1964{
Johannes Berg43b19952010-10-07 13:10:30 +02001965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1966 struct net_device *dev = info->user_ptr[1];
1967 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001968 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001969
1970 if (!info->attrs[NL80211_ATTR_MAC])
1971 return -EINVAL;
1972
Johannes Berg43b19952010-10-07 13:10:30 +02001973 if (netif_running(dev))
1974 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001975
Johannes Berg43b19952010-10-07 13:10:30 +02001976 if (!rdev->ops->set_wds_peer)
1977 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001978
Johannes Berg43b19952010-10-07 13:10:30 +02001979 if (wdev->iftype != NL80211_IFTYPE_WDS)
1980 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001981
1982 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001983 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001984}
1985
1986
Johannes Berg55682962007-09-20 13:09:35 -04001987static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1988{
1989 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001990 struct net_device *netdev = NULL;
1991 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001992 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001993 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001994 u32 changed;
1995 u8 retry_short = 0, retry_long = 0;
1996 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001997 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001998
Johannes Berg5fe231e2013-05-08 21:45:15 +02001999 ASSERT_RTNL();
2000
Johannes Bergf444de02010-05-05 15:25:02 +02002001 /*
2002 * Try to find the wiphy and netdev. Normally this
2003 * function shouldn't need the netdev, but this is
2004 * done for backward compatibility -- previously
2005 * setting the channel was done per wiphy, but now
2006 * it is per netdev. Previous userland like hostapd
2007 * also passed a netdev to set_wiphy, so that it is
2008 * possible to let that go to the right netdev!
2009 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002010
Johannes Bergf444de02010-05-05 15:25:02 +02002011 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2012 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2013
Ying Xue7f2b8562014-01-15 10:23:45 +08002014 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002015 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002016 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002017 else
Johannes Bergf444de02010-05-05 15:25:02 +02002018 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002019 }
2020
Johannes Bergf444de02010-05-05 15:25:02 +02002021 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002022 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2023 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002024 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002025 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002026 wdev = NULL;
2027 netdev = NULL;
2028 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002029 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002030 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002031
2032 /*
2033 * end workaround code, by now the rdev is available
2034 * and locked, and wdev may or may not be NULL.
2035 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002036
2037 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002038 result = cfg80211_dev_rename(
2039 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002040
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002041 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002042 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002043
Jouni Malinen31888482008-10-30 16:59:24 +02002044 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2045 struct ieee80211_txq_params txq_params;
2046 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2047
Ying Xue7f2b8562014-01-15 10:23:45 +08002048 if (!rdev->ops->set_txq_params)
2049 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002050
Ying Xue7f2b8562014-01-15 10:23:45 +08002051 if (!netdev)
2052 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002053
Johannes Berg133a3ff2011-11-03 14:50:13 +01002054 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002055 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2056 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002057
Ying Xue7f2b8562014-01-15 10:23:45 +08002058 if (!netif_running(netdev))
2059 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002060
Jouni Malinen31888482008-10-30 16:59:24 +02002061 nla_for_each_nested(nl_txq_params,
2062 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2063 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002064 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2065 nla_data(nl_txq_params),
2066 nla_len(nl_txq_params),
2067 txq_params_policy);
2068 if (result)
2069 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002070 result = parse_txq_params(tb, &txq_params);
2071 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002072 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002073
Hila Gonene35e4d22012-06-27 17:19:42 +03002074 result = rdev_set_txq_params(rdev, netdev,
2075 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002076 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002077 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002078 }
2079 }
2080
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002081 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002082 result = __nl80211_set_channel(rdev,
2083 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2084 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002085 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002086 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002087 }
2088
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002089 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002090 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002091 enum nl80211_tx_power_setting type;
2092 int idx, mbm = 0;
2093
Johannes Bergc8442112012-10-24 10:17:18 +02002094 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2095 txp_wdev = NULL;
2096
Ying Xue7f2b8562014-01-15 10:23:45 +08002097 if (!rdev->ops->set_tx_power)
2098 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002099
2100 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2101 type = nla_get_u32(info->attrs[idx]);
2102
2103 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002104 (type != NL80211_TX_POWER_AUTOMATIC))
2105 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002106
2107 if (type != NL80211_TX_POWER_AUTOMATIC) {
2108 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2109 mbm = nla_get_u32(info->attrs[idx]);
2110 }
2111
Johannes Bergc8442112012-10-24 10:17:18 +02002112 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002113 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002114 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002115 }
2116
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002117 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2118 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2119 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002120 if ((!rdev->wiphy.available_antennas_tx &&
2121 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002122 !rdev->ops->set_antenna)
2123 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002124
2125 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2126 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2127
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002128 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002129 * available antenna masks, except for the "all" mask */
2130 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002131 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2132 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002133
Bruno Randolf7f531e02010-12-16 11:30:22 +09002134 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2135 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002136
Hila Gonene35e4d22012-06-27 17:19:42 +03002137 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002138 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002139 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002140 }
2141
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002142 changed = 0;
2143
2144 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2145 retry_short = nla_get_u8(
2146 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002147 if (retry_short == 0)
2148 return -EINVAL;
2149
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002150 changed |= WIPHY_PARAM_RETRY_SHORT;
2151 }
2152
2153 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2154 retry_long = nla_get_u8(
2155 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002156 if (retry_long == 0)
2157 return -EINVAL;
2158
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002159 changed |= WIPHY_PARAM_RETRY_LONG;
2160 }
2161
2162 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2163 frag_threshold = nla_get_u32(
2164 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002165 if (frag_threshold < 256)
2166 return -EINVAL;
2167
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002168 if (frag_threshold != (u32) -1) {
2169 /*
2170 * Fragments (apart from the last one) are required to
2171 * have even length. Make the fragmentation code
2172 * simpler by stripping LSB should someone try to use
2173 * odd threshold value.
2174 */
2175 frag_threshold &= ~0x1;
2176 }
2177 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2178 }
2179
2180 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2181 rts_threshold = nla_get_u32(
2182 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2183 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2184 }
2185
Lukáš Turek81077e82009-12-21 22:50:47 +01002186 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2187 coverage_class = nla_get_u8(
2188 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2189 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2190 }
2191
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002192 if (changed) {
2193 u8 old_retry_short, old_retry_long;
2194 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002195 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002196
Ying Xue7f2b8562014-01-15 10:23:45 +08002197 if (!rdev->ops->set_wiphy_params)
2198 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002199
2200 old_retry_short = rdev->wiphy.retry_short;
2201 old_retry_long = rdev->wiphy.retry_long;
2202 old_frag_threshold = rdev->wiphy.frag_threshold;
2203 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002204 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002205
2206 if (changed & WIPHY_PARAM_RETRY_SHORT)
2207 rdev->wiphy.retry_short = retry_short;
2208 if (changed & WIPHY_PARAM_RETRY_LONG)
2209 rdev->wiphy.retry_long = retry_long;
2210 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2211 rdev->wiphy.frag_threshold = frag_threshold;
2212 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2213 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002214 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2215 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002216
Hila Gonene35e4d22012-06-27 17:19:42 +03002217 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002218 if (result) {
2219 rdev->wiphy.retry_short = old_retry_short;
2220 rdev->wiphy.retry_long = old_retry_long;
2221 rdev->wiphy.frag_threshold = old_frag_threshold;
2222 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002223 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002224 }
2225 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002226 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002227}
2228
Johannes Berg71bbc992012-06-15 15:30:18 +02002229static inline u64 wdev_id(struct wireless_dev *wdev)
2230{
2231 return (u64)wdev->identifier |
2232 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2233}
Johannes Berg55682962007-09-20 13:09:35 -04002234
Johannes Berg683b6d32012-11-08 21:25:48 +01002235static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002236 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002237{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002238 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002239
Johannes Berg683b6d32012-11-08 21:25:48 +01002240 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2241 chandef->chan->center_freq))
2242 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002243 switch (chandef->width) {
2244 case NL80211_CHAN_WIDTH_20_NOHT:
2245 case NL80211_CHAN_WIDTH_20:
2246 case NL80211_CHAN_WIDTH_40:
2247 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2248 cfg80211_get_chandef_type(chandef)))
2249 return -ENOBUFS;
2250 break;
2251 default:
2252 break;
2253 }
2254 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2255 return -ENOBUFS;
2256 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2257 return -ENOBUFS;
2258 if (chandef->center_freq2 &&
2259 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002260 return -ENOBUFS;
2261 return 0;
2262}
2263
Eric W. Biederman15e47302012-09-07 20:12:54 +00002264static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002265 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002266 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002267{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002268 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002269 void *hdr;
2270
Eric W. Biederman15e47302012-09-07 20:12:54 +00002271 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002272 if (!hdr)
2273 return -1;
2274
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002275 if (dev &&
2276 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002277 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002278 goto nla_put_failure;
2279
2280 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2281 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002282 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002283 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002284 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2285 rdev->devlist_generation ^
2286 (cfg80211_rdev_list_generation << 2)))
2287 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002288
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002289 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002290 int ret;
2291 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002292
Johannes Berg683b6d32012-11-08 21:25:48 +01002293 ret = rdev_get_channel(rdev, wdev, &chandef);
2294 if (ret == 0) {
2295 if (nl80211_send_chandef(msg, &chandef))
2296 goto nla_put_failure;
2297 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002298 }
2299
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002300 if (wdev->ssid_len) {
2301 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2302 goto nla_put_failure;
2303 }
2304
Johannes Berg55682962007-09-20 13:09:35 -04002305 return genlmsg_end(msg, hdr);
2306
2307 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002308 genlmsg_cancel(msg, hdr);
2309 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002310}
2311
2312static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2313{
2314 int wp_idx = 0;
2315 int if_idx = 0;
2316 int wp_start = cb->args[0];
2317 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002318 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002319 struct wireless_dev *wdev;
2320
Johannes Berg5fe231e2013-05-08 21:45:15 +02002321 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002322 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2323 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002324 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002325 if (wp_idx < wp_start) {
2326 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002327 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002328 }
Johannes Berg55682962007-09-20 13:09:35 -04002329 if_idx = 0;
2330
Johannes Berg89a54e42012-06-15 14:33:17 +02002331 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002332 if (if_idx < if_start) {
2333 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002334 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002336 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002337 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002338 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339 goto out;
2340 }
2341 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002342 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002343
2344 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002345 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002346 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002347 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002348
2349 cb->args[0] = wp_idx;
2350 cb->args[1] = if_idx;
2351
2352 return skb->len;
2353}
2354
2355static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2356{
2357 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002358 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002359 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002360
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002362 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002363 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002364
Eric W. Biederman15e47302012-09-07 20:12:54 +00002365 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002366 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002367 nlmsg_free(msg);
2368 return -ENOBUFS;
2369 }
Johannes Berg55682962007-09-20 13:09:35 -04002370
Johannes Berg134e6372009-07-10 09:51:34 +00002371 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002372}
2373
Michael Wu66f7ac52008-01-31 19:48:22 +01002374static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2375 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2376 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2377 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2378 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2379 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002380 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002381};
2382
2383static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2384{
2385 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2386 int flag;
2387
2388 *mntrflags = 0;
2389
2390 if (!nla)
2391 return -EINVAL;
2392
2393 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2394 nla, mntr_flags_policy))
2395 return -EINVAL;
2396
2397 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2398 if (flags[flag])
2399 *mntrflags |= (1<<flag);
2400
2401 return 0;
2402}
2403
Johannes Berg9bc383d2009-11-19 11:55:19 +01002404static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002405 struct net_device *netdev, u8 use_4addr,
2406 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002407{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002408 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002409 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002410 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002411 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002412 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002413
2414 switch (iftype) {
2415 case NL80211_IFTYPE_AP_VLAN:
2416 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2417 return 0;
2418 break;
2419 case NL80211_IFTYPE_STATION:
2420 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2421 return 0;
2422 break;
2423 default:
2424 break;
2425 }
2426
2427 return -EOPNOTSUPP;
2428}
2429
Johannes Berg55682962007-09-20 13:09:35 -04002430static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2431{
Johannes Berg4c476992010-10-04 21:36:35 +02002432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002433 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002434 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002435 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002436 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002437 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002438 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002439
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002440 memset(&params, 0, sizeof(params));
2441
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002443
Johannes Berg723b0382008-09-16 20:22:09 +02002444 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002445 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002446 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002447 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002448 if (ntype > NL80211_IFTYPE_MAX)
2449 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002450 }
2451
Johannes Berg92ffe052008-09-16 20:39:36 +02002452 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002453 struct wireless_dev *wdev = dev->ieee80211_ptr;
2454
Johannes Berg4c476992010-10-04 21:36:35 +02002455 if (ntype != NL80211_IFTYPE_MESH_POINT)
2456 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002457 if (netif_running(dev))
2458 return -EBUSY;
2459
2460 wdev_lock(wdev);
2461 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2462 IEEE80211_MAX_MESH_ID_LEN);
2463 wdev->mesh_id_up_len =
2464 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2465 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2466 wdev->mesh_id_up_len);
2467 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002468 }
2469
Felix Fietkau8b787642009-11-10 18:53:10 +01002470 if (info->attrs[NL80211_ATTR_4ADDR]) {
2471 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2472 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002473 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002474 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002475 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002476 } else {
2477 params.use_4addr = -1;
2478 }
2479
Johannes Berg92ffe052008-09-16 20:39:36 +02002480 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002481 if (ntype != NL80211_IFTYPE_MONITOR)
2482 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002483 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2484 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002485 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002486 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002487
2488 flags = &_flags;
2489 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002490 }
Johannes Berg3b858752009-03-12 09:55:09 +01002491
Luciano Coelho18003292013-08-29 13:26:57 +03002492 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002493 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2494 return -EOPNOTSUPP;
2495
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002496 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002497 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002498 else
2499 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002500
Johannes Berg9bc383d2009-11-19 11:55:19 +01002501 if (!err && params.use_4addr != -1)
2502 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2503
Johannes Berg55682962007-09-20 13:09:35 -04002504 return err;
2505}
2506
2507static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2508{
Johannes Berg4c476992010-10-04 21:36:35 +02002509 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002510 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002511 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002512 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002513 int err;
2514 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002515 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002516
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002517 memset(&params, 0, sizeof(params));
2518
Johannes Berg55682962007-09-20 13:09:35 -04002519 if (!info->attrs[NL80211_ATTR_IFNAME])
2520 return -EINVAL;
2521
2522 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2523 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2524 if (type > NL80211_IFTYPE_MAX)
2525 return -EINVAL;
2526 }
2527
Johannes Berg79c97e92009-07-07 03:56:12 +02002528 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002529 !(rdev->wiphy.interface_modes & (1 << type)))
2530 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002531
Arend van Spriel1c18f142013-01-08 10:17:27 +01002532 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2533 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2534 ETH_ALEN);
2535 if (!is_valid_ether_addr(params.macaddr))
2536 return -EADDRNOTAVAIL;
2537 }
2538
Johannes Berg9bc383d2009-11-19 11:55:19 +01002539 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002540 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002541 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002542 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002543 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002544 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002545
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2547 if (!msg)
2548 return -ENOMEM;
2549
Michael Wu66f7ac52008-01-31 19:48:22 +01002550 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2551 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2552 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002553
Luciano Coelho18003292013-08-29 13:26:57 +03002554 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002555 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2556 return -EOPNOTSUPP;
2557
Hila Gonene35e4d22012-06-27 17:19:42 +03002558 wdev = rdev_add_virtual_intf(rdev,
2559 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2560 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002561 if (IS_ERR(wdev)) {
2562 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002563 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002564 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002565
Johannes Berg98104fde2012-06-16 00:19:54 +02002566 switch (type) {
2567 case NL80211_IFTYPE_MESH_POINT:
2568 if (!info->attrs[NL80211_ATTR_MESH_ID])
2569 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002570 wdev_lock(wdev);
2571 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2572 IEEE80211_MAX_MESH_ID_LEN);
2573 wdev->mesh_id_up_len =
2574 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2575 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2576 wdev->mesh_id_up_len);
2577 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002578 break;
2579 case NL80211_IFTYPE_P2P_DEVICE:
2580 /*
2581 * P2P Device doesn't have a netdev, so doesn't go
2582 * through the netdev notifier and must be added here
2583 */
2584 mutex_init(&wdev->mtx);
2585 INIT_LIST_HEAD(&wdev->event_list);
2586 spin_lock_init(&wdev->event_lock);
2587 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2588 spin_lock_init(&wdev->mgmt_registrations_lock);
2589
Johannes Berg98104fde2012-06-16 00:19:54 +02002590 wdev->identifier = ++rdev->wdev_id;
2591 list_add_rcu(&wdev->list, &rdev->wdev_list);
2592 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002593 break;
2594 default:
2595 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002596 }
2597
Eric W. Biederman15e47302012-09-07 20:12:54 +00002598 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002599 rdev, wdev) < 0) {
2600 nlmsg_free(msg);
2601 return -ENOBUFS;
2602 }
2603
2604 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002605}
2606
2607static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2608{
Johannes Berg4c476992010-10-04 21:36:35 +02002609 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002610 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002611
Johannes Berg4c476992010-10-04 21:36:35 +02002612 if (!rdev->ops->del_virtual_intf)
2613 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002614
Johannes Berg84efbb82012-06-16 00:00:26 +02002615 /*
2616 * If we remove a wireless device without a netdev then clear
2617 * user_ptr[1] so that nl80211_post_doit won't dereference it
2618 * to check if it needs to do dev_put(). Otherwise it crashes
2619 * since the wdev has been freed, unlike with a netdev where
2620 * we need the dev_put() for the netdev to really be freed.
2621 */
2622 if (!wdev->netdev)
2623 info->user_ptr[1] = NULL;
2624
Hila Gonene35e4d22012-06-27 17:19:42 +03002625 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002626}
2627
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002628static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2629{
2630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2631 struct net_device *dev = info->user_ptr[1];
2632 u16 noack_map;
2633
2634 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2635 return -EINVAL;
2636
2637 if (!rdev->ops->set_noack_map)
2638 return -EOPNOTSUPP;
2639
2640 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2641
Hila Gonene35e4d22012-06-27 17:19:42 +03002642 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002643}
2644
Johannes Berg41ade002007-12-19 02:03:29 +01002645struct get_key_cookie {
2646 struct sk_buff *msg;
2647 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002648 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002649};
2650
2651static void get_key_callback(void *c, struct key_params *params)
2652{
Johannes Bergb9454e82009-07-08 13:29:08 +02002653 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002654 struct get_key_cookie *cookie = c;
2655
David S. Miller9360ffd2012-03-29 04:41:26 -04002656 if ((params->key &&
2657 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2658 params->key_len, params->key)) ||
2659 (params->seq &&
2660 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2661 params->seq_len, params->seq)) ||
2662 (params->cipher &&
2663 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2664 params->cipher)))
2665 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002666
Johannes Bergb9454e82009-07-08 13:29:08 +02002667 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2668 if (!key)
2669 goto nla_put_failure;
2670
David S. Miller9360ffd2012-03-29 04:41:26 -04002671 if ((params->key &&
2672 nla_put(cookie->msg, NL80211_KEY_DATA,
2673 params->key_len, params->key)) ||
2674 (params->seq &&
2675 nla_put(cookie->msg, NL80211_KEY_SEQ,
2676 params->seq_len, params->seq)) ||
2677 (params->cipher &&
2678 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2679 params->cipher)))
2680 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002681
David S. Miller9360ffd2012-03-29 04:41:26 -04002682 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2683 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002684
2685 nla_nest_end(cookie->msg, key);
2686
Johannes Berg41ade002007-12-19 02:03:29 +01002687 return;
2688 nla_put_failure:
2689 cookie->error = 1;
2690}
2691
2692static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2693{
Johannes Berg4c476992010-10-04 21:36:35 +02002694 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002695 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002696 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002697 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002698 const u8 *mac_addr = NULL;
2699 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002700 struct get_key_cookie cookie = {
2701 .error = 0,
2702 };
2703 void *hdr;
2704 struct sk_buff *msg;
2705
2706 if (info->attrs[NL80211_ATTR_KEY_IDX])
2707 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2708
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002709 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002710 return -EINVAL;
2711
2712 if (info->attrs[NL80211_ATTR_MAC])
2713 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2714
Johannes Berge31b8212010-10-05 19:39:30 +02002715 pairwise = !!mac_addr;
2716 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2717 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2718 if (kt >= NUM_NL80211_KEYTYPES)
2719 return -EINVAL;
2720 if (kt != NL80211_KEYTYPE_GROUP &&
2721 kt != NL80211_KEYTYPE_PAIRWISE)
2722 return -EINVAL;
2723 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2724 }
2725
Johannes Berg4c476992010-10-04 21:36:35 +02002726 if (!rdev->ops->get_key)
2727 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002728
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002729 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002730 if (!msg)
2731 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002732
Eric W. Biederman15e47302012-09-07 20:12:54 +00002733 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002734 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002735 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002736 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002737
2738 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002739 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002740
David S. Miller9360ffd2012-03-29 04:41:26 -04002741 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2742 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2743 goto nla_put_failure;
2744 if (mac_addr &&
2745 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2746 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002747
Johannes Berge31b8212010-10-05 19:39:30 +02002748 if (pairwise && mac_addr &&
2749 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2750 return -ENOENT;
2751
Hila Gonene35e4d22012-06-27 17:19:42 +03002752 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2753 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002754
2755 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002756 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002757
2758 if (cookie.error)
2759 goto nla_put_failure;
2760
2761 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002762 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002763
2764 nla_put_failure:
2765 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002766 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002767 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002768 return err;
2769}
2770
2771static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2772{
Johannes Berg4c476992010-10-04 21:36:35 +02002773 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002774 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002775 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002776 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002777
Johannes Bergb9454e82009-07-08 13:29:08 +02002778 err = nl80211_parse_key(info, &key);
2779 if (err)
2780 return err;
2781
2782 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002783 return -EINVAL;
2784
Johannes Bergb9454e82009-07-08 13:29:08 +02002785 /* only support setting default key */
2786 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002787 return -EINVAL;
2788
Johannes Bergfffd0932009-07-08 14:22:54 +02002789 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002790
2791 if (key.def) {
2792 if (!rdev->ops->set_default_key) {
2793 err = -EOPNOTSUPP;
2794 goto out;
2795 }
2796
2797 err = nl80211_key_allowed(dev->ieee80211_ptr);
2798 if (err)
2799 goto out;
2800
Hila Gonene35e4d22012-06-27 17:19:42 +03002801 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002802 key.def_uni, key.def_multi);
2803
2804 if (err)
2805 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002806
Johannes Berg3d23e342009-09-29 23:27:28 +02002807#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002808 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002809#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002810 } else {
2811 if (key.def_uni || !key.def_multi) {
2812 err = -EINVAL;
2813 goto out;
2814 }
2815
2816 if (!rdev->ops->set_default_mgmt_key) {
2817 err = -EOPNOTSUPP;
2818 goto out;
2819 }
2820
2821 err = nl80211_key_allowed(dev->ieee80211_ptr);
2822 if (err)
2823 goto out;
2824
Hila Gonene35e4d22012-06-27 17:19:42 +03002825 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002826 if (err)
2827 goto out;
2828
2829#ifdef CONFIG_CFG80211_WEXT
2830 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2831#endif
2832 }
2833
2834 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002835 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002836
Johannes Berg41ade002007-12-19 02:03:29 +01002837 return err;
2838}
2839
2840static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2841{
Johannes Berg4c476992010-10-04 21:36:35 +02002842 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002843 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002844 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002845 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002846 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002847
Johannes Bergb9454e82009-07-08 13:29:08 +02002848 err = nl80211_parse_key(info, &key);
2849 if (err)
2850 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002851
Johannes Bergb9454e82009-07-08 13:29:08 +02002852 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002853 return -EINVAL;
2854
Johannes Berg41ade002007-12-19 02:03:29 +01002855 if (info->attrs[NL80211_ATTR_MAC])
2856 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2857
Johannes Berge31b8212010-10-05 19:39:30 +02002858 if (key.type == -1) {
2859 if (mac_addr)
2860 key.type = NL80211_KEYTYPE_PAIRWISE;
2861 else
2862 key.type = NL80211_KEYTYPE_GROUP;
2863 }
2864
2865 /* for now */
2866 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2867 key.type != NL80211_KEYTYPE_GROUP)
2868 return -EINVAL;
2869
Johannes Berg4c476992010-10-04 21:36:35 +02002870 if (!rdev->ops->add_key)
2871 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002872
Johannes Berge31b8212010-10-05 19:39:30 +02002873 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2874 key.type == NL80211_KEYTYPE_PAIRWISE,
2875 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002876 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002877
2878 wdev_lock(dev->ieee80211_ptr);
2879 err = nl80211_key_allowed(dev->ieee80211_ptr);
2880 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002881 err = rdev_add_key(rdev, dev, key.idx,
2882 key.type == NL80211_KEYTYPE_PAIRWISE,
2883 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002884 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002885
Johannes Berg41ade002007-12-19 02:03:29 +01002886 return err;
2887}
2888
2889static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2890{
Johannes Berg4c476992010-10-04 21:36:35 +02002891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002892 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002893 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002894 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002895 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002896
Johannes Bergb9454e82009-07-08 13:29:08 +02002897 err = nl80211_parse_key(info, &key);
2898 if (err)
2899 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002900
2901 if (info->attrs[NL80211_ATTR_MAC])
2902 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2903
Johannes Berge31b8212010-10-05 19:39:30 +02002904 if (key.type == -1) {
2905 if (mac_addr)
2906 key.type = NL80211_KEYTYPE_PAIRWISE;
2907 else
2908 key.type = NL80211_KEYTYPE_GROUP;
2909 }
2910
2911 /* for now */
2912 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2913 key.type != NL80211_KEYTYPE_GROUP)
2914 return -EINVAL;
2915
Johannes Berg4c476992010-10-04 21:36:35 +02002916 if (!rdev->ops->del_key)
2917 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002918
Johannes Bergfffd0932009-07-08 14:22:54 +02002919 wdev_lock(dev->ieee80211_ptr);
2920 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002921
2922 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2923 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2924 err = -ENOENT;
2925
Johannes Bergfffd0932009-07-08 14:22:54 +02002926 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002927 err = rdev_del_key(rdev, dev, key.idx,
2928 key.type == NL80211_KEYTYPE_PAIRWISE,
2929 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002930
Johannes Berg3d23e342009-09-29 23:27:28 +02002931#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002932 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002933 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002934 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002935 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002936 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2937 }
2938#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002939 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002940
Johannes Berg41ade002007-12-19 02:03:29 +01002941 return err;
2942}
2943
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302944/* This function returns an error or the number of nested attributes */
2945static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2946{
2947 struct nlattr *attr;
2948 int n_entries = 0, tmp;
2949
2950 nla_for_each_nested(attr, nl_attr, tmp) {
2951 if (nla_len(attr) != ETH_ALEN)
2952 return -EINVAL;
2953
2954 n_entries++;
2955 }
2956
2957 return n_entries;
2958}
2959
2960/*
2961 * This function parses ACL information and allocates memory for ACL data.
2962 * On successful return, the calling function is responsible to free the
2963 * ACL buffer returned by this function.
2964 */
2965static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2966 struct genl_info *info)
2967{
2968 enum nl80211_acl_policy acl_policy;
2969 struct nlattr *attr;
2970 struct cfg80211_acl_data *acl;
2971 int i = 0, n_entries, tmp;
2972
2973 if (!wiphy->max_acl_mac_addrs)
2974 return ERR_PTR(-EOPNOTSUPP);
2975
2976 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2977 return ERR_PTR(-EINVAL);
2978
2979 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2980 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2981 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2982 return ERR_PTR(-EINVAL);
2983
2984 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2985 return ERR_PTR(-EINVAL);
2986
2987 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2988 if (n_entries < 0)
2989 return ERR_PTR(n_entries);
2990
2991 if (n_entries > wiphy->max_acl_mac_addrs)
2992 return ERR_PTR(-ENOTSUPP);
2993
2994 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2995 GFP_KERNEL);
2996 if (!acl)
2997 return ERR_PTR(-ENOMEM);
2998
2999 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3000 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3001 i++;
3002 }
3003
3004 acl->n_acl_entries = n_entries;
3005 acl->acl_policy = acl_policy;
3006
3007 return acl;
3008}
3009
3010static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3011{
3012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3013 struct net_device *dev = info->user_ptr[1];
3014 struct cfg80211_acl_data *acl;
3015 int err;
3016
3017 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3018 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3019 return -EOPNOTSUPP;
3020
3021 if (!dev->ieee80211_ptr->beacon_interval)
3022 return -EINVAL;
3023
3024 acl = parse_acl_data(&rdev->wiphy, info);
3025 if (IS_ERR(acl))
3026 return PTR_ERR(acl);
3027
3028 err = rdev_set_mac_acl(rdev, dev, acl);
3029
3030 kfree(acl);
3031
3032 return err;
3033}
3034
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003035static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003036 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003037{
Johannes Berg88600202012-02-13 15:17:18 +01003038 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003039
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003040 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3041 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3042 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3043 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003044 return -EINVAL;
3045
Johannes Berg88600202012-02-13 15:17:18 +01003046 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003047
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003048 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3049 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3050 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003051 if (!bcn->head_len)
3052 return -EINVAL;
3053 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003054 }
3055
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003056 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3057 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3058 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003059 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060 }
3061
Johannes Berg4c476992010-10-04 21:36:35 +02003062 if (!haveinfo)
3063 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003064
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003065 if (attrs[NL80211_ATTR_IE]) {
3066 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3067 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003068 }
3069
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003070 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003071 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003072 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003073 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003074 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003075 }
3076
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003077 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003078 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003079 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003080 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003081 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003082 }
3083
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003084 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3085 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3086 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003087 }
3088
Johannes Berg88600202012-02-13 15:17:18 +01003089 return 0;
3090}
3091
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003092static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3093 struct cfg80211_ap_settings *params)
3094{
3095 struct wireless_dev *wdev;
3096 bool ret = false;
3097
Johannes Berg89a54e42012-06-15 14:33:17 +02003098 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003099 if (wdev->iftype != NL80211_IFTYPE_AP &&
3100 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3101 continue;
3102
Johannes Berg683b6d32012-11-08 21:25:48 +01003103 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003104 continue;
3105
Johannes Berg683b6d32012-11-08 21:25:48 +01003106 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003107 ret = true;
3108 break;
3109 }
3110
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003111 return ret;
3112}
3113
Jouni Malinene39e5b52012-09-30 19:29:39 +03003114static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3115 enum nl80211_auth_type auth_type,
3116 enum nl80211_commands cmd)
3117{
3118 if (auth_type > NL80211_AUTHTYPE_MAX)
3119 return false;
3120
3121 switch (cmd) {
3122 case NL80211_CMD_AUTHENTICATE:
3123 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3124 auth_type == NL80211_AUTHTYPE_SAE)
3125 return false;
3126 return true;
3127 case NL80211_CMD_CONNECT:
3128 case NL80211_CMD_START_AP:
3129 /* SAE not supported yet */
3130 if (auth_type == NL80211_AUTHTYPE_SAE)
3131 return false;
3132 return true;
3133 default:
3134 return false;
3135 }
3136}
3137
Johannes Berg88600202012-02-13 15:17:18 +01003138static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3139{
3140 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3141 struct net_device *dev = info->user_ptr[1];
3142 struct wireless_dev *wdev = dev->ieee80211_ptr;
3143 struct cfg80211_ap_settings params;
3144 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003145 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003146
3147 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3148 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3149 return -EOPNOTSUPP;
3150
3151 if (!rdev->ops->start_ap)
3152 return -EOPNOTSUPP;
3153
3154 if (wdev->beacon_interval)
3155 return -EALREADY;
3156
3157 memset(&params, 0, sizeof(params));
3158
3159 /* these are required for START_AP */
3160 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3161 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3162 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3163 return -EINVAL;
3164
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003165 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003166 if (err)
3167 return err;
3168
3169 params.beacon_interval =
3170 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3171 params.dtim_period =
3172 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3173
3174 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3175 if (err)
3176 return err;
3177
3178 /*
3179 * In theory, some of these attributes should be required here
3180 * but since they were not used when the command was originally
3181 * added, keep them optional for old user space programs to let
3182 * them continue to work with drivers that do not need the
3183 * additional information -- drivers must check!
3184 */
3185 if (info->attrs[NL80211_ATTR_SSID]) {
3186 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3187 params.ssid_len =
3188 nla_len(info->attrs[NL80211_ATTR_SSID]);
3189 if (params.ssid_len == 0 ||
3190 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3191 return -EINVAL;
3192 }
3193
3194 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3195 params.hidden_ssid = nla_get_u32(
3196 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3197 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3198 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3199 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3200 return -EINVAL;
3201 }
3202
3203 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3204
3205 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3206 params.auth_type = nla_get_u32(
3207 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003208 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3209 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003210 return -EINVAL;
3211 } else
3212 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3213
3214 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3215 NL80211_MAX_NR_CIPHER_SUITES);
3216 if (err)
3217 return err;
3218
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303219 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3220 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3221 return -EOPNOTSUPP;
3222 params.inactivity_timeout = nla_get_u16(
3223 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3224 }
3225
Johannes Berg53cabad2012-11-14 15:17:28 +01003226 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3227 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3228 return -EINVAL;
3229 params.p2p_ctwindow =
3230 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3231 if (params.p2p_ctwindow > 127)
3232 return -EINVAL;
3233 if (params.p2p_ctwindow != 0 &&
3234 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3235 return -EINVAL;
3236 }
3237
3238 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3239 u8 tmp;
3240
3241 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3242 return -EINVAL;
3243 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3244 if (tmp > 1)
3245 return -EINVAL;
3246 params.p2p_opp_ps = tmp;
3247 if (params.p2p_opp_ps != 0 &&
3248 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3249 return -EINVAL;
3250 }
3251
Johannes Bergaa430da2012-05-16 23:50:18 +02003252 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003253 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3254 if (err)
3255 return err;
3256 } else if (wdev->preset_chandef.chan) {
3257 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003258 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003259 return -EINVAL;
3260
Johannes Berg683b6d32012-11-08 21:25:48 +01003261 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003262 return -EINVAL;
3263
Simon Wunderlich04f39042013-02-08 18:16:19 +01003264 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3265 if (err < 0)
3266 return err;
3267 if (err) {
3268 radar_detect_width = BIT(params.chandef.width);
3269 params.radar_required = true;
3270 }
3271
Simon Wunderlich04f39042013-02-08 18:16:19 +01003272 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3273 params.chandef.chan,
3274 CHAN_MODE_SHARED,
3275 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003276 if (err)
3277 return err;
3278
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303279 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3280 params.acl = parse_acl_data(&rdev->wiphy, info);
3281 if (IS_ERR(params.acl))
3282 return PTR_ERR(params.acl);
3283 }
3284
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003285 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003286 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003287 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003288 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003289 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003290 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003291 wdev->ssid_len = params.ssid_len;
3292 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003293 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003294 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303295
3296 kfree(params.acl);
3297
Johannes Berg56d18932011-05-09 18:41:15 +02003298 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003299}
3300
Johannes Berg88600202012-02-13 15:17:18 +01003301static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3302{
3303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3304 struct net_device *dev = info->user_ptr[1];
3305 struct wireless_dev *wdev = dev->ieee80211_ptr;
3306 struct cfg80211_beacon_data params;
3307 int err;
3308
3309 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3310 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3311 return -EOPNOTSUPP;
3312
3313 if (!rdev->ops->change_beacon)
3314 return -EOPNOTSUPP;
3315
3316 if (!wdev->beacon_interval)
3317 return -EINVAL;
3318
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003319 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003320 if (err)
3321 return err;
3322
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003323 wdev_lock(wdev);
3324 err = rdev_change_beacon(rdev, dev, &params);
3325 wdev_unlock(wdev);
3326
3327 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003328}
3329
3330static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003331{
Johannes Berg4c476992010-10-04 21:36:35 +02003332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3333 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003334
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003335 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003336}
3337
Johannes Berg5727ef12007-12-19 02:03:34 +01003338static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3339 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3340 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3341 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003342 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003343 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003344 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003345};
3346
Johannes Bergeccb8e82009-05-11 21:57:56 +03003347static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003348 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003349 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003350{
3351 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003352 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003353 int flag;
3354
Johannes Bergeccb8e82009-05-11 21:57:56 +03003355 /*
3356 * Try parsing the new attribute first so userspace
3357 * can specify both for older kernels.
3358 */
3359 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3360 if (nla) {
3361 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003362
Johannes Bergeccb8e82009-05-11 21:57:56 +03003363 sta_flags = nla_data(nla);
3364 params->sta_flags_mask = sta_flags->mask;
3365 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003366 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003367 if ((params->sta_flags_mask |
3368 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3369 return -EINVAL;
3370 return 0;
3371 }
3372
3373 /* if present, parse the old attribute */
3374
3375 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003376 if (!nla)
3377 return 0;
3378
3379 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3380 nla, sta_flags_policy))
3381 return -EINVAL;
3382
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003383 /*
3384 * Only allow certain flags for interface types so that
3385 * other attributes are silently ignored. Remember that
3386 * this is backward compatibility code with old userspace
3387 * and shouldn't be hit in other cases anyway.
3388 */
3389 switch (iftype) {
3390 case NL80211_IFTYPE_AP:
3391 case NL80211_IFTYPE_AP_VLAN:
3392 case NL80211_IFTYPE_P2P_GO:
3393 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3394 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3395 BIT(NL80211_STA_FLAG_WME) |
3396 BIT(NL80211_STA_FLAG_MFP);
3397 break;
3398 case NL80211_IFTYPE_P2P_CLIENT:
3399 case NL80211_IFTYPE_STATION:
3400 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3401 BIT(NL80211_STA_FLAG_TDLS_PEER);
3402 break;
3403 case NL80211_IFTYPE_MESH_POINT:
3404 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3405 BIT(NL80211_STA_FLAG_MFP) |
3406 BIT(NL80211_STA_FLAG_AUTHORIZED);
3407 default:
3408 return -EINVAL;
3409 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003410
Johannes Berg3383b5a2012-05-10 20:14:43 +02003411 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3412 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003413 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003414
Johannes Berg3383b5a2012-05-10 20:14:43 +02003415 /* no longer support new API additions in old API */
3416 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3417 return -EINVAL;
3418 }
3419 }
3420
Johannes Berg5727ef12007-12-19 02:03:34 +01003421 return 0;
3422}
3423
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003424static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3425 int attr)
3426{
3427 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003428 u32 bitrate;
3429 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003430
3431 rate = nla_nest_start(msg, attr);
3432 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003433 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003434
3435 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3436 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003437 /* report 16-bit bitrate only if we can */
3438 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003439 if (bitrate > 0 &&
3440 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3441 return false;
3442 if (bitrate_compat > 0 &&
3443 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3444 return false;
3445
3446 if (info->flags & RATE_INFO_FLAGS_MCS) {
3447 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3448 return false;
3449 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3450 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3451 return false;
3452 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3453 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3454 return false;
3455 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3456 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3457 return false;
3458 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3459 return false;
3460 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3461 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3462 return false;
3463 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3464 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3465 return false;
3466 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3467 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3468 return false;
3469 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3470 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3471 return false;
3472 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3473 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3474 return false;
3475 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003476
3477 nla_nest_end(msg, rate);
3478 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003479}
3480
Felix Fietkau119363c2013-04-22 16:29:30 +02003481static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3482 int id)
3483{
3484 void *attr;
3485 int i = 0;
3486
3487 if (!mask)
3488 return true;
3489
3490 attr = nla_nest_start(msg, id);
3491 if (!attr)
3492 return false;
3493
3494 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3495 if (!(mask & BIT(i)))
3496 continue;
3497
3498 if (nla_put_u8(msg, i, signal[i]))
3499 return false;
3500 }
3501
3502 nla_nest_end(msg, attr);
3503
3504 return true;
3505}
3506
Eric W. Biederman15e47302012-09-07 20:12:54 +00003507static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003508 int flags,
3509 struct cfg80211_registered_device *rdev,
3510 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003511 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003512{
3513 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003514 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003515
Eric W. Biederman15e47302012-09-07 20:12:54 +00003516 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003517 if (!hdr)
3518 return -1;
3519
David S. Miller9360ffd2012-03-29 04:41:26 -04003520 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3521 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3522 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3523 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003524
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003525 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3526 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003527 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003528 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3529 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3530 sinfo->connected_time))
3531 goto nla_put_failure;
3532 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3533 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3534 sinfo->inactive_time))
3535 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003536 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3537 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003538 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003539 (u32)sinfo->rx_bytes))
3540 goto nla_put_failure;
3541 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003542 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003543 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3544 (u32)sinfo->tx_bytes))
3545 goto nla_put_failure;
3546 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3547 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003548 sinfo->rx_bytes))
3549 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003550 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3551 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003552 sinfo->tx_bytes))
3553 goto nla_put_failure;
3554 if ((sinfo->filled & STATION_INFO_LLID) &&
3555 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3556 goto nla_put_failure;
3557 if ((sinfo->filled & STATION_INFO_PLID) &&
3558 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3559 goto nla_put_failure;
3560 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3561 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3562 sinfo->plink_state))
3563 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003564 switch (rdev->wiphy.signal_type) {
3565 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003566 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3567 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3568 sinfo->signal))
3569 goto nla_put_failure;
3570 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3571 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3572 sinfo->signal_avg))
3573 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003574 break;
3575 default:
3576 break;
3577 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003578 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3579 if (!nl80211_put_signal(msg, sinfo->chains,
3580 sinfo->chain_signal,
3581 NL80211_STA_INFO_CHAIN_SIGNAL))
3582 goto nla_put_failure;
3583 }
3584 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3585 if (!nl80211_put_signal(msg, sinfo->chains,
3586 sinfo->chain_signal_avg,
3587 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3588 goto nla_put_failure;
3589 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003590 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003591 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3592 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003593 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003594 }
3595 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3596 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3597 NL80211_STA_INFO_RX_BITRATE))
3598 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003599 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003600 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3601 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3602 sinfo->rx_packets))
3603 goto nla_put_failure;
3604 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3605 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3606 sinfo->tx_packets))
3607 goto nla_put_failure;
3608 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3609 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3610 sinfo->tx_retries))
3611 goto nla_put_failure;
3612 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3613 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3614 sinfo->tx_failed))
3615 goto nla_put_failure;
3616 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3617 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3618 sinfo->beacon_loss_count))
3619 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003620 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3621 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3622 sinfo->local_pm))
3623 goto nla_put_failure;
3624 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3625 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3626 sinfo->peer_pm))
3627 goto nla_put_failure;
3628 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3629 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3630 sinfo->nonpeer_pm))
3631 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003632 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3633 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3634 if (!bss_param)
3635 goto nla_put_failure;
3636
David S. Miller9360ffd2012-03-29 04:41:26 -04003637 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3638 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3639 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3640 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3641 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3642 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3643 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3644 sinfo->bss_param.dtim_period) ||
3645 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3646 sinfo->bss_param.beacon_interval))
3647 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003648
3649 nla_nest_end(msg, bss_param);
3650 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003651 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3652 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3653 sizeof(struct nl80211_sta_flag_update),
3654 &sinfo->sta_flags))
3655 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003656 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3657 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3658 sinfo->t_offset))
3659 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003660 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003661
David S. Miller9360ffd2012-03-29 04:41:26 -04003662 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3663 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3664 sinfo->assoc_req_ies))
3665 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003666
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003667 return genlmsg_end(msg, hdr);
3668
3669 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003670 genlmsg_cancel(msg, hdr);
3671 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003672}
3673
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003674static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003675 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003676{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003677 struct station_info sinfo;
3678 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003679 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003680 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003681 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003682 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003683
Johannes Berg97990a02013-04-19 01:02:55 +02003684 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003685 if (err)
3686 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003687
Johannes Berg97990a02013-04-19 01:02:55 +02003688 if (!wdev->netdev) {
3689 err = -EINVAL;
3690 goto out_err;
3691 }
3692
Johannes Bergbba95fe2008-07-29 13:22:51 +02003693 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003694 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003695 goto out_err;
3696 }
3697
Johannes Bergbba95fe2008-07-29 13:22:51 +02003698 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003699 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003700 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003701 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003702 if (err == -ENOENT)
3703 break;
3704 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003705 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003706
3707 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003708 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003709 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003710 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003711 &sinfo) < 0)
3712 goto out;
3713
3714 sta_idx++;
3715 }
3716
3717
3718 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003719 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003720 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003721 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003722 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003723
3724 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003725}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003726
Johannes Berg5727ef12007-12-19 02:03:34 +01003727static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3728{
Johannes Berg4c476992010-10-04 21:36:35 +02003729 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3730 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003731 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003732 struct sk_buff *msg;
3733 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003734 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003735
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003736 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003737
3738 if (!info->attrs[NL80211_ATTR_MAC])
3739 return -EINVAL;
3740
3741 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3742
Johannes Berg4c476992010-10-04 21:36:35 +02003743 if (!rdev->ops->get_station)
3744 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003745
Hila Gonene35e4d22012-06-27 17:19:42 +03003746 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003747 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003748 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003749
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003751 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003752 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003753
Eric W. Biederman15e47302012-09-07 20:12:54 +00003754 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003755 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003756 nlmsg_free(msg);
3757 return -ENOBUFS;
3758 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003759
Johannes Berg4c476992010-10-04 21:36:35 +02003760 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003761}
3762
Johannes Berg77ee7c82013-02-15 00:48:33 +01003763int cfg80211_check_station_change(struct wiphy *wiphy,
3764 struct station_parameters *params,
3765 enum cfg80211_station_type statype)
3766{
3767 if (params->listen_interval != -1)
3768 return -EINVAL;
3769 if (params->aid)
3770 return -EINVAL;
3771
3772 /* When you run into this, adjust the code below for the new flag */
3773 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3774
3775 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003776 case CFG80211_STA_MESH_PEER_KERNEL:
3777 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003778 /*
3779 * No ignoring the TDLS flag here -- the userspace mesh
3780 * code doesn't have the bug of including TDLS in the
3781 * mask everywhere.
3782 */
3783 if (params->sta_flags_mask &
3784 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3785 BIT(NL80211_STA_FLAG_MFP) |
3786 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3787 return -EINVAL;
3788 break;
3789 case CFG80211_STA_TDLS_PEER_SETUP:
3790 case CFG80211_STA_TDLS_PEER_ACTIVE:
3791 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3792 return -EINVAL;
3793 /* ignore since it can't change */
3794 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3795 break;
3796 default:
3797 /* disallow mesh-specific things */
3798 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3799 return -EINVAL;
3800 if (params->local_pm)
3801 return -EINVAL;
3802 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3803 return -EINVAL;
3804 }
3805
3806 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3807 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3808 /* TDLS can't be set, ... */
3809 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3810 return -EINVAL;
3811 /*
3812 * ... but don't bother the driver with it. This works around
3813 * a hostapd/wpa_supplicant issue -- it always includes the
3814 * TLDS_PEER flag in the mask even for AP mode.
3815 */
3816 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3817 }
3818
3819 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3820 /* reject other things that can't change */
3821 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3822 return -EINVAL;
3823 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3824 return -EINVAL;
3825 if (params->supported_rates)
3826 return -EINVAL;
3827 if (params->ext_capab || params->ht_capa || params->vht_capa)
3828 return -EINVAL;
3829 }
3830
3831 if (statype != CFG80211_STA_AP_CLIENT) {
3832 if (params->vlan)
3833 return -EINVAL;
3834 }
3835
3836 switch (statype) {
3837 case CFG80211_STA_AP_MLME_CLIENT:
3838 /* Use this only for authorizing/unauthorizing a station */
3839 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3840 return -EOPNOTSUPP;
3841 break;
3842 case CFG80211_STA_AP_CLIENT:
3843 /* accept only the listed bits */
3844 if (params->sta_flags_mask &
3845 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3846 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3847 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3848 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3849 BIT(NL80211_STA_FLAG_WME) |
3850 BIT(NL80211_STA_FLAG_MFP)))
3851 return -EINVAL;
3852
3853 /* but authenticated/associated only if driver handles it */
3854 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3855 params->sta_flags_mask &
3856 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3857 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3858 return -EINVAL;
3859 break;
3860 case CFG80211_STA_IBSS:
3861 case CFG80211_STA_AP_STA:
3862 /* reject any changes other than AUTHORIZED */
3863 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3864 return -EINVAL;
3865 break;
3866 case CFG80211_STA_TDLS_PEER_SETUP:
3867 /* reject any changes other than AUTHORIZED or WME */
3868 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3869 BIT(NL80211_STA_FLAG_WME)))
3870 return -EINVAL;
3871 /* force (at least) rates when authorizing */
3872 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3873 !params->supported_rates)
3874 return -EINVAL;
3875 break;
3876 case CFG80211_STA_TDLS_PEER_ACTIVE:
3877 /* reject any changes */
3878 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003879 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003880 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3881 return -EINVAL;
3882 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003883 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003884 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3885 return -EINVAL;
3886 break;
3887 }
3888
3889 return 0;
3890}
3891EXPORT_SYMBOL(cfg80211_check_station_change);
3892
Johannes Berg5727ef12007-12-19 02:03:34 +01003893/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003894 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003895 */
Johannes Berg80b99892011-11-18 16:23:01 +01003896static struct net_device *get_vlan(struct genl_info *info,
3897 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003898{
Johannes Berg463d0182009-07-14 00:33:35 +02003899 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003900 struct net_device *v;
3901 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003902
Johannes Berg80b99892011-11-18 16:23:01 +01003903 if (!vlanattr)
3904 return NULL;
3905
3906 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3907 if (!v)
3908 return ERR_PTR(-ENODEV);
3909
3910 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3911 ret = -EINVAL;
3912 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003913 }
Johannes Berg80b99892011-11-18 16:23:01 +01003914
Johannes Berg77ee7c82013-02-15 00:48:33 +01003915 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3916 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3917 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3918 ret = -EINVAL;
3919 goto error;
3920 }
3921
Johannes Berg80b99892011-11-18 16:23:01 +01003922 if (!netif_running(v)) {
3923 ret = -ENETDOWN;
3924 goto error;
3925 }
3926
3927 return v;
3928 error:
3929 dev_put(v);
3930 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003931}
3932
Johannes Berg94e860f2014-01-20 23:58:15 +01003933static const struct nla_policy
3934nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003935 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3936 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3937};
3938
Johannes Bergff276692013-02-15 00:09:01 +01003939static int nl80211_parse_sta_wme(struct genl_info *info,
3940 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003941{
Jouni Malinendf881292013-02-14 21:10:54 +02003942 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3943 struct nlattr *nla;
3944 int err;
3945
Jouni Malinendf881292013-02-14 21:10:54 +02003946 /* parse WME attributes if present */
3947 if (!info->attrs[NL80211_ATTR_STA_WME])
3948 return 0;
3949
3950 nla = info->attrs[NL80211_ATTR_STA_WME];
3951 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3952 nl80211_sta_wme_policy);
3953 if (err)
3954 return err;
3955
3956 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3957 params->uapsd_queues = nla_get_u8(
3958 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3959 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3960 return -EINVAL;
3961
3962 if (tb[NL80211_STA_WME_MAX_SP])
3963 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3964
3965 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3966 return -EINVAL;
3967
3968 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3969
3970 return 0;
3971}
3972
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303973static int nl80211_parse_sta_channel_info(struct genl_info *info,
3974 struct station_parameters *params)
3975{
3976 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3977 params->supported_channels =
3978 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3979 params->supported_channels_len =
3980 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3981 /*
3982 * Need to include at least one (first channel, number of
3983 * channels) tuple for each subband, and must have proper
3984 * tuples for the rest of the data as well.
3985 */
3986 if (params->supported_channels_len < 2)
3987 return -EINVAL;
3988 if (params->supported_channels_len % 2)
3989 return -EINVAL;
3990 }
3991
3992 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3993 params->supported_oper_classes =
3994 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3995 params->supported_oper_classes_len =
3996 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3997 /*
3998 * The value of the Length field of the Supported Operating
3999 * Classes element is between 2 and 253.
4000 */
4001 if (params->supported_oper_classes_len < 2 ||
4002 params->supported_oper_classes_len > 253)
4003 return -EINVAL;
4004 }
4005 return 0;
4006}
4007
Johannes Bergff276692013-02-15 00:09:01 +01004008static int nl80211_set_station_tdls(struct genl_info *info,
4009 struct station_parameters *params)
4010{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304011 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004012 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004013 if (info->attrs[NL80211_ATTR_PEER_AID])
4014 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004015 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4016 params->ht_capa =
4017 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4018 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4019 params->vht_capa =
4020 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4021
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304022 err = nl80211_parse_sta_channel_info(info, params);
4023 if (err)
4024 return err;
4025
Johannes Bergff276692013-02-15 00:09:01 +01004026 return nl80211_parse_sta_wme(info, params);
4027}
4028
Johannes Berg5727ef12007-12-19 02:03:34 +01004029static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4030{
Johannes Berg4c476992010-10-04 21:36:35 +02004031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004032 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004033 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004034 u8 *mac_addr;
4035 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004036
4037 memset(&params, 0, sizeof(params));
4038
4039 params.listen_interval = -1;
4040
Johannes Berg77ee7c82013-02-15 00:48:33 +01004041 if (!rdev->ops->change_station)
4042 return -EOPNOTSUPP;
4043
Johannes Berg5727ef12007-12-19 02:03:34 +01004044 if (info->attrs[NL80211_ATTR_STA_AID])
4045 return -EINVAL;
4046
4047 if (!info->attrs[NL80211_ATTR_MAC])
4048 return -EINVAL;
4049
4050 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4051
4052 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4053 params.supported_rates =
4054 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4055 params.supported_rates_len =
4056 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4057 }
4058
Jouni Malinen9d62a982013-02-14 21:10:13 +02004059 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4060 params.capability =
4061 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4062 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4063 }
4064
4065 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4066 params.ext_capab =
4067 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4068 params.ext_capab_len =
4069 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4070 }
4071
Jouni Malinendf881292013-02-14 21:10:54 +02004072 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004073 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004074
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004075 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004076 return -EINVAL;
4077
Johannes Bergf8bacc22013-02-14 23:27:01 +01004078 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004079 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004080 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4081 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4082 return -EINVAL;
4083 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004084
Johannes Bergf8bacc22013-02-14 23:27:01 +01004085 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004086 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004087 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4088 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4089 return -EINVAL;
4090 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4091 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004092
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004093 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4094 enum nl80211_mesh_power_mode pm = nla_get_u32(
4095 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4096
4097 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4098 pm > NL80211_MESH_POWER_MAX)
4099 return -EINVAL;
4100
4101 params.local_pm = pm;
4102 }
4103
Johannes Berg77ee7c82013-02-15 00:48:33 +01004104 /* Include parameters for TDLS peer (will check later) */
4105 err = nl80211_set_station_tdls(info, &params);
4106 if (err)
4107 return err;
4108
4109 params.vlan = get_vlan(info, rdev);
4110 if (IS_ERR(params.vlan))
4111 return PTR_ERR(params.vlan);
4112
Johannes Berga97f4422009-06-18 17:23:43 +02004113 switch (dev->ieee80211_ptr->iftype) {
4114 case NL80211_IFTYPE_AP:
4115 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004116 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004117 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004118 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004119 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004120 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004121 break;
4122 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004123 err = -EOPNOTSUPP;
4124 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004125 }
4126
Johannes Berg77ee7c82013-02-15 00:48:33 +01004127 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004128 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004129
Johannes Berg77ee7c82013-02-15 00:48:33 +01004130 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004131 if (params.vlan)
4132 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004133
Johannes Berg5727ef12007-12-19 02:03:34 +01004134 return err;
4135}
4136
4137static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4138{
Johannes Berg4c476992010-10-04 21:36:35 +02004139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004140 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004141 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004142 struct station_parameters params;
4143 u8 *mac_addr = NULL;
4144
4145 memset(&params, 0, sizeof(params));
4146
Johannes Berg984c3112013-02-14 23:43:25 +01004147 if (!rdev->ops->add_station)
4148 return -EOPNOTSUPP;
4149
Johannes Berg5727ef12007-12-19 02:03:34 +01004150 if (!info->attrs[NL80211_ATTR_MAC])
4151 return -EINVAL;
4152
Johannes Berg5727ef12007-12-19 02:03:34 +01004153 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4154 return -EINVAL;
4155
4156 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4157 return -EINVAL;
4158
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004159 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4160 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004161 return -EINVAL;
4162
Johannes Berg5727ef12007-12-19 02:03:34 +01004163 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4164 params.supported_rates =
4165 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4166 params.supported_rates_len =
4167 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4168 params.listen_interval =
4169 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004170
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004171 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004172 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004173 else
4174 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004175 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4176 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004177
Jouni Malinen9d62a982013-02-14 21:10:13 +02004178 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4179 params.capability =
4180 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4181 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4182 }
4183
4184 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4185 params.ext_capab =
4186 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4187 params.ext_capab_len =
4188 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4189 }
4190
Jouni Malinen36aedc902008-08-25 11:58:58 +03004191 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4192 params.ht_capa =
4193 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004194
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004195 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4196 params.vht_capa =
4197 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4198
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004199 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4200 params.opmode_notif_used = true;
4201 params.opmode_notif =
4202 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4203 }
4204
Johannes Bergf8bacc22013-02-14 23:27:01 +01004205 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004206 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004207 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4208 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4209 return -EINVAL;
4210 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004211
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304212 err = nl80211_parse_sta_channel_info(info, &params);
4213 if (err)
4214 return err;
4215
Johannes Bergff276692013-02-15 00:09:01 +01004216 err = nl80211_parse_sta_wme(info, &params);
4217 if (err)
4218 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004219
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004220 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004221 return -EINVAL;
4222
Johannes Berg77ee7c82013-02-15 00:48:33 +01004223 /* When you run into this, adjust the code below for the new flag */
4224 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4225
Johannes Bergbdd90d52011-12-14 12:20:27 +01004226 switch (dev->ieee80211_ptr->iftype) {
4227 case NL80211_IFTYPE_AP:
4228 case NL80211_IFTYPE_AP_VLAN:
4229 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004230 /* ignore WME attributes if iface/sta is not capable */
4231 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4232 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4233 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004234
Johannes Bergbdd90d52011-12-14 12:20:27 +01004235 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004236 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4237 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004238 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004239 /* but don't bother the driver with it */
4240 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004241
Johannes Bergd582cff2012-10-26 17:53:44 +02004242 /* allow authenticated/associated only if driver handles it */
4243 if (!(rdev->wiphy.features &
4244 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4245 params.sta_flags_mask &
4246 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4247 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4248 return -EINVAL;
4249
Johannes Bergbdd90d52011-12-14 12:20:27 +01004250 /* must be last in here for error handling */
4251 params.vlan = get_vlan(info, rdev);
4252 if (IS_ERR(params.vlan))
4253 return PTR_ERR(params.vlan);
4254 break;
4255 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004256 /* ignore uAPSD data */
4257 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4258
Johannes Bergd582cff2012-10-26 17:53:44 +02004259 /* associated is disallowed */
4260 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4261 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004262 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004263 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4264 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004265 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004266 break;
4267 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004268 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004269 /* ignore uAPSD data */
4270 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4271
Johannes Berg77ee7c82013-02-15 00:48:33 +01004272 /* these are disallowed */
4273 if (params.sta_flags_mask &
4274 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4275 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004276 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004277 /* Only TDLS peers can be added */
4278 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4279 return -EINVAL;
4280 /* Can only add if TDLS ... */
4281 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4282 return -EOPNOTSUPP;
4283 /* ... with external setup is supported */
4284 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4285 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004286 /*
4287 * Older wpa_supplicant versions always mark the TDLS peer
4288 * as authorized, but it shouldn't yet be.
4289 */
4290 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004291 break;
4292 default:
4293 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004294 }
4295
Johannes Bergbdd90d52011-12-14 12:20:27 +01004296 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004297
Hila Gonene35e4d22012-06-27 17:19:42 +03004298 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004299
Johannes Berg5727ef12007-12-19 02:03:34 +01004300 if (params.vlan)
4301 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004302 return err;
4303}
4304
4305static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4306{
Johannes Berg4c476992010-10-04 21:36:35 +02004307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4308 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004309 u8 *mac_addr = NULL;
4310
4311 if (info->attrs[NL80211_ATTR_MAC])
4312 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4313
Johannes Berge80cf852009-05-11 14:43:13 +02004314 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004315 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004316 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004317 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4318 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004319
Johannes Berg4c476992010-10-04 21:36:35 +02004320 if (!rdev->ops->del_station)
4321 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004322
Hila Gonene35e4d22012-06-27 17:19:42 +03004323 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004324}
4325
Eric W. Biederman15e47302012-09-07 20:12:54 +00004326static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004327 int flags, struct net_device *dev,
4328 u8 *dst, u8 *next_hop,
4329 struct mpath_info *pinfo)
4330{
4331 void *hdr;
4332 struct nlattr *pinfoattr;
4333
Eric W. Biederman15e47302012-09-07 20:12:54 +00004334 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335 if (!hdr)
4336 return -1;
4337
David S. Miller9360ffd2012-03-29 04:41:26 -04004338 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4339 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4340 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4341 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4342 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004343
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004344 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4345 if (!pinfoattr)
4346 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004347 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4348 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4349 pinfo->frame_qlen))
4350 goto nla_put_failure;
4351 if (((pinfo->filled & MPATH_INFO_SN) &&
4352 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4353 ((pinfo->filled & MPATH_INFO_METRIC) &&
4354 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4355 pinfo->metric)) ||
4356 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4357 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4358 pinfo->exptime)) ||
4359 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4360 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4361 pinfo->flags)) ||
4362 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4363 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4364 pinfo->discovery_timeout)) ||
4365 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4366 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4367 pinfo->discovery_retries)))
4368 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004369
4370 nla_nest_end(msg, pinfoattr);
4371
4372 return genlmsg_end(msg, hdr);
4373
4374 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004375 genlmsg_cancel(msg, hdr);
4376 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004377}
4378
4379static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004380 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004381{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004382 struct mpath_info pinfo;
4383 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004384 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004385 u8 dst[ETH_ALEN];
4386 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004387 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004388 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004389
Johannes Berg97990a02013-04-19 01:02:55 +02004390 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004391 if (err)
4392 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004393
4394 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004395 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004396 goto out_err;
4397 }
4398
Johannes Berg97990a02013-04-19 01:02:55 +02004399 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004400 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004401 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004402 }
4403
Johannes Bergbba95fe2008-07-29 13:22:51 +02004404 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004405 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4406 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004407 if (err == -ENOENT)
4408 break;
4409 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004410 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004411
Eric W. Biederman15e47302012-09-07 20:12:54 +00004412 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004413 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004414 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004415 &pinfo) < 0)
4416 goto out;
4417
4418 path_idx++;
4419 }
4420
4421
4422 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004423 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004424 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004425 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004426 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004427 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004428}
4429
4430static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4431{
Johannes Berg4c476992010-10-04 21:36:35 +02004432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004433 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004434 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004435 struct mpath_info pinfo;
4436 struct sk_buff *msg;
4437 u8 *dst = NULL;
4438 u8 next_hop[ETH_ALEN];
4439
4440 memset(&pinfo, 0, sizeof(pinfo));
4441
4442 if (!info->attrs[NL80211_ATTR_MAC])
4443 return -EINVAL;
4444
4445 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4446
Johannes Berg4c476992010-10-04 21:36:35 +02004447 if (!rdev->ops->get_mpath)
4448 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004449
Johannes Berg4c476992010-10-04 21:36:35 +02004450 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4451 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004452
Hila Gonene35e4d22012-06-27 17:19:42 +03004453 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004454 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004455 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004456
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004457 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004458 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004459 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004460
Eric W. Biederman15e47302012-09-07 20:12:54 +00004461 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004462 dev, dst, next_hop, &pinfo) < 0) {
4463 nlmsg_free(msg);
4464 return -ENOBUFS;
4465 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004466
Johannes Berg4c476992010-10-04 21:36:35 +02004467 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004468}
4469
4470static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4471{
Johannes Berg4c476992010-10-04 21:36:35 +02004472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4473 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004474 u8 *dst = NULL;
4475 u8 *next_hop = NULL;
4476
4477 if (!info->attrs[NL80211_ATTR_MAC])
4478 return -EINVAL;
4479
4480 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4481 return -EINVAL;
4482
4483 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4484 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4485
Johannes Berg4c476992010-10-04 21:36:35 +02004486 if (!rdev->ops->change_mpath)
4487 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004488
Johannes Berg4c476992010-10-04 21:36:35 +02004489 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4490 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491
Hila Gonene35e4d22012-06-27 17:19:42 +03004492 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004493}
Johannes Berg4c476992010-10-04 21:36:35 +02004494
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004495static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4496{
Johannes Berg4c476992010-10-04 21:36:35 +02004497 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4498 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004499 u8 *dst = NULL;
4500 u8 *next_hop = NULL;
4501
4502 if (!info->attrs[NL80211_ATTR_MAC])
4503 return -EINVAL;
4504
4505 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4506 return -EINVAL;
4507
4508 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4509 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4510
Johannes Berg4c476992010-10-04 21:36:35 +02004511 if (!rdev->ops->add_mpath)
4512 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004513
Johannes Berg4c476992010-10-04 21:36:35 +02004514 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4515 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004516
Hila Gonene35e4d22012-06-27 17:19:42 +03004517 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004518}
4519
4520static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4521{
Johannes Berg4c476992010-10-04 21:36:35 +02004522 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4523 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004524 u8 *dst = NULL;
4525
4526 if (info->attrs[NL80211_ATTR_MAC])
4527 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4528
Johannes Berg4c476992010-10-04 21:36:35 +02004529 if (!rdev->ops->del_mpath)
4530 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004531
Hila Gonene35e4d22012-06-27 17:19:42 +03004532 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004533}
4534
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004535static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4536{
Johannes Berg4c476992010-10-04 21:36:35 +02004537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4538 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004539 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004540 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004541 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004542
4543 memset(&params, 0, sizeof(params));
4544 /* default to not changing parameters */
4545 params.use_cts_prot = -1;
4546 params.use_short_preamble = -1;
4547 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004548 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004549 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004550 params.p2p_ctwindow = -1;
4551 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004552
4553 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4554 params.use_cts_prot =
4555 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4556 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4557 params.use_short_preamble =
4558 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4559 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4560 params.use_short_slot_time =
4561 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004562 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4563 params.basic_rates =
4564 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4565 params.basic_rates_len =
4566 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4567 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004568 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4569 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004570 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4571 params.ht_opmode =
4572 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004573
Johannes Berg53cabad2012-11-14 15:17:28 +01004574 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4575 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4576 return -EINVAL;
4577 params.p2p_ctwindow =
4578 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4579 if (params.p2p_ctwindow < 0)
4580 return -EINVAL;
4581 if (params.p2p_ctwindow != 0 &&
4582 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4583 return -EINVAL;
4584 }
4585
4586 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4587 u8 tmp;
4588
4589 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4590 return -EINVAL;
4591 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4592 if (tmp > 1)
4593 return -EINVAL;
4594 params.p2p_opp_ps = tmp;
4595 if (params.p2p_opp_ps &&
4596 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4597 return -EINVAL;
4598 }
4599
Johannes Berg4c476992010-10-04 21:36:35 +02004600 if (!rdev->ops->change_bss)
4601 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004602
Johannes Berg074ac8d2010-09-16 14:58:22 +02004603 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004604 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4605 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004606
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004607 wdev_lock(wdev);
4608 err = rdev_change_bss(rdev, dev, &params);
4609 wdev_unlock(wdev);
4610
4611 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004612}
4613
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004614static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004615 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4616 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4617 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4618 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4619 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4620 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004621 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004622};
4623
4624static int parse_reg_rule(struct nlattr *tb[],
4625 struct ieee80211_reg_rule *reg_rule)
4626{
4627 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4628 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4629
4630 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4631 return -EINVAL;
4632 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4633 return -EINVAL;
4634 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4635 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004636 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4637 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004638 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4639 return -EINVAL;
4640
4641 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4642
4643 freq_range->start_freq_khz =
4644 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4645 freq_range->end_freq_khz =
4646 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004647 freq_range->max_bandwidth_khz =
4648 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004649
4650 power_rule->max_eirp =
4651 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4652
4653 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4654 power_rule->max_antenna_gain =
4655 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4656
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004657 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4658 reg_rule->dfs_cac_ms =
4659 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4660
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004661 return 0;
4662}
4663
4664static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4665{
4666 int r;
4667 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004668 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004669
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004670 /*
4671 * You should only get this when cfg80211 hasn't yet initialized
4672 * completely when built-in to the kernel right between the time
4673 * window between nl80211_init() and regulatory_init(), if that is
4674 * even possible.
4675 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004676 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004677 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004678
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004679 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4680 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004681
4682 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4683
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004684 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4685 user_reg_hint_type =
4686 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4687 else
4688 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4689
4690 switch (user_reg_hint_type) {
4691 case NL80211_USER_REG_HINT_USER:
4692 case NL80211_USER_REG_HINT_CELL_BASE:
4693 break;
4694 default:
4695 return -EINVAL;
4696 }
4697
4698 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004699
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004700 return r;
4701}
4702
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004703static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004704 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004705{
Johannes Berg4c476992010-10-04 21:36:35 +02004706 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004707 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004708 struct wireless_dev *wdev = dev->ieee80211_ptr;
4709 struct mesh_config cur_params;
4710 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004711 void *hdr;
4712 struct nlattr *pinfoattr;
4713 struct sk_buff *msg;
4714
Johannes Berg29cbe682010-12-03 09:20:44 +01004715 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4716 return -EOPNOTSUPP;
4717
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004718 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004719 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004720
Johannes Berg29cbe682010-12-03 09:20:44 +01004721 wdev_lock(wdev);
4722 /* If not connected, get default parameters */
4723 if (!wdev->mesh_id_len)
4724 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4725 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004726 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004727 wdev_unlock(wdev);
4728
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004729 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004730 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004731
4732 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004733 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004734 if (!msg)
4735 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004736 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004737 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004738 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004739 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004740 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004741 if (!pinfoattr)
4742 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004743 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4744 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4745 cur_params.dot11MeshRetryTimeout) ||
4746 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4747 cur_params.dot11MeshConfirmTimeout) ||
4748 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4749 cur_params.dot11MeshHoldingTimeout) ||
4750 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4751 cur_params.dot11MeshMaxPeerLinks) ||
4752 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4753 cur_params.dot11MeshMaxRetries) ||
4754 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4755 cur_params.dot11MeshTTL) ||
4756 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4757 cur_params.element_ttl) ||
4758 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4759 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004760 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4761 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004762 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4763 cur_params.dot11MeshHWMPmaxPREQretries) ||
4764 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4765 cur_params.path_refresh_time) ||
4766 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4767 cur_params.min_discovery_timeout) ||
4768 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4769 cur_params.dot11MeshHWMPactivePathTimeout) ||
4770 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4771 cur_params.dot11MeshHWMPpreqMinInterval) ||
4772 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4773 cur_params.dot11MeshHWMPperrMinInterval) ||
4774 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4775 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4776 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4777 cur_params.dot11MeshHWMPRootMode) ||
4778 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4779 cur_params.dot11MeshHWMPRannInterval) ||
4780 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4781 cur_params.dot11MeshGateAnnouncementProtocol) ||
4782 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4783 cur_params.dot11MeshForwarding) ||
4784 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004785 cur_params.rssi_threshold) ||
4786 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004787 cur_params.ht_opmode) ||
4788 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4789 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4790 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004791 cur_params.dot11MeshHWMProotInterval) ||
4792 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004793 cur_params.dot11MeshHWMPconfirmationInterval) ||
4794 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4795 cur_params.power_mode) ||
4796 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004797 cur_params.dot11MeshAwakeWindowDuration) ||
4798 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4799 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004800 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004801 nla_nest_end(msg, pinfoattr);
4802 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004803 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004804
Johannes Berg3b858752009-03-12 09:55:09 +01004805 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004806 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004807 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004808 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004809 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004810}
4811
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004812static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004813 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4814 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4815 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4816 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4817 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4818 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004819 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004820 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004821 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004822 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4823 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4824 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4825 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4826 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004827 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004828 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004829 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004830 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004831 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004832 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004833 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4834 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004835 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4836 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004837 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004838 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4839 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004840 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004841};
4842
Javier Cardonac80d5452010-12-16 17:37:49 -08004843static const struct nla_policy
4844 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004845 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004846 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4847 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004848 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004849 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004850 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004851 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004852 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004853 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004854};
4855
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004856static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004857 struct mesh_config *cfg,
4858 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004859{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004860 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004861 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004862
Marco Porschea54fba2013-01-07 16:04:48 +01004863#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4864do { \
4865 if (tb[attr]) { \
4866 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4867 return -EINVAL; \
4868 cfg->param = fn(tb[attr]); \
4869 mask |= (1 << (attr - 1)); \
4870 } \
4871} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004872
4873
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004874 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004875 return -EINVAL;
4876 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004877 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004878 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004879 return -EINVAL;
4880
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004881 /* This makes sure that there aren't more than 32 mesh config
4882 * parameters (otherwise our bitfield scheme would not work.) */
4883 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4884
4885 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004886 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004887 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4888 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004889 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004890 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4891 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004892 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004893 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4894 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004895 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004896 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4897 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004898 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004899 mask, NL80211_MESHCONF_MAX_RETRIES,
4900 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004901 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004902 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004903 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004904 mask, NL80211_MESHCONF_ELEMENT_TTL,
4905 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004907 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4908 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4910 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004911 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4912 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004914 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4915 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004917 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4918 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004920 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4921 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4923 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004924 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4925 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004927 1, 65535, mask,
4928 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004929 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004930 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004931 1, 65535, mask,
4932 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004933 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004934 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004935 dot11MeshHWMPnetDiameterTraversalTime,
4936 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004937 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4938 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004939 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4940 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4941 nla_get_u8);
4942 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4943 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004944 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004945 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004946 dot11MeshGateAnnouncementProtocol, 0, 1,
4947 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004948 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004949 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004950 mask, NL80211_MESHCONF_FORWARDING,
4951 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004952 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004953 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004954 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004955 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004956 mask, NL80211_MESHCONF_HT_OPMODE,
4957 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004958 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004959 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004960 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4961 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004962 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004963 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4964 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004965 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004966 dot11MeshHWMPconfirmationInterval,
4967 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004968 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4969 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004970 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4971 NL80211_MESH_POWER_ACTIVE,
4972 NL80211_MESH_POWER_MAX,
4973 mask, NL80211_MESHCONF_POWER_MODE,
4974 nla_get_u32);
4975 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4976 0, 65535, mask,
4977 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004978 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4979 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4980 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004981 if (mask_out)
4982 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004983
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004984 return 0;
4985
4986#undef FILL_IN_MESH_PARAM_IF_SET
4987}
4988
Javier Cardonac80d5452010-12-16 17:37:49 -08004989static int nl80211_parse_mesh_setup(struct genl_info *info,
4990 struct mesh_setup *setup)
4991{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004993 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4994
4995 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4996 return -EINVAL;
4997 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4998 info->attrs[NL80211_ATTR_MESH_SETUP],
4999 nl80211_mesh_setup_params_policy))
5000 return -EINVAL;
5001
Javier Cardonad299a1f2012-03-31 11:31:33 -07005002 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5003 setup->sync_method =
5004 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5005 IEEE80211_SYNC_METHOD_VENDOR :
5006 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5007
Javier Cardonac80d5452010-12-16 17:37:49 -08005008 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5009 setup->path_sel_proto =
5010 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5011 IEEE80211_PATH_PROTOCOL_VENDOR :
5012 IEEE80211_PATH_PROTOCOL_HWMP;
5013
5014 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5015 setup->path_metric =
5016 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5017 IEEE80211_PATH_METRIC_VENDOR :
5018 IEEE80211_PATH_METRIC_AIRTIME;
5019
Javier Cardona581a8b02011-04-07 15:08:27 -07005020
5021 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005022 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005023 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005024 if (!is_valid_ie_attr(ieattr))
5025 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005026 setup->ie = nla_data(ieattr);
5027 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005028 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005029 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5030 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5031 return -EINVAL;
5032 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005033 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5034 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005035 if (setup->is_secure)
5036 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005037
Colleen Twitty6e16d902013-05-08 11:45:59 -07005038 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5039 if (!setup->user_mpm)
5040 return -EINVAL;
5041 setup->auth_id =
5042 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5043 }
5044
Javier Cardonac80d5452010-12-16 17:37:49 -08005045 return 0;
5046}
5047
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005048static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005049 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005050{
5051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5052 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005053 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005054 struct mesh_config cfg;
5055 u32 mask;
5056 int err;
5057
Johannes Berg29cbe682010-12-03 09:20:44 +01005058 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5059 return -EOPNOTSUPP;
5060
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005061 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005062 return -EOPNOTSUPP;
5063
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005064 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005065 if (err)
5066 return err;
5067
Johannes Berg29cbe682010-12-03 09:20:44 +01005068 wdev_lock(wdev);
5069 if (!wdev->mesh_id_len)
5070 err = -ENOLINK;
5071
5072 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005073 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005074
5075 wdev_unlock(wdev);
5076
5077 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005078}
5079
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005080static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5081{
Johannes Berg458f4f92012-12-06 15:47:38 +01005082 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005083 struct sk_buff *msg;
5084 void *hdr = NULL;
5085 struct nlattr *nl_reg_rules;
5086 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005087
5088 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005089 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005090
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005091 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005092 if (!msg)
5093 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005094
Eric W. Biederman15e47302012-09-07 20:12:54 +00005095 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005096 NL80211_CMD_GET_REG);
5097 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005098 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005099
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005100 if (reg_last_request_cell_base() &&
5101 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5102 NL80211_USER_REG_HINT_CELL_BASE))
5103 goto nla_put_failure;
5104
Johannes Berg458f4f92012-12-06 15:47:38 +01005105 rcu_read_lock();
5106 regdom = rcu_dereference(cfg80211_regdomain);
5107
5108 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5109 (regdom->dfs_region &&
5110 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5111 goto nla_put_failure_rcu;
5112
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005113 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5114 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005115 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005116
Johannes Berg458f4f92012-12-06 15:47:38 +01005117 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005118 struct nlattr *nl_reg_rule;
5119 const struct ieee80211_reg_rule *reg_rule;
5120 const struct ieee80211_freq_range *freq_range;
5121 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005122 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005123
Johannes Berg458f4f92012-12-06 15:47:38 +01005124 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005125 freq_range = &reg_rule->freq_range;
5126 power_rule = &reg_rule->power_rule;
5127
5128 nl_reg_rule = nla_nest_start(msg, i);
5129 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005130 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005131
Janusz Dziedzic97524822014-01-30 09:52:20 +01005132 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5133 if (!max_bandwidth_khz)
5134 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5135 reg_rule);
5136
David S. Miller9360ffd2012-03-29 04:41:26 -04005137 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5138 reg_rule->flags) ||
5139 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5140 freq_range->start_freq_khz) ||
5141 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5142 freq_range->end_freq_khz) ||
5143 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005144 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005145 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5146 power_rule->max_antenna_gain) ||
5147 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005148 power_rule->max_eirp) ||
5149 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5150 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005151 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005152
5153 nla_nest_end(msg, nl_reg_rule);
5154 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005155 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005156
5157 nla_nest_end(msg, nl_reg_rules);
5158
5159 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005160 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005161
Johannes Berg458f4f92012-12-06 15:47:38 +01005162nla_put_failure_rcu:
5163 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005164nla_put_failure:
5165 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005166put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005167 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005168 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005169}
5170
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005171static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5172{
5173 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5174 struct nlattr *nl_reg_rule;
5175 char *alpha2 = NULL;
5176 int rem_reg_rules = 0, r = 0;
5177 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005178 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005179 struct ieee80211_regdomain *rd = NULL;
5180
5181 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5182 return -EINVAL;
5183
5184 if (!info->attrs[NL80211_ATTR_REG_RULES])
5185 return -EINVAL;
5186
5187 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5188
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005189 if (info->attrs[NL80211_ATTR_DFS_REGION])
5190 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5191
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005192 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005193 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005194 num_rules++;
5195 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005196 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005197 }
5198
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005199 if (!reg_is_valid_request(alpha2))
5200 return -EINVAL;
5201
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005202 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005203 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005204
5205 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005206 if (!rd)
5207 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005208
5209 rd->n_reg_rules = num_rules;
5210 rd->alpha2[0] = alpha2[0];
5211 rd->alpha2[1] = alpha2[1];
5212
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005213 /*
5214 * Disable DFS master mode if the DFS region was
5215 * not supported or known on this kernel.
5216 */
5217 if (reg_supported_dfs_region(dfs_region))
5218 rd->dfs_region = dfs_region;
5219
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005220 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005221 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005222 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5223 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5224 reg_rule_policy);
5225 if (r)
5226 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005227 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5228 if (r)
5229 goto bad_reg;
5230
5231 rule_idx++;
5232
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005233 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5234 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005235 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005236 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005237 }
5238
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005239 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005240 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005241 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005242
Johannes Bergd2372b32008-10-24 20:32:20 +02005243 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005244 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005245 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005246}
5247
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005248static int validate_scan_freqs(struct nlattr *freqs)
5249{
5250 struct nlattr *attr1, *attr2;
5251 int n_channels = 0, tmp1, tmp2;
5252
5253 nla_for_each_nested(attr1, freqs, tmp1) {
5254 n_channels++;
5255 /*
5256 * Some hardware has a limited channel list for
5257 * scanning, and it is pretty much nonsensical
5258 * to scan for a channel twice, so disallow that
5259 * and don't require drivers to check that the
5260 * channel list they get isn't longer than what
5261 * they can scan, as long as they can scan all
5262 * the channels they registered at once.
5263 */
5264 nla_for_each_nested(attr2, freqs, tmp2)
5265 if (attr1 != attr2 &&
5266 nla_get_u32(attr1) == nla_get_u32(attr2))
5267 return 0;
5268 }
5269
5270 return n_channels;
5271}
5272
Johannes Berg2a519312009-02-10 21:25:55 +01005273static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5274{
Johannes Berg4c476992010-10-04 21:36:35 +02005275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005276 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005277 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005278 struct nlattr *attr;
5279 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005280 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005281 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005282
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005283 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5284 return -EINVAL;
5285
Johannes Berg79c97e92009-07-07 03:56:12 +02005286 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005287
Johannes Berg4c476992010-10-04 21:36:35 +02005288 if (!rdev->ops->scan)
5289 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005290
Johannes Bergf9d15d12014-01-22 11:14:19 +02005291 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005292 err = -EBUSY;
5293 goto unlock;
5294 }
Johannes Berg2a519312009-02-10 21:25:55 +01005295
5296 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005297 n_channels = validate_scan_freqs(
5298 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005299 if (!n_channels) {
5300 err = -EINVAL;
5301 goto unlock;
5302 }
Johannes Berg2a519312009-02-10 21:25:55 +01005303 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005304 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005305 }
5306
5307 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5308 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5309 n_ssids++;
5310
Johannes Bergf9f47522013-03-19 15:04:07 +01005311 if (n_ssids > wiphy->max_scan_ssids) {
5312 err = -EINVAL;
5313 goto unlock;
5314 }
Johannes Berg2a519312009-02-10 21:25:55 +01005315
Jouni Malinen70692ad2009-02-16 19:39:13 +02005316 if (info->attrs[NL80211_ATTR_IE])
5317 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5318 else
5319 ie_len = 0;
5320
Johannes Bergf9f47522013-03-19 15:04:07 +01005321 if (ie_len > wiphy->max_scan_ie_len) {
5322 err = -EINVAL;
5323 goto unlock;
5324 }
Johannes Berg18a83652009-03-31 12:12:05 +02005325
Johannes Berg2a519312009-02-10 21:25:55 +01005326 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005327 + sizeof(*request->ssids) * n_ssids
5328 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005329 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005330 if (!request) {
5331 err = -ENOMEM;
5332 goto unlock;
5333 }
Johannes Berg2a519312009-02-10 21:25:55 +01005334
Johannes Berg2a519312009-02-10 21:25:55 +01005335 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005336 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005337 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005338 if (ie_len) {
5339 if (request->ssids)
5340 request->ie = (void *)(request->ssids + n_ssids);
5341 else
5342 request->ie = (void *)(request->channels + n_channels);
5343 }
Johannes Berg2a519312009-02-10 21:25:55 +01005344
Johannes Berg584991d2009-11-02 13:32:03 +01005345 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005346 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5347 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005348 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005349 struct ieee80211_channel *chan;
5350
5351 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5352
5353 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005354 err = -EINVAL;
5355 goto out_free;
5356 }
Johannes Berg584991d2009-11-02 13:32:03 +01005357
5358 /* ignore disabled channels */
5359 if (chan->flags & IEEE80211_CHAN_DISABLED)
5360 continue;
5361
5362 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005363 i++;
5364 }
5365 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005366 enum ieee80211_band band;
5367
Johannes Berg2a519312009-02-10 21:25:55 +01005368 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005369 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5370 int j;
5371 if (!wiphy->bands[band])
5372 continue;
5373 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005374 struct ieee80211_channel *chan;
5375
5376 chan = &wiphy->bands[band]->channels[j];
5377
5378 if (chan->flags & IEEE80211_CHAN_DISABLED)
5379 continue;
5380
5381 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005382 i++;
5383 }
5384 }
5385 }
5386
Johannes Berg584991d2009-11-02 13:32:03 +01005387 if (!i) {
5388 err = -EINVAL;
5389 goto out_free;
5390 }
5391
5392 request->n_channels = i;
5393
Johannes Berg2a519312009-02-10 21:25:55 +01005394 i = 0;
5395 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5396 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005397 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005398 err = -EINVAL;
5399 goto out_free;
5400 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005401 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005402 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005403 i++;
5404 }
5405 }
5406
Jouni Malinen70692ad2009-02-16 19:39:13 +02005407 if (info->attrs[NL80211_ATTR_IE]) {
5408 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005409 memcpy((void *)request->ie,
5410 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005411 request->ie_len);
5412 }
5413
Johannes Berg34850ab2011-07-18 18:08:35 +02005414 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005415 if (wiphy->bands[i])
5416 request->rates[i] =
5417 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005418
5419 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5420 nla_for_each_nested(attr,
5421 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5422 tmp) {
5423 enum ieee80211_band band = nla_type(attr);
5424
Dan Carpenter84404622011-07-29 11:52:18 +03005425 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005426 err = -EINVAL;
5427 goto out_free;
5428 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005429
5430 if (!wiphy->bands[band])
5431 continue;
5432
Johannes Berg34850ab2011-07-18 18:08:35 +02005433 err = ieee80211_get_ratemask(wiphy->bands[band],
5434 nla_data(attr),
5435 nla_len(attr),
5436 &request->rates[band]);
5437 if (err)
5438 goto out_free;
5439 }
5440 }
5441
Sam Leffler46856bb2012-10-11 21:03:32 -07005442 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005443 request->flags = nla_get_u32(
5444 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005445 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5446 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005447 err = -EOPNOTSUPP;
5448 goto out_free;
5449 }
5450 }
Sam Lefflered4737712012-10-11 21:03:31 -07005451
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305452 request->no_cck =
5453 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5454
Johannes Bergfd014282012-06-18 19:17:03 +02005455 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005456 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005457 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005458
Johannes Berg79c97e92009-07-07 03:56:12 +02005459 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005460 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005461
Johannes Berg463d0182009-07-14 00:33:35 +02005462 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005463 nl80211_send_scan_start(rdev, wdev);
5464 if (wdev->netdev)
5465 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005466 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005467 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005468 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005469 kfree(request);
5470 }
Johannes Berg3b858752009-03-12 09:55:09 +01005471
Johannes Bergf9f47522013-03-19 15:04:07 +01005472 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005473 return err;
5474}
5475
Luciano Coelho807f8a82011-05-11 17:09:35 +03005476static int nl80211_start_sched_scan(struct sk_buff *skb,
5477 struct genl_info *info)
5478{
5479 struct cfg80211_sched_scan_request *request;
5480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5481 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005482 struct nlattr *attr;
5483 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005484 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005485 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005486 enum ieee80211_band band;
5487 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005488 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005489 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005490
5491 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5492 !rdev->ops->sched_scan_start)
5493 return -EOPNOTSUPP;
5494
5495 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5496 return -EINVAL;
5497
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005498 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5499 return -EINVAL;
5500
5501 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5502 if (interval == 0)
5503 return -EINVAL;
5504
Luciano Coelho807f8a82011-05-11 17:09:35 +03005505 wiphy = &rdev->wiphy;
5506
5507 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5508 n_channels = validate_scan_freqs(
5509 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5510 if (!n_channels)
5511 return -EINVAL;
5512 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005513 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005514 }
5515
5516 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5517 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5518 tmp)
5519 n_ssids++;
5520
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005521 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005522 return -EINVAL;
5523
Johannes Bergea73cbc2014-01-24 10:53:53 +01005524 /*
5525 * First, count the number of 'real' matchsets. Due to an issue with
5526 * the old implementation, matchsets containing only the RSSI attribute
5527 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5528 * RSSI for all matchsets, rather than their own matchset for reporting
5529 * all APs with a strong RSSI. This is needed to be compatible with
5530 * older userspace that treated a matchset with only the RSSI as the
5531 * global RSSI for all other matchsets - if there are other matchsets.
5532 */
5533 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005534 nla_for_each_nested(attr,
5535 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005536 tmp) {
5537 struct nlattr *rssi;
5538
5539 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5540 nla_data(attr), nla_len(attr),
5541 nl80211_match_policy);
5542 if (err)
5543 return err;
5544 /* add other standalone attributes here */
5545 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5546 n_match_sets++;
5547 continue;
5548 }
5549 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5550 if (rssi)
5551 default_match_rssi = nla_get_s32(rssi);
5552 }
5553 }
5554
5555 /* However, if there's no other matchset, add the RSSI one */
5556 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5557 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005558
5559 if (n_match_sets > wiphy->max_match_sets)
5560 return -EINVAL;
5561
Luciano Coelho807f8a82011-05-11 17:09:35 +03005562 if (info->attrs[NL80211_ATTR_IE])
5563 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5564 else
5565 ie_len = 0;
5566
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005567 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005568 return -EINVAL;
5569
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005570 if (rdev->sched_scan_req) {
5571 err = -EINPROGRESS;
5572 goto out;
5573 }
5574
Luciano Coelho807f8a82011-05-11 17:09:35 +03005575 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005576 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005577 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005578 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005579 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005580 if (!request) {
5581 err = -ENOMEM;
5582 goto out;
5583 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005584
5585 if (n_ssids)
5586 request->ssids = (void *)&request->channels[n_channels];
5587 request->n_ssids = n_ssids;
5588 if (ie_len) {
5589 if (request->ssids)
5590 request->ie = (void *)(request->ssids + n_ssids);
5591 else
5592 request->ie = (void *)(request->channels + n_channels);
5593 }
5594
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005595 if (n_match_sets) {
5596 if (request->ie)
5597 request->match_sets = (void *)(request->ie + ie_len);
5598 else if (request->ssids)
5599 request->match_sets =
5600 (void *)(request->ssids + n_ssids);
5601 else
5602 request->match_sets =
5603 (void *)(request->channels + n_channels);
5604 }
5605 request->n_match_sets = n_match_sets;
5606
Luciano Coelho807f8a82011-05-11 17:09:35 +03005607 i = 0;
5608 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5609 /* user specified, bail out if channel not found */
5610 nla_for_each_nested(attr,
5611 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5612 tmp) {
5613 struct ieee80211_channel *chan;
5614
5615 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5616
5617 if (!chan) {
5618 err = -EINVAL;
5619 goto out_free;
5620 }
5621
5622 /* ignore disabled channels */
5623 if (chan->flags & IEEE80211_CHAN_DISABLED)
5624 continue;
5625
5626 request->channels[i] = chan;
5627 i++;
5628 }
5629 } else {
5630 /* all channels */
5631 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5632 int j;
5633 if (!wiphy->bands[band])
5634 continue;
5635 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5636 struct ieee80211_channel *chan;
5637
5638 chan = &wiphy->bands[band]->channels[j];
5639
5640 if (chan->flags & IEEE80211_CHAN_DISABLED)
5641 continue;
5642
5643 request->channels[i] = chan;
5644 i++;
5645 }
5646 }
5647 }
5648
5649 if (!i) {
5650 err = -EINVAL;
5651 goto out_free;
5652 }
5653
5654 request->n_channels = i;
5655
5656 i = 0;
5657 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5658 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5659 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005660 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005661 err = -EINVAL;
5662 goto out_free;
5663 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005664 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005665 memcpy(request->ssids[i].ssid, nla_data(attr),
5666 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005667 i++;
5668 }
5669 }
5670
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005671 i = 0;
5672 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5673 nla_for_each_nested(attr,
5674 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5675 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005676 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005677
Johannes Bergae811e22014-01-24 10:17:47 +01005678 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5679 nla_data(attr), nla_len(attr),
5680 nl80211_match_policy);
5681 if (err)
5682 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005683 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005684 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005685 if (WARN_ON(i >= n_match_sets)) {
5686 /* this indicates a programming error,
5687 * the loop above should have verified
5688 * things properly
5689 */
5690 err = -EINVAL;
5691 goto out_free;
5692 }
5693
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005694 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5695 err = -EINVAL;
5696 goto out_free;
5697 }
5698 memcpy(request->match_sets[i].ssid.ssid,
5699 nla_data(ssid), nla_len(ssid));
5700 request->match_sets[i].ssid.ssid_len =
5701 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005702 /* special attribute - old implemenation w/a */
5703 request->match_sets[i].rssi_thold =
5704 default_match_rssi;
5705 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5706 if (rssi)
5707 request->match_sets[i].rssi_thold =
5708 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005709 }
5710 i++;
5711 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005712
5713 /* there was no other matchset, so the RSSI one is alone */
5714 if (i == 0)
5715 request->match_sets[0].rssi_thold = default_match_rssi;
5716
5717 request->min_rssi_thold = INT_MAX;
5718 for (i = 0; i < n_match_sets; i++)
5719 request->min_rssi_thold =
5720 min(request->match_sets[i].rssi_thold,
5721 request->min_rssi_thold);
5722 } else {
5723 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005724 }
5725
Johannes Berg9900e482014-02-04 21:01:25 +01005726 if (ie_len) {
5727 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005728 memcpy((void *)request->ie,
5729 nla_data(info->attrs[NL80211_ATTR_IE]),
5730 request->ie_len);
5731 }
5732
Sam Leffler46856bb2012-10-11 21:03:32 -07005733 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005734 request->flags = nla_get_u32(
5735 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005736 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5737 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005738 err = -EOPNOTSUPP;
5739 goto out_free;
5740 }
5741 }
Sam Lefflered4737712012-10-11 21:03:31 -07005742
Luciano Coelho807f8a82011-05-11 17:09:35 +03005743 request->dev = dev;
5744 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005745 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005746 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005747
Hila Gonene35e4d22012-06-27 17:19:42 +03005748 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005749 if (!err) {
5750 rdev->sched_scan_req = request;
5751 nl80211_send_sched_scan(rdev, dev,
5752 NL80211_CMD_START_SCHED_SCAN);
5753 goto out;
5754 }
5755
5756out_free:
5757 kfree(request);
5758out:
5759 return err;
5760}
5761
5762static int nl80211_stop_sched_scan(struct sk_buff *skb,
5763 struct genl_info *info)
5764{
5765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5766
5767 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5768 !rdev->ops->sched_scan_stop)
5769 return -EOPNOTSUPP;
5770
Johannes Berg5fe231e2013-05-08 21:45:15 +02005771 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005772}
5773
Simon Wunderlich04f39042013-02-08 18:16:19 +01005774static int nl80211_start_radar_detection(struct sk_buff *skb,
5775 struct genl_info *info)
5776{
5777 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5778 struct net_device *dev = info->user_ptr[1];
5779 struct wireless_dev *wdev = dev->ieee80211_ptr;
5780 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005781 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005782 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005783 int err;
5784
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005785 dfs_region = reg_get_dfs_region(wdev->wiphy);
5786 if (dfs_region == NL80211_DFS_UNSET)
5787 return -EINVAL;
5788
Simon Wunderlich04f39042013-02-08 18:16:19 +01005789 err = nl80211_parse_chandef(rdev, info, &chandef);
5790 if (err)
5791 return err;
5792
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005793 if (netif_carrier_ok(dev))
5794 return -EBUSY;
5795
Simon Wunderlich04f39042013-02-08 18:16:19 +01005796 if (wdev->cac_started)
5797 return -EBUSY;
5798
5799 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5800 if (err < 0)
5801 return err;
5802
5803 if (err == 0)
5804 return -EINVAL;
5805
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005806 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005807 return -EINVAL;
5808
5809 if (!rdev->ops->start_radar_detection)
5810 return -EOPNOTSUPP;
5811
Simon Wunderlich04f39042013-02-08 18:16:19 +01005812 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5813 chandef.chan, CHAN_MODE_SHARED,
5814 BIT(chandef.width));
5815 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005816 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005817
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005818 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
5819 if (WARN_ON(!cac_time_ms))
5820 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
5821
5822 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
5823 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01005824 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005825 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005826 wdev->cac_started = true;
5827 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005828 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005829 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005830 return err;
5831}
5832
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005833static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5834{
5835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5836 struct net_device *dev = info->user_ptr[1];
5837 struct wireless_dev *wdev = dev->ieee80211_ptr;
5838 struct cfg80211_csa_settings params;
5839 /* csa_attrs is defined static to avoid waste of stack size - this
5840 * function is called under RTNL lock, so this should not be a problem.
5841 */
5842 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5843 u8 radar_detect_width = 0;
5844 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005845 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005846
5847 if (!rdev->ops->channel_switch ||
5848 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5849 return -EOPNOTSUPP;
5850
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005851 switch (dev->ieee80211_ptr->iftype) {
5852 case NL80211_IFTYPE_AP:
5853 case NL80211_IFTYPE_P2P_GO:
5854 need_new_beacon = true;
5855
5856 /* useless if AP is not running */
5857 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005858 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005859 break;
5860 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005861 if (!wdev->ssid_len)
5862 return -ENOTCONN;
5863 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005864 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005865 if (!wdev->mesh_id_len)
5866 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005867 break;
5868 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005869 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005870 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005871
5872 memset(&params, 0, sizeof(params));
5873
5874 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5875 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5876 return -EINVAL;
5877
5878 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005879 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005880 return -EINVAL;
5881
5882 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5883
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005884 if (!need_new_beacon)
5885 goto skip_beacons;
5886
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005887 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5888 if (err)
5889 return err;
5890
5891 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5892 info->attrs[NL80211_ATTR_CSA_IES],
5893 nl80211_policy);
5894 if (err)
5895 return err;
5896
5897 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5898 if (err)
5899 return err;
5900
5901 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5902 return -EINVAL;
5903
5904 params.counter_offset_beacon =
5905 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5906 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5907 return -EINVAL;
5908
5909 /* sanity check - counters should be the same */
5910 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5911 params.count)
5912 return -EINVAL;
5913
5914 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5915 params.counter_offset_presp =
5916 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5917 if (params.counter_offset_presp >=
5918 params.beacon_csa.probe_resp_len)
5919 return -EINVAL;
5920
5921 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5922 params.count)
5923 return -EINVAL;
5924 }
5925
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005926skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005927 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5928 if (err)
5929 return err;
5930
5931 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5932 return -EINVAL;
5933
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005934 switch (dev->ieee80211_ptr->iftype) {
5935 case NL80211_IFTYPE_AP:
5936 case NL80211_IFTYPE_P2P_GO:
5937 case NL80211_IFTYPE_ADHOC:
5938 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005939 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5940 &params.chandef);
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005941 if (err < 0)
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005942 return err;
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005943 if (err) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005944 radar_detect_width = BIT(params.chandef.width);
5945 params.radar_required = true;
5946 }
Luciano Coelhoe5d2f952014-02-20 16:36:20 +02005947 break;
5948 default:
5949 break;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005950 }
5951
5952 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5953 params.chandef.chan,
5954 CHAN_MODE_SHARED,
5955 radar_detect_width);
5956 if (err)
5957 return err;
5958
5959 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5960 params.block_tx = true;
5961
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005962 wdev_lock(wdev);
5963 err = rdev_channel_switch(rdev, dev, &params);
5964 wdev_unlock(wdev);
5965
5966 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005967}
5968
Johannes Berg9720bb32011-06-21 09:45:33 +02005969static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5970 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005971 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005972 struct wireless_dev *wdev,
5973 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005974{
Johannes Berg48ab9052009-07-10 18:42:31 +02005975 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005976 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005977 void *hdr;
5978 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005979 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005980
5981 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005982
Eric W. Biederman15e47302012-09-07 20:12:54 +00005983 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005984 NL80211_CMD_NEW_SCAN_RESULTS);
5985 if (!hdr)
5986 return -1;
5987
Johannes Berg9720bb32011-06-21 09:45:33 +02005988 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5989
Johannes Berg97990a02013-04-19 01:02:55 +02005990 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5991 goto nla_put_failure;
5992 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005993 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5994 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005995 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5996 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005997
5998 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5999 if (!bss)
6000 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006001 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006002 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006003 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006004
6005 rcu_read_lock();
6006 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006007 if (ies) {
6008 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6009 goto fail_unlock_rcu;
6010 tsf = true;
6011 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6012 ies->len, ies->data))
6013 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006014 }
6015 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006016 if (ies) {
6017 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6018 goto fail_unlock_rcu;
6019 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6020 ies->len, ies->data))
6021 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006022 }
6023 rcu_read_unlock();
6024
David S. Miller9360ffd2012-03-29 04:41:26 -04006025 if (res->beacon_interval &&
6026 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6027 goto nla_put_failure;
6028 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6029 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006030 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006031 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6032 jiffies_to_msecs(jiffies - intbss->ts)))
6033 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006034
Johannes Berg77965c92009-02-18 18:45:06 +01006035 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006036 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006037 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6038 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006039 break;
6040 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006041 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6042 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006043 break;
6044 default:
6045 break;
6046 }
6047
Johannes Berg48ab9052009-07-10 18:42:31 +02006048 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006049 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006050 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006051 if (intbss == wdev->current_bss &&
6052 nla_put_u32(msg, NL80211_BSS_STATUS,
6053 NL80211_BSS_STATUS_ASSOCIATED))
6054 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006055 break;
6056 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006057 if (intbss == wdev->current_bss &&
6058 nla_put_u32(msg, NL80211_BSS_STATUS,
6059 NL80211_BSS_STATUS_IBSS_JOINED))
6060 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006061 break;
6062 default:
6063 break;
6064 }
6065
Johannes Berg2a519312009-02-10 21:25:55 +01006066 nla_nest_end(msg, bss);
6067
6068 return genlmsg_end(msg, hdr);
6069
Johannes Berg8cef2c92013-02-05 16:54:31 +01006070 fail_unlock_rcu:
6071 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006072 nla_put_failure:
6073 genlmsg_cancel(msg, hdr);
6074 return -EMSGSIZE;
6075}
6076
Johannes Berg97990a02013-04-19 01:02:55 +02006077static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006078{
Johannes Berg48ab9052009-07-10 18:42:31 +02006079 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006080 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006081 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006082 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006083 int err;
6084
Johannes Berg97990a02013-04-19 01:02:55 +02006085 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006086 if (err)
6087 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006088
Johannes Berg48ab9052009-07-10 18:42:31 +02006089 wdev_lock(wdev);
6090 spin_lock_bh(&rdev->bss_lock);
6091 cfg80211_bss_expire(rdev);
6092
Johannes Berg9720bb32011-06-21 09:45:33 +02006093 cb->seq = rdev->bss_generation;
6094
Johannes Berg48ab9052009-07-10 18:42:31 +02006095 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006096 if (++idx <= start)
6097 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006098 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006099 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006100 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006101 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006102 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006103 }
6104 }
6105
Johannes Berg48ab9052009-07-10 18:42:31 +02006106 spin_unlock_bh(&rdev->bss_lock);
6107 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006108
Johannes Berg97990a02013-04-19 01:02:55 +02006109 cb->args[2] = idx;
6110 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006111
Johannes Berg67748892010-10-04 21:14:06 +02006112 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006113}
6114
Eric W. Biederman15e47302012-09-07 20:12:54 +00006115static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006116 int flags, struct net_device *dev,
6117 struct survey_info *survey)
6118{
6119 void *hdr;
6120 struct nlattr *infoattr;
6121
Eric W. Biederman15e47302012-09-07 20:12:54 +00006122 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006123 NL80211_CMD_NEW_SURVEY_RESULTS);
6124 if (!hdr)
6125 return -ENOMEM;
6126
David S. Miller9360ffd2012-03-29 04:41:26 -04006127 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6128 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006129
6130 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6131 if (!infoattr)
6132 goto nla_put_failure;
6133
David S. Miller9360ffd2012-03-29 04:41:26 -04006134 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6135 survey->channel->center_freq))
6136 goto nla_put_failure;
6137
6138 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6139 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6140 goto nla_put_failure;
6141 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6142 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6143 goto nla_put_failure;
6144 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6145 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6146 survey->channel_time))
6147 goto nla_put_failure;
6148 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6149 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6150 survey->channel_time_busy))
6151 goto nla_put_failure;
6152 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6153 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6154 survey->channel_time_ext_busy))
6155 goto nla_put_failure;
6156 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6157 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6158 survey->channel_time_rx))
6159 goto nla_put_failure;
6160 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6161 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6162 survey->channel_time_tx))
6163 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006164
6165 nla_nest_end(msg, infoattr);
6166
6167 return genlmsg_end(msg, hdr);
6168
6169 nla_put_failure:
6170 genlmsg_cancel(msg, hdr);
6171 return -EMSGSIZE;
6172}
6173
6174static int nl80211_dump_survey(struct sk_buff *skb,
6175 struct netlink_callback *cb)
6176{
6177 struct survey_info survey;
6178 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006179 struct wireless_dev *wdev;
6180 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006181 int res;
6182
Johannes Berg97990a02013-04-19 01:02:55 +02006183 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006184 if (res)
6185 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006186
Johannes Berg97990a02013-04-19 01:02:55 +02006187 if (!wdev->netdev) {
6188 res = -EINVAL;
6189 goto out_err;
6190 }
6191
Holger Schurig61fa7132009-11-11 12:25:40 +01006192 if (!dev->ops->dump_survey) {
6193 res = -EOPNOTSUPP;
6194 goto out_err;
6195 }
6196
6197 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006198 struct ieee80211_channel *chan;
6199
Johannes Berg97990a02013-04-19 01:02:55 +02006200 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006201 if (res == -ENOENT)
6202 break;
6203 if (res)
6204 goto out_err;
6205
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006206 /* Survey without a channel doesn't make sense */
6207 if (!survey.channel) {
6208 res = -EINVAL;
6209 goto out;
6210 }
6211
6212 chan = ieee80211_get_channel(&dev->wiphy,
6213 survey.channel->center_freq);
6214 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6215 survey_idx++;
6216 continue;
6217 }
6218
Holger Schurig61fa7132009-11-11 12:25:40 +01006219 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006220 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006221 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006222 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006223 goto out;
6224 survey_idx++;
6225 }
6226
6227 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006228 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006229 res = skb->len;
6230 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006231 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006232 return res;
6233}
6234
Samuel Ortizb23aa672009-07-01 21:26:54 +02006235static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6236{
6237 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6238 NL80211_WPA_VERSION_2));
6239}
6240
Jouni Malinen636a5d32009-03-19 13:39:22 +02006241static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6242{
Johannes Berg4c476992010-10-04 21:36:35 +02006243 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6244 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006245 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006246 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6247 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006248 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006249 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006250 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006251
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006252 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6253 return -EINVAL;
6254
6255 if (!info->attrs[NL80211_ATTR_MAC])
6256 return -EINVAL;
6257
Jouni Malinen17780922009-03-27 20:52:47 +02006258 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6259 return -EINVAL;
6260
Johannes Berg19957bb2009-07-02 17:20:43 +02006261 if (!info->attrs[NL80211_ATTR_SSID])
6262 return -EINVAL;
6263
6264 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6265 return -EINVAL;
6266
Johannes Bergfffd0932009-07-08 14:22:54 +02006267 err = nl80211_parse_key(info, &key);
6268 if (err)
6269 return err;
6270
6271 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006272 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6273 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006274 if (!key.p.key || !key.p.key_len)
6275 return -EINVAL;
6276 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6277 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6278 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6279 key.p.key_len != WLAN_KEY_LEN_WEP104))
6280 return -EINVAL;
6281 if (key.idx > 4)
6282 return -EINVAL;
6283 } else {
6284 key.p.key_len = 0;
6285 key.p.key = NULL;
6286 }
6287
Johannes Bergafea0b72010-08-10 09:46:42 +02006288 if (key.idx >= 0) {
6289 int i;
6290 bool ok = false;
6291 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6292 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6293 ok = true;
6294 break;
6295 }
6296 }
Johannes Berg4c476992010-10-04 21:36:35 +02006297 if (!ok)
6298 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006299 }
6300
Johannes Berg4c476992010-10-04 21:36:35 +02006301 if (!rdev->ops->auth)
6302 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006303
Johannes Berg074ac8d2010-09-16 14:58:22 +02006304 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006305 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6306 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006307
Johannes Berg19957bb2009-07-02 17:20:43 +02006308 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006309 chan = nl80211_get_valid_chan(&rdev->wiphy,
6310 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6311 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006312 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006313
Johannes Berg19957bb2009-07-02 17:20:43 +02006314 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6315 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6316
6317 if (info->attrs[NL80211_ATTR_IE]) {
6318 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6319 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6320 }
6321
6322 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006323 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006324 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006325
Jouni Malinene39e5b52012-09-30 19:29:39 +03006326 if (auth_type == NL80211_AUTHTYPE_SAE &&
6327 !info->attrs[NL80211_ATTR_SAE_DATA])
6328 return -EINVAL;
6329
6330 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6331 if (auth_type != NL80211_AUTHTYPE_SAE)
6332 return -EINVAL;
6333 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6334 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6335 /* need to include at least Auth Transaction and Status Code */
6336 if (sae_data_len < 4)
6337 return -EINVAL;
6338 }
6339
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006340 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6341
Johannes Berg95de8172012-01-20 13:55:25 +01006342 /*
6343 * Since we no longer track auth state, ignore
6344 * requests to only change local state.
6345 */
6346 if (local_state_change)
6347 return 0;
6348
Johannes Berg91bf9b22013-05-15 17:44:01 +02006349 wdev_lock(dev->ieee80211_ptr);
6350 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6351 ssid, ssid_len, ie, ie_len,
6352 key.p.key, key.p.key_len, key.idx,
6353 sae_data, sae_data_len);
6354 wdev_unlock(dev->ieee80211_ptr);
6355 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006356}
6357
Johannes Bergc0692b82010-08-27 14:26:53 +03006358static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6359 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006360 struct cfg80211_crypto_settings *settings,
6361 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006362{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006363 memset(settings, 0, sizeof(*settings));
6364
Samuel Ortizb23aa672009-07-01 21:26:54 +02006365 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6366
Johannes Bergc0692b82010-08-27 14:26:53 +03006367 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6368 u16 proto;
6369 proto = nla_get_u16(
6370 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6371 settings->control_port_ethertype = cpu_to_be16(proto);
6372 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6373 proto != ETH_P_PAE)
6374 return -EINVAL;
6375 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6376 settings->control_port_no_encrypt = true;
6377 } else
6378 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6379
Samuel Ortizb23aa672009-07-01 21:26:54 +02006380 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6381 void *data;
6382 int len, i;
6383
6384 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6385 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6386 settings->n_ciphers_pairwise = len / sizeof(u32);
6387
6388 if (len % sizeof(u32))
6389 return -EINVAL;
6390
Johannes Berg3dc27d22009-07-02 21:36:37 +02006391 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006392 return -EINVAL;
6393
6394 memcpy(settings->ciphers_pairwise, data, len);
6395
6396 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006397 if (!cfg80211_supported_cipher_suite(
6398 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006399 settings->ciphers_pairwise[i]))
6400 return -EINVAL;
6401 }
6402
6403 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6404 settings->cipher_group =
6405 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006406 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6407 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006408 return -EINVAL;
6409 }
6410
6411 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6412 settings->wpa_versions =
6413 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6414 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6415 return -EINVAL;
6416 }
6417
6418 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6419 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006420 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006421
6422 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6423 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6424 settings->n_akm_suites = len / sizeof(u32);
6425
6426 if (len % sizeof(u32))
6427 return -EINVAL;
6428
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006429 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6430 return -EINVAL;
6431
Samuel Ortizb23aa672009-07-01 21:26:54 +02006432 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006433 }
6434
6435 return 0;
6436}
6437
Jouni Malinen636a5d32009-03-19 13:39:22 +02006438static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6439{
Johannes Berg4c476992010-10-04 21:36:35 +02006440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6441 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006442 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006443 struct cfg80211_assoc_request req = {};
6444 const u8 *bssid, *ssid;
6445 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006446
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006447 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6448 return -EINVAL;
6449
6450 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006451 !info->attrs[NL80211_ATTR_SSID] ||
6452 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006453 return -EINVAL;
6454
Johannes Berg4c476992010-10-04 21:36:35 +02006455 if (!rdev->ops->assoc)
6456 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006457
Johannes Berg074ac8d2010-09-16 14:58:22 +02006458 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006459 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6460 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006461
Johannes Berg19957bb2009-07-02 17:20:43 +02006462 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006463
Jouni Malinen664834d2014-01-15 00:01:44 +02006464 chan = nl80211_get_valid_chan(&rdev->wiphy,
6465 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6466 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006467 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006468
Johannes Berg19957bb2009-07-02 17:20:43 +02006469 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6470 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006471
6472 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006473 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6474 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006475 }
6476
Jouni Malinendc6382c2009-05-06 22:09:37 +03006477 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006478 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006479 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006480 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006481 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006482 else if (mfp != NL80211_MFP_NO)
6483 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006484 }
6485
Johannes Berg3e5d7642009-07-07 14:37:26 +02006486 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006487 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006488
Ben Greear7e7c8922011-11-18 11:31:59 -08006489 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006490 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006491
6492 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006493 memcpy(&req.ht_capa_mask,
6494 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6495 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006496
6497 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006498 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006499 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006500 memcpy(&req.ht_capa,
6501 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6502 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006503 }
6504
Johannes Bergee2aca32013-02-21 17:36:01 +01006505 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006506 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006507
6508 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006509 memcpy(&req.vht_capa_mask,
6510 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6511 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006512
6513 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006514 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006515 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006516 memcpy(&req.vht_capa,
6517 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6518 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006519 }
6520
Johannes Bergf62fab72013-02-21 20:09:09 +01006521 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006522 if (!err) {
6523 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006524 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6525 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006526 wdev_unlock(dev->ieee80211_ptr);
6527 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006528
Jouni Malinen636a5d32009-03-19 13:39:22 +02006529 return err;
6530}
6531
6532static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6533{
Johannes Berg4c476992010-10-04 21:36:35 +02006534 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6535 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006536 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006537 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006538 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006539 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006540
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006541 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6542 return -EINVAL;
6543
6544 if (!info->attrs[NL80211_ATTR_MAC])
6545 return -EINVAL;
6546
6547 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6548 return -EINVAL;
6549
Johannes Berg4c476992010-10-04 21:36:35 +02006550 if (!rdev->ops->deauth)
6551 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006552
Johannes Berg074ac8d2010-09-16 14:58:22 +02006553 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006554 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6555 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006556
Johannes Berg19957bb2009-07-02 17:20:43 +02006557 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006558
Johannes Berg19957bb2009-07-02 17:20:43 +02006559 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6560 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006561 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006562 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006563 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006564
6565 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006566 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6567 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006568 }
6569
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006570 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6571
Johannes Berg91bf9b22013-05-15 17:44:01 +02006572 wdev_lock(dev->ieee80211_ptr);
6573 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6574 local_state_change);
6575 wdev_unlock(dev->ieee80211_ptr);
6576 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006577}
6578
6579static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6580{
Johannes Berg4c476992010-10-04 21:36:35 +02006581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6582 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006583 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006584 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006585 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006586 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006587
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006588 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6589 return -EINVAL;
6590
6591 if (!info->attrs[NL80211_ATTR_MAC])
6592 return -EINVAL;
6593
6594 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6595 return -EINVAL;
6596
Johannes Berg4c476992010-10-04 21:36:35 +02006597 if (!rdev->ops->disassoc)
6598 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006599
Johannes Berg074ac8d2010-09-16 14:58:22 +02006600 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006601 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6602 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006603
Johannes Berg19957bb2009-07-02 17:20:43 +02006604 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006605
Johannes Berg19957bb2009-07-02 17:20:43 +02006606 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6607 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006608 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006609 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006610 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006611
6612 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006613 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6614 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006615 }
6616
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006617 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6618
Johannes Berg91bf9b22013-05-15 17:44:01 +02006619 wdev_lock(dev->ieee80211_ptr);
6620 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6621 local_state_change);
6622 wdev_unlock(dev->ieee80211_ptr);
6623 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006624}
6625
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006626static bool
6627nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6628 int mcast_rate[IEEE80211_NUM_BANDS],
6629 int rateval)
6630{
6631 struct wiphy *wiphy = &rdev->wiphy;
6632 bool found = false;
6633 int band, i;
6634
6635 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6636 struct ieee80211_supported_band *sband;
6637
6638 sband = wiphy->bands[band];
6639 if (!sband)
6640 continue;
6641
6642 for (i = 0; i < sband->n_bitrates; i++) {
6643 if (sband->bitrates[i].bitrate == rateval) {
6644 mcast_rate[band] = i + 1;
6645 found = true;
6646 break;
6647 }
6648 }
6649 }
6650
6651 return found;
6652}
6653
Johannes Berg04a773a2009-04-19 21:24:32 +02006654static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6655{
Johannes Berg4c476992010-10-04 21:36:35 +02006656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6657 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006658 struct cfg80211_ibss_params ibss;
6659 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006660 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006661 int err;
6662
Johannes Berg8e30bc52009-04-22 17:45:38 +02006663 memset(&ibss, 0, sizeof(ibss));
6664
Johannes Berg04a773a2009-04-19 21:24:32 +02006665 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6666 return -EINVAL;
6667
Johannes Berg683b6d32012-11-08 21:25:48 +01006668 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006669 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6670 return -EINVAL;
6671
Johannes Berg8e30bc52009-04-22 17:45:38 +02006672 ibss.beacon_interval = 100;
6673
6674 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6675 ibss.beacon_interval =
6676 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6677 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6678 return -EINVAL;
6679 }
6680
Johannes Berg4c476992010-10-04 21:36:35 +02006681 if (!rdev->ops->join_ibss)
6682 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006683
Johannes Berg4c476992010-10-04 21:36:35 +02006684 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6685 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006686
Johannes Berg79c97e92009-07-07 03:56:12 +02006687 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006688
Johannes Berg39193492011-09-16 13:45:25 +02006689 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006690 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006691
6692 if (!is_valid_ether_addr(ibss.bssid))
6693 return -EINVAL;
6694 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006695 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6696 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6697
6698 if (info->attrs[NL80211_ATTR_IE]) {
6699 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6700 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6701 }
6702
Johannes Berg683b6d32012-11-08 21:25:48 +01006703 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6704 if (err)
6705 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006706
Johannes Berg683b6d32012-11-08 21:25:48 +01006707 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006708 return -EINVAL;
6709
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006710 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006711 case NL80211_CHAN_WIDTH_5:
6712 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006713 case NL80211_CHAN_WIDTH_20_NOHT:
6714 break;
6715 case NL80211_CHAN_WIDTH_20:
6716 case NL80211_CHAN_WIDTH_40:
6717 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6718 break;
6719 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006720 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006721 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006722
Johannes Berg04a773a2009-04-19 21:24:32 +02006723 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006724 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006725
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006726 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6727 u8 *rates =
6728 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6729 int n_rates =
6730 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6731 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006732 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006733
Johannes Berg34850ab2011-07-18 18:08:35 +02006734 err = ieee80211_get_ratemask(sband, rates, n_rates,
6735 &ibss.basic_rates);
6736 if (err)
6737 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006738 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006739
Simon Wunderlich803768f2013-06-28 10:39:58 +02006740 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6741 memcpy(&ibss.ht_capa_mask,
6742 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6743 sizeof(ibss.ht_capa_mask));
6744
6745 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6746 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6747 return -EINVAL;
6748 memcpy(&ibss.ht_capa,
6749 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6750 sizeof(ibss.ht_capa));
6751 }
6752
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006753 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6754 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6755 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6756 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006757
Johannes Berg4c476992010-10-04 21:36:35 +02006758 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306759 bool no_ht = false;
6760
Johannes Berg4c476992010-10-04 21:36:35 +02006761 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306762 info->attrs[NL80211_ATTR_KEYS],
6763 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006764 if (IS_ERR(connkeys))
6765 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306766
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006767 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6768 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306769 kfree(connkeys);
6770 return -EINVAL;
6771 }
Johannes Berg4c476992010-10-04 21:36:35 +02006772 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006773
Antonio Quartulli267335d2012-01-31 20:25:47 +01006774 ibss.control_port =
6775 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6776
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006777 ibss.userspace_handles_dfs =
6778 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6779
Johannes Berg4c476992010-10-04 21:36:35 +02006780 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006781 if (err)
6782 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006783 return err;
6784}
6785
6786static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6787{
Johannes Berg4c476992010-10-04 21:36:35 +02006788 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6789 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006790
Johannes Berg4c476992010-10-04 21:36:35 +02006791 if (!rdev->ops->leave_ibss)
6792 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006793
Johannes Berg4c476992010-10-04 21:36:35 +02006794 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6795 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006796
Johannes Berg4c476992010-10-04 21:36:35 +02006797 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006798}
6799
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006800static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6801{
6802 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6803 struct net_device *dev = info->user_ptr[1];
6804 int mcast_rate[IEEE80211_NUM_BANDS];
6805 u32 nla_rate;
6806 int err;
6807
6808 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6809 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6810 return -EOPNOTSUPP;
6811
6812 if (!rdev->ops->set_mcast_rate)
6813 return -EOPNOTSUPP;
6814
6815 memset(mcast_rate, 0, sizeof(mcast_rate));
6816
6817 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6818 return -EINVAL;
6819
6820 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6821 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6822 return -EINVAL;
6823
6824 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6825
6826 return err;
6827}
6828
Johannes Bergad7e7182013-11-13 13:37:47 +01006829static struct sk_buff *
6830__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6831 int approxlen, u32 portid, u32 seq,
6832 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006833 enum nl80211_attrs attr,
6834 const struct nl80211_vendor_cmd_info *info,
6835 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006836{
6837 struct sk_buff *skb;
6838 void *hdr;
6839 struct nlattr *data;
6840
6841 skb = nlmsg_new(approxlen + 100, gfp);
6842 if (!skb)
6843 return NULL;
6844
6845 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6846 if (!hdr) {
6847 kfree_skb(skb);
6848 return NULL;
6849 }
6850
6851 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6852 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006853
6854 if (info) {
6855 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6856 info->vendor_id))
6857 goto nla_put_failure;
6858 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6859 info->subcmd))
6860 goto nla_put_failure;
6861 }
6862
Johannes Bergad7e7182013-11-13 13:37:47 +01006863 data = nla_nest_start(skb, attr);
6864
6865 ((void **)skb->cb)[0] = rdev;
6866 ((void **)skb->cb)[1] = hdr;
6867 ((void **)skb->cb)[2] = data;
6868
6869 return skb;
6870
6871 nla_put_failure:
6872 kfree_skb(skb);
6873 return NULL;
6874}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006875
Johannes Berge03ad6e2014-01-01 17:22:30 +01006876struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6877 enum nl80211_commands cmd,
6878 enum nl80211_attrs attr,
6879 int vendor_event_idx,
6880 int approxlen, gfp_t gfp)
6881{
6882 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6883 const struct nl80211_vendor_cmd_info *info;
6884
6885 switch (cmd) {
6886 case NL80211_CMD_TESTMODE:
6887 if (WARN_ON(vendor_event_idx != -1))
6888 return NULL;
6889 info = NULL;
6890 break;
6891 case NL80211_CMD_VENDOR:
6892 if (WARN_ON(vendor_event_idx < 0 ||
6893 vendor_event_idx >= wiphy->n_vendor_events))
6894 return NULL;
6895 info = &wiphy->vendor_events[vendor_event_idx];
6896 break;
6897 default:
6898 WARN_ON(1);
6899 return NULL;
6900 }
6901
6902 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6903 cmd, attr, info, gfp);
6904}
6905EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6906
6907void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6908{
6909 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6910 void *hdr = ((void **)skb->cb)[1];
6911 struct nlattr *data = ((void **)skb->cb)[2];
6912 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6913
6914 nla_nest_end(skb, data);
6915 genlmsg_end(skb, hdr);
6916
6917 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6918 mcgrp = NL80211_MCGRP_VENDOR;
6919
6920 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6921 mcgrp, gfp);
6922}
6923EXPORT_SYMBOL(__cfg80211_send_event_skb);
6924
Johannes Bergaff89a92009-07-01 21:26:51 +02006925#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006926static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6927{
Johannes Berg4c476992010-10-04 21:36:35 +02006928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006929 struct wireless_dev *wdev =
6930 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006931 int err;
6932
David Spinadelfc73f112013-07-31 18:04:15 +03006933 if (!rdev->ops->testmode_cmd)
6934 return -EOPNOTSUPP;
6935
6936 if (IS_ERR(wdev)) {
6937 err = PTR_ERR(wdev);
6938 if (err != -EINVAL)
6939 return err;
6940 wdev = NULL;
6941 } else if (wdev->wiphy != &rdev->wiphy) {
6942 return -EINVAL;
6943 }
6944
Johannes Bergaff89a92009-07-01 21:26:51 +02006945 if (!info->attrs[NL80211_ATTR_TESTDATA])
6946 return -EINVAL;
6947
Johannes Bergad7e7182013-11-13 13:37:47 +01006948 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006949 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006950 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6951 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006952 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006953
Johannes Bergaff89a92009-07-01 21:26:51 +02006954 return err;
6955}
6956
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006957static int nl80211_testmode_dump(struct sk_buff *skb,
6958 struct netlink_callback *cb)
6959{
Johannes Berg00918d32011-12-13 17:22:05 +01006960 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006961 int err;
6962 long phy_idx;
6963 void *data = NULL;
6964 int data_len = 0;
6965
Johannes Berg5fe231e2013-05-08 21:45:15 +02006966 rtnl_lock();
6967
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006968 if (cb->args[0]) {
6969 /*
6970 * 0 is a valid index, but not valid for args[0],
6971 * so we need to offset by 1.
6972 */
6973 phy_idx = cb->args[0] - 1;
6974 } else {
6975 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6976 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6977 nl80211_policy);
6978 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006979 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006980
Johannes Berg2bd7e352012-06-15 14:23:16 +02006981 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6982 nl80211_fam.attrbuf);
6983 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006984 err = PTR_ERR(rdev);
6985 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006986 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006987 phy_idx = rdev->wiphy_idx;
6988 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006989
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006990 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6991 cb->args[1] =
6992 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6993 }
6994
6995 if (cb->args[1]) {
6996 data = nla_data((void *)cb->args[1]);
6997 data_len = nla_len((void *)cb->args[1]);
6998 }
6999
Johannes Berg00918d32011-12-13 17:22:05 +01007000 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7001 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007002 err = -ENOENT;
7003 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007004 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007005
Johannes Berg00918d32011-12-13 17:22:05 +01007006 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007007 err = -EOPNOTSUPP;
7008 goto out_err;
7009 }
7010
7011 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007012 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007013 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7014 NL80211_CMD_TESTMODE);
7015 struct nlattr *tmdata;
7016
Dan Carpentercb35fba2013-08-14 14:50:01 +03007017 if (!hdr)
7018 break;
7019
David S. Miller9360ffd2012-03-29 04:41:26 -04007020 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007021 genlmsg_cancel(skb, hdr);
7022 break;
7023 }
7024
7025 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7026 if (!tmdata) {
7027 genlmsg_cancel(skb, hdr);
7028 break;
7029 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007030 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007031 nla_nest_end(skb, tmdata);
7032
7033 if (err == -ENOBUFS || err == -ENOENT) {
7034 genlmsg_cancel(skb, hdr);
7035 break;
7036 } else if (err) {
7037 genlmsg_cancel(skb, hdr);
7038 goto out_err;
7039 }
7040
7041 genlmsg_end(skb, hdr);
7042 }
7043
7044 err = skb->len;
7045 /* see above */
7046 cb->args[0] = phy_idx + 1;
7047 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007048 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007049 return err;
7050}
Johannes Bergaff89a92009-07-01 21:26:51 +02007051#endif
7052
Samuel Ortizb23aa672009-07-01 21:26:54 +02007053static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7054{
Johannes Berg4c476992010-10-04 21:36:35 +02007055 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7056 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007057 struct cfg80211_connect_params connect;
7058 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007059 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007060 int err;
7061
7062 memset(&connect, 0, sizeof(connect));
7063
7064 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7065 return -EINVAL;
7066
7067 if (!info->attrs[NL80211_ATTR_SSID] ||
7068 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7069 return -EINVAL;
7070
7071 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7072 connect.auth_type =
7073 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007074 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7075 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007076 return -EINVAL;
7077 } else
7078 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7079
7080 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7081
Johannes Bergc0692b82010-08-27 14:26:53 +03007082 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007083 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007084 if (err)
7085 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007086
Johannes Berg074ac8d2010-09-16 14:58:22 +02007087 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007088 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7089 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007090
Johannes Berg79c97e92009-07-07 03:56:12 +02007091 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007092
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307093 connect.bg_scan_period = -1;
7094 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7095 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7096 connect.bg_scan_period =
7097 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7098 }
7099
Samuel Ortizb23aa672009-07-01 21:26:54 +02007100 if (info->attrs[NL80211_ATTR_MAC])
7101 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007102 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7103 connect.bssid_hint =
7104 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007105 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7106 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7107
7108 if (info->attrs[NL80211_ATTR_IE]) {
7109 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7110 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7111 }
7112
Jouni Malinencee00a92013-01-15 17:15:57 +02007113 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7114 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7115 if (connect.mfp != NL80211_MFP_REQUIRED &&
7116 connect.mfp != NL80211_MFP_NO)
7117 return -EINVAL;
7118 } else {
7119 connect.mfp = NL80211_MFP_NO;
7120 }
7121
Samuel Ortizb23aa672009-07-01 21:26:54 +02007122 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007123 connect.channel = nl80211_get_valid_chan(
7124 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7125 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007126 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007127 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007128 connect.channel_hint = nl80211_get_valid_chan(
7129 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7130 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007131 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007132 }
7133
Johannes Bergfffd0932009-07-08 14:22:54 +02007134 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7135 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307136 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007137 if (IS_ERR(connkeys))
7138 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007139 }
7140
Ben Greear7e7c8922011-11-18 11:31:59 -08007141 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7142 connect.flags |= ASSOC_REQ_DISABLE_HT;
7143
7144 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7145 memcpy(&connect.ht_capa_mask,
7146 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7147 sizeof(connect.ht_capa_mask));
7148
7149 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007150 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7151 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007152 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007153 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007154 memcpy(&connect.ht_capa,
7155 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7156 sizeof(connect.ht_capa));
7157 }
7158
Johannes Bergee2aca32013-02-21 17:36:01 +01007159 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7160 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7161
7162 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7163 memcpy(&connect.vht_capa_mask,
7164 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7165 sizeof(connect.vht_capa_mask));
7166
7167 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7168 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7169 kfree(connkeys);
7170 return -EINVAL;
7171 }
7172 memcpy(&connect.vht_capa,
7173 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7174 sizeof(connect.vht_capa));
7175 }
7176
Johannes Berg83739b02013-05-15 17:44:01 +02007177 wdev_lock(dev->ieee80211_ptr);
7178 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7179 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007180 if (err)
7181 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007182 return err;
7183}
7184
7185static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7186{
Johannes Berg4c476992010-10-04 21:36:35 +02007187 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7188 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007189 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007190 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007191
7192 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7193 reason = WLAN_REASON_DEAUTH_LEAVING;
7194 else
7195 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7196
7197 if (reason == 0)
7198 return -EINVAL;
7199
Johannes Berg074ac8d2010-09-16 14:58:22 +02007200 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007201 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7202 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007203
Johannes Berg83739b02013-05-15 17:44:01 +02007204 wdev_lock(dev->ieee80211_ptr);
7205 ret = cfg80211_disconnect(rdev, dev, reason, true);
7206 wdev_unlock(dev->ieee80211_ptr);
7207 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007208}
7209
Johannes Berg463d0182009-07-14 00:33:35 +02007210static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7211{
Johannes Berg4c476992010-10-04 21:36:35 +02007212 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007213 struct net *net;
7214 int err;
7215 u32 pid;
7216
7217 if (!info->attrs[NL80211_ATTR_PID])
7218 return -EINVAL;
7219
7220 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7221
Johannes Berg463d0182009-07-14 00:33:35 +02007222 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007223 if (IS_ERR(net))
7224 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007225
7226 err = 0;
7227
7228 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007229 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7230 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007231
Johannes Berg463d0182009-07-14 00:33:35 +02007232 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007233 return err;
7234}
7235
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007236static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7237{
Johannes Berg4c476992010-10-04 21:36:35 +02007238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007239 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7240 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007241 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007242 struct cfg80211_pmksa pmksa;
7243
7244 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7245
7246 if (!info->attrs[NL80211_ATTR_MAC])
7247 return -EINVAL;
7248
7249 if (!info->attrs[NL80211_ATTR_PMKID])
7250 return -EINVAL;
7251
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007252 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7253 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7254
Johannes Berg074ac8d2010-09-16 14:58:22 +02007255 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007256 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7257 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007258
7259 switch (info->genlhdr->cmd) {
7260 case NL80211_CMD_SET_PMKSA:
7261 rdev_ops = rdev->ops->set_pmksa;
7262 break;
7263 case NL80211_CMD_DEL_PMKSA:
7264 rdev_ops = rdev->ops->del_pmksa;
7265 break;
7266 default:
7267 WARN_ON(1);
7268 break;
7269 }
7270
Johannes Berg4c476992010-10-04 21:36:35 +02007271 if (!rdev_ops)
7272 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007273
Johannes Berg4c476992010-10-04 21:36:35 +02007274 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007275}
7276
7277static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7278{
Johannes Berg4c476992010-10-04 21:36:35 +02007279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7280 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007281
Johannes Berg074ac8d2010-09-16 14:58:22 +02007282 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007283 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7284 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007285
Johannes Berg4c476992010-10-04 21:36:35 +02007286 if (!rdev->ops->flush_pmksa)
7287 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007288
Hila Gonene35e4d22012-06-27 17:19:42 +03007289 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007290}
7291
Arik Nemtsov109086c2011-09-28 14:12:50 +03007292static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7293{
7294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7295 struct net_device *dev = info->user_ptr[1];
7296 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307297 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007298 u16 status_code;
7299 u8 *peer;
7300
7301 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7302 !rdev->ops->tdls_mgmt)
7303 return -EOPNOTSUPP;
7304
7305 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7306 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7307 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7308 !info->attrs[NL80211_ATTR_IE] ||
7309 !info->attrs[NL80211_ATTR_MAC])
7310 return -EINVAL;
7311
7312 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7313 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7314 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7315 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307316 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7317 peer_capability =
7318 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007319
Hila Gonene35e4d22012-06-27 17:19:42 +03007320 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307321 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007322 nla_data(info->attrs[NL80211_ATTR_IE]),
7323 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007324}
7325
7326static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7327{
7328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7329 struct net_device *dev = info->user_ptr[1];
7330 enum nl80211_tdls_operation operation;
7331 u8 *peer;
7332
7333 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7334 !rdev->ops->tdls_oper)
7335 return -EOPNOTSUPP;
7336
7337 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7338 !info->attrs[NL80211_ATTR_MAC])
7339 return -EINVAL;
7340
7341 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7342 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7343
Hila Gonene35e4d22012-06-27 17:19:42 +03007344 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007345}
7346
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007347static int nl80211_remain_on_channel(struct sk_buff *skb,
7348 struct genl_info *info)
7349{
Johannes Berg4c476992010-10-04 21:36:35 +02007350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007351 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007352 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007353 struct sk_buff *msg;
7354 void *hdr;
7355 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007356 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007357 int err;
7358
7359 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7360 !info->attrs[NL80211_ATTR_DURATION])
7361 return -EINVAL;
7362
7363 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7364
Johannes Berg7c4ef712011-11-18 15:33:48 +01007365 if (!rdev->ops->remain_on_channel ||
7366 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007367 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007368
Johannes Bergebf348f2012-06-01 12:50:54 +02007369 /*
7370 * We should be on that channel for at least a minimum amount of
7371 * time (10ms) but no longer than the driver supports.
7372 */
7373 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7374 duration > rdev->wiphy.max_remain_on_channel_duration)
7375 return -EINVAL;
7376
Johannes Berg683b6d32012-11-08 21:25:48 +01007377 err = nl80211_parse_chandef(rdev, info, &chandef);
7378 if (err)
7379 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007380
7381 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007382 if (!msg)
7383 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007384
Eric W. Biederman15e47302012-09-07 20:12:54 +00007385 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007386 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007387 if (!hdr) {
7388 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007389 goto free_msg;
7390 }
7391
Johannes Berg683b6d32012-11-08 21:25:48 +01007392 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7393 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007394
7395 if (err)
7396 goto free_msg;
7397
David S. Miller9360ffd2012-03-29 04:41:26 -04007398 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7399 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007400
7401 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007402
7403 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007404
7405 nla_put_failure:
7406 err = -ENOBUFS;
7407 free_msg:
7408 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007409 return err;
7410}
7411
7412static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7413 struct genl_info *info)
7414{
Johannes Berg4c476992010-10-04 21:36:35 +02007415 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007416 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007417 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007418
7419 if (!info->attrs[NL80211_ATTR_COOKIE])
7420 return -EINVAL;
7421
Johannes Berg4c476992010-10-04 21:36:35 +02007422 if (!rdev->ops->cancel_remain_on_channel)
7423 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007424
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007425 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7426
Hila Gonene35e4d22012-06-27 17:19:42 +03007427 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007428}
7429
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007430static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7431 u8 *rates, u8 rates_len)
7432{
7433 u8 i;
7434 u32 mask = 0;
7435
7436 for (i = 0; i < rates_len; i++) {
7437 int rate = (rates[i] & 0x7f) * 5;
7438 int ridx;
7439 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7440 struct ieee80211_rate *srate =
7441 &sband->bitrates[ridx];
7442 if (rate == srate->bitrate) {
7443 mask |= 1 << ridx;
7444 break;
7445 }
7446 }
7447 if (ridx == sband->n_bitrates)
7448 return 0; /* rate not found */
7449 }
7450
7451 return mask;
7452}
7453
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007454static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7455 u8 *rates, u8 rates_len,
7456 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7457{
7458 u8 i;
7459
7460 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7461
7462 for (i = 0; i < rates_len; i++) {
7463 int ridx, rbit;
7464
7465 ridx = rates[i] / 8;
7466 rbit = BIT(rates[i] % 8);
7467
7468 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007469 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007470 return false;
7471
7472 /* check availability */
7473 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7474 mcs[ridx] |= rbit;
7475 else
7476 return false;
7477 }
7478
7479 return true;
7480}
7481
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007482static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7483{
7484 u16 mcs_mask = 0;
7485
7486 switch (vht_mcs_map) {
7487 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7488 break;
7489 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7490 mcs_mask = 0x00FF;
7491 break;
7492 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7493 mcs_mask = 0x01FF;
7494 break;
7495 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7496 mcs_mask = 0x03FF;
7497 break;
7498 default:
7499 break;
7500 }
7501
7502 return mcs_mask;
7503}
7504
7505static void vht_build_mcs_mask(u16 vht_mcs_map,
7506 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7507{
7508 u8 nss;
7509
7510 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7511 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7512 vht_mcs_map >>= 2;
7513 }
7514}
7515
7516static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7517 struct nl80211_txrate_vht *txrate,
7518 u16 mcs[NL80211_VHT_NSS_MAX])
7519{
7520 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7521 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7522 u8 i;
7523
7524 if (!sband->vht_cap.vht_supported)
7525 return false;
7526
7527 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7528
7529 /* Build vht_mcs_mask from VHT capabilities */
7530 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7531
7532 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7533 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7534 mcs[i] = txrate->mcs[i];
7535 else
7536 return false;
7537 }
7538
7539 return true;
7540}
7541
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007542static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007543 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7544 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007545 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7546 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007547 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007548 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007549};
7550
7551static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7552 struct genl_info *info)
7553{
7554 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007555 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007556 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007557 int rem, i;
7558 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007559 struct nlattr *tx_rates;
7560 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007561 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007562
Johannes Berg4c476992010-10-04 21:36:35 +02007563 if (!rdev->ops->set_bitrate_mask)
7564 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007565
7566 memset(&mask, 0, sizeof(mask));
7567 /* Default to all rates enabled */
7568 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7569 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007570
7571 if (!sband)
7572 continue;
7573
7574 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007575 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007576 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007577 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007578
7579 if (!sband->vht_cap.vht_supported)
7580 continue;
7581
7582 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7583 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007584 }
7585
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007586 /* if no rates are given set it back to the defaults */
7587 if (!info->attrs[NL80211_ATTR_TX_RATES])
7588 goto out;
7589
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007590 /*
7591 * The nested attribute uses enum nl80211_band as the index. This maps
7592 * directly to the enum ieee80211_band values used in cfg80211.
7593 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007594 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007595 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007596 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007597 int err;
7598
Johannes Berg4c476992010-10-04 21:36:35 +02007599 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7600 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007601 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007602 if (sband == NULL)
7603 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007604 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7605 nla_len(tx_rates), nl80211_txattr_policy);
7606 if (err)
7607 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007608 if (tb[NL80211_TXRATE_LEGACY]) {
7609 mask.control[band].legacy = rateset_to_mask(
7610 sband,
7611 nla_data(tb[NL80211_TXRATE_LEGACY]),
7612 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307613 if ((mask.control[band].legacy == 0) &&
7614 nla_len(tb[NL80211_TXRATE_LEGACY]))
7615 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007616 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007617 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007618 if (!ht_rateset_to_mask(
7619 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007620 nla_data(tb[NL80211_TXRATE_HT]),
7621 nla_len(tb[NL80211_TXRATE_HT]),
7622 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007623 return -EINVAL;
7624 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007625 if (tb[NL80211_TXRATE_VHT]) {
7626 if (!vht_set_mcs_mask(
7627 sband,
7628 nla_data(tb[NL80211_TXRATE_VHT]),
7629 mask.control[band].vht_mcs))
7630 return -EINVAL;
7631 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007632 if (tb[NL80211_TXRATE_GI]) {
7633 mask.control[band].gi =
7634 nla_get_u8(tb[NL80211_TXRATE_GI]);
7635 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7636 return -EINVAL;
7637 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007638
7639 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007640 /* don't allow empty legacy rates if HT or VHT
7641 * are not even supported.
7642 */
7643 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7644 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007645 return -EINVAL;
7646
7647 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007648 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007649 goto out;
7650
7651 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7652 if (mask.control[band].vht_mcs[i])
7653 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007654
7655 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007656 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007657 }
7658 }
7659
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007660out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007661 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007662}
7663
Johannes Berg2e161f72010-08-12 15:38:38 +02007664static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007665{
Johannes Berg4c476992010-10-04 21:36:35 +02007666 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007667 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007668 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007669
7670 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7671 return -EINVAL;
7672
Johannes Berg2e161f72010-08-12 15:38:38 +02007673 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7674 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007675
Johannes Berg71bbc992012-06-15 15:30:18 +02007676 switch (wdev->iftype) {
7677 case NL80211_IFTYPE_STATION:
7678 case NL80211_IFTYPE_ADHOC:
7679 case NL80211_IFTYPE_P2P_CLIENT:
7680 case NL80211_IFTYPE_AP:
7681 case NL80211_IFTYPE_AP_VLAN:
7682 case NL80211_IFTYPE_MESH_POINT:
7683 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007684 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007685 break;
7686 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007687 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007688 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007689
7690 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007691 if (!rdev->ops->mgmt_tx)
7692 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007693
Eric W. Biederman15e47302012-09-07 20:12:54 +00007694 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007695 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7696 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007697}
7698
Johannes Berg2e161f72010-08-12 15:38:38 +02007699static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007700{
Johannes Berg4c476992010-10-04 21:36:35 +02007701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007702 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007703 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007704 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007705 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007706 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007707 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007708 struct cfg80211_mgmt_tx_params params = {
7709 .dont_wait_for_ack =
7710 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7711 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007712
Johannes Berg683b6d32012-11-08 21:25:48 +01007713 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007714 return -EINVAL;
7715
Johannes Berg4c476992010-10-04 21:36:35 +02007716 if (!rdev->ops->mgmt_tx)
7717 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007718
Johannes Berg71bbc992012-06-15 15:30:18 +02007719 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007720 case NL80211_IFTYPE_P2P_DEVICE:
7721 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7722 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007723 case NL80211_IFTYPE_STATION:
7724 case NL80211_IFTYPE_ADHOC:
7725 case NL80211_IFTYPE_P2P_CLIENT:
7726 case NL80211_IFTYPE_AP:
7727 case NL80211_IFTYPE_AP_VLAN:
7728 case NL80211_IFTYPE_MESH_POINT:
7729 case NL80211_IFTYPE_P2P_GO:
7730 break;
7731 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007732 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007733 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007734
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007735 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007736 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007737 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007738 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007739
7740 /*
7741 * We should wait on the channel for at least a minimum amount
7742 * of time (10ms) but no longer than the driver supports.
7743 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007744 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7745 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007746 return -EINVAL;
7747
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007748 }
7749
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007750 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007751
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007752 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007753 return -EINVAL;
7754
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007755 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307756
Antonio Quartulliea141b752013-06-11 14:20:03 +02007757 /* get the channel if any has been specified, otherwise pass NULL to
7758 * the driver. The latter will use the current one
7759 */
7760 chandef.chan = NULL;
7761 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7762 err = nl80211_parse_chandef(rdev, info, &chandef);
7763 if (err)
7764 return err;
7765 }
7766
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007767 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007768 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007769
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007770 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007771 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7772 if (!msg)
7773 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007774
Eric W. Biederman15e47302012-09-07 20:12:54 +00007775 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007776 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007777 if (!hdr) {
7778 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007779 goto free_msg;
7780 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007781 }
Johannes Berge247bd902011-11-04 11:18:21 +01007782
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007783 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7784 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7785 params.chan = chandef.chan;
7786 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007787 if (err)
7788 goto free_msg;
7789
Johannes Berge247bd902011-11-04 11:18:21 +01007790 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007791 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7792 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007793
Johannes Berge247bd902011-11-04 11:18:21 +01007794 genlmsg_end(msg, hdr);
7795 return genlmsg_reply(msg, info);
7796 }
7797
7798 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007799
7800 nla_put_failure:
7801 err = -ENOBUFS;
7802 free_msg:
7803 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007804 return err;
7805}
7806
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007807static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7808{
7809 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007810 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007811 u64 cookie;
7812
7813 if (!info->attrs[NL80211_ATTR_COOKIE])
7814 return -EINVAL;
7815
7816 if (!rdev->ops->mgmt_tx_cancel_wait)
7817 return -EOPNOTSUPP;
7818
Johannes Berg71bbc992012-06-15 15:30:18 +02007819 switch (wdev->iftype) {
7820 case NL80211_IFTYPE_STATION:
7821 case NL80211_IFTYPE_ADHOC:
7822 case NL80211_IFTYPE_P2P_CLIENT:
7823 case NL80211_IFTYPE_AP:
7824 case NL80211_IFTYPE_AP_VLAN:
7825 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007826 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007827 break;
7828 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007829 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007830 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007831
7832 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7833
Hila Gonene35e4d22012-06-27 17:19:42 +03007834 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007835}
7836
Kalle Valoffb9eb32010-02-17 17:58:10 +02007837static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7838{
Johannes Berg4c476992010-10-04 21:36:35 +02007839 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007840 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007841 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007842 u8 ps_state;
7843 bool state;
7844 int err;
7845
Johannes Berg4c476992010-10-04 21:36:35 +02007846 if (!info->attrs[NL80211_ATTR_PS_STATE])
7847 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007848
7849 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7850
Johannes Berg4c476992010-10-04 21:36:35 +02007851 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7852 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007853
7854 wdev = dev->ieee80211_ptr;
7855
Johannes Berg4c476992010-10-04 21:36:35 +02007856 if (!rdev->ops->set_power_mgmt)
7857 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007858
7859 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7860
7861 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007862 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007863
Hila Gonene35e4d22012-06-27 17:19:42 +03007864 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007865 if (!err)
7866 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007867 return err;
7868}
7869
7870static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7871{
Johannes Berg4c476992010-10-04 21:36:35 +02007872 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007873 enum nl80211_ps_state ps_state;
7874 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007875 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007876 struct sk_buff *msg;
7877 void *hdr;
7878 int err;
7879
Kalle Valoffb9eb32010-02-17 17:58:10 +02007880 wdev = dev->ieee80211_ptr;
7881
Johannes Berg4c476992010-10-04 21:36:35 +02007882 if (!rdev->ops->set_power_mgmt)
7883 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007884
7885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007886 if (!msg)
7887 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007888
Eric W. Biederman15e47302012-09-07 20:12:54 +00007889 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007890 NL80211_CMD_GET_POWER_SAVE);
7891 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007892 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007893 goto free_msg;
7894 }
7895
7896 if (wdev->ps)
7897 ps_state = NL80211_PS_ENABLED;
7898 else
7899 ps_state = NL80211_PS_DISABLED;
7900
David S. Miller9360ffd2012-03-29 04:41:26 -04007901 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7902 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007903
7904 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007905 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007906
Johannes Berg4c476992010-10-04 21:36:35 +02007907 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007908 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007909 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007910 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007911 return err;
7912}
7913
Johannes Berg94e860f2014-01-20 23:58:15 +01007914static const struct nla_policy
7915nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007916 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7917 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7918 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007919 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7920 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7921 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007922};
7923
Thomas Pedersen84f10702012-07-12 16:17:33 -07007924static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007925 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007926{
7927 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007928 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007929 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007930
Johannes Bergd9d8b012012-11-26 12:51:52 +01007931 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007932 return -EINVAL;
7933
Thomas Pedersen84f10702012-07-12 16:17:33 -07007934 if (!rdev->ops->set_cqm_txe_config)
7935 return -EOPNOTSUPP;
7936
7937 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7938 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7939 return -EOPNOTSUPP;
7940
Hila Gonene35e4d22012-06-27 17:19:42 +03007941 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007942}
7943
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007944static int nl80211_set_cqm_rssi(struct genl_info *info,
7945 s32 threshold, u32 hysteresis)
7946{
Johannes Berg4c476992010-10-04 21:36:35 +02007947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007948 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007949 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007950
7951 if (threshold > 0)
7952 return -EINVAL;
7953
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007954 /* disabling - hysteresis should also be zero then */
7955 if (threshold == 0)
7956 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007957
Johannes Berg4c476992010-10-04 21:36:35 +02007958 if (!rdev->ops->set_cqm_rssi_config)
7959 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007960
Johannes Berg074ac8d2010-09-16 14:58:22 +02007961 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007962 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7963 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007964
Hila Gonene35e4d22012-06-27 17:19:42 +03007965 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007966}
7967
7968static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7969{
7970 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7971 struct nlattr *cqm;
7972 int err;
7973
7974 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007975 if (!cqm)
7976 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007977
7978 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7979 nl80211_attr_cqm_policy);
7980 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007981 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007982
7983 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7984 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007985 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7986 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007987
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007988 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7989 }
7990
7991 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7992 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7993 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7994 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7995 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7996 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7997
7998 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7999 }
8000
8001 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008002}
8003
Johannes Berg29cbe682010-12-03 09:20:44 +01008004static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8005{
8006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8007 struct net_device *dev = info->user_ptr[1];
8008 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008009 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008010 int err;
8011
8012 /* start with default */
8013 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008014 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008015
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008016 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008017 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008018 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008019 if (err)
8020 return err;
8021 }
8022
8023 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8024 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8025 return -EINVAL;
8026
Javier Cardonac80d5452010-12-16 17:37:49 -08008027 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8028 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8029
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008030 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8031 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8032 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8033 return -EINVAL;
8034
Marco Porsch9bdbf042013-01-07 16:04:51 +01008035 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8036 setup.beacon_interval =
8037 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8038 if (setup.beacon_interval < 10 ||
8039 setup.beacon_interval > 10000)
8040 return -EINVAL;
8041 }
8042
8043 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8044 setup.dtim_period =
8045 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8046 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8047 return -EINVAL;
8048 }
8049
Javier Cardonac80d5452010-12-16 17:37:49 -08008050 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8051 /* parse additional setup parameters if given */
8052 err = nl80211_parse_mesh_setup(info, &setup);
8053 if (err)
8054 return err;
8055 }
8056
Thomas Pedersend37bb182013-03-04 13:06:13 -08008057 if (setup.user_mpm)
8058 cfg.auto_open_plinks = false;
8059
Johannes Bergcc1d2802012-05-16 23:50:20 +02008060 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008061 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8062 if (err)
8063 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008064 } else {
8065 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008066 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008067 }
8068
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008069 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8070 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8071 int n_rates =
8072 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8073 struct ieee80211_supported_band *sband;
8074
8075 if (!setup.chandef.chan)
8076 return -EINVAL;
8077
8078 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8079
8080 err = ieee80211_get_ratemask(sband, rates, n_rates,
8081 &setup.basic_rates);
8082 if (err)
8083 return err;
8084 }
8085
Javier Cardonac80d5452010-12-16 17:37:49 -08008086 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008087}
8088
8089static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8090{
8091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8092 struct net_device *dev = info->user_ptr[1];
8093
8094 return cfg80211_leave_mesh(rdev, dev);
8095}
8096
Johannes Bergdfb89c52012-06-27 09:23:48 +02008097#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008098static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8099 struct cfg80211_registered_device *rdev)
8100{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008101 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008102 struct nlattr *nl_pats, *nl_pat;
8103 int i, pat_len;
8104
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008105 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008106 return 0;
8107
8108 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8109 if (!nl_pats)
8110 return -ENOBUFS;
8111
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008112 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008113 nl_pat = nla_nest_start(msg, i + 1);
8114 if (!nl_pat)
8115 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008116 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008117 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008118 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008119 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8120 wowlan->patterns[i].pattern) ||
8121 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008122 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008123 return -ENOBUFS;
8124 nla_nest_end(msg, nl_pat);
8125 }
8126 nla_nest_end(msg, nl_pats);
8127
8128 return 0;
8129}
8130
Johannes Berg2a0e0472013-01-23 22:57:40 +01008131static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8132 struct cfg80211_wowlan_tcp *tcp)
8133{
8134 struct nlattr *nl_tcp;
8135
8136 if (!tcp)
8137 return 0;
8138
8139 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8140 if (!nl_tcp)
8141 return -ENOBUFS;
8142
8143 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8144 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8145 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8146 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8147 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8148 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8149 tcp->payload_len, tcp->payload) ||
8150 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8151 tcp->data_interval) ||
8152 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8153 tcp->wake_len, tcp->wake_data) ||
8154 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8155 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8156 return -ENOBUFS;
8157
8158 if (tcp->payload_seq.len &&
8159 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8160 sizeof(tcp->payload_seq), &tcp->payload_seq))
8161 return -ENOBUFS;
8162
8163 if (tcp->payload_tok.len &&
8164 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8165 sizeof(tcp->payload_tok) + tcp->tokens_size,
8166 &tcp->payload_tok))
8167 return -ENOBUFS;
8168
Johannes Berge248ad32013-05-16 10:24:28 +02008169 nla_nest_end(msg, nl_tcp);
8170
Johannes Berg2a0e0472013-01-23 22:57:40 +01008171 return 0;
8172}
8173
Johannes Bergff1b6e62011-05-04 15:37:28 +02008174static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8175{
8176 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8177 struct sk_buff *msg;
8178 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008179 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008180
Johannes Berg964dc9e2013-06-03 17:25:34 +02008181 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008182 return -EOPNOTSUPP;
8183
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008184 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008185 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008186 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8187 rdev->wiphy.wowlan_config->tcp->payload_len +
8188 rdev->wiphy.wowlan_config->tcp->wake_len +
8189 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008190 }
8191
8192 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008193 if (!msg)
8194 return -ENOMEM;
8195
Eric W. Biederman15e47302012-09-07 20:12:54 +00008196 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008197 NL80211_CMD_GET_WOWLAN);
8198 if (!hdr)
8199 goto nla_put_failure;
8200
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008201 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008202 struct nlattr *nl_wowlan;
8203
8204 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8205 if (!nl_wowlan)
8206 goto nla_put_failure;
8207
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008208 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008209 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008210 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008211 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008212 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008214 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008216 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008218 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008220 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008221 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8222 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008223
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008224 if (nl80211_send_wowlan_patterns(msg, rdev))
8225 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008226
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008227 if (nl80211_send_wowlan_tcp(msg,
8228 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008229 goto nla_put_failure;
8230
Johannes Bergff1b6e62011-05-04 15:37:28 +02008231 nla_nest_end(msg, nl_wowlan);
8232 }
8233
8234 genlmsg_end(msg, hdr);
8235 return genlmsg_reply(msg, info);
8236
8237nla_put_failure:
8238 nlmsg_free(msg);
8239 return -ENOBUFS;
8240}
8241
Johannes Berg2a0e0472013-01-23 22:57:40 +01008242static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8243 struct nlattr *attr,
8244 struct cfg80211_wowlan *trig)
8245{
8246 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8247 struct cfg80211_wowlan_tcp *cfg;
8248 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8249 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8250 u32 size;
8251 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8252 int err, port;
8253
Johannes Berg964dc9e2013-06-03 17:25:34 +02008254 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008255 return -EINVAL;
8256
8257 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8258 nla_data(attr), nla_len(attr),
8259 nl80211_wowlan_tcp_policy);
8260 if (err)
8261 return err;
8262
8263 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8264 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8265 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8266 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8267 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8268 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8269 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8270 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8271 return -EINVAL;
8272
8273 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008274 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008275 return -EINVAL;
8276
8277 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008278 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008279 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008280 return -EINVAL;
8281
8282 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008283 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008284 return -EINVAL;
8285
8286 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8287 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8288 return -EINVAL;
8289
8290 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8291 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8292
8293 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8294 tokens_size = tokln - sizeof(*tok);
8295
8296 if (!tok->len || tokens_size % tok->len)
8297 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008298 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008299 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008300 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008301 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008302 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008303 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008304 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008305 return -EINVAL;
8306 if (tok->offset + tok->len > data_size)
8307 return -EINVAL;
8308 }
8309
8310 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8311 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008312 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008313 return -EINVAL;
8314 if (seq->len == 0 || seq->len > 4)
8315 return -EINVAL;
8316 if (seq->len + seq->offset > data_size)
8317 return -EINVAL;
8318 }
8319
8320 size = sizeof(*cfg);
8321 size += data_size;
8322 size += wake_size + wake_mask_size;
8323 size += tokens_size;
8324
8325 cfg = kzalloc(size, GFP_KERNEL);
8326 if (!cfg)
8327 return -ENOMEM;
8328 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8329 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8330 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8331 ETH_ALEN);
8332 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8333 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8334 else
8335 port = 0;
8336#ifdef CONFIG_INET
8337 /* allocate a socket and port for it and use it */
8338 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8339 IPPROTO_TCP, &cfg->sock, 1);
8340 if (err) {
8341 kfree(cfg);
8342 return err;
8343 }
8344 if (inet_csk_get_port(cfg->sock->sk, port)) {
8345 sock_release(cfg->sock);
8346 kfree(cfg);
8347 return -EADDRINUSE;
8348 }
8349 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8350#else
8351 if (!port) {
8352 kfree(cfg);
8353 return -EINVAL;
8354 }
8355 cfg->src_port = port;
8356#endif
8357
8358 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8359 cfg->payload_len = data_size;
8360 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8361 memcpy((void *)cfg->payload,
8362 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8363 data_size);
8364 if (seq)
8365 cfg->payload_seq = *seq;
8366 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8367 cfg->wake_len = wake_size;
8368 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8369 memcpy((void *)cfg->wake_data,
8370 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8371 wake_size);
8372 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8373 data_size + wake_size;
8374 memcpy((void *)cfg->wake_mask,
8375 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8376 wake_mask_size);
8377 if (tok) {
8378 cfg->tokens_size = tokens_size;
8379 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8380 }
8381
8382 trig->tcp = cfg;
8383
8384 return 0;
8385}
8386
Johannes Bergff1b6e62011-05-04 15:37:28 +02008387static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8388{
8389 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8390 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008391 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008392 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008393 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008394 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008395 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008396
Johannes Berg964dc9e2013-06-03 17:25:34 +02008397 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008398 return -EOPNOTSUPP;
8399
Johannes Bergae33bd82012-07-12 16:25:02 +02008400 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8401 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008402 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008403 goto set_wakeup;
8404 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008405
8406 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8407 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8408 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8409 nl80211_wowlan_policy);
8410 if (err)
8411 return err;
8412
8413 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8414 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8415 return -EINVAL;
8416 new_triggers.any = true;
8417 }
8418
8419 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8420 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8421 return -EINVAL;
8422 new_triggers.disconnect = true;
8423 }
8424
8425 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8426 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8427 return -EINVAL;
8428 new_triggers.magic_pkt = true;
8429 }
8430
Johannes Berg77dbbb12011-07-13 10:48:55 +02008431 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8432 return -EINVAL;
8433
8434 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8435 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8436 return -EINVAL;
8437 new_triggers.gtk_rekey_failure = true;
8438 }
8439
8440 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8441 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8442 return -EINVAL;
8443 new_triggers.eap_identity_req = true;
8444 }
8445
8446 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8447 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8448 return -EINVAL;
8449 new_triggers.four_way_handshake = true;
8450 }
8451
8452 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8453 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8454 return -EINVAL;
8455 new_triggers.rfkill_release = true;
8456 }
8457
Johannes Bergff1b6e62011-05-04 15:37:28 +02008458 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8459 struct nlattr *pat;
8460 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008461 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008462 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008463
8464 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8465 rem)
8466 n_patterns++;
8467 if (n_patterns > wowlan->n_patterns)
8468 return -EINVAL;
8469
8470 new_triggers.patterns = kcalloc(n_patterns,
8471 sizeof(new_triggers.patterns[0]),
8472 GFP_KERNEL);
8473 if (!new_triggers.patterns)
8474 return -ENOMEM;
8475
8476 new_triggers.n_patterns = n_patterns;
8477 i = 0;
8478
8479 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8480 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008481 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8482 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008483 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008484 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8485 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008486 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008487 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008488 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008489 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008490 goto error;
8491 if (pat_len > wowlan->pattern_max_len ||
8492 pat_len < wowlan->pattern_min_len)
8493 goto error;
8494
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008495 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008496 pkt_offset = 0;
8497 else
8498 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008499 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008500 if (pkt_offset > wowlan->max_pkt_offset)
8501 goto error;
8502 new_triggers.patterns[i].pkt_offset = pkt_offset;
8503
Johannes Bergff1b6e62011-05-04 15:37:28 +02008504 new_triggers.patterns[i].mask =
8505 kmalloc(mask_len + pat_len, GFP_KERNEL);
8506 if (!new_triggers.patterns[i].mask) {
8507 err = -ENOMEM;
8508 goto error;
8509 }
8510 new_triggers.patterns[i].pattern =
8511 new_triggers.patterns[i].mask + mask_len;
8512 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008513 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008514 mask_len);
8515 new_triggers.patterns[i].pattern_len = pat_len;
8516 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008517 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008518 pat_len);
8519 i++;
8520 }
8521 }
8522
Johannes Berg2a0e0472013-01-23 22:57:40 +01008523 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8524 err = nl80211_parse_wowlan_tcp(
8525 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8526 &new_triggers);
8527 if (err)
8528 goto error;
8529 }
8530
Johannes Bergae33bd82012-07-12 16:25:02 +02008531 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8532 if (!ntrig) {
8533 err = -ENOMEM;
8534 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008535 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008536 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008537 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008538
Johannes Bergae33bd82012-07-12 16:25:02 +02008539 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008540 if (rdev->ops->set_wakeup &&
8541 prev_enabled != !!rdev->wiphy.wowlan_config)
8542 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008543
Johannes Bergff1b6e62011-05-04 15:37:28 +02008544 return 0;
8545 error:
8546 for (i = 0; i < new_triggers.n_patterns; i++)
8547 kfree(new_triggers.patterns[i].mask);
8548 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008549 if (new_triggers.tcp && new_triggers.tcp->sock)
8550 sock_release(new_triggers.tcp->sock);
8551 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008552 return err;
8553}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008554#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008555
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008556static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8557 struct cfg80211_registered_device *rdev)
8558{
8559 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8560 int i, j, pat_len;
8561 struct cfg80211_coalesce_rules *rule;
8562
8563 if (!rdev->coalesce->n_rules)
8564 return 0;
8565
8566 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8567 if (!nl_rules)
8568 return -ENOBUFS;
8569
8570 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8571 nl_rule = nla_nest_start(msg, i + 1);
8572 if (!nl_rule)
8573 return -ENOBUFS;
8574
8575 rule = &rdev->coalesce->rules[i];
8576 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8577 rule->delay))
8578 return -ENOBUFS;
8579
8580 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8581 rule->condition))
8582 return -ENOBUFS;
8583
8584 nl_pats = nla_nest_start(msg,
8585 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8586 if (!nl_pats)
8587 return -ENOBUFS;
8588
8589 for (j = 0; j < rule->n_patterns; j++) {
8590 nl_pat = nla_nest_start(msg, j + 1);
8591 if (!nl_pat)
8592 return -ENOBUFS;
8593 pat_len = rule->patterns[j].pattern_len;
8594 if (nla_put(msg, NL80211_PKTPAT_MASK,
8595 DIV_ROUND_UP(pat_len, 8),
8596 rule->patterns[j].mask) ||
8597 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8598 rule->patterns[j].pattern) ||
8599 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8600 rule->patterns[j].pkt_offset))
8601 return -ENOBUFS;
8602 nla_nest_end(msg, nl_pat);
8603 }
8604 nla_nest_end(msg, nl_pats);
8605 nla_nest_end(msg, nl_rule);
8606 }
8607 nla_nest_end(msg, nl_rules);
8608
8609 return 0;
8610}
8611
8612static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8613{
8614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8615 struct sk_buff *msg;
8616 void *hdr;
8617
8618 if (!rdev->wiphy.coalesce)
8619 return -EOPNOTSUPP;
8620
8621 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8622 if (!msg)
8623 return -ENOMEM;
8624
8625 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8626 NL80211_CMD_GET_COALESCE);
8627 if (!hdr)
8628 goto nla_put_failure;
8629
8630 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8631 goto nla_put_failure;
8632
8633 genlmsg_end(msg, hdr);
8634 return genlmsg_reply(msg, info);
8635
8636nla_put_failure:
8637 nlmsg_free(msg);
8638 return -ENOBUFS;
8639}
8640
8641void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8642{
8643 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8644 int i, j;
8645 struct cfg80211_coalesce_rules *rule;
8646
8647 if (!coalesce)
8648 return;
8649
8650 for (i = 0; i < coalesce->n_rules; i++) {
8651 rule = &coalesce->rules[i];
8652 for (j = 0; j < rule->n_patterns; j++)
8653 kfree(rule->patterns[j].mask);
8654 kfree(rule->patterns);
8655 }
8656 kfree(coalesce->rules);
8657 kfree(coalesce);
8658 rdev->coalesce = NULL;
8659}
8660
8661static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8662 struct nlattr *rule,
8663 struct cfg80211_coalesce_rules *new_rule)
8664{
8665 int err, i;
8666 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8667 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8668 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8669 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8670
8671 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8672 nla_len(rule), nl80211_coalesce_policy);
8673 if (err)
8674 return err;
8675
8676 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8677 new_rule->delay =
8678 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8679 if (new_rule->delay > coalesce->max_delay)
8680 return -EINVAL;
8681
8682 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8683 new_rule->condition =
8684 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8685 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8686 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8687 return -EINVAL;
8688
8689 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8690 return -EINVAL;
8691
8692 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8693 rem)
8694 n_patterns++;
8695 if (n_patterns > coalesce->n_patterns)
8696 return -EINVAL;
8697
8698 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8699 GFP_KERNEL);
8700 if (!new_rule->patterns)
8701 return -ENOMEM;
8702
8703 new_rule->n_patterns = n_patterns;
8704 i = 0;
8705
8706 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8707 rem) {
8708 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8709 nla_len(pat), NULL);
8710 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8711 !pat_tb[NL80211_PKTPAT_PATTERN])
8712 return -EINVAL;
8713 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8714 mask_len = DIV_ROUND_UP(pat_len, 8);
8715 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8716 return -EINVAL;
8717 if (pat_len > coalesce->pattern_max_len ||
8718 pat_len < coalesce->pattern_min_len)
8719 return -EINVAL;
8720
8721 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8722 pkt_offset = 0;
8723 else
8724 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8725 if (pkt_offset > coalesce->max_pkt_offset)
8726 return -EINVAL;
8727 new_rule->patterns[i].pkt_offset = pkt_offset;
8728
8729 new_rule->patterns[i].mask =
8730 kmalloc(mask_len + pat_len, GFP_KERNEL);
8731 if (!new_rule->patterns[i].mask)
8732 return -ENOMEM;
8733 new_rule->patterns[i].pattern =
8734 new_rule->patterns[i].mask + mask_len;
8735 memcpy(new_rule->patterns[i].mask,
8736 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8737 new_rule->patterns[i].pattern_len = pat_len;
8738 memcpy(new_rule->patterns[i].pattern,
8739 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8740 i++;
8741 }
8742
8743 return 0;
8744}
8745
8746static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8747{
8748 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8749 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8750 struct cfg80211_coalesce new_coalesce = {};
8751 struct cfg80211_coalesce *n_coalesce;
8752 int err, rem_rule, n_rules = 0, i, j;
8753 struct nlattr *rule;
8754 struct cfg80211_coalesce_rules *tmp_rule;
8755
8756 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8757 return -EOPNOTSUPP;
8758
8759 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8760 cfg80211_rdev_free_coalesce(rdev);
8761 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8762 return 0;
8763 }
8764
8765 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8766 rem_rule)
8767 n_rules++;
8768 if (n_rules > coalesce->n_rules)
8769 return -EINVAL;
8770
8771 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8772 GFP_KERNEL);
8773 if (!new_coalesce.rules)
8774 return -ENOMEM;
8775
8776 new_coalesce.n_rules = n_rules;
8777 i = 0;
8778
8779 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8780 rem_rule) {
8781 err = nl80211_parse_coalesce_rule(rdev, rule,
8782 &new_coalesce.rules[i]);
8783 if (err)
8784 goto error;
8785
8786 i++;
8787 }
8788
8789 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8790 if (err)
8791 goto error;
8792
8793 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8794 if (!n_coalesce) {
8795 err = -ENOMEM;
8796 goto error;
8797 }
8798 cfg80211_rdev_free_coalesce(rdev);
8799 rdev->coalesce = n_coalesce;
8800
8801 return 0;
8802error:
8803 for (i = 0; i < new_coalesce.n_rules; i++) {
8804 tmp_rule = &new_coalesce.rules[i];
8805 for (j = 0; j < tmp_rule->n_patterns; j++)
8806 kfree(tmp_rule->patterns[j].mask);
8807 kfree(tmp_rule->patterns);
8808 }
8809 kfree(new_coalesce.rules);
8810
8811 return err;
8812}
8813
Johannes Berge5497d72011-07-05 16:35:40 +02008814static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8815{
8816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8817 struct net_device *dev = info->user_ptr[1];
8818 struct wireless_dev *wdev = dev->ieee80211_ptr;
8819 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8820 struct cfg80211_gtk_rekey_data rekey_data;
8821 int err;
8822
8823 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8824 return -EINVAL;
8825
8826 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8827 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8828 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8829 nl80211_rekey_policy);
8830 if (err)
8831 return err;
8832
8833 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8834 return -ERANGE;
8835 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8836 return -ERANGE;
8837 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8838 return -ERANGE;
8839
8840 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8841 NL80211_KEK_LEN);
8842 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8843 NL80211_KCK_LEN);
8844 memcpy(rekey_data.replay_ctr,
8845 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8846 NL80211_REPLAY_CTR_LEN);
8847
8848 wdev_lock(wdev);
8849 if (!wdev->current_bss) {
8850 err = -ENOTCONN;
8851 goto out;
8852 }
8853
8854 if (!rdev->ops->set_rekey_data) {
8855 err = -EOPNOTSUPP;
8856 goto out;
8857 }
8858
Hila Gonene35e4d22012-06-27 17:19:42 +03008859 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008860 out:
8861 wdev_unlock(wdev);
8862 return err;
8863}
8864
Johannes Berg28946da2011-11-04 11:18:12 +01008865static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8866 struct genl_info *info)
8867{
8868 struct net_device *dev = info->user_ptr[1];
8869 struct wireless_dev *wdev = dev->ieee80211_ptr;
8870
8871 if (wdev->iftype != NL80211_IFTYPE_AP &&
8872 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8873 return -EINVAL;
8874
Eric W. Biederman15e47302012-09-07 20:12:54 +00008875 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008876 return -EBUSY;
8877
Eric W. Biederman15e47302012-09-07 20:12:54 +00008878 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008879 return 0;
8880}
8881
Johannes Berg7f6cf312011-11-04 11:18:15 +01008882static int nl80211_probe_client(struct sk_buff *skb,
8883 struct genl_info *info)
8884{
8885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8886 struct net_device *dev = info->user_ptr[1];
8887 struct wireless_dev *wdev = dev->ieee80211_ptr;
8888 struct sk_buff *msg;
8889 void *hdr;
8890 const u8 *addr;
8891 u64 cookie;
8892 int err;
8893
8894 if (wdev->iftype != NL80211_IFTYPE_AP &&
8895 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8896 return -EOPNOTSUPP;
8897
8898 if (!info->attrs[NL80211_ATTR_MAC])
8899 return -EINVAL;
8900
8901 if (!rdev->ops->probe_client)
8902 return -EOPNOTSUPP;
8903
8904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8905 if (!msg)
8906 return -ENOMEM;
8907
Eric W. Biederman15e47302012-09-07 20:12:54 +00008908 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008909 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008910 if (!hdr) {
8911 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008912 goto free_msg;
8913 }
8914
8915 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8916
Hila Gonene35e4d22012-06-27 17:19:42 +03008917 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008918 if (err)
8919 goto free_msg;
8920
David S. Miller9360ffd2012-03-29 04:41:26 -04008921 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8922 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008923
8924 genlmsg_end(msg, hdr);
8925
8926 return genlmsg_reply(msg, info);
8927
8928 nla_put_failure:
8929 err = -ENOBUFS;
8930 free_msg:
8931 nlmsg_free(msg);
8932 return err;
8933}
8934
Johannes Berg5e7602302011-11-04 11:18:17 +01008935static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8936{
8937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008938 struct cfg80211_beacon_registration *reg, *nreg;
8939 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008940
8941 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8942 return -EOPNOTSUPP;
8943
Ben Greear37c73b52012-10-26 14:49:25 -07008944 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8945 if (!nreg)
8946 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008947
Ben Greear37c73b52012-10-26 14:49:25 -07008948 /* First, check if already registered. */
8949 spin_lock_bh(&rdev->beacon_registrations_lock);
8950 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8951 if (reg->nlportid == info->snd_portid) {
8952 rv = -EALREADY;
8953 goto out_err;
8954 }
8955 }
8956 /* Add it to the list */
8957 nreg->nlportid = info->snd_portid;
8958 list_add(&nreg->list, &rdev->beacon_registrations);
8959
8960 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008961
8962 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008963out_err:
8964 spin_unlock_bh(&rdev->beacon_registrations_lock);
8965 kfree(nreg);
8966 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008967}
8968
Johannes Berg98104fde2012-06-16 00:19:54 +02008969static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8970{
8971 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8972 struct wireless_dev *wdev = info->user_ptr[1];
8973 int err;
8974
8975 if (!rdev->ops->start_p2p_device)
8976 return -EOPNOTSUPP;
8977
8978 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8979 return -EOPNOTSUPP;
8980
8981 if (wdev->p2p_started)
8982 return 0;
8983
Johannes Berg98104fde2012-06-16 00:19:54 +02008984 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008985 if (err)
8986 return err;
8987
Johannes Bergeeb126e2012-10-23 15:16:50 +02008988 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008989 if (err)
8990 return err;
8991
8992 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008993 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008994
8995 return 0;
8996}
8997
8998static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8999{
9000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9001 struct wireless_dev *wdev = info->user_ptr[1];
9002
9003 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9004 return -EOPNOTSUPP;
9005
9006 if (!rdev->ops->stop_p2p_device)
9007 return -EOPNOTSUPP;
9008
Johannes Bergf9f47522013-03-19 15:04:07 +01009009 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009010
9011 return 0;
9012}
9013
Johannes Berg3713b4e2013-02-14 16:19:38 +01009014static int nl80211_get_protocol_features(struct sk_buff *skb,
9015 struct genl_info *info)
9016{
9017 void *hdr;
9018 struct sk_buff *msg;
9019
9020 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9021 if (!msg)
9022 return -ENOMEM;
9023
9024 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9025 NL80211_CMD_GET_PROTOCOL_FEATURES);
9026 if (!hdr)
9027 goto nla_put_failure;
9028
9029 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9030 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9031 goto nla_put_failure;
9032
9033 genlmsg_end(msg, hdr);
9034 return genlmsg_reply(msg, info);
9035
9036 nla_put_failure:
9037 kfree_skb(msg);
9038 return -ENOBUFS;
9039}
9040
Jouni Malinen355199e2013-02-27 17:14:27 +02009041static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9042{
9043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9044 struct cfg80211_update_ft_ies_params ft_params;
9045 struct net_device *dev = info->user_ptr[1];
9046
9047 if (!rdev->ops->update_ft_ies)
9048 return -EOPNOTSUPP;
9049
9050 if (!info->attrs[NL80211_ATTR_MDID] ||
9051 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9052 return -EINVAL;
9053
9054 memset(&ft_params, 0, sizeof(ft_params));
9055 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9056 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9057 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9058
9059 return rdev_update_ft_ies(rdev, dev, &ft_params);
9060}
9061
Arend van Spriel5de17982013-04-18 15:49:00 +02009062static int nl80211_crit_protocol_start(struct sk_buff *skb,
9063 struct genl_info *info)
9064{
9065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9066 struct wireless_dev *wdev = info->user_ptr[1];
9067 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9068 u16 duration;
9069 int ret;
9070
9071 if (!rdev->ops->crit_proto_start)
9072 return -EOPNOTSUPP;
9073
9074 if (WARN_ON(!rdev->ops->crit_proto_stop))
9075 return -EINVAL;
9076
9077 if (rdev->crit_proto_nlportid)
9078 return -EBUSY;
9079
9080 /* determine protocol if provided */
9081 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9082 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9083
9084 if (proto >= NUM_NL80211_CRIT_PROTO)
9085 return -EINVAL;
9086
9087 /* timeout must be provided */
9088 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9089 return -EINVAL;
9090
9091 duration =
9092 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9093
9094 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9095 return -ERANGE;
9096
9097 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9098 if (!ret)
9099 rdev->crit_proto_nlportid = info->snd_portid;
9100
9101 return ret;
9102}
9103
9104static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9105 struct genl_info *info)
9106{
9107 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9108 struct wireless_dev *wdev = info->user_ptr[1];
9109
9110 if (!rdev->ops->crit_proto_stop)
9111 return -EOPNOTSUPP;
9112
9113 if (rdev->crit_proto_nlportid) {
9114 rdev->crit_proto_nlportid = 0;
9115 rdev_crit_proto_stop(rdev, wdev);
9116 }
9117 return 0;
9118}
9119
Johannes Bergad7e7182013-11-13 13:37:47 +01009120static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9121{
9122 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9123 struct wireless_dev *wdev =
9124 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9125 int i, err;
9126 u32 vid, subcmd;
9127
9128 if (!rdev->wiphy.vendor_commands)
9129 return -EOPNOTSUPP;
9130
9131 if (IS_ERR(wdev)) {
9132 err = PTR_ERR(wdev);
9133 if (err != -EINVAL)
9134 return err;
9135 wdev = NULL;
9136 } else if (wdev->wiphy != &rdev->wiphy) {
9137 return -EINVAL;
9138 }
9139
9140 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9141 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9142 return -EINVAL;
9143
9144 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9145 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9146 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9147 const struct wiphy_vendor_command *vcmd;
9148 void *data = NULL;
9149 int len = 0;
9150
9151 vcmd = &rdev->wiphy.vendor_commands[i];
9152
9153 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9154 continue;
9155
9156 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9157 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9158 if (!wdev)
9159 return -EINVAL;
9160 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9161 !wdev->netdev)
9162 return -EINVAL;
9163
9164 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9165 if (wdev->netdev &&
9166 !netif_running(wdev->netdev))
9167 return -ENETDOWN;
9168 if (!wdev->netdev && !wdev->p2p_started)
9169 return -ENETDOWN;
9170 }
9171 } else {
9172 wdev = NULL;
9173 }
9174
9175 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9176 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9177 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9178 }
9179
9180 rdev->cur_cmd_info = info;
9181 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9182 data, len);
9183 rdev->cur_cmd_info = NULL;
9184 return err;
9185 }
9186
9187 return -EOPNOTSUPP;
9188}
9189
9190struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9191 enum nl80211_commands cmd,
9192 enum nl80211_attrs attr,
9193 int approxlen)
9194{
9195 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9196
9197 if (WARN_ON(!rdev->cur_cmd_info))
9198 return NULL;
9199
9200 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9201 rdev->cur_cmd_info->snd_portid,
9202 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009203 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009204}
9205EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9206
9207int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9208{
9209 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9210 void *hdr = ((void **)skb->cb)[1];
9211 struct nlattr *data = ((void **)skb->cb)[2];
9212
9213 if (WARN_ON(!rdev->cur_cmd_info)) {
9214 kfree_skb(skb);
9215 return -EINVAL;
9216 }
9217
9218 nla_nest_end(skb, data);
9219 genlmsg_end(skb, hdr);
9220 return genlmsg_reply(skb, rdev->cur_cmd_info);
9221}
9222EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9223
9224
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009225static int nl80211_set_qos_map(struct sk_buff *skb,
9226 struct genl_info *info)
9227{
9228 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9229 struct cfg80211_qos_map *qos_map = NULL;
9230 struct net_device *dev = info->user_ptr[1];
9231 u8 *pos, len, num_des, des_len, des;
9232 int ret;
9233
9234 if (!rdev->ops->set_qos_map)
9235 return -EOPNOTSUPP;
9236
9237 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9238 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9239 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9240
9241 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9242 len > IEEE80211_QOS_MAP_LEN_MAX)
9243 return -EINVAL;
9244
9245 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9246 if (!qos_map)
9247 return -ENOMEM;
9248
9249 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9250 if (num_des) {
9251 des_len = num_des *
9252 sizeof(struct cfg80211_dscp_exception);
9253 memcpy(qos_map->dscp_exception, pos, des_len);
9254 qos_map->num_des = num_des;
9255 for (des = 0; des < num_des; des++) {
9256 if (qos_map->dscp_exception[des].up > 7) {
9257 kfree(qos_map);
9258 return -EINVAL;
9259 }
9260 }
9261 pos += des_len;
9262 }
9263 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9264 }
9265
9266 wdev_lock(dev->ieee80211_ptr);
9267 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9268 if (!ret)
9269 ret = rdev_set_qos_map(rdev, dev, qos_map);
9270 wdev_unlock(dev->ieee80211_ptr);
9271
9272 kfree(qos_map);
9273 return ret;
9274}
9275
Johannes Berg4c476992010-10-04 21:36:35 +02009276#define NL80211_FLAG_NEED_WIPHY 0x01
9277#define NL80211_FLAG_NEED_NETDEV 0x02
9278#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009279#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9280#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9281 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009282#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009283/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009284#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9285 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009286
Johannes Bergf84f7712013-11-14 17:14:45 +01009287static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009288 struct genl_info *info)
9289{
9290 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009291 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009292 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009293 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9294
9295 if (rtnl)
9296 rtnl_lock();
9297
9298 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009299 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009300 if (IS_ERR(rdev)) {
9301 if (rtnl)
9302 rtnl_unlock();
9303 return PTR_ERR(rdev);
9304 }
9305 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009306 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9307 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009308 ASSERT_RTNL();
9309
Johannes Berg89a54e42012-06-15 14:33:17 +02009310 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9311 info->attrs);
9312 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009313 if (rtnl)
9314 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009315 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009316 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009317
Johannes Berg89a54e42012-06-15 14:33:17 +02009318 dev = wdev->netdev;
9319 rdev = wiphy_to_dev(wdev->wiphy);
9320
Johannes Berg1bf614e2012-06-15 15:23:36 +02009321 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9322 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009323 if (rtnl)
9324 rtnl_unlock();
9325 return -EINVAL;
9326 }
9327
9328 info->user_ptr[1] = dev;
9329 } else {
9330 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009331 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009332
Johannes Berg1bf614e2012-06-15 15:23:36 +02009333 if (dev) {
9334 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9335 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009336 if (rtnl)
9337 rtnl_unlock();
9338 return -ENETDOWN;
9339 }
9340
9341 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009342 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9343 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009344 if (rtnl)
9345 rtnl_unlock();
9346 return -ENETDOWN;
9347 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009348 }
9349
Johannes Berg4c476992010-10-04 21:36:35 +02009350 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009351 }
9352
9353 return 0;
9354}
9355
Johannes Bergf84f7712013-11-14 17:14:45 +01009356static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009357 struct genl_info *info)
9358{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009359 if (info->user_ptr[1]) {
9360 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9361 struct wireless_dev *wdev = info->user_ptr[1];
9362
9363 if (wdev->netdev)
9364 dev_put(wdev->netdev);
9365 } else {
9366 dev_put(info->user_ptr[1]);
9367 }
9368 }
Johannes Berg4c476992010-10-04 21:36:35 +02009369 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9370 rtnl_unlock();
9371}
9372
Johannes Berg4534de82013-11-14 17:14:46 +01009373static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009374 {
9375 .cmd = NL80211_CMD_GET_WIPHY,
9376 .doit = nl80211_get_wiphy,
9377 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009378 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009379 .policy = nl80211_policy,
9380 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009381 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9382 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009383 },
9384 {
9385 .cmd = NL80211_CMD_SET_WIPHY,
9386 .doit = nl80211_set_wiphy,
9387 .policy = nl80211_policy,
9388 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009389 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009390 },
9391 {
9392 .cmd = NL80211_CMD_GET_INTERFACE,
9393 .doit = nl80211_get_interface,
9394 .dumpit = nl80211_dump_interface,
9395 .policy = nl80211_policy,
9396 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009397 .internal_flags = NL80211_FLAG_NEED_WDEV |
9398 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009399 },
9400 {
9401 .cmd = NL80211_CMD_SET_INTERFACE,
9402 .doit = nl80211_set_interface,
9403 .policy = nl80211_policy,
9404 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009405 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9406 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009407 },
9408 {
9409 .cmd = NL80211_CMD_NEW_INTERFACE,
9410 .doit = nl80211_new_interface,
9411 .policy = nl80211_policy,
9412 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009413 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9414 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009415 },
9416 {
9417 .cmd = NL80211_CMD_DEL_INTERFACE,
9418 .doit = nl80211_del_interface,
9419 .policy = nl80211_policy,
9420 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009421 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009422 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009423 },
Johannes Berg41ade002007-12-19 02:03:29 +01009424 {
9425 .cmd = NL80211_CMD_GET_KEY,
9426 .doit = nl80211_get_key,
9427 .policy = nl80211_policy,
9428 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +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 },
9432 {
9433 .cmd = NL80211_CMD_SET_KEY,
9434 .doit = nl80211_set_key,
9435 .policy = nl80211_policy,
9436 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009437 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009438 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009439 },
9440 {
9441 .cmd = NL80211_CMD_NEW_KEY,
9442 .doit = nl80211_new_key,
9443 .policy = nl80211_policy,
9444 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009445 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009446 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009447 },
9448 {
9449 .cmd = NL80211_CMD_DEL_KEY,
9450 .doit = nl80211_del_key,
9451 .policy = nl80211_policy,
9452 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009453 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009454 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009455 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009456 {
9457 .cmd = NL80211_CMD_SET_BEACON,
9458 .policy = nl80211_policy,
9459 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009460 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009461 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009462 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009463 },
9464 {
Johannes Berg88600202012-02-13 15:17:18 +01009465 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009466 .policy = nl80211_policy,
9467 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009468 .doit = nl80211_start_ap,
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 Berged1b6cc2007-12-19 02:03:32 +01009471 },
9472 {
Johannes Berg88600202012-02-13 15:17:18 +01009473 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009474 .policy = nl80211_policy,
9475 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009476 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009478 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009479 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009480 {
9481 .cmd = NL80211_CMD_GET_STATION,
9482 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009483 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009484 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009485 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9486 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009487 },
9488 {
9489 .cmd = NL80211_CMD_SET_STATION,
9490 .doit = nl80211_set_station,
9491 .policy = nl80211_policy,
9492 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009493 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009494 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009495 },
9496 {
9497 .cmd = NL80211_CMD_NEW_STATION,
9498 .doit = nl80211_new_station,
9499 .policy = nl80211_policy,
9500 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009501 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009502 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009503 },
9504 {
9505 .cmd = NL80211_CMD_DEL_STATION,
9506 .doit = nl80211_del_station,
9507 .policy = nl80211_policy,
9508 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009509 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009510 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009511 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009512 {
9513 .cmd = NL80211_CMD_GET_MPATH,
9514 .doit = nl80211_get_mpath,
9515 .dumpit = nl80211_dump_mpath,
9516 .policy = nl80211_policy,
9517 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +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 },
9521 {
9522 .cmd = NL80211_CMD_SET_MPATH,
9523 .doit = nl80211_set_mpath,
9524 .policy = nl80211_policy,
9525 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009527 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009528 },
9529 {
9530 .cmd = NL80211_CMD_NEW_MPATH,
9531 .doit = nl80211_new_mpath,
9532 .policy = nl80211_policy,
9533 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009535 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009536 },
9537 {
9538 .cmd = NL80211_CMD_DEL_MPATH,
9539 .doit = nl80211_del_mpath,
9540 .policy = nl80211_policy,
9541 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009543 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009544 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009545 {
9546 .cmd = NL80211_CMD_SET_BSS,
9547 .doit = nl80211_set_bss,
9548 .policy = nl80211_policy,
9549 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009551 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009552 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009553 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009554 .cmd = NL80211_CMD_GET_REG,
9555 .doit = nl80211_get_reg,
9556 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009557 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009558 /* can be retrieved by unprivileged users */
9559 },
9560 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009561 .cmd = NL80211_CMD_SET_REG,
9562 .doit = nl80211_set_reg,
9563 .policy = nl80211_policy,
9564 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009565 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009566 },
9567 {
9568 .cmd = NL80211_CMD_REQ_SET_REG,
9569 .doit = nl80211_req_set_reg,
9570 .policy = nl80211_policy,
9571 .flags = GENL_ADMIN_PERM,
9572 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009573 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009574 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9575 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009576 .policy = nl80211_policy,
9577 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009578 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009579 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009580 },
9581 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009582 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9583 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009584 .policy = nl80211_policy,
9585 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009586 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009587 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009588 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009589 {
Johannes Berg2a519312009-02-10 21:25:55 +01009590 .cmd = NL80211_CMD_TRIGGER_SCAN,
9591 .doit = nl80211_trigger_scan,
9592 .policy = nl80211_policy,
9593 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009594 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009595 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009596 },
9597 {
9598 .cmd = NL80211_CMD_GET_SCAN,
9599 .policy = nl80211_policy,
9600 .dumpit = nl80211_dump_scan,
9601 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009602 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009603 .cmd = NL80211_CMD_START_SCHED_SCAN,
9604 .doit = nl80211_start_sched_scan,
9605 .policy = nl80211_policy,
9606 .flags = GENL_ADMIN_PERM,
9607 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9608 NL80211_FLAG_NEED_RTNL,
9609 },
9610 {
9611 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9612 .doit = nl80211_stop_sched_scan,
9613 .policy = nl80211_policy,
9614 .flags = GENL_ADMIN_PERM,
9615 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9616 NL80211_FLAG_NEED_RTNL,
9617 },
9618 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009619 .cmd = NL80211_CMD_AUTHENTICATE,
9620 .doit = nl80211_authenticate,
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 },
9626 {
9627 .cmd = NL80211_CMD_ASSOCIATE,
9628 .doit = nl80211_associate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009633 },
9634 {
9635 .cmd = NL80211_CMD_DEAUTHENTICATE,
9636 .doit = nl80211_deauthenticate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009641 },
9642 {
9643 .cmd = NL80211_CMD_DISASSOCIATE,
9644 .doit = nl80211_disassociate,
9645 .policy = nl80211_policy,
9646 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009648 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009649 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009650 {
9651 .cmd = NL80211_CMD_JOIN_IBSS,
9652 .doit = nl80211_join_ibss,
9653 .policy = nl80211_policy,
9654 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009655 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009656 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009657 },
9658 {
9659 .cmd = NL80211_CMD_LEAVE_IBSS,
9660 .doit = nl80211_leave_ibss,
9661 .policy = nl80211_policy,
9662 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009663 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009664 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009665 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009666#ifdef CONFIG_NL80211_TESTMODE
9667 {
9668 .cmd = NL80211_CMD_TESTMODE,
9669 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009670 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009671 .policy = nl80211_policy,
9672 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009673 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9674 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009675 },
9676#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009677 {
9678 .cmd = NL80211_CMD_CONNECT,
9679 .doit = nl80211_connect,
9680 .policy = nl80211_policy,
9681 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009682 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009683 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009684 },
9685 {
9686 .cmd = NL80211_CMD_DISCONNECT,
9687 .doit = nl80211_disconnect,
9688 .policy = nl80211_policy,
9689 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009690 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009691 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009692 },
Johannes Berg463d0182009-07-14 00:33:35 +02009693 {
9694 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9695 .doit = nl80211_wiphy_netns,
9696 .policy = nl80211_policy,
9697 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009698 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9699 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009700 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009701 {
9702 .cmd = NL80211_CMD_GET_SURVEY,
9703 .policy = nl80211_policy,
9704 .dumpit = nl80211_dump_survey,
9705 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009706 {
9707 .cmd = NL80211_CMD_SET_PMKSA,
9708 .doit = nl80211_setdel_pmksa,
9709 .policy = nl80211_policy,
9710 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009711 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009712 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009713 },
9714 {
9715 .cmd = NL80211_CMD_DEL_PMKSA,
9716 .doit = nl80211_setdel_pmksa,
9717 .policy = nl80211_policy,
9718 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009719 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009720 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009721 },
9722 {
9723 .cmd = NL80211_CMD_FLUSH_PMKSA,
9724 .doit = nl80211_flush_pmksa,
9725 .policy = nl80211_policy,
9726 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009727 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009728 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009729 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009730 {
9731 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9732 .doit = nl80211_remain_on_channel,
9733 .policy = nl80211_policy,
9734 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009735 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009736 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009737 },
9738 {
9739 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9740 .doit = nl80211_cancel_remain_on_channel,
9741 .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 Malinen9588bbd2009-12-23 13:15:41 +01009745 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009746 {
9747 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9748 .doit = nl80211_set_tx_bitrate_mask,
9749 .policy = nl80211_policy,
9750 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009751 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9752 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009753 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009754 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009755 .cmd = NL80211_CMD_REGISTER_FRAME,
9756 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009757 .policy = nl80211_policy,
9758 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009759 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009760 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009761 },
9762 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009763 .cmd = NL80211_CMD_FRAME,
9764 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009765 .policy = nl80211_policy,
9766 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009767 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009768 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009769 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009770 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009771 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9772 .doit = nl80211_tx_mgmt_cancel_wait,
9773 .policy = nl80211_policy,
9774 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009775 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009776 NL80211_FLAG_NEED_RTNL,
9777 },
9778 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009779 .cmd = NL80211_CMD_SET_POWER_SAVE,
9780 .doit = nl80211_set_power_save,
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,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009785 },
9786 {
9787 .cmd = NL80211_CMD_GET_POWER_SAVE,
9788 .doit = nl80211_get_power_save,
9789 .policy = nl80211_policy,
9790 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009791 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9792 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009793 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009794 {
9795 .cmd = NL80211_CMD_SET_CQM,
9796 .doit = nl80211_set_cqm,
9797 .policy = nl80211_policy,
9798 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009799 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9800 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009801 },
Johannes Bergf444de02010-05-05 15:25:02 +02009802 {
9803 .cmd = NL80211_CMD_SET_CHANNEL,
9804 .doit = nl80211_set_channel,
9805 .policy = nl80211_policy,
9806 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009807 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9808 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009809 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009810 {
9811 .cmd = NL80211_CMD_SET_WDS_PEER,
9812 .doit = nl80211_set_wds_peer,
9813 .policy = nl80211_policy,
9814 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009815 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9816 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009817 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009818 {
9819 .cmd = NL80211_CMD_JOIN_MESH,
9820 .doit = nl80211_join_mesh,
9821 .policy = nl80211_policy,
9822 .flags = GENL_ADMIN_PERM,
9823 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9824 NL80211_FLAG_NEED_RTNL,
9825 },
9826 {
9827 .cmd = NL80211_CMD_LEAVE_MESH,
9828 .doit = nl80211_leave_mesh,
9829 .policy = nl80211_policy,
9830 .flags = GENL_ADMIN_PERM,
9831 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9832 NL80211_FLAG_NEED_RTNL,
9833 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009834#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009835 {
9836 .cmd = NL80211_CMD_GET_WOWLAN,
9837 .doit = nl80211_get_wowlan,
9838 .policy = nl80211_policy,
9839 /* can be retrieved by unprivileged users */
9840 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9841 NL80211_FLAG_NEED_RTNL,
9842 },
9843 {
9844 .cmd = NL80211_CMD_SET_WOWLAN,
9845 .doit = nl80211_set_wowlan,
9846 .policy = nl80211_policy,
9847 .flags = GENL_ADMIN_PERM,
9848 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9849 NL80211_FLAG_NEED_RTNL,
9850 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009851#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009852 {
9853 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9854 .doit = nl80211_set_rekey_data,
9855 .policy = nl80211_policy,
9856 .flags = GENL_ADMIN_PERM,
9857 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9858 NL80211_FLAG_NEED_RTNL,
9859 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009860 {
9861 .cmd = NL80211_CMD_TDLS_MGMT,
9862 .doit = nl80211_tdls_mgmt,
9863 .policy = nl80211_policy,
9864 .flags = GENL_ADMIN_PERM,
9865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9866 NL80211_FLAG_NEED_RTNL,
9867 },
9868 {
9869 .cmd = NL80211_CMD_TDLS_OPER,
9870 .doit = nl80211_tdls_oper,
9871 .policy = nl80211_policy,
9872 .flags = GENL_ADMIN_PERM,
9873 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9874 NL80211_FLAG_NEED_RTNL,
9875 },
Johannes Berg28946da2011-11-04 11:18:12 +01009876 {
9877 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9878 .doit = nl80211_register_unexpected_frame,
9879 .policy = nl80211_policy,
9880 .flags = GENL_ADMIN_PERM,
9881 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9882 NL80211_FLAG_NEED_RTNL,
9883 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009884 {
9885 .cmd = NL80211_CMD_PROBE_CLIENT,
9886 .doit = nl80211_probe_client,
9887 .policy = nl80211_policy,
9888 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009889 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009890 NL80211_FLAG_NEED_RTNL,
9891 },
Johannes Berg5e7602302011-11-04 11:18:17 +01009892 {
9893 .cmd = NL80211_CMD_REGISTER_BEACONS,
9894 .doit = nl80211_register_beacons,
9895 .policy = nl80211_policy,
9896 .flags = GENL_ADMIN_PERM,
9897 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9898 NL80211_FLAG_NEED_RTNL,
9899 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009900 {
9901 .cmd = NL80211_CMD_SET_NOACK_MAP,
9902 .doit = nl80211_set_noack_map,
9903 .policy = nl80211_policy,
9904 .flags = GENL_ADMIN_PERM,
9905 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9906 NL80211_FLAG_NEED_RTNL,
9907 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009908 {
9909 .cmd = NL80211_CMD_START_P2P_DEVICE,
9910 .doit = nl80211_start_p2p_device,
9911 .policy = nl80211_policy,
9912 .flags = GENL_ADMIN_PERM,
9913 .internal_flags = NL80211_FLAG_NEED_WDEV |
9914 NL80211_FLAG_NEED_RTNL,
9915 },
9916 {
9917 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9918 .doit = nl80211_stop_p2p_device,
9919 .policy = nl80211_policy,
9920 .flags = GENL_ADMIN_PERM,
9921 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9922 NL80211_FLAG_NEED_RTNL,
9923 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009924 {
9925 .cmd = NL80211_CMD_SET_MCAST_RATE,
9926 .doit = nl80211_set_mcast_rate,
9927 .policy = nl80211_policy,
9928 .flags = GENL_ADMIN_PERM,
9929 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9930 NL80211_FLAG_NEED_RTNL,
9931 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309932 {
9933 .cmd = NL80211_CMD_SET_MAC_ACL,
9934 .doit = nl80211_set_mac_acl,
9935 .policy = nl80211_policy,
9936 .flags = GENL_ADMIN_PERM,
9937 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9938 NL80211_FLAG_NEED_RTNL,
9939 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009940 {
9941 .cmd = NL80211_CMD_RADAR_DETECT,
9942 .doit = nl80211_start_radar_detection,
9943 .policy = nl80211_policy,
9944 .flags = GENL_ADMIN_PERM,
9945 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9946 NL80211_FLAG_NEED_RTNL,
9947 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009948 {
9949 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9950 .doit = nl80211_get_protocol_features,
9951 .policy = nl80211_policy,
9952 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009953 {
9954 .cmd = NL80211_CMD_UPDATE_FT_IES,
9955 .doit = nl80211_update_ft_ies,
9956 .policy = nl80211_policy,
9957 .flags = GENL_ADMIN_PERM,
9958 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9959 NL80211_FLAG_NEED_RTNL,
9960 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009961 {
9962 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9963 .doit = nl80211_crit_protocol_start,
9964 .policy = nl80211_policy,
9965 .flags = GENL_ADMIN_PERM,
9966 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9967 NL80211_FLAG_NEED_RTNL,
9968 },
9969 {
9970 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9971 .doit = nl80211_crit_protocol_stop,
9972 .policy = nl80211_policy,
9973 .flags = GENL_ADMIN_PERM,
9974 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9975 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009976 },
9977 {
9978 .cmd = NL80211_CMD_GET_COALESCE,
9979 .doit = nl80211_get_coalesce,
9980 .policy = nl80211_policy,
9981 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9982 NL80211_FLAG_NEED_RTNL,
9983 },
9984 {
9985 .cmd = NL80211_CMD_SET_COALESCE,
9986 .doit = nl80211_set_coalesce,
9987 .policy = nl80211_policy,
9988 .flags = GENL_ADMIN_PERM,
9989 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9990 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009991 },
9992 {
9993 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9994 .doit = nl80211_channel_switch,
9995 .policy = nl80211_policy,
9996 .flags = GENL_ADMIN_PERM,
9997 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9998 NL80211_FLAG_NEED_RTNL,
9999 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010000 {
10001 .cmd = NL80211_CMD_VENDOR,
10002 .doit = nl80211_vendor_cmd,
10003 .policy = nl80211_policy,
10004 .flags = GENL_ADMIN_PERM,
10005 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10006 NL80211_FLAG_NEED_RTNL,
10007 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010008 {
10009 .cmd = NL80211_CMD_SET_QOS_MAP,
10010 .doit = nl80211_set_qos_map,
10011 .policy = nl80211_policy,
10012 .flags = GENL_ADMIN_PERM,
10013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10014 NL80211_FLAG_NEED_RTNL,
10015 },
Johannes Berg55682962007-09-20 13:09:35 -040010016};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010017
Johannes Berg55682962007-09-20 13:09:35 -040010018/* notification functions */
10019
10020void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
10021{
10022 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010023 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010024
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010026 if (!msg)
10027 return;
10028
Johannes Berg86e8cf92013-06-19 10:57:22 +020010029 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010030 nlmsg_free(msg);
10031 return;
10032 }
10033
Johannes Berg68eb5502013-11-19 15:19:38 +010010034 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010035 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010036}
10037
Johannes Berg362a4152009-05-24 16:43:15 +020010038static int nl80211_add_scan_req(struct sk_buff *msg,
10039 struct cfg80211_registered_device *rdev)
10040{
10041 struct cfg80211_scan_request *req = rdev->scan_req;
10042 struct nlattr *nest;
10043 int i;
10044
10045 if (WARN_ON(!req))
10046 return 0;
10047
10048 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10049 if (!nest)
10050 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010051 for (i = 0; i < req->n_ssids; i++) {
10052 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10053 goto nla_put_failure;
10054 }
Johannes Berg362a4152009-05-24 16:43:15 +020010055 nla_nest_end(msg, nest);
10056
10057 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10058 if (!nest)
10059 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010060 for (i = 0; i < req->n_channels; i++) {
10061 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10062 goto nla_put_failure;
10063 }
Johannes Berg362a4152009-05-24 16:43:15 +020010064 nla_nest_end(msg, nest);
10065
David S. Miller9360ffd2012-03-29 04:41:26 -040010066 if (req->ie &&
10067 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10068 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010069
Johannes Bergae917c92013-10-25 11:05:22 +020010070 if (req->flags &&
10071 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10072 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010073
Johannes Berg362a4152009-05-24 16:43:15 +020010074 return 0;
10075 nla_put_failure:
10076 return -ENOBUFS;
10077}
10078
Johannes Berga538e2d2009-06-16 19:56:42 +020010079static int nl80211_send_scan_msg(struct sk_buff *msg,
10080 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010081 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010082 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010083 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010084{
10085 void *hdr;
10086
Eric W. Biederman15e47302012-09-07 20:12:54 +000010087 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010088 if (!hdr)
10089 return -1;
10090
David S. Miller9360ffd2012-03-29 04:41:26 -040010091 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010092 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10093 wdev->netdev->ifindex)) ||
10094 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010095 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010096
Johannes Berg362a4152009-05-24 16:43:15 +020010097 /* ignore errors and send incomplete event anyway */
10098 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010099
10100 return genlmsg_end(msg, hdr);
10101
10102 nla_put_failure:
10103 genlmsg_cancel(msg, hdr);
10104 return -EMSGSIZE;
10105}
10106
Luciano Coelho807f8a82011-05-11 17:09:35 +030010107static int
10108nl80211_send_sched_scan_msg(struct sk_buff *msg,
10109 struct cfg80211_registered_device *rdev,
10110 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010111 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010112{
10113 void *hdr;
10114
Eric W. Biederman15e47302012-09-07 20:12:54 +000010115 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010116 if (!hdr)
10117 return -1;
10118
David S. Miller9360ffd2012-03-29 04:41:26 -040010119 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10120 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10121 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010122
10123 return genlmsg_end(msg, hdr);
10124
10125 nla_put_failure:
10126 genlmsg_cancel(msg, hdr);
10127 return -EMSGSIZE;
10128}
10129
Johannes Berga538e2d2009-06-16 19:56:42 +020010130void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010131 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010132{
10133 struct sk_buff *msg;
10134
Thomas Graf58050fc2012-06-28 03:57:45 +000010135 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010136 if (!msg)
10137 return;
10138
Johannes Bergfd014282012-06-18 19:17:03 +020010139 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010140 NL80211_CMD_TRIGGER_SCAN) < 0) {
10141 nlmsg_free(msg);
10142 return;
10143 }
10144
Johannes Berg68eb5502013-11-19 15:19:38 +010010145 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010146 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010147}
10148
Johannes Bergf9d15d12014-01-22 11:14:19 +020010149struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10150 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010151{
10152 struct sk_buff *msg;
10153
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010154 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010155 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010156 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010157
Johannes Bergfd014282012-06-18 19:17:03 +020010158 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010159 aborted ? NL80211_CMD_SCAN_ABORTED :
10160 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010161 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010162 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010163 }
10164
Johannes Bergf9d15d12014-01-22 11:14:19 +020010165 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010166}
10167
Johannes Bergf9d15d12014-01-22 11:14:19 +020010168void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10169 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010170{
Johannes Berg2a519312009-02-10 21:25:55 +010010171 if (!msg)
10172 return;
10173
Johannes Berg68eb5502013-11-19 15:19:38 +010010174 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010175 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010176}
10177
Luciano Coelho807f8a82011-05-11 17:09:35 +030010178void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10179 struct net_device *netdev)
10180{
10181 struct sk_buff *msg;
10182
10183 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10184 if (!msg)
10185 return;
10186
10187 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10188 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10189 nlmsg_free(msg);
10190 return;
10191 }
10192
Johannes Berg68eb5502013-11-19 15:19:38 +010010193 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010194 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010195}
10196
10197void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10198 struct net_device *netdev, u32 cmd)
10199{
10200 struct sk_buff *msg;
10201
Thomas Graf58050fc2012-06-28 03:57:45 +000010202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010203 if (!msg)
10204 return;
10205
10206 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10207 nlmsg_free(msg);
10208 return;
10209 }
10210
Johannes Berg68eb5502013-11-19 15:19:38 +010010211 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010212 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010213}
10214
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010215/*
10216 * This can happen on global regulatory changes or device specific settings
10217 * based on custom world regulatory domains.
10218 */
10219void nl80211_send_reg_change_event(struct regulatory_request *request)
10220{
10221 struct sk_buff *msg;
10222 void *hdr;
10223
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010224 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010225 if (!msg)
10226 return;
10227
10228 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10229 if (!hdr) {
10230 nlmsg_free(msg);
10231 return;
10232 }
10233
10234 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010235 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10236 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010237
David S. Miller9360ffd2012-03-29 04:41:26 -040010238 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10239 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10240 NL80211_REGDOM_TYPE_WORLD))
10241 goto nla_put_failure;
10242 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10243 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10244 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10245 goto nla_put_failure;
10246 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10247 request->intersect) {
10248 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10249 NL80211_REGDOM_TYPE_INTERSECTION))
10250 goto nla_put_failure;
10251 } else {
10252 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10253 NL80211_REGDOM_TYPE_COUNTRY) ||
10254 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10255 request->alpha2))
10256 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010257 }
10258
Johannes Bergf4173762012-12-03 18:23:37 +010010259 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010260 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10261 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010262
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010263 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010264
Johannes Bergbc43b282009-07-25 10:54:13 +020010265 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010266 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010267 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010268 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010269
10270 return;
10271
10272nla_put_failure:
10273 genlmsg_cancel(msg, hdr);
10274 nlmsg_free(msg);
10275}
10276
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010277static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10278 struct net_device *netdev,
10279 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010280 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010281{
10282 struct sk_buff *msg;
10283 void *hdr;
10284
Johannes Berge6d6e342009-07-01 21:26:47 +020010285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010286 if (!msg)
10287 return;
10288
10289 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10290 if (!hdr) {
10291 nlmsg_free(msg);
10292 return;
10293 }
10294
David S. Miller9360ffd2012-03-29 04:41:26 -040010295 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10296 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10297 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10298 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010299
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010300 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010301
Johannes Berg68eb5502013-11-19 15:19:38 +010010302 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010303 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010304 return;
10305
10306 nla_put_failure:
10307 genlmsg_cancel(msg, hdr);
10308 nlmsg_free(msg);
10309}
10310
10311void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010312 struct net_device *netdev, const u8 *buf,
10313 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_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010317}
10318
10319void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10320 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010321 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010322{
Johannes Berge6d6e342009-07-01 21:26:47 +020010323 nl80211_send_mlme_event(rdev, netdev, buf, len,
10324 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010325}
10326
Jouni Malinen53b46b82009-03-27 20:53:56 +020010327void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010328 struct net_device *netdev, const u8 *buf,
10329 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010330{
10331 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010332 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010333}
10334
Jouni Malinen53b46b82009-03-27 20:53:56 +020010335void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10336 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010337 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010338{
10339 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010340 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010341}
10342
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010343void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10344 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010345{
Johannes Berg947add32013-02-22 22:05:20 +010010346 struct wireless_dev *wdev = dev->ieee80211_ptr;
10347 struct wiphy *wiphy = wdev->wiphy;
10348 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010349 const struct ieee80211_mgmt *mgmt = (void *)buf;
10350 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010351
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010352 if (WARN_ON(len < 2))
10353 return;
10354
10355 if (ieee80211_is_deauth(mgmt->frame_control))
10356 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10357 else
10358 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10359
10360 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10361 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010362}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010363EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010364
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010365static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10366 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010367 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010368{
10369 struct sk_buff *msg;
10370 void *hdr;
10371
Johannes Berge6d6e342009-07-01 21:26:47 +020010372 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010373 if (!msg)
10374 return;
10375
10376 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10377 if (!hdr) {
10378 nlmsg_free(msg);
10379 return;
10380 }
10381
David S. Miller9360ffd2012-03-29 04:41:26 -040010382 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10383 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10384 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10385 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10386 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010387
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010388 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010389
Johannes Berg68eb5502013-11-19 15:19:38 +010010390 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010391 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010392 return;
10393
10394 nla_put_failure:
10395 genlmsg_cancel(msg, hdr);
10396 nlmsg_free(msg);
10397}
10398
10399void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010400 struct net_device *netdev, const u8 *addr,
10401 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010402{
10403 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010404 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010405}
10406
10407void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010408 struct net_device *netdev, const u8 *addr,
10409 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010410{
Johannes Berge6d6e342009-07-01 21:26:47 +020010411 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10412 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010413}
10414
Samuel Ortizb23aa672009-07-01 21:26:54 +020010415void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10416 struct net_device *netdev, const u8 *bssid,
10417 const u8 *req_ie, size_t req_ie_len,
10418 const u8 *resp_ie, size_t resp_ie_len,
10419 u16 status, gfp_t gfp)
10420{
10421 struct sk_buff *msg;
10422 void *hdr;
10423
Thomas Graf58050fc2012-06-28 03:57:45 +000010424 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010425 if (!msg)
10426 return;
10427
10428 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10429 if (!hdr) {
10430 nlmsg_free(msg);
10431 return;
10432 }
10433
David S. Miller9360ffd2012-03-29 04:41:26 -040010434 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10435 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10436 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10437 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10438 (req_ie &&
10439 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10440 (resp_ie &&
10441 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10442 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010443
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010444 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010445
Johannes Berg68eb5502013-11-19 15:19:38 +010010446 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010447 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010448 return;
10449
10450 nla_put_failure:
10451 genlmsg_cancel(msg, hdr);
10452 nlmsg_free(msg);
10453
10454}
10455
10456void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10457 struct net_device *netdev, const u8 *bssid,
10458 const u8 *req_ie, size_t req_ie_len,
10459 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10460{
10461 struct sk_buff *msg;
10462 void *hdr;
10463
Thomas Graf58050fc2012-06-28 03:57:45 +000010464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010465 if (!msg)
10466 return;
10467
10468 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10469 if (!hdr) {
10470 nlmsg_free(msg);
10471 return;
10472 }
10473
David S. Miller9360ffd2012-03-29 04:41:26 -040010474 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10475 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10476 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10477 (req_ie &&
10478 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10479 (resp_ie &&
10480 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10481 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010482
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010483 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010484
Johannes Berg68eb5502013-11-19 15:19:38 +010010485 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010486 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010487 return;
10488
10489 nla_put_failure:
10490 genlmsg_cancel(msg, hdr);
10491 nlmsg_free(msg);
10492
10493}
10494
10495void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10496 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010497 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010498{
10499 struct sk_buff *msg;
10500 void *hdr;
10501
Thomas Graf58050fc2012-06-28 03:57:45 +000010502 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010503 if (!msg)
10504 return;
10505
10506 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10507 if (!hdr) {
10508 nlmsg_free(msg);
10509 return;
10510 }
10511
David S. Miller9360ffd2012-03-29 04:41:26 -040010512 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10513 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10514 (from_ap && reason &&
10515 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10516 (from_ap &&
10517 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10518 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10519 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010520
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010521 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010522
Johannes Berg68eb5502013-11-19 15:19:38 +010010523 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010524 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010525 return;
10526
10527 nla_put_failure:
10528 genlmsg_cancel(msg, hdr);
10529 nlmsg_free(msg);
10530
10531}
10532
Johannes Berg04a773a2009-04-19 21:24:32 +020010533void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10534 struct net_device *netdev, const u8 *bssid,
10535 gfp_t gfp)
10536{
10537 struct sk_buff *msg;
10538 void *hdr;
10539
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010540 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010541 if (!msg)
10542 return;
10543
10544 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10545 if (!hdr) {
10546 nlmsg_free(msg);
10547 return;
10548 }
10549
David S. Miller9360ffd2012-03-29 04:41:26 -040010550 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10551 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10552 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10553 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010554
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010555 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010556
Johannes Berg68eb5502013-11-19 15:19:38 +010010557 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010558 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010559 return;
10560
10561 nla_put_failure:
10562 genlmsg_cancel(msg, hdr);
10563 nlmsg_free(msg);
10564}
10565
Johannes Berg947add32013-02-22 22:05:20 +010010566void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10567 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010568{
Johannes Berg947add32013-02-22 22:05:20 +010010569 struct wireless_dev *wdev = dev->ieee80211_ptr;
10570 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010571 struct sk_buff *msg;
10572 void *hdr;
10573
Johannes Berg947add32013-02-22 22:05:20 +010010574 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10575 return;
10576
10577 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10578
Javier Cardonac93b5e72011-04-07 15:08:34 -070010579 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10580 if (!msg)
10581 return;
10582
10583 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10584 if (!hdr) {
10585 nlmsg_free(msg);
10586 return;
10587 }
10588
David S. Miller9360ffd2012-03-29 04:41:26 -040010589 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010590 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10591 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010592 (ie_len && ie &&
10593 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10594 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010595
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010596 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010597
Johannes Berg68eb5502013-11-19 15:19:38 +010010598 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010599 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010600 return;
10601
10602 nla_put_failure:
10603 genlmsg_cancel(msg, hdr);
10604 nlmsg_free(msg);
10605}
Johannes Berg947add32013-02-22 22:05:20 +010010606EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010607
Jouni Malinena3b8b052009-03-27 21:59:49 +020010608void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10609 struct net_device *netdev, const u8 *addr,
10610 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010611 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010612{
10613 struct sk_buff *msg;
10614 void *hdr;
10615
Johannes Berge6d6e342009-07-01 21:26:47 +020010616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010617 if (!msg)
10618 return;
10619
10620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10621 if (!hdr) {
10622 nlmsg_free(msg);
10623 return;
10624 }
10625
David S. Miller9360ffd2012-03-29 04:41:26 -040010626 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10627 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10628 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10629 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10630 (key_id != -1 &&
10631 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10632 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10633 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010634
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010635 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010636
Johannes Berg68eb5502013-11-19 15:19:38 +010010637 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010638 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010639 return;
10640
10641 nla_put_failure:
10642 genlmsg_cancel(msg, hdr);
10643 nlmsg_free(msg);
10644}
10645
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010646void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10647 struct ieee80211_channel *channel_before,
10648 struct ieee80211_channel *channel_after)
10649{
10650 struct sk_buff *msg;
10651 void *hdr;
10652 struct nlattr *nl_freq;
10653
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010654 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010655 if (!msg)
10656 return;
10657
10658 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10659 if (!hdr) {
10660 nlmsg_free(msg);
10661 return;
10662 }
10663
10664 /*
10665 * Since we are applying the beacon hint to a wiphy we know its
10666 * wiphy_idx is valid
10667 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010668 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10669 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010670
10671 /* Before */
10672 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10673 if (!nl_freq)
10674 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010675 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010676 goto nla_put_failure;
10677 nla_nest_end(msg, nl_freq);
10678
10679 /* After */
10680 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10681 if (!nl_freq)
10682 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010683 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010684 goto nla_put_failure;
10685 nla_nest_end(msg, nl_freq);
10686
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010687 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010688
Johannes Berg463d0182009-07-14 00:33:35 +020010689 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010690 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010691 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010692 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010693
10694 return;
10695
10696nla_put_failure:
10697 genlmsg_cancel(msg, hdr);
10698 nlmsg_free(msg);
10699}
10700
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010701static void nl80211_send_remain_on_chan_event(
10702 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010703 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010704 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010705 unsigned int duration, gfp_t gfp)
10706{
10707 struct sk_buff *msg;
10708 void *hdr;
10709
10710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10711 if (!msg)
10712 return;
10713
10714 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10715 if (!hdr) {
10716 nlmsg_free(msg);
10717 return;
10718 }
10719
David S. Miller9360ffd2012-03-29 04:41:26 -040010720 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010721 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10722 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010723 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010724 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010725 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10726 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010727 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10728 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010729
David S. Miller9360ffd2012-03-29 04:41:26 -040010730 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10731 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10732 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010733
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010734 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010735
Johannes Berg68eb5502013-11-19 15:19:38 +010010736 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010737 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010738 return;
10739
10740 nla_put_failure:
10741 genlmsg_cancel(msg, hdr);
10742 nlmsg_free(msg);
10743}
10744
Johannes Berg947add32013-02-22 22:05:20 +010010745void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10746 struct ieee80211_channel *chan,
10747 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010748{
Johannes Berg947add32013-02-22 22:05:20 +010010749 struct wiphy *wiphy = wdev->wiphy;
10750 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10751
10752 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010753 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010754 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010755 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010756}
Johannes Berg947add32013-02-22 22:05:20 +010010757EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010758
Johannes Berg947add32013-02-22 22:05:20 +010010759void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10760 struct ieee80211_channel *chan,
10761 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010762{
Johannes Berg947add32013-02-22 22:05:20 +010010763 struct wiphy *wiphy = wdev->wiphy;
10764 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10765
10766 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010767 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010768 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010769}
Johannes Berg947add32013-02-22 22:05:20 +010010770EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010771
Johannes Berg947add32013-02-22 22:05:20 +010010772void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10773 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010774{
Johannes Berg947add32013-02-22 22:05:20 +010010775 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10776 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010777 struct sk_buff *msg;
10778
Johannes Berg947add32013-02-22 22:05:20 +010010779 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10780
Thomas Graf58050fc2012-06-28 03:57:45 +000010781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010782 if (!msg)
10783 return;
10784
John W. Linville66266b32012-03-15 13:25:41 -040010785 if (nl80211_send_station(msg, 0, 0, 0,
10786 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010787 nlmsg_free(msg);
10788 return;
10789 }
10790
Johannes Berg68eb5502013-11-19 15:19:38 +010010791 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010792 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010793}
Johannes Berg947add32013-02-22 22:05:20 +010010794EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010795
Johannes Berg947add32013-02-22 22:05:20 +010010796void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010797{
Johannes Berg947add32013-02-22 22:05:20 +010010798 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10799 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010800 struct sk_buff *msg;
10801 void *hdr;
10802
Johannes Berg947add32013-02-22 22:05:20 +010010803 trace_cfg80211_del_sta(dev, mac_addr);
10804
Thomas Graf58050fc2012-06-28 03:57:45 +000010805 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010806 if (!msg)
10807 return;
10808
10809 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10810 if (!hdr) {
10811 nlmsg_free(msg);
10812 return;
10813 }
10814
David S. Miller9360ffd2012-03-29 04:41:26 -040010815 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10816 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10817 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010818
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010819 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010820
Johannes Berg68eb5502013-11-19 15:19:38 +010010821 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010822 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010823 return;
10824
10825 nla_put_failure:
10826 genlmsg_cancel(msg, hdr);
10827 nlmsg_free(msg);
10828}
Johannes Berg947add32013-02-22 22:05:20 +010010829EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010830
Johannes Berg947add32013-02-22 22:05:20 +010010831void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10832 enum nl80211_connect_failed_reason reason,
10833 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010834{
Johannes Berg947add32013-02-22 22:05:20 +010010835 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10836 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010837 struct sk_buff *msg;
10838 void *hdr;
10839
10840 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10841 if (!msg)
10842 return;
10843
10844 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10845 if (!hdr) {
10846 nlmsg_free(msg);
10847 return;
10848 }
10849
10850 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10851 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10852 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10853 goto nla_put_failure;
10854
10855 genlmsg_end(msg, hdr);
10856
Johannes Berg68eb5502013-11-19 15:19:38 +010010857 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010858 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010859 return;
10860
10861 nla_put_failure:
10862 genlmsg_cancel(msg, hdr);
10863 nlmsg_free(msg);
10864}
Johannes Berg947add32013-02-22 22:05:20 +010010865EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010866
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010867static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10868 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010869{
10870 struct wireless_dev *wdev = dev->ieee80211_ptr;
10871 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10872 struct sk_buff *msg;
10873 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010874 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010875
Eric W. Biederman15e47302012-09-07 20:12:54 +000010876 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010877 return false;
10878
10879 msg = nlmsg_new(100, gfp);
10880 if (!msg)
10881 return true;
10882
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010883 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010884 if (!hdr) {
10885 nlmsg_free(msg);
10886 return true;
10887 }
10888
David S. Miller9360ffd2012-03-29 04:41:26 -040010889 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10890 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10891 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10892 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010893
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010894 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010895 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010896 return true;
10897
10898 nla_put_failure:
10899 genlmsg_cancel(msg, hdr);
10900 nlmsg_free(msg);
10901 return true;
10902}
10903
Johannes Berg947add32013-02-22 22:05:20 +010010904bool cfg80211_rx_spurious_frame(struct net_device *dev,
10905 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010906{
Johannes Berg947add32013-02-22 22:05:20 +010010907 struct wireless_dev *wdev = dev->ieee80211_ptr;
10908 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010909
Johannes Berg947add32013-02-22 22:05:20 +010010910 trace_cfg80211_rx_spurious_frame(dev, addr);
10911
10912 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10913 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10914 trace_cfg80211_return_bool(false);
10915 return false;
10916 }
10917 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10918 addr, gfp);
10919 trace_cfg80211_return_bool(ret);
10920 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010921}
Johannes Berg947add32013-02-22 22:05:20 +010010922EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10923
10924bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10925 const u8 *addr, gfp_t gfp)
10926{
10927 struct wireless_dev *wdev = dev->ieee80211_ptr;
10928 bool ret;
10929
10930 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10931
10932 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10933 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10934 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10935 trace_cfg80211_return_bool(false);
10936 return false;
10937 }
10938 ret = __nl80211_unexpected_frame(dev,
10939 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10940 addr, gfp);
10941 trace_cfg80211_return_bool(ret);
10942 return ret;
10943}
10944EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010945
Johannes Berg2e161f72010-08-12 15:38:38 +020010946int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010947 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010948 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010949 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010950{
Johannes Berg71bbc992012-06-15 15:30:18 +020010951 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010952 struct sk_buff *msg;
10953 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010954
10955 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10956 if (!msg)
10957 return -ENOMEM;
10958
Johannes Berg2e161f72010-08-12 15:38:38 +020010959 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010960 if (!hdr) {
10961 nlmsg_free(msg);
10962 return -ENOMEM;
10963 }
10964
David S. Miller9360ffd2012-03-29 04:41:26 -040010965 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010966 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10967 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010968 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010969 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10970 (sig_dbm &&
10971 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010972 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10973 (flags &&
10974 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010975 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010976
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010977 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010978
Eric W. Biederman15e47302012-09-07 20:12:54 +000010979 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010980
10981 nla_put_failure:
10982 genlmsg_cancel(msg, hdr);
10983 nlmsg_free(msg);
10984 return -ENOBUFS;
10985}
10986
Johannes Berg947add32013-02-22 22:05:20 +010010987void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10988 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010989{
Johannes Berg947add32013-02-22 22:05:20 +010010990 struct wiphy *wiphy = wdev->wiphy;
10991 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010992 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010993 struct sk_buff *msg;
10994 void *hdr;
10995
Johannes Berg947add32013-02-22 22:05:20 +010010996 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10997
Jouni Malinen026331c2010-02-15 12:53:10 +020010998 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10999 if (!msg)
11000 return;
11001
Johannes Berg2e161f72010-08-12 15:38:38 +020011002 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011003 if (!hdr) {
11004 nlmsg_free(msg);
11005 return;
11006 }
11007
David S. Miller9360ffd2012-03-29 04:41:26 -040011008 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011009 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11010 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011011 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011012 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11013 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11014 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11015 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011016
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011017 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011018
Johannes Berg68eb5502013-11-19 15:19:38 +010011019 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011020 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011021 return;
11022
11023 nla_put_failure:
11024 genlmsg_cancel(msg, hdr);
11025 nlmsg_free(msg);
11026}
Johannes Berg947add32013-02-22 22:05:20 +010011027EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011028
Johannes Berg947add32013-02-22 22:05:20 +010011029void cfg80211_cqm_rssi_notify(struct net_device *dev,
11030 enum nl80211_cqm_rssi_threshold_event rssi_event,
11031 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011032{
Johannes Berg947add32013-02-22 22:05:20 +010011033 struct wireless_dev *wdev = dev->ieee80211_ptr;
11034 struct wiphy *wiphy = wdev->wiphy;
11035 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011036 struct sk_buff *msg;
11037 struct nlattr *pinfoattr;
11038 void *hdr;
11039
Johannes Berg947add32013-02-22 22:05:20 +010011040 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11041
Thomas Graf58050fc2012-06-28 03:57:45 +000011042 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011043 if (!msg)
11044 return;
11045
11046 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11047 if (!hdr) {
11048 nlmsg_free(msg);
11049 return;
11050 }
11051
David S. Miller9360ffd2012-03-29 04:41:26 -040011052 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011053 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011054 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011055
11056 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11057 if (!pinfoattr)
11058 goto nla_put_failure;
11059
David S. Miller9360ffd2012-03-29 04:41:26 -040011060 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11061 rssi_event))
11062 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011063
11064 nla_nest_end(msg, pinfoattr);
11065
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011066 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011067
Johannes Berg68eb5502013-11-19 15:19:38 +010011068 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011069 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011070 return;
11071
11072 nla_put_failure:
11073 genlmsg_cancel(msg, hdr);
11074 nlmsg_free(msg);
11075}
Johannes Berg947add32013-02-22 22:05:20 +010011076EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011077
Johannes Berg947add32013-02-22 22:05:20 +010011078static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11079 struct net_device *netdev, const u8 *bssid,
11080 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011081{
11082 struct sk_buff *msg;
11083 struct nlattr *rekey_attr;
11084 void *hdr;
11085
Thomas Graf58050fc2012-06-28 03:57:45 +000011086 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011087 if (!msg)
11088 return;
11089
11090 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11091 if (!hdr) {
11092 nlmsg_free(msg);
11093 return;
11094 }
11095
David S. Miller9360ffd2012-03-29 04:41:26 -040011096 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11097 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11098 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11099 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011100
11101 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11102 if (!rekey_attr)
11103 goto nla_put_failure;
11104
David S. Miller9360ffd2012-03-29 04:41:26 -040011105 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11106 NL80211_REPLAY_CTR_LEN, replay_ctr))
11107 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011108
11109 nla_nest_end(msg, rekey_attr);
11110
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011111 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011112
Johannes Berg68eb5502013-11-19 15:19:38 +010011113 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011114 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011115 return;
11116
11117 nla_put_failure:
11118 genlmsg_cancel(msg, hdr);
11119 nlmsg_free(msg);
11120}
11121
Johannes Berg947add32013-02-22 22:05:20 +010011122void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11123 const u8 *replay_ctr, gfp_t gfp)
11124{
11125 struct wireless_dev *wdev = dev->ieee80211_ptr;
11126 struct wiphy *wiphy = wdev->wiphy;
11127 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11128
11129 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11130 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11131}
11132EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11133
11134static void
11135nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11136 struct net_device *netdev, int index,
11137 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011138{
11139 struct sk_buff *msg;
11140 struct nlattr *attr;
11141 void *hdr;
11142
Thomas Graf58050fc2012-06-28 03:57:45 +000011143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011144 if (!msg)
11145 return;
11146
11147 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11148 if (!hdr) {
11149 nlmsg_free(msg);
11150 return;
11151 }
11152
David S. Miller9360ffd2012-03-29 04:41:26 -040011153 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11154 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11155 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011156
11157 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11158 if (!attr)
11159 goto nla_put_failure;
11160
David S. Miller9360ffd2012-03-29 04:41:26 -040011161 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11162 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11163 (preauth &&
11164 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11165 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011166
11167 nla_nest_end(msg, attr);
11168
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011169 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011170
Johannes Berg68eb5502013-11-19 15:19:38 +010011171 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011172 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011173 return;
11174
11175 nla_put_failure:
11176 genlmsg_cancel(msg, hdr);
11177 nlmsg_free(msg);
11178}
11179
Johannes Berg947add32013-02-22 22:05:20 +010011180void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11181 const u8 *bssid, bool preauth, gfp_t gfp)
11182{
11183 struct wireless_dev *wdev = dev->ieee80211_ptr;
11184 struct wiphy *wiphy = wdev->wiphy;
11185 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11186
11187 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11188 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11189}
11190EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11191
11192static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11193 struct net_device *netdev,
11194 struct cfg80211_chan_def *chandef,
11195 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011196{
11197 struct sk_buff *msg;
11198 void *hdr;
11199
Thomas Graf58050fc2012-06-28 03:57:45 +000011200 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011201 if (!msg)
11202 return;
11203
11204 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11205 if (!hdr) {
11206 nlmsg_free(msg);
11207 return;
11208 }
11209
Johannes Berg683b6d32012-11-08 21:25:48 +010011210 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11211 goto nla_put_failure;
11212
11213 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011214 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011215
11216 genlmsg_end(msg, hdr);
11217
Johannes Berg68eb5502013-11-19 15:19:38 +010011218 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011219 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011220 return;
11221
11222 nla_put_failure:
11223 genlmsg_cancel(msg, hdr);
11224 nlmsg_free(msg);
11225}
11226
Johannes Berg947add32013-02-22 22:05:20 +010011227void cfg80211_ch_switch_notify(struct net_device *dev,
11228 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011229{
Johannes Berg947add32013-02-22 22:05:20 +010011230 struct wireless_dev *wdev = dev->ieee80211_ptr;
11231 struct wiphy *wiphy = wdev->wiphy;
11232 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11233
Simon Wunderliche487eae2013-11-21 18:19:51 +010011234 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011235
Simon Wunderliche487eae2013-11-21 18:19:51 +010011236 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011237
11238 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011239 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011240 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11241 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011242 return;
Johannes Berg947add32013-02-22 22:05:20 +010011243
Michal Kazior9e0e2962014-01-29 14:22:27 +010011244 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011245 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011246 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011247}
11248EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11249
11250void cfg80211_cqm_txe_notify(struct net_device *dev,
11251 const u8 *peer, u32 num_packets,
11252 u32 rate, u32 intvl, gfp_t gfp)
11253{
11254 struct wireless_dev *wdev = dev->ieee80211_ptr;
11255 struct wiphy *wiphy = wdev->wiphy;
11256 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011257 struct sk_buff *msg;
11258 struct nlattr *pinfoattr;
11259 void *hdr;
11260
11261 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11262 if (!msg)
11263 return;
11264
11265 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11266 if (!hdr) {
11267 nlmsg_free(msg);
11268 return;
11269 }
11270
11271 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011272 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011273 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11274 goto nla_put_failure;
11275
11276 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11277 if (!pinfoattr)
11278 goto nla_put_failure;
11279
11280 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11281 goto nla_put_failure;
11282
11283 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11284 goto nla_put_failure;
11285
11286 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11287 goto nla_put_failure;
11288
11289 nla_nest_end(msg, pinfoattr);
11290
11291 genlmsg_end(msg, hdr);
11292
Johannes Berg68eb5502013-11-19 15:19:38 +010011293 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011294 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011295 return;
11296
11297 nla_put_failure:
11298 genlmsg_cancel(msg, hdr);
11299 nlmsg_free(msg);
11300}
Johannes Berg947add32013-02-22 22:05:20 +010011301EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011302
11303void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011304nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011305 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011306 enum nl80211_radar_event event,
11307 struct net_device *netdev, gfp_t gfp)
11308{
11309 struct sk_buff *msg;
11310 void *hdr;
11311
11312 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11313 if (!msg)
11314 return;
11315
11316 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11317 if (!hdr) {
11318 nlmsg_free(msg);
11319 return;
11320 }
11321
11322 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11323 goto nla_put_failure;
11324
11325 /* NOP and radar events don't need a netdev parameter */
11326 if (netdev) {
11327 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11328
11329 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11330 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11331 goto nla_put_failure;
11332 }
11333
11334 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11335 goto nla_put_failure;
11336
11337 if (nl80211_send_chandef(msg, chandef))
11338 goto nla_put_failure;
11339
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011340 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011341
Johannes Berg68eb5502013-11-19 15:19:38 +010011342 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011343 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011344 return;
11345
11346 nla_put_failure:
11347 genlmsg_cancel(msg, hdr);
11348 nlmsg_free(msg);
11349}
11350
Johannes Berg947add32013-02-22 22:05:20 +010011351void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11352 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011353{
Johannes Berg947add32013-02-22 22:05:20 +010011354 struct wireless_dev *wdev = dev->ieee80211_ptr;
11355 struct wiphy *wiphy = wdev->wiphy;
11356 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011357 struct sk_buff *msg;
11358 struct nlattr *pinfoattr;
11359 void *hdr;
11360
Johannes Berg947add32013-02-22 22:05:20 +010011361 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11362
Thomas Graf58050fc2012-06-28 03:57:45 +000011363 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011364 if (!msg)
11365 return;
11366
11367 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11368 if (!hdr) {
11369 nlmsg_free(msg);
11370 return;
11371 }
11372
David S. Miller9360ffd2012-03-29 04:41:26 -040011373 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011374 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011375 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11376 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011377
11378 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11379 if (!pinfoattr)
11380 goto nla_put_failure;
11381
David S. Miller9360ffd2012-03-29 04:41:26 -040011382 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11383 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011384
11385 nla_nest_end(msg, pinfoattr);
11386
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011387 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011388
Johannes Berg68eb5502013-11-19 15:19:38 +010011389 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011390 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011391 return;
11392
11393 nla_put_failure:
11394 genlmsg_cancel(msg, hdr);
11395 nlmsg_free(msg);
11396}
Johannes Berg947add32013-02-22 22:05:20 +010011397EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011398
Johannes Berg7f6cf312011-11-04 11:18:15 +010011399void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11400 u64 cookie, bool acked, gfp_t gfp)
11401{
11402 struct wireless_dev *wdev = dev->ieee80211_ptr;
11403 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11404 struct sk_buff *msg;
11405 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011406
Beni Lev4ee3e062012-08-27 12:49:39 +030011407 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11408
Thomas Graf58050fc2012-06-28 03:57:45 +000011409 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011410
Johannes Berg7f6cf312011-11-04 11:18:15 +010011411 if (!msg)
11412 return;
11413
11414 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11415 if (!hdr) {
11416 nlmsg_free(msg);
11417 return;
11418 }
11419
David S. Miller9360ffd2012-03-29 04:41:26 -040011420 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11421 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11422 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11423 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11424 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11425 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011426
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011427 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011428
Johannes Berg68eb5502013-11-19 15:19:38 +010011429 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011430 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011431 return;
11432
11433 nla_put_failure:
11434 genlmsg_cancel(msg, hdr);
11435 nlmsg_free(msg);
11436}
11437EXPORT_SYMBOL(cfg80211_probe_status);
11438
Johannes Berg5e7602302011-11-04 11:18:17 +010011439void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11440 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011441 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010011442{
11443 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11444 struct sk_buff *msg;
11445 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011446 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010011447
Beni Lev4ee3e062012-08-27 12:49:39 +030011448 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11449
Ben Greear37c73b52012-10-26 14:49:25 -070011450 spin_lock_bh(&rdev->beacon_registrations_lock);
11451 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11452 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11453 if (!msg) {
11454 spin_unlock_bh(&rdev->beacon_registrations_lock);
11455 return;
11456 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011457
Ben Greear37c73b52012-10-26 14:49:25 -070011458 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11459 if (!hdr)
11460 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010011461
Ben Greear37c73b52012-10-26 14:49:25 -070011462 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11463 (freq &&
11464 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11465 (sig_dbm &&
11466 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11467 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11468 goto nla_put_failure;
11469
11470 genlmsg_end(msg, hdr);
11471
11472 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010011473 }
Ben Greear37c73b52012-10-26 14:49:25 -070011474 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011475 return;
11476
11477 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011478 spin_unlock_bh(&rdev->beacon_registrations_lock);
11479 if (hdr)
11480 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010011481 nlmsg_free(msg);
11482}
11483EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11484
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011485#ifdef CONFIG_PM
11486void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11487 struct cfg80211_wowlan_wakeup *wakeup,
11488 gfp_t gfp)
11489{
11490 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11491 struct sk_buff *msg;
11492 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011493 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011494
11495 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11496
11497 if (wakeup)
11498 size += wakeup->packet_present_len;
11499
11500 msg = nlmsg_new(size, gfp);
11501 if (!msg)
11502 return;
11503
11504 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11505 if (!hdr)
11506 goto free_msg;
11507
11508 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11509 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11510 goto free_msg;
11511
11512 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11513 wdev->netdev->ifindex))
11514 goto free_msg;
11515
11516 if (wakeup) {
11517 struct nlattr *reasons;
11518
11519 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011520 if (!reasons)
11521 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011522
11523 if (wakeup->disconnect &&
11524 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11525 goto free_msg;
11526 if (wakeup->magic_pkt &&
11527 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11528 goto free_msg;
11529 if (wakeup->gtk_rekey_failure &&
11530 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11531 goto free_msg;
11532 if (wakeup->eap_identity_req &&
11533 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11534 goto free_msg;
11535 if (wakeup->four_way_handshake &&
11536 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11537 goto free_msg;
11538 if (wakeup->rfkill_release &&
11539 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11540 goto free_msg;
11541
11542 if (wakeup->pattern_idx >= 0 &&
11543 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11544 wakeup->pattern_idx))
11545 goto free_msg;
11546
Johannes Bergae917c92013-10-25 11:05:22 +020011547 if (wakeup->tcp_match &&
11548 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11549 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011550
Johannes Bergae917c92013-10-25 11:05:22 +020011551 if (wakeup->tcp_connlost &&
11552 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11553 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011554
Johannes Bergae917c92013-10-25 11:05:22 +020011555 if (wakeup->tcp_nomoretokens &&
11556 nla_put_flag(msg,
11557 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11558 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011559
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011560 if (wakeup->packet) {
11561 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11562 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11563
11564 if (!wakeup->packet_80211) {
11565 pkt_attr =
11566 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11567 len_attr =
11568 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11569 }
11570
11571 if (wakeup->packet_len &&
11572 nla_put_u32(msg, len_attr, wakeup->packet_len))
11573 goto free_msg;
11574
11575 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11576 wakeup->packet))
11577 goto free_msg;
11578 }
11579
11580 nla_nest_end(msg, reasons);
11581 }
11582
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011583 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011584
Johannes Berg68eb5502013-11-19 15:19:38 +010011585 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011586 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011587 return;
11588
11589 free_msg:
11590 nlmsg_free(msg);
11591}
11592EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11593#endif
11594
Jouni Malinen3475b092012-11-16 22:49:57 +020011595void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11596 enum nl80211_tdls_operation oper,
11597 u16 reason_code, gfp_t gfp)
11598{
11599 struct wireless_dev *wdev = dev->ieee80211_ptr;
11600 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11601 struct sk_buff *msg;
11602 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011603
11604 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11605 reason_code);
11606
11607 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11608 if (!msg)
11609 return;
11610
11611 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11612 if (!hdr) {
11613 nlmsg_free(msg);
11614 return;
11615 }
11616
11617 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11618 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11619 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11620 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11621 (reason_code > 0 &&
11622 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11623 goto nla_put_failure;
11624
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011625 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011626
Johannes Berg68eb5502013-11-19 15:19:38 +010011627 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011628 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011629 return;
11630
11631 nla_put_failure:
11632 genlmsg_cancel(msg, hdr);
11633 nlmsg_free(msg);
11634}
11635EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11636
Jouni Malinen026331c2010-02-15 12:53:10 +020011637static int nl80211_netlink_notify(struct notifier_block * nb,
11638 unsigned long state,
11639 void *_notify)
11640{
11641 struct netlink_notify *notify = _notify;
11642 struct cfg80211_registered_device *rdev;
11643 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011644 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011645
11646 if (state != NETLINK_URELEASE)
11647 return NOTIFY_DONE;
11648
11649 rcu_read_lock();
11650
Johannes Berg5e7602302011-11-04 11:18:17 +010011651 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011652 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011653 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011654
11655 spin_lock_bh(&rdev->beacon_registrations_lock);
11656 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11657 list) {
11658 if (reg->nlportid == notify->portid) {
11659 list_del(&reg->list);
11660 kfree(reg);
11661 break;
11662 }
11663 }
11664 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011665 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011666
11667 rcu_read_unlock();
11668
11669 return NOTIFY_DONE;
11670}
11671
11672static struct notifier_block nl80211_netlink_notifier = {
11673 .notifier_call = nl80211_netlink_notify,
11674};
11675
Jouni Malinen355199e2013-02-27 17:14:27 +020011676void cfg80211_ft_event(struct net_device *netdev,
11677 struct cfg80211_ft_event_params *ft_event)
11678{
11679 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11680 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11681 struct sk_buff *msg;
11682 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011683
11684 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11685
11686 if (!ft_event->target_ap)
11687 return;
11688
11689 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11690 if (!msg)
11691 return;
11692
11693 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011694 if (!hdr)
11695 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011696
Johannes Bergae917c92013-10-25 11:05:22 +020011697 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11698 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11699 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11700 goto out;
11701
11702 if (ft_event->ies &&
11703 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11704 goto out;
11705 if (ft_event->ric_ies &&
11706 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11707 ft_event->ric_ies))
11708 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011709
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011710 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011711
Johannes Berg68eb5502013-11-19 15:19:38 +010011712 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011713 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011714 return;
11715 out:
11716 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011717}
11718EXPORT_SYMBOL(cfg80211_ft_event);
11719
Arend van Spriel5de17982013-04-18 15:49:00 +020011720void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11721{
11722 struct cfg80211_registered_device *rdev;
11723 struct sk_buff *msg;
11724 void *hdr;
11725 u32 nlportid;
11726
11727 rdev = wiphy_to_dev(wdev->wiphy);
11728 if (!rdev->crit_proto_nlportid)
11729 return;
11730
11731 nlportid = rdev->crit_proto_nlportid;
11732 rdev->crit_proto_nlportid = 0;
11733
11734 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11735 if (!msg)
11736 return;
11737
11738 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11739 if (!hdr)
11740 goto nla_put_failure;
11741
11742 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11743 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11744 goto nla_put_failure;
11745
11746 genlmsg_end(msg, hdr);
11747
11748 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11749 return;
11750
11751 nla_put_failure:
11752 if (hdr)
11753 genlmsg_cancel(msg, hdr);
11754 nlmsg_free(msg);
11755
11756}
11757EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11758
Johannes Berg348baf02014-01-24 14:06:29 +010011759void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11760{
11761 struct wiphy *wiphy = wdev->wiphy;
11762 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11763 struct sk_buff *msg;
11764 void *hdr;
11765
11766 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11767 if (!msg)
11768 return;
11769
11770 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11771 if (!hdr)
11772 goto out;
11773
11774 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11775 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11776 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11777 goto out;
11778
11779 genlmsg_end(msg, hdr);
11780
11781 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11782 NL80211_MCGRP_MLME, GFP_KERNEL);
11783 return;
11784 out:
11785 nlmsg_free(msg);
11786}
11787
Johannes Berg55682962007-09-20 13:09:35 -040011788/* initialisation/exit functions */
11789
11790int nl80211_init(void)
11791{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011792 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011793
Johannes Berg2a94fe42013-11-19 15:19:39 +010011794 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11795 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011796 if (err)
11797 return err;
11798
Jouni Malinen026331c2010-02-15 12:53:10 +020011799 err = netlink_register_notifier(&nl80211_netlink_notifier);
11800 if (err)
11801 goto err_out;
11802
Johannes Berg55682962007-09-20 13:09:35 -040011803 return 0;
11804 err_out:
11805 genl_unregister_family(&nl80211_fam);
11806 return err;
11807}
11808
11809void nl80211_exit(void)
11810{
Jouni Malinen026331c2010-02-15 12:53:10 +020011811 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011812 genl_unregister_family(&nl80211_fam);
11813}