blob: 4f82b9b71db10fbb0b115dc82535cc3b94db0b05 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Bergf84f7712013-11-14 17:14:45 +010033static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020034 struct genl_info *info);
Johannes Bergf84f7712013-11-14 17:14:45 +010035static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +020036 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070040 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040044 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg2a94fe42013-11-19 15:19:39 +010050/* multicast groups */
51enum nl80211_multicast_groups {
52 NL80211_MCGRP_CONFIG,
53 NL80211_MCGRP_SCAN,
54 NL80211_MCGRP_REGULATORY,
55 NL80211_MCGRP_MLME,
Johannes Berg567ffc32013-12-18 14:43:31 +010056 NL80211_MCGRP_VENDOR,
Johannes Berg2a94fe42013-11-19 15:19:39 +010057 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
58};
59
60static const struct genl_multicast_group nl80211_mcgrps[] = {
61 [NL80211_MCGRP_CONFIG] = { .name = "config", },
62 [NL80211_MCGRP_SCAN] = { .name = "scan", },
63 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
64 [NL80211_MCGRP_MLME] = { .name = "mlme", },
Johannes Berg567ffc32013-12-18 14:43:31 +010065 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
Johannes Berg2a94fe42013-11-19 15:19:39 +010066#ifdef CONFIG_NL80211_TESTMODE
67 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
68#endif
69};
70
Johannes Berg89a54e42012-06-15 14:33:17 +020071/* returns ERR_PTR values */
72static struct wireless_dev *
73__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040074{
Johannes Berg89a54e42012-06-15 14:33:17 +020075 struct cfg80211_registered_device *rdev;
76 struct wireless_dev *result = NULL;
77 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
78 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
79 u64 wdev_id;
80 int wiphy_idx = -1;
81 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040082
Johannes Berg5fe231e2013-05-08 21:45:15 +020083 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040084
Johannes Berg89a54e42012-06-15 14:33:17 +020085 if (!have_ifidx && !have_wdev_id)
86 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040087
Johannes Berg89a54e42012-06-15 14:33:17 +020088 if (have_ifidx)
89 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
90 if (have_wdev_id) {
91 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
92 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040093 }
94
Johannes Berg89a54e42012-06-15 14:33:17 +020095 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
96 struct wireless_dev *wdev;
97
98 if (wiphy_net(&rdev->wiphy) != netns)
99 continue;
100
101 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
102 continue;
103
Johannes Berg89a54e42012-06-15 14:33:17 +0200104 list_for_each_entry(wdev, &rdev->wdev_list, list) {
105 if (have_ifidx && wdev->netdev &&
106 wdev->netdev->ifindex == ifidx) {
107 result = wdev;
108 break;
109 }
110 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
111 result = wdev;
112 break;
113 }
114 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200115
116 if (result)
117 break;
118 }
119
120 if (result)
121 return result;
122 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400123}
124
Johannes Berga9455402012-06-15 13:32:49 +0200125static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200126__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200127{
Johannes Berg7fee4772012-06-15 14:09:58 +0200128 struct cfg80211_registered_device *rdev = NULL, *tmp;
129 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200130
Johannes Berg5fe231e2013-05-08 21:45:15 +0200131 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200132
Johannes Berg878d9ec2012-06-15 14:18:32 +0200133 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200134 !attrs[NL80211_ATTR_IFINDEX] &&
135 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200136 return ERR_PTR(-EINVAL);
137
Johannes Berg878d9ec2012-06-15 14:18:32 +0200138 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200139 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200140 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200141
Johannes Berg89a54e42012-06-15 14:33:17 +0200142 if (attrs[NL80211_ATTR_WDEV]) {
143 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
144 struct wireless_dev *wdev;
145 bool found = false;
146
147 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
148 if (tmp) {
149 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200150 list_for_each_entry(wdev, &tmp->wdev_list, list) {
151 if (wdev->identifier != (u32)wdev_id)
152 continue;
153 found = true;
154 break;
155 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200156
157 if (!found)
158 tmp = NULL;
159
160 if (rdev && tmp != rdev)
161 return ERR_PTR(-EINVAL);
162 rdev = tmp;
163 }
164 }
165
Johannes Berg878d9ec2012-06-15 14:18:32 +0200166 if (attrs[NL80211_ATTR_IFINDEX]) {
167 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Ying Xue7f2b8562014-01-15 10:23:45 +0800168 netdev = __dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200169 if (netdev) {
170 if (netdev->ieee80211_ptr)
171 tmp = wiphy_to_dev(
172 netdev->ieee80211_ptr->wiphy);
173 else
174 tmp = NULL;
175
Johannes Berg7fee4772012-06-15 14:09:58 +0200176 /* not wireless device -- return error */
177 if (!tmp)
178 return ERR_PTR(-EINVAL);
179
180 /* mismatch -- return error */
181 if (rdev && tmp != rdev)
182 return ERR_PTR(-EINVAL);
183
184 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200185 }
Johannes Berga9455402012-06-15 13:32:49 +0200186 }
187
Johannes Berg4f7eff12012-06-15 14:14:22 +0200188 if (!rdev)
189 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200190
Johannes Berg4f7eff12012-06-15 14:14:22 +0200191 if (netns != wiphy_net(&rdev->wiphy))
192 return ERR_PTR(-ENODEV);
193
194 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200195}
196
197/*
198 * This function returns a pointer to the driver
199 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200200 *
201 * The result of this can be a PTR_ERR and hence must
202 * be checked with IS_ERR() for errors.
203 */
204static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200205cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200206{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200207 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208}
209
Johannes Berg55682962007-09-20 13:09:35 -0400210/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000211static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400212 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
213 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700214 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200215 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100216
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200217 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530218 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100219 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
220 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
222
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200223 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
224 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
226 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100227 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400228
229 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
230 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
231 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100232
Eliad Pellere007b852011-11-24 18:13:56 +0200233 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
234 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100235
Johannes Bergb9454e82009-07-08 13:29:08 +0200236 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100237 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
238 .len = WLAN_MAX_KEY_LEN },
239 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
240 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
241 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200242 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200243 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100244
245 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
246 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
247 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
248 .len = IEEE80211_MAX_DATA_LEN },
249 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100251 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
252 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
253 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
255 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100256 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100257 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200258 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100259 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800260 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300262
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700263 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
264 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
265
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300266 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
267 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
268 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200269 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
270 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100271 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300272
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800273 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700274 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700275
Johannes Berg6c739412011-11-03 09:27:01 +0100276 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200277
278 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
279 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
280 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100281 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
282 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200283
284 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_SSID_LEN },
286 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
287 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200288 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300289 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300290 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300291 [NL80211_ATTR_STA_FLAGS2] = {
292 .len = sizeof(struct nl80211_sta_flag_update),
293 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300294 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300295 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
296 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200297 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
299 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200300 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100301 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100302 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
303 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100304 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
305 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200306 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200307 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
308 .len = IEEE80211_MAX_DATA_LEN },
309 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200310 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200311 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300312 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200313 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300314 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
315 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200316 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900317 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100319 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100320 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100321 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200322 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700323 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300324 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200325 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200326 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300327 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300328 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
329 .len = IEEE80211_MAX_DATA_LEN },
330 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530332 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300333 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530334 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300335 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
336 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
337 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
339 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100340 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200341 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700343 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800344 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
346 .len = NL80211_HT_CAPABILITY_LEN
347 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100348 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530349 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530350 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200351 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700352 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300353 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000354 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700355 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100356 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
357 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530358 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
359 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200360 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
361 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100362 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100363 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
364 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
365 .len = NL80211_VHT_CAPABILITY_LEN,
366 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200367 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
368 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
369 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300370 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200371 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
372 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100379 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100380 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
381 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
382 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800383 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
384 .len = IEEE80211_QOS_MAP_LEN_MAX },
Jouni Malinen1df4a512014-01-15 00:00:47 +0200385 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
386 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +0530387 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
Johannes Berg78f22b62014-03-24 17:57:27 +0100388 [NL80211_ATTR_IFACE_SOCKET_OWNER] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400389};
390
Johannes Berge31b8212010-10-05 19:39:30 +0200391/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000392static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200393 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200394 [NL80211_KEY_IDX] = { .type = NLA_U8 },
395 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200396 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200397 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
398 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200399 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100400 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
401};
402
403/* policy for the key default flags */
404static const struct nla_policy
405nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
406 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
407 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200408};
409
Johannes Bergff1b6e62011-05-04 15:37:28 +0200410/* policy for WoWLAN attributes */
411static const struct nla_policy
412nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
413 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
414 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
416 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200417 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
418 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
420 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100421 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
422};
423
424static const struct nla_policy
425nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
426 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
427 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
428 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
429 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
430 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
431 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
433 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
434 },
435 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
436 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
437 },
438 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
439 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
440 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200441};
442
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700443/* policy for coalesce rule attributes */
444static const struct nla_policy
445nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
446 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
447 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
448 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
449};
450
Johannes Berge5497d72011-07-05 16:35:40 +0200451/* policy for GTK rekey offload attributes */
452static const struct nla_policy
453nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
454 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
455 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
456 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
457};
458
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300459static const struct nla_policy
460nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200461 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300462 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700463 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300464};
465
Johannes Berg97990a02013-04-19 01:02:55 +0200466static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
467 struct netlink_callback *cb,
468 struct cfg80211_registered_device **rdev,
469 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100470{
Johannes Berg67748892010-10-04 21:14:06 +0200471 int err;
472
Johannes Berg67748892010-10-04 21:14:06 +0200473 rtnl_lock();
474
Johannes Berg97990a02013-04-19 01:02:55 +0200475 if (!cb->args[0]) {
476 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
477 nl80211_fam.attrbuf, nl80211_fam.maxattr,
478 nl80211_policy);
479 if (err)
480 goto out_unlock;
481
482 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
483 nl80211_fam.attrbuf);
484 if (IS_ERR(*wdev)) {
485 err = PTR_ERR(*wdev);
486 goto out_unlock;
487 }
488 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200489 /* 0 is the first index - add 1 to parse only once */
490 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200491 cb->args[1] = (*wdev)->identifier;
492 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200493 /* subtract the 1 again here */
494 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200495 struct wireless_dev *tmp;
496
497 if (!wiphy) {
498 err = -ENODEV;
499 goto out_unlock;
500 }
501 *rdev = wiphy_to_dev(wiphy);
502 *wdev = NULL;
503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
505 if (tmp->identifier == cb->args[1]) {
506 *wdev = tmp;
507 break;
508 }
509 }
Johannes Berg97990a02013-04-19 01:02:55 +0200510
511 if (!*wdev) {
512 err = -ENODEV;
513 goto out_unlock;
514 }
Johannes Berg67748892010-10-04 21:14:06 +0200515 }
516
Johannes Berg67748892010-10-04 21:14:06 +0200517 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200518 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200519 rtnl_unlock();
520 return err;
521}
522
Johannes Berg97990a02013-04-19 01:02:55 +0200523static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200524{
Johannes Berg67748892010-10-04 21:14:06 +0200525 rtnl_unlock();
526}
527
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100528/* IE validation */
529static bool is_valid_ie_attr(const struct nlattr *attr)
530{
531 const u8 *pos;
532 int len;
533
534 if (!attr)
535 return true;
536
537 pos = nla_data(attr);
538 len = nla_len(attr);
539
540 while (len) {
541 u8 elemlen;
542
543 if (len < 2)
544 return false;
545 len -= 2;
546
547 elemlen = pos[1];
548 if (elemlen > len)
549 return false;
550
551 len -= elemlen;
552 pos += 2 + elemlen;
553 }
554
555 return true;
556}
557
Johannes Berg55682962007-09-20 13:09:35 -0400558/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000559static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400560 int flags, u8 cmd)
561{
562 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000563 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400564}
565
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400566static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100567 struct ieee80211_channel *chan,
568 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400569{
David S. Miller9360ffd2012-03-29 04:41:26 -0400570 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
571 chan->center_freq))
572 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400573
David S. Miller9360ffd2012-03-29 04:41:26 -0400574 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
575 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
576 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200577 if (chan->flags & IEEE80211_CHAN_NO_IR) {
578 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
579 goto nla_put_failure;
580 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
581 goto nla_put_failure;
582 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100583 if (chan->flags & IEEE80211_CHAN_RADAR) {
584 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
585 goto nla_put_failure;
586 if (large) {
587 u32 time;
588
589 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
590
591 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
592 chan->dfs_state))
593 goto nla_put_failure;
594 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
595 time))
596 goto nla_put_failure;
Janusz Dziedzic089027e2014-02-21 19:46:12 +0100597 if (nla_put_u32(msg,
598 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
599 chan->dfs_cac_ms))
600 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100601 }
602 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400603
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100604 if (large) {
605 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
606 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
607 goto nla_put_failure;
608 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
609 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
610 goto nla_put_failure;
611 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
612 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
613 goto nla_put_failure;
614 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
615 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
616 goto nla_put_failure;
David Spinadel570dbde2014-02-23 09:12:59 +0200617 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
618 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
619 goto nla_put_failure;
620 if ((chan->flags & IEEE80211_CHAN_GO_CONCURRENT) &&
621 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_GO_CONCURRENT))
622 goto nla_put_failure;
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100623 }
624
David S. Miller9360ffd2012-03-29 04:41:26 -0400625 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
626 DBM_TO_MBM(chan->max_power)))
627 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400628
629 return 0;
630
631 nla_put_failure:
632 return -ENOBUFS;
633}
634
Johannes Berg55682962007-09-20 13:09:35 -0400635/* netlink command implementations */
636
Johannes Bergb9454e82009-07-08 13:29:08 +0200637struct key_parse {
638 struct key_params p;
639 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200640 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200641 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100642 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200643};
644
645static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
646{
647 struct nlattr *tb[NL80211_KEY_MAX + 1];
648 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
649 nl80211_key_policy);
650 if (err)
651 return err;
652
653 k->def = !!tb[NL80211_KEY_DEFAULT];
654 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
655
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100656 if (k->def) {
657 k->def_uni = true;
658 k->def_multi = true;
659 }
660 if (k->defmgmt)
661 k->def_multi = true;
662
Johannes Bergb9454e82009-07-08 13:29:08 +0200663 if (tb[NL80211_KEY_IDX])
664 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
665
666 if (tb[NL80211_KEY_DATA]) {
667 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
668 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
669 }
670
671 if (tb[NL80211_KEY_SEQ]) {
672 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
673 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
674 }
675
676 if (tb[NL80211_KEY_CIPHER])
677 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
678
Johannes Berge31b8212010-10-05 19:39:30 +0200679 if (tb[NL80211_KEY_TYPE]) {
680 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
681 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
682 return -EINVAL;
683 }
684
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100685 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
686 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100687 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
688 tb[NL80211_KEY_DEFAULT_TYPES],
689 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100690 if (err)
691 return err;
692
693 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
694 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
695 }
696
Johannes Bergb9454e82009-07-08 13:29:08 +0200697 return 0;
698}
699
700static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
701{
702 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
703 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
704 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
705 }
706
707 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
708 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
709 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
710 }
711
712 if (info->attrs[NL80211_ATTR_KEY_IDX])
713 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
714
715 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
716 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
717
718 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
719 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
720
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100721 if (k->def) {
722 k->def_uni = true;
723 k->def_multi = true;
724 }
725 if (k->defmgmt)
726 k->def_multi = true;
727
Johannes Berge31b8212010-10-05 19:39:30 +0200728 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
729 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
730 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
731 return -EINVAL;
732 }
733
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
735 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
736 int err = nla_parse_nested(
737 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
738 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
739 nl80211_key_default_policy);
740 if (err)
741 return err;
742
743 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
744 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
745 }
746
Johannes Bergb9454e82009-07-08 13:29:08 +0200747 return 0;
748}
749
750static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
751{
752 int err;
753
754 memset(k, 0, sizeof(*k));
755 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200756 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200757
758 if (info->attrs[NL80211_ATTR_KEY])
759 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
760 else
761 err = nl80211_parse_key_old(info, k);
762
763 if (err)
764 return err;
765
766 if (k->def && k->defmgmt)
767 return -EINVAL;
768
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100769 if (k->defmgmt) {
770 if (k->def_uni || !k->def_multi)
771 return -EINVAL;
772 }
773
Johannes Bergb9454e82009-07-08 13:29:08 +0200774 if (k->idx != -1) {
775 if (k->defmgmt) {
776 if (k->idx < 4 || k->idx > 5)
777 return -EINVAL;
778 } else if (k->def) {
779 if (k->idx < 0 || k->idx > 3)
780 return -EINVAL;
781 } else {
782 if (k->idx < 0 || k->idx > 5)
783 return -EINVAL;
784 }
785 }
786
787 return 0;
788}
789
Johannes Bergfffd0932009-07-08 14:22:54 +0200790static struct cfg80211_cached_keys *
791nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530792 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200793{
794 struct key_parse parse;
795 struct nlattr *key;
796 struct cfg80211_cached_keys *result;
797 int rem, err, def = 0;
798
799 result = kzalloc(sizeof(*result), GFP_KERNEL);
800 if (!result)
801 return ERR_PTR(-ENOMEM);
802
803 result->def = -1;
804 result->defmgmt = -1;
805
806 nla_for_each_nested(key, keys, rem) {
807 memset(&parse, 0, sizeof(parse));
808 parse.idx = -1;
809
810 err = nl80211_parse_key_new(key, &parse);
811 if (err)
812 goto error;
813 err = -EINVAL;
814 if (!parse.p.key)
815 goto error;
816 if (parse.idx < 0 || parse.idx > 4)
817 goto error;
818 if (parse.def) {
819 if (def)
820 goto error;
821 def = 1;
822 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100823 if (!parse.def_uni || !parse.def_multi)
824 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200825 } else if (parse.defmgmt)
826 goto error;
827 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200828 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200829 if (err)
830 goto error;
831 result->params[parse.idx].cipher = parse.p.cipher;
832 result->params[parse.idx].key_len = parse.p.key_len;
833 result->params[parse.idx].key = result->data[parse.idx];
834 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530835
836 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
837 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
838 if (no_ht)
839 *no_ht = true;
840 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200841 }
842
843 return result;
844 error:
845 kfree(result);
846 return ERR_PTR(err);
847}
848
849static int nl80211_key_allowed(struct wireless_dev *wdev)
850{
851 ASSERT_WDEV_LOCK(wdev);
852
Johannes Bergfffd0932009-07-08 14:22:54 +0200853 switch (wdev->iftype) {
854 case NL80211_IFTYPE_AP:
855 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200856 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700857 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200858 break;
859 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200860 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200861 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200862 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200863 return -ENOLINK;
864 break;
865 default:
866 return -EINVAL;
867 }
868
869 return 0;
870}
871
Jouni Malinen664834d2014-01-15 00:01:44 +0200872static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
873 struct nlattr *tb)
874{
875 struct ieee80211_channel *chan;
876
877 if (tb == NULL)
878 return NULL;
879 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
880 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
881 return NULL;
882 return chan;
883}
884
Johannes Berg7527a782011-05-13 10:58:57 +0200885static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
886{
887 struct nlattr *nl_modes = nla_nest_start(msg, attr);
888 int i;
889
890 if (!nl_modes)
891 goto nla_put_failure;
892
893 i = 0;
894 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400895 if ((ifmodes & 1) && nla_put_flag(msg, i))
896 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200897 ifmodes >>= 1;
898 i++;
899 }
900
901 nla_nest_end(msg, nl_modes);
902 return 0;
903
904nla_put_failure:
905 return -ENOBUFS;
906}
907
908static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100909 struct sk_buff *msg,
910 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200911{
912 struct nlattr *nl_combis;
913 int i, j;
914
915 nl_combis = nla_nest_start(msg,
916 NL80211_ATTR_INTERFACE_COMBINATIONS);
917 if (!nl_combis)
918 goto nla_put_failure;
919
920 for (i = 0; i < wiphy->n_iface_combinations; i++) {
921 const struct ieee80211_iface_combination *c;
922 struct nlattr *nl_combi, *nl_limits;
923
924 c = &wiphy->iface_combinations[i];
925
926 nl_combi = nla_nest_start(msg, i + 1);
927 if (!nl_combi)
928 goto nla_put_failure;
929
930 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
931 if (!nl_limits)
932 goto nla_put_failure;
933
934 for (j = 0; j < c->n_limits; j++) {
935 struct nlattr *nl_limit;
936
937 nl_limit = nla_nest_start(msg, j + 1);
938 if (!nl_limit)
939 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
941 c->limits[j].max))
942 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200943 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
944 c->limits[j].types))
945 goto nla_put_failure;
946 nla_nest_end(msg, nl_limit);
947 }
948
949 nla_nest_end(msg, nl_limits);
950
David S. Miller9360ffd2012-03-29 04:41:26 -0400951 if (c->beacon_int_infra_match &&
952 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
953 goto nla_put_failure;
954 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
955 c->num_different_channels) ||
956 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
957 c->max_interfaces))
958 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100959 if (large &&
960 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
961 c->radar_detect_widths))
962 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200963
964 nla_nest_end(msg, nl_combi);
965 }
966
967 nla_nest_end(msg, nl_combis);
968
969 return 0;
970nla_put_failure:
971 return -ENOBUFS;
972}
973
Johannes Berg3713b4e2013-02-14 16:19:38 +0100974#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100975static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
976 struct sk_buff *msg)
977{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200978 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100979 struct nlattr *nl_tcp;
980
981 if (!tcp)
982 return 0;
983
984 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
985 if (!nl_tcp)
986 return -ENOBUFS;
987
988 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
989 tcp->data_payload_max))
990 return -ENOBUFS;
991
992 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
993 tcp->data_payload_max))
994 return -ENOBUFS;
995
996 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
997 return -ENOBUFS;
998
999 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1000 sizeof(*tcp->tok), tcp->tok))
1001 return -ENOBUFS;
1002
1003 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1004 tcp->data_interval_max))
1005 return -ENOBUFS;
1006
1007 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1008 tcp->wake_payload_max))
1009 return -ENOBUFS;
1010
1011 nla_nest_end(msg, nl_tcp);
1012 return 0;
1013}
1014
Johannes Berg3713b4e2013-02-14 16:19:38 +01001015static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +01001016 struct cfg80211_registered_device *dev,
1017 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001018{
1019 struct nlattr *nl_wowlan;
1020
Johannes Berg964dc9e2013-06-03 17:25:34 +02001021 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001022 return 0;
1023
1024 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1025 if (!nl_wowlan)
1026 return -ENOBUFS;
1027
Johannes Berg964dc9e2013-06-03 17:25:34 +02001028 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001029 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001030 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001031 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001032 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001033 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001034 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001035 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001036 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001037 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001038 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001039 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001040 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +02001042 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001043 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1044 return -ENOBUFS;
1045
Johannes Berg964dc9e2013-06-03 17:25:34 +02001046 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07001047 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001048 .max_patterns = dev->wiphy.wowlan->n_patterns,
1049 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1050 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1051 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001052 };
1053
1054 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1055 sizeof(pat), &pat))
1056 return -ENOBUFS;
1057 }
1058
Johannes Bergb56cf722013-02-20 01:02:38 +01001059 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1060 return -ENOBUFS;
1061
Johannes Berg3713b4e2013-02-14 16:19:38 +01001062 nla_nest_end(msg, nl_wowlan);
1063
1064 return 0;
1065}
1066#endif
1067
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001068static int nl80211_send_coalesce(struct sk_buff *msg,
1069 struct cfg80211_registered_device *dev)
1070{
1071 struct nl80211_coalesce_rule_support rule;
1072
1073 if (!dev->wiphy.coalesce)
1074 return 0;
1075
1076 rule.max_rules = dev->wiphy.coalesce->n_rules;
1077 rule.max_delay = dev->wiphy.coalesce->max_delay;
1078 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1079 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1080 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1081 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1082
1083 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1084 return -ENOBUFS;
1085
1086 return 0;
1087}
1088
Johannes Berg3713b4e2013-02-14 16:19:38 +01001089static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1090 struct ieee80211_supported_band *sband)
1091{
1092 struct nlattr *nl_rates, *nl_rate;
1093 struct ieee80211_rate *rate;
1094 int i;
1095
1096 /* add HT info */
1097 if (sband->ht_cap.ht_supported &&
1098 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1099 sizeof(sband->ht_cap.mcs),
1100 &sband->ht_cap.mcs) ||
1101 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1102 sband->ht_cap.cap) ||
1103 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1104 sband->ht_cap.ampdu_factor) ||
1105 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1106 sband->ht_cap.ampdu_density)))
1107 return -ENOBUFS;
1108
1109 /* add VHT info */
1110 if (sband->vht_cap.vht_supported &&
1111 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1112 sizeof(sband->vht_cap.vht_mcs),
1113 &sband->vht_cap.vht_mcs) ||
1114 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1115 sband->vht_cap.cap)))
1116 return -ENOBUFS;
1117
1118 /* add bitrates */
1119 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1120 if (!nl_rates)
1121 return -ENOBUFS;
1122
1123 for (i = 0; i < sband->n_bitrates; i++) {
1124 nl_rate = nla_nest_start(msg, i);
1125 if (!nl_rate)
1126 return -ENOBUFS;
1127
1128 rate = &sband->bitrates[i];
1129 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1130 rate->bitrate))
1131 return -ENOBUFS;
1132 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1133 nla_put_flag(msg,
1134 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1135 return -ENOBUFS;
1136
1137 nla_nest_end(msg, nl_rate);
1138 }
1139
1140 nla_nest_end(msg, nl_rates);
1141
1142 return 0;
1143}
1144
1145static int
1146nl80211_send_mgmt_stypes(struct sk_buff *msg,
1147 const struct ieee80211_txrx_stypes *mgmt_stypes)
1148{
1149 u16 stypes;
1150 struct nlattr *nl_ftypes, *nl_ifs;
1151 enum nl80211_iftype ift;
1152 int i;
1153
1154 if (!mgmt_stypes)
1155 return 0;
1156
1157 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1158 if (!nl_ifs)
1159 return -ENOBUFS;
1160
1161 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1162 nl_ftypes = nla_nest_start(msg, ift);
1163 if (!nl_ftypes)
1164 return -ENOBUFS;
1165 i = 0;
1166 stypes = mgmt_stypes[ift].tx;
1167 while (stypes) {
1168 if ((stypes & 1) &&
1169 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1170 (i << 4) | IEEE80211_FTYPE_MGMT))
1171 return -ENOBUFS;
1172 stypes >>= 1;
1173 i++;
1174 }
1175 nla_nest_end(msg, nl_ftypes);
1176 }
1177
1178 nla_nest_end(msg, nl_ifs);
1179
1180 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1181 if (!nl_ifs)
1182 return -ENOBUFS;
1183
1184 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1185 nl_ftypes = nla_nest_start(msg, ift);
1186 if (!nl_ftypes)
1187 return -ENOBUFS;
1188 i = 0;
1189 stypes = mgmt_stypes[ift].rx;
1190 while (stypes) {
1191 if ((stypes & 1) &&
1192 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1193 (i << 4) | IEEE80211_FTYPE_MGMT))
1194 return -ENOBUFS;
1195 stypes >>= 1;
1196 i++;
1197 }
1198 nla_nest_end(msg, nl_ftypes);
1199 }
1200 nla_nest_end(msg, nl_ifs);
1201
1202 return 0;
1203}
1204
Johannes Berg86e8cf92013-06-19 10:57:22 +02001205struct nl80211_dump_wiphy_state {
1206 s64 filter_wiphy;
1207 long start;
1208 long split_start, band_start, chan_start;
1209 bool split;
1210};
1211
Johannes Berg3713b4e2013-02-14 16:19:38 +01001212static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1213 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001214 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001215{
1216 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001217 struct nlattr *nl_bands, *nl_band;
1218 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001219 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001220 enum ieee80211_band band;
1221 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001222 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001223 const struct ieee80211_txrx_stypes *mgmt_stypes =
1224 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001225 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001226
Eric W. Biederman15e47302012-09-07 20:12:54 +00001227 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001228 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001229 return -ENOBUFS;
1230
Johannes Berg86e8cf92013-06-19 10:57:22 +02001231 if (WARN_ON(!state))
1232 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001233
David S. Miller9360ffd2012-03-29 04:41:26 -04001234 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001235 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1236 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001237 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001238 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001239 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001240
Johannes Berg86e8cf92013-06-19 10:57:22 +02001241 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001242 case 0:
1243 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1244 dev->wiphy.retry_short) ||
1245 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1246 dev->wiphy.retry_long) ||
1247 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1248 dev->wiphy.frag_threshold) ||
1249 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1250 dev->wiphy.rts_threshold) ||
1251 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1252 dev->wiphy.coverage_class) ||
1253 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1254 dev->wiphy.max_scan_ssids) ||
1255 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1256 dev->wiphy.max_sched_scan_ssids) ||
1257 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1258 dev->wiphy.max_scan_ie_len) ||
1259 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1260 dev->wiphy.max_sched_scan_ie_len) ||
1261 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1262 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001263 goto nla_put_failure;
1264
Johannes Berg3713b4e2013-02-14 16:19:38 +01001265 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1266 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1267 goto nla_put_failure;
1268 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1269 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1270 goto nla_put_failure;
1271 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1272 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1273 goto nla_put_failure;
1274 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1275 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1276 goto nla_put_failure;
1277 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1278 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1279 goto nla_put_failure;
1280 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1281 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001282 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001283 state->split_start++;
1284 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001285 break;
1286 case 1:
1287 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1288 sizeof(u32) * dev->wiphy.n_cipher_suites,
1289 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001290 goto nla_put_failure;
1291
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1293 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001294 goto nla_put_failure;
1295
Johannes Berg3713b4e2013-02-14 16:19:38 +01001296 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1297 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1298 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001299
Johannes Berg3713b4e2013-02-14 16:19:38 +01001300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1301 dev->wiphy.available_antennas_tx) ||
1302 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1303 dev->wiphy.available_antennas_rx))
1304 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001305
Johannes Berg3713b4e2013-02-14 16:19:38 +01001306 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1307 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1308 dev->wiphy.probe_resp_offload))
1309 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001310
Johannes Berg3713b4e2013-02-14 16:19:38 +01001311 if ((dev->wiphy.available_antennas_tx ||
1312 dev->wiphy.available_antennas_rx) &&
1313 dev->ops->get_antenna) {
1314 u32 tx_ant = 0, rx_ant = 0;
1315 int res;
1316 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1317 if (!res) {
1318 if (nla_put_u32(msg,
1319 NL80211_ATTR_WIPHY_ANTENNA_TX,
1320 tx_ant) ||
1321 nla_put_u32(msg,
1322 NL80211_ATTR_WIPHY_ANTENNA_RX,
1323 rx_ant))
1324 goto nla_put_failure;
1325 }
Johannes Bergee688b002008-01-24 19:38:39 +01001326 }
1327
Johannes Berg86e8cf92013-06-19 10:57:22 +02001328 state->split_start++;
1329 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330 break;
1331 case 2:
1332 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1333 dev->wiphy.interface_modes))
1334 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001335 state->split_start++;
1336 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001337 break;
1338 case 3:
1339 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1340 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001341 goto nla_put_failure;
1342
Johannes Berg86e8cf92013-06-19 10:57:22 +02001343 for (band = state->band_start;
1344 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001345 struct ieee80211_supported_band *sband;
1346
1347 sband = dev->wiphy.bands[band];
1348
1349 if (!sband)
1350 continue;
1351
1352 nl_band = nla_nest_start(msg, band);
1353 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001354 goto nla_put_failure;
1355
Johannes Berg86e8cf92013-06-19 10:57:22 +02001356 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001357 case 0:
1358 if (nl80211_send_band_rateinfo(msg, sband))
1359 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001360 state->chan_start++;
1361 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001362 break;
1363 default:
1364 /* add frequencies */
1365 nl_freqs = nla_nest_start(
1366 msg, NL80211_BAND_ATTR_FREQS);
1367 if (!nl_freqs)
1368 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001369
Johannes Berg86e8cf92013-06-19 10:57:22 +02001370 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001371 i < sband->n_channels;
1372 i++) {
1373 nl_freq = nla_nest_start(msg, i);
1374 if (!nl_freq)
1375 goto nla_put_failure;
1376
1377 chan = &sband->channels[i];
1378
Johannes Berg86e8cf92013-06-19 10:57:22 +02001379 if (nl80211_msg_put_channel(
1380 msg, chan,
1381 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001382 goto nla_put_failure;
1383
1384 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001385 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001386 break;
1387 }
1388 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001389 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001390 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001391 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001392 nla_nest_end(msg, nl_freqs);
1393 }
1394
1395 nla_nest_end(msg, nl_band);
1396
Johannes Berg86e8cf92013-06-19 10:57:22 +02001397 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001398 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001399 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001400 band--;
1401 break;
1402 }
Johannes Bergee688b002008-01-24 19:38:39 +01001403 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001404 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001405
Johannes Berg3713b4e2013-02-14 16:19:38 +01001406 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001407 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001408 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001409 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001410
Johannes Berg3713b4e2013-02-14 16:19:38 +01001411 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001412 if (state->band_start == 0 && state->chan_start == 0)
1413 state->split_start++;
1414 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001415 break;
1416 case 4:
1417 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1418 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001419 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001420
1421 i = 0;
1422#define CMD(op, n) \
1423 do { \
1424 if (dev->ops->op) { \
1425 i++; \
1426 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1427 goto nla_put_failure; \
1428 } \
1429 } while (0)
1430
1431 CMD(add_virtual_intf, NEW_INTERFACE);
1432 CMD(change_virtual_intf, SET_INTERFACE);
1433 CMD(add_key, NEW_KEY);
1434 CMD(start_ap, START_AP);
1435 CMD(add_station, NEW_STATION);
1436 CMD(add_mpath, NEW_MPATH);
1437 CMD(update_mesh_config, SET_MESH_CONFIG);
1438 CMD(change_bss, SET_BSS);
1439 CMD(auth, AUTHENTICATE);
1440 CMD(assoc, ASSOCIATE);
1441 CMD(deauth, DEAUTHENTICATE);
1442 CMD(disassoc, DISASSOCIATE);
1443 CMD(join_ibss, JOIN_IBSS);
1444 CMD(join_mesh, JOIN_MESH);
1445 CMD(set_pmksa, SET_PMKSA);
1446 CMD(del_pmksa, DEL_PMKSA);
1447 CMD(flush_pmksa, FLUSH_PMKSA);
1448 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1449 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1450 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1451 CMD(mgmt_tx, FRAME);
1452 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1453 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1454 i++;
1455 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1456 goto nla_put_failure;
1457 }
1458 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1459 dev->ops->join_mesh) {
1460 i++;
1461 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1462 goto nla_put_failure;
1463 }
1464 CMD(set_wds_peer, SET_WDS_PEER);
1465 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1466 CMD(tdls_mgmt, TDLS_MGMT);
1467 CMD(tdls_oper, TDLS_OPER);
1468 }
1469 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1470 CMD(sched_scan_start, START_SCHED_SCAN);
1471 CMD(probe_client, PROBE_CLIENT);
1472 CMD(set_noack_map, SET_NOACK_MAP);
1473 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1474 i++;
1475 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1476 goto nla_put_failure;
1477 }
1478 CMD(start_p2p_device, START_P2P_DEVICE);
1479 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001480 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001481 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1482 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001483 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1484 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001485 }
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001486 CMD(set_qos_map, SET_QOS_MAP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001487
Kalle Valo4745fc02011-11-17 19:06:10 +02001488#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001490#endif
1491
Johannes Berg8fdc6212009-03-14 09:34:01 +01001492#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001493
Johannes Berg3713b4e2013-02-14 16:19:38 +01001494 if (dev->ops->connect || dev->ops->auth) {
1495 i++;
1496 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001497 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001498 }
1499
Johannes Berg3713b4e2013-02-14 16:19:38 +01001500 if (dev->ops->disconnect || dev->ops->deauth) {
1501 i++;
1502 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1503 goto nla_put_failure;
1504 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001505
Johannes Berg3713b4e2013-02-14 16:19:38 +01001506 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001507 state->split_start++;
1508 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001509 break;
1510 case 5:
1511 if (dev->ops->remain_on_channel &&
1512 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1513 nla_put_u32(msg,
1514 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1515 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001516 goto nla_put_failure;
1517
Johannes Berg3713b4e2013-02-14 16:19:38 +01001518 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1519 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1520 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001521
Johannes Berg3713b4e2013-02-14 16:19:38 +01001522 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1523 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001524 state->split_start++;
1525 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001526 break;
1527 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001528#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001529 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001530 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001531 state->split_start++;
1532 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001533 break;
1534#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001535 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001536#endif
1537 case 7:
1538 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1539 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001540 goto nla_put_failure;
1541
Johannes Berg86e8cf92013-06-19 10:57:22 +02001542 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1543 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001544 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001545
Johannes Berg86e8cf92013-06-19 10:57:22 +02001546 state->split_start++;
1547 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001548 break;
1549 case 8:
1550 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1551 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1552 dev->wiphy.ap_sme_capa))
1553 goto nla_put_failure;
1554
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001555 features = dev->wiphy.features;
1556 /*
1557 * We can only add the per-channel limit information if the
1558 * dump is split, otherwise it makes it too big. Therefore
1559 * only advertise it in that case.
1560 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001561 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001562 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1563 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001564 goto nla_put_failure;
1565
1566 if (dev->wiphy.ht_capa_mod_mask &&
1567 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1568 sizeof(*dev->wiphy.ht_capa_mod_mask),
1569 dev->wiphy.ht_capa_mod_mask))
1570 goto nla_put_failure;
1571
1572 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1573 dev->wiphy.max_acl_mac_addrs &&
1574 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1575 dev->wiphy.max_acl_mac_addrs))
1576 goto nla_put_failure;
1577
1578 /*
1579 * Any information below this point is only available to
1580 * applications that can deal with it being split. This
1581 * helps ensure that newly added capabilities don't break
1582 * older tools by overrunning their buffers.
1583 *
1584 * We still increment split_start so that in the split
1585 * case we'll continue with more data in the next round,
1586 * but break unconditionally so unsplit data stops here.
1587 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001588 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001589 break;
1590 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001591 if (dev->wiphy.extended_capabilities &&
1592 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1593 dev->wiphy.extended_capabilities_len,
1594 dev->wiphy.extended_capabilities) ||
1595 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1596 dev->wiphy.extended_capabilities_len,
1597 dev->wiphy.extended_capabilities_mask)))
1598 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001599
Johannes Bergee2aca32013-02-21 17:36:01 +01001600 if (dev->wiphy.vht_capa_mod_mask &&
1601 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1602 sizeof(*dev->wiphy.vht_capa_mod_mask),
1603 dev->wiphy.vht_capa_mod_mask))
1604 goto nla_put_failure;
1605
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001606 state->split_start++;
1607 break;
1608 case 10:
1609 if (nl80211_send_coalesce(msg, dev))
1610 goto nla_put_failure;
1611
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001612 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1613 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1614 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1615 goto nla_put_failure;
Jouni Malinenb43504c2014-01-15 00:01:08 +02001616
1617 if (dev->wiphy.max_ap_assoc_sta &&
1618 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1619 dev->wiphy.max_ap_assoc_sta))
1620 goto nla_put_failure;
1621
Johannes Bergad7e7182013-11-13 13:37:47 +01001622 state->split_start++;
1623 break;
1624 case 11:
Johannes Berg567ffc32013-12-18 14:43:31 +01001625 if (dev->wiphy.n_vendor_commands) {
1626 const struct nl80211_vendor_cmd_info *info;
1627 struct nlattr *nested;
Johannes Bergad7e7182013-11-13 13:37:47 +01001628
Johannes Berg567ffc32013-12-18 14:43:31 +01001629 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1630 if (!nested)
Johannes Bergad7e7182013-11-13 13:37:47 +01001631 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01001632
1633 for (i = 0; i < dev->wiphy.n_vendor_commands; i++) {
1634 info = &dev->wiphy.vendor_commands[i].info;
1635 if (nla_put(msg, i + 1, sizeof(*info), info))
1636 goto nla_put_failure;
1637 }
1638 nla_nest_end(msg, nested);
1639 }
1640
1641 if (dev->wiphy.n_vendor_events) {
1642 const struct nl80211_vendor_cmd_info *info;
1643 struct nlattr *nested;
1644
1645 nested = nla_nest_start(msg,
1646 NL80211_ATTR_VENDOR_EVENTS);
1647 if (!nested)
1648 goto nla_put_failure;
1649
1650 for (i = 0; i < dev->wiphy.n_vendor_events; i++) {
1651 info = &dev->wiphy.vendor_events[i];
1652 if (nla_put(msg, i + 1, sizeof(*info), info))
1653 goto nla_put_failure;
1654 }
1655 nla_nest_end(msg, nested);
1656 }
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001657
Johannes Berg3713b4e2013-02-14 16:19:38 +01001658 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001659 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001660 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001661 }
Johannes Berg55682962007-09-20 13:09:35 -04001662 return genlmsg_end(msg, hdr);
1663
1664 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001665 genlmsg_cancel(msg, hdr);
1666 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001667}
1668
Johannes Berg86e8cf92013-06-19 10:57:22 +02001669static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1670 struct netlink_callback *cb,
1671 struct nl80211_dump_wiphy_state *state)
1672{
1673 struct nlattr **tb = nl80211_fam.attrbuf;
1674 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1675 tb, nl80211_fam.maxattr, nl80211_policy);
1676 /* ignore parse errors for backward compatibility */
1677 if (ret)
1678 return 0;
1679
1680 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1681 if (tb[NL80211_ATTR_WIPHY])
1682 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1683 if (tb[NL80211_ATTR_WDEV])
1684 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1685 if (tb[NL80211_ATTR_IFINDEX]) {
1686 struct net_device *netdev;
1687 struct cfg80211_registered_device *rdev;
1688 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1689
Ying Xue7f2b8562014-01-15 10:23:45 +08001690 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001691 if (!netdev)
1692 return -ENODEV;
1693 if (netdev->ieee80211_ptr) {
1694 rdev = wiphy_to_dev(
1695 netdev->ieee80211_ptr->wiphy);
1696 state->filter_wiphy = rdev->wiphy_idx;
1697 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001698 }
1699
1700 return 0;
1701}
1702
Johannes Berg55682962007-09-20 13:09:35 -04001703static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1704{
Johannes Berg645e77d2013-03-01 14:03:49 +01001705 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001706 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001707 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001708
Johannes Berg5fe231e2013-05-08 21:45:15 +02001709 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001710 if (!state) {
1711 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001712 if (!state) {
1713 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001714 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716 state->filter_wiphy = -1;
1717 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1718 if (ret) {
1719 kfree(state);
1720 rtnl_unlock();
1721 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001722 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001723 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001724 }
1725
Johannes Berg79c97e92009-07-07 03:56:12 +02001726 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001727 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1728 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001729 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001730 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001731 if (state->filter_wiphy != -1 &&
1732 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001733 continue;
1734 /* attempt to fit multiple wiphy data chunks into the skb */
1735 do {
1736 ret = nl80211_send_wiphy(dev, skb,
1737 NETLINK_CB(cb->skb).portid,
1738 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001739 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001740 if (ret < 0) {
1741 /*
1742 * If sending the wiphy data didn't fit (ENOBUFS
1743 * or EMSGSIZE returned), this SKB is still
1744 * empty (so it's not too big because another
1745 * wiphy dataset is already in the skb) and
1746 * we've not tried to adjust the dump allocation
1747 * yet ... then adjust the alloc size to be
1748 * bigger, and return 1 but with the empty skb.
1749 * This results in an empty message being RX'ed
1750 * in userspace, but that is ignored.
1751 *
1752 * We can then retry with the larger buffer.
1753 */
1754 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001755 !skb->len && !state->split &&
Johannes Berg3713b4e2013-02-14 16:19:38 +01001756 cb->min_dump_alloc < 4096) {
1757 cb->min_dump_alloc = 4096;
Pontus Fuchsf12cb282014-01-16 15:00:40 +01001758 state->split_start = 0;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001759 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001760 return 1;
1761 }
1762 idx--;
1763 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001764 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001765 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001766 break;
Johannes Berg55682962007-09-20 13:09:35 -04001767 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001768 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001769
Johannes Berg86e8cf92013-06-19 10:57:22 +02001770 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001771
1772 return skb->len;
1773}
1774
Johannes Berg86e8cf92013-06-19 10:57:22 +02001775static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1776{
1777 kfree((void *)cb->args[0]);
1778 return 0;
1779}
1780
Johannes Berg55682962007-09-20 13:09:35 -04001781static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1782{
1783 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001784 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001785 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001786
Johannes Berg645e77d2013-03-01 14:03:49 +01001787 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001788 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001789 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001790
Johannes Berg3713b4e2013-02-14 16:19:38 +01001791 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001792 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001793 nlmsg_free(msg);
1794 return -ENOBUFS;
1795 }
Johannes Berg55682962007-09-20 13:09:35 -04001796
Johannes Berg134e6372009-07-10 09:51:34 +00001797 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001798}
1799
Jouni Malinen31888482008-10-30 16:59:24 +02001800static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1801 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1802 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1803 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1804 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1805 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1806};
1807
1808static int parse_txq_params(struct nlattr *tb[],
1809 struct ieee80211_txq_params *txq_params)
1810{
Johannes Berga3304b02012-03-28 11:04:24 +02001811 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001812 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1813 !tb[NL80211_TXQ_ATTR_AIFS])
1814 return -EINVAL;
1815
Johannes Berga3304b02012-03-28 11:04:24 +02001816 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001817 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1818 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1819 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1820 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1821
Johannes Berga3304b02012-03-28 11:04:24 +02001822 if (txq_params->ac >= NL80211_NUM_ACS)
1823 return -EINVAL;
1824
Jouni Malinen31888482008-10-30 16:59:24 +02001825 return 0;
1826}
1827
Johannes Bergf444de02010-05-05 15:25:02 +02001828static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1829{
1830 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001831 * You can only set the channel explicitly for WDS interfaces,
1832 * all others have their channel managed via their respective
1833 * "establish a connection" command (connect, join, ...)
1834 *
1835 * For AP/GO and mesh mode, the channel can be set with the
1836 * channel userspace API, but is only stored and passed to the
1837 * low-level driver when the AP starts or the mesh is joined.
1838 * This is for backward compatibility, userspace can also give
1839 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001840 *
1841 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001842 * whatever else is going on, so they have their own special
1843 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001844 */
1845 return !wdev ||
1846 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001847 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001848 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1849 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001850}
1851
Johannes Berg683b6d32012-11-08 21:25:48 +01001852static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1853 struct genl_info *info,
1854 struct cfg80211_chan_def *chandef)
1855{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301856 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001857
1858 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1859 return -EINVAL;
1860
1861 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1862
1863 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001864 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1865 chandef->center_freq1 = control_freq;
1866 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001867
1868 /* Primary channel not allowed */
1869 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1870 return -EINVAL;
1871
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001872 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1873 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001874
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001875 chantype = nla_get_u32(
1876 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1877
1878 switch (chantype) {
1879 case NL80211_CHAN_NO_HT:
1880 case NL80211_CHAN_HT20:
1881 case NL80211_CHAN_HT40PLUS:
1882 case NL80211_CHAN_HT40MINUS:
1883 cfg80211_chandef_create(chandef, chandef->chan,
1884 chantype);
1885 break;
1886 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001887 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001888 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001889 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1890 chandef->width =
1891 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1892 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1893 chandef->center_freq1 =
1894 nla_get_u32(
1895 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1896 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1897 chandef->center_freq2 =
1898 nla_get_u32(
1899 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1900 }
1901
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001902 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001903 return -EINVAL;
1904
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001905 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1906 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001907 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001908
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001909 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1910 chandef->width == NL80211_CHAN_WIDTH_10) &&
1911 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1912 return -EINVAL;
1913
Johannes Berg683b6d32012-11-08 21:25:48 +01001914 return 0;
1915}
1916
Johannes Bergf444de02010-05-05 15:25:02 +02001917static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1918 struct wireless_dev *wdev,
1919 struct genl_info *info)
1920{
Johannes Berg683b6d32012-11-08 21:25:48 +01001921 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001922 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001923 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1924
1925 if (wdev)
1926 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001927
Johannes Bergf444de02010-05-05 15:25:02 +02001928 if (!nl80211_can_set_dev_channel(wdev))
1929 return -EOPNOTSUPP;
1930
Johannes Berg683b6d32012-11-08 21:25:48 +01001931 result = nl80211_parse_chandef(rdev, info, &chandef);
1932 if (result)
1933 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001934
Johannes Berge8c9bd52012-06-06 08:18:22 +02001935 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001936 case NL80211_IFTYPE_AP:
1937 case NL80211_IFTYPE_P2P_GO:
1938 if (wdev->beacon_interval) {
1939 result = -EBUSY;
1940 break;
1941 }
Ilan Peer174e0cd2014-02-23 09:13:01 +02001942 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, iftype)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001943 result = -EINVAL;
1944 break;
1945 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001946 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001947 result = 0;
1948 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001949 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001950 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001951 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001952 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001953 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001954 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001955 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001956 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001957 }
Johannes Bergf444de02010-05-05 15:25:02 +02001958
1959 return result;
1960}
1961
1962static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1963{
Johannes Berg4c476992010-10-04 21:36:35 +02001964 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1965 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001966
Johannes Berg4c476992010-10-04 21:36:35 +02001967 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001968}
1969
Bill Jordane8347eb2010-10-01 13:54:28 -04001970static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1971{
Johannes Berg43b19952010-10-07 13:10:30 +02001972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1973 struct net_device *dev = info->user_ptr[1];
1974 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001975 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001976
1977 if (!info->attrs[NL80211_ATTR_MAC])
1978 return -EINVAL;
1979
Johannes Berg43b19952010-10-07 13:10:30 +02001980 if (netif_running(dev))
1981 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001982
Johannes Berg43b19952010-10-07 13:10:30 +02001983 if (!rdev->ops->set_wds_peer)
1984 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001985
Johannes Berg43b19952010-10-07 13:10:30 +02001986 if (wdev->iftype != NL80211_IFTYPE_WDS)
1987 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001988
1989 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001990 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001991}
1992
1993
Johannes Berg55682962007-09-20 13:09:35 -04001994static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1995{
1996 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001997 struct net_device *netdev = NULL;
1998 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001999 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02002000 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002001 u32 changed;
2002 u8 retry_short = 0, retry_long = 0;
2003 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01002004 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04002005
Johannes Berg5fe231e2013-05-08 21:45:15 +02002006 ASSERT_RTNL();
2007
Johannes Bergf444de02010-05-05 15:25:02 +02002008 /*
2009 * Try to find the wiphy and netdev. Normally this
2010 * function shouldn't need the netdev, but this is
2011 * done for backward compatibility -- previously
2012 * setting the channel was done per wiphy, but now
2013 * it is per netdev. Previous userland like hostapd
2014 * also passed a netdev to set_wiphy, so that it is
2015 * possible to let that go to the right netdev!
2016 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002017
Johannes Bergf444de02010-05-05 15:25:02 +02002018 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2019 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2020
Ying Xue7f2b8562014-01-15 10:23:45 +08002021 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002022 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02002023 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002024 else
Johannes Bergf444de02010-05-05 15:25:02 +02002025 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002026 }
2027
Johannes Bergf444de02010-05-05 15:25:02 +02002028 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02002029 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2030 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02002031 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02002032 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02002033 wdev = NULL;
2034 netdev = NULL;
2035 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02002036 } else
Johannes Bergf444de02010-05-05 15:25:02 +02002037 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02002038
2039 /*
2040 * end workaround code, by now the rdev is available
2041 * and locked, and wdev may or may not be NULL.
2042 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002043
2044 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02002045 result = cfg80211_dev_rename(
2046 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002047
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002048 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002049 return result;
Johannes Berg55682962007-09-20 13:09:35 -04002050
Jouni Malinen31888482008-10-30 16:59:24 +02002051 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2052 struct ieee80211_txq_params txq_params;
2053 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2054
Ying Xue7f2b8562014-01-15 10:23:45 +08002055 if (!rdev->ops->set_txq_params)
2056 return -EOPNOTSUPP;
Jouni Malinen31888482008-10-30 16:59:24 +02002057
Ying Xue7f2b8562014-01-15 10:23:45 +08002058 if (!netdev)
2059 return -EINVAL;
Eliad Pellerf70f01c2011-09-25 20:06:53 +03002060
Johannes Berg133a3ff2011-11-03 14:50:13 +01002061 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002062 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2063 return -EINVAL;
Johannes Berg133a3ff2011-11-03 14:50:13 +01002064
Ying Xue7f2b8562014-01-15 10:23:45 +08002065 if (!netif_running(netdev))
2066 return -ENETDOWN;
Johannes Berg2b5f8b02012-04-02 10:51:55 +02002067
Jouni Malinen31888482008-10-30 16:59:24 +02002068 nla_for_each_nested(nl_txq_params,
2069 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2070 rem_txq_params) {
Johannes Bergae811e22014-01-24 10:17:47 +01002071 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2072 nla_data(nl_txq_params),
2073 nla_len(nl_txq_params),
2074 txq_params_policy);
2075 if (result)
2076 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002077 result = parse_txq_params(tb, &txq_params);
2078 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002079 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002080
Hila Gonene35e4d22012-06-27 17:19:42 +03002081 result = rdev_set_txq_params(rdev, netdev,
2082 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002083 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002084 return result;
Jouni Malinen31888482008-10-30 16:59:24 +02002085 }
2086 }
2087
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002088 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002089 result = __nl80211_set_channel(rdev,
2090 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2091 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002092 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002093 return result;
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002094 }
2095
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002096 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002097 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002098 enum nl80211_tx_power_setting type;
2099 int idx, mbm = 0;
2100
Johannes Bergc8442112012-10-24 10:17:18 +02002101 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2102 txp_wdev = NULL;
2103
Ying Xue7f2b8562014-01-15 10:23:45 +08002104 if (!rdev->ops->set_tx_power)
2105 return -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002106
2107 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2108 type = nla_get_u32(info->attrs[idx]);
2109
2110 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
Ying Xue7f2b8562014-01-15 10:23:45 +08002111 (type != NL80211_TX_POWER_AUTOMATIC))
2112 return -EINVAL;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002113
2114 if (type != NL80211_TX_POWER_AUTOMATIC) {
2115 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2116 mbm = nla_get_u32(info->attrs[idx]);
2117 }
2118
Johannes Bergc8442112012-10-24 10:17:18 +02002119 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002120 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002121 return result;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002122 }
2123
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002124 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2125 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2126 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002127 if ((!rdev->wiphy.available_antennas_tx &&
2128 !rdev->wiphy.available_antennas_rx) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002129 !rdev->ops->set_antenna)
2130 return -EOPNOTSUPP;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002131
2132 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2133 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2134
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002135 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002136 * available antenna masks, except for the "all" mask */
2137 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
Ying Xue7f2b8562014-01-15 10:23:45 +08002138 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2139 return -EINVAL;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002140
Bruno Randolf7f531e02010-12-16 11:30:22 +09002141 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2142 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002143
Hila Gonene35e4d22012-06-27 17:19:42 +03002144 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002145 if (result)
Ying Xue7f2b8562014-01-15 10:23:45 +08002146 return result;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002147 }
2148
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002149 changed = 0;
2150
2151 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2152 retry_short = nla_get_u8(
2153 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002154 if (retry_short == 0)
2155 return -EINVAL;
2156
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002157 changed |= WIPHY_PARAM_RETRY_SHORT;
2158 }
2159
2160 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2161 retry_long = nla_get_u8(
2162 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002163 if (retry_long == 0)
2164 return -EINVAL;
2165
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002166 changed |= WIPHY_PARAM_RETRY_LONG;
2167 }
2168
2169 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2170 frag_threshold = nla_get_u32(
2171 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
Ying Xue7f2b8562014-01-15 10:23:45 +08002172 if (frag_threshold < 256)
2173 return -EINVAL;
2174
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002175 if (frag_threshold != (u32) -1) {
2176 /*
2177 * Fragments (apart from the last one) are required to
2178 * have even length. Make the fragmentation code
2179 * simpler by stripping LSB should someone try to use
2180 * odd threshold value.
2181 */
2182 frag_threshold &= ~0x1;
2183 }
2184 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2185 }
2186
2187 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2188 rts_threshold = nla_get_u32(
2189 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2190 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2191 }
2192
Lukáš Turek81077e82009-12-21 22:50:47 +01002193 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2194 coverage_class = nla_get_u8(
2195 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2196 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2197 }
2198
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002199 if (changed) {
2200 u8 old_retry_short, old_retry_long;
2201 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002202 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002203
Ying Xue7f2b8562014-01-15 10:23:45 +08002204 if (!rdev->ops->set_wiphy_params)
2205 return -EOPNOTSUPP;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002206
2207 old_retry_short = rdev->wiphy.retry_short;
2208 old_retry_long = rdev->wiphy.retry_long;
2209 old_frag_threshold = rdev->wiphy.frag_threshold;
2210 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002211 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002212
2213 if (changed & WIPHY_PARAM_RETRY_SHORT)
2214 rdev->wiphy.retry_short = retry_short;
2215 if (changed & WIPHY_PARAM_RETRY_LONG)
2216 rdev->wiphy.retry_long = retry_long;
2217 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2218 rdev->wiphy.frag_threshold = frag_threshold;
2219 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2220 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002221 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2222 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002223
Hila Gonene35e4d22012-06-27 17:19:42 +03002224 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002225 if (result) {
2226 rdev->wiphy.retry_short = old_retry_short;
2227 rdev->wiphy.retry_long = old_retry_long;
2228 rdev->wiphy.frag_threshold = old_frag_threshold;
2229 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002230 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002231 }
2232 }
Ying Xue7f2b8562014-01-15 10:23:45 +08002233 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002234}
2235
Johannes Berg71bbc992012-06-15 15:30:18 +02002236static inline u64 wdev_id(struct wireless_dev *wdev)
2237{
2238 return (u64)wdev->identifier |
2239 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2240}
Johannes Berg55682962007-09-20 13:09:35 -04002241
Johannes Berg683b6d32012-11-08 21:25:48 +01002242static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002243 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002244{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002245 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002246
Johannes Berg683b6d32012-11-08 21:25:48 +01002247 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2248 chandef->chan->center_freq))
2249 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002250 switch (chandef->width) {
2251 case NL80211_CHAN_WIDTH_20_NOHT:
2252 case NL80211_CHAN_WIDTH_20:
2253 case NL80211_CHAN_WIDTH_40:
2254 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2255 cfg80211_get_chandef_type(chandef)))
2256 return -ENOBUFS;
2257 break;
2258 default:
2259 break;
2260 }
2261 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2262 return -ENOBUFS;
2263 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2264 return -ENOBUFS;
2265 if (chandef->center_freq2 &&
2266 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002267 return -ENOBUFS;
2268 return 0;
2269}
2270
Eric W. Biederman15e47302012-09-07 20:12:54 +00002271static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002272 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002273 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002274{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002275 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002276 void *hdr;
2277
Eric W. Biederman15e47302012-09-07 20:12:54 +00002278 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002279 if (!hdr)
2280 return -1;
2281
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002282 if (dev &&
2283 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002284 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002285 goto nla_put_failure;
2286
2287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2288 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002289 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002290 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002291 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2292 rdev->devlist_generation ^
2293 (cfg80211_rdev_list_generation << 2)))
2294 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002295
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002296 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002297 int ret;
2298 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002299
Johannes Berg683b6d32012-11-08 21:25:48 +01002300 ret = rdev_get_channel(rdev, wdev, &chandef);
2301 if (ret == 0) {
2302 if (nl80211_send_chandef(msg, &chandef))
2303 goto nla_put_failure;
2304 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002305 }
2306
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002307 if (wdev->ssid_len) {
2308 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2309 goto nla_put_failure;
2310 }
2311
Johannes Berg55682962007-09-20 13:09:35 -04002312 return genlmsg_end(msg, hdr);
2313
2314 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002315 genlmsg_cancel(msg, hdr);
2316 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002317}
2318
2319static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2320{
2321 int wp_idx = 0;
2322 int if_idx = 0;
2323 int wp_start = cb->args[0];
2324 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002325 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002326 struct wireless_dev *wdev;
2327
Johannes Berg5fe231e2013-05-08 21:45:15 +02002328 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002329 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2330 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002331 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002332 if (wp_idx < wp_start) {
2333 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002334 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002335 }
Johannes Berg55682962007-09-20 13:09:35 -04002336 if_idx = 0;
2337
Johannes Berg89a54e42012-06-15 14:33:17 +02002338 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339 if (if_idx < if_start) {
2340 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002341 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002343 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002344 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002345 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002346 goto out;
2347 }
2348 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002349 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002350
2351 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002352 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002353 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002354 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002355
2356 cb->args[0] = wp_idx;
2357 cb->args[1] = if_idx;
2358
2359 return skb->len;
2360}
2361
2362static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2363{
2364 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002365 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002366 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002367
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002369 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002370 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002371
Eric W. Biederman15e47302012-09-07 20:12:54 +00002372 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002373 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002374 nlmsg_free(msg);
2375 return -ENOBUFS;
2376 }
Johannes Berg55682962007-09-20 13:09:35 -04002377
Johannes Berg134e6372009-07-10 09:51:34 +00002378 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002379}
2380
Michael Wu66f7ac52008-01-31 19:48:22 +01002381static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2382 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2383 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2384 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2385 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2386 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002387 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002388};
2389
2390static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2391{
2392 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2393 int flag;
2394
2395 *mntrflags = 0;
2396
2397 if (!nla)
2398 return -EINVAL;
2399
2400 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2401 nla, mntr_flags_policy))
2402 return -EINVAL;
2403
2404 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2405 if (flags[flag])
2406 *mntrflags |= (1<<flag);
2407
2408 return 0;
2409}
2410
Johannes Berg9bc383d2009-11-19 11:55:19 +01002411static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002412 struct net_device *netdev, u8 use_4addr,
2413 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002414{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002415 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002416 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002417 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002418 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002419 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002420
2421 switch (iftype) {
2422 case NL80211_IFTYPE_AP_VLAN:
2423 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2424 return 0;
2425 break;
2426 case NL80211_IFTYPE_STATION:
2427 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2428 return 0;
2429 break;
2430 default:
2431 break;
2432 }
2433
2434 return -EOPNOTSUPP;
2435}
2436
Johannes Berg55682962007-09-20 13:09:35 -04002437static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2438{
Johannes Berg4c476992010-10-04 21:36:35 +02002439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002440 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002441 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002442 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002443 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002444 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002445 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002446
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002447 memset(&params, 0, sizeof(params));
2448
Johannes Berg04a773a2009-04-19 21:24:32 +02002449 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002450
Johannes Berg723b0382008-09-16 20:22:09 +02002451 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002452 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002453 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002454 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002455 if (ntype > NL80211_IFTYPE_MAX)
2456 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002457 }
2458
Johannes Berg92ffe052008-09-16 20:39:36 +02002459 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002460 struct wireless_dev *wdev = dev->ieee80211_ptr;
2461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 if (ntype != NL80211_IFTYPE_MESH_POINT)
2463 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002464 if (netif_running(dev))
2465 return -EBUSY;
2466
2467 wdev_lock(wdev);
2468 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2469 IEEE80211_MAX_MESH_ID_LEN);
2470 wdev->mesh_id_up_len =
2471 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2472 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2473 wdev->mesh_id_up_len);
2474 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 }
2476
Felix Fietkau8b787642009-11-10 18:53:10 +01002477 if (info->attrs[NL80211_ATTR_4ADDR]) {
2478 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2479 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002480 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002482 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002483 } else {
2484 params.use_4addr = -1;
2485 }
2486
Johannes Berg92ffe052008-09-16 20:39:36 +02002487 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002488 if (ntype != NL80211_IFTYPE_MONITOR)
2489 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002490 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2491 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002492 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002493 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002494
2495 flags = &_flags;
2496 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002497 }
Johannes Berg3b858752009-03-12 09:55:09 +01002498
Luciano Coelho18003292013-08-29 13:26:57 +03002499 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002500 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2501 return -EOPNOTSUPP;
2502
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002503 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002504 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002505 else
2506 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002507
Johannes Berg9bc383d2009-11-19 11:55:19 +01002508 if (!err && params.use_4addr != -1)
2509 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2510
Johannes Berg55682962007-09-20 13:09:35 -04002511 return err;
2512}
2513
2514static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2515{
Johannes Berg4c476992010-10-04 21:36:35 +02002516 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002517 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002518 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002519 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002520 int err;
2521 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002522 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002523
Johannes Berg78f22b62014-03-24 17:57:27 +01002524 /* to avoid failing a new interface creation due to pending removal */
2525 cfg80211_destroy_ifaces(rdev);
2526
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002527 memset(&params, 0, sizeof(params));
2528
Johannes Berg55682962007-09-20 13:09:35 -04002529 if (!info->attrs[NL80211_ATTR_IFNAME])
2530 return -EINVAL;
2531
2532 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2533 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2534 if (type > NL80211_IFTYPE_MAX)
2535 return -EINVAL;
2536 }
2537
Johannes Berg79c97e92009-07-07 03:56:12 +02002538 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002539 !(rdev->wiphy.interface_modes & (1 << type)))
2540 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002541
Arend van Spriel1c18f142013-01-08 10:17:27 +01002542 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2543 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2544 ETH_ALEN);
2545 if (!is_valid_ether_addr(params.macaddr))
2546 return -EADDRNOTAVAIL;
2547 }
2548
Johannes Berg9bc383d2009-11-19 11:55:19 +01002549 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002550 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002551 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002552 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002553 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002554 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002555
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2557 if (!msg)
2558 return -ENOMEM;
2559
Michael Wu66f7ac52008-01-31 19:48:22 +01002560 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2561 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2562 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002563
Luciano Coelho18003292013-08-29 13:26:57 +03002564 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002565 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2566 return -EOPNOTSUPP;
2567
Hila Gonene35e4d22012-06-27 17:19:42 +03002568 wdev = rdev_add_virtual_intf(rdev,
2569 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2570 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002571 if (IS_ERR(wdev)) {
2572 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002573 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002574 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002575
Johannes Berg78f22b62014-03-24 17:57:27 +01002576 if (info->attrs[NL80211_ATTR_IFACE_SOCKET_OWNER])
2577 wdev->owner_nlportid = info->snd_portid;
2578
Johannes Berg98104fde2012-06-16 00:19:54 +02002579 switch (type) {
2580 case NL80211_IFTYPE_MESH_POINT:
2581 if (!info->attrs[NL80211_ATTR_MESH_ID])
2582 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002583 wdev_lock(wdev);
2584 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2585 IEEE80211_MAX_MESH_ID_LEN);
2586 wdev->mesh_id_up_len =
2587 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2588 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2589 wdev->mesh_id_up_len);
2590 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002591 break;
2592 case NL80211_IFTYPE_P2P_DEVICE:
2593 /*
2594 * P2P Device doesn't have a netdev, so doesn't go
2595 * through the netdev notifier and must be added here
2596 */
2597 mutex_init(&wdev->mtx);
2598 INIT_LIST_HEAD(&wdev->event_list);
2599 spin_lock_init(&wdev->event_lock);
2600 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2601 spin_lock_init(&wdev->mgmt_registrations_lock);
2602
Johannes Berg98104fde2012-06-16 00:19:54 +02002603 wdev->identifier = ++rdev->wdev_id;
2604 list_add_rcu(&wdev->list, &rdev->wdev_list);
2605 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002606 break;
2607 default:
2608 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002609 }
2610
Eric W. Biederman15e47302012-09-07 20:12:54 +00002611 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002612 rdev, wdev) < 0) {
2613 nlmsg_free(msg);
2614 return -ENOBUFS;
2615 }
2616
2617 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002618}
2619
2620static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2621{
Johannes Berg4c476992010-10-04 21:36:35 +02002622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002623 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002624
Johannes Berg4c476992010-10-04 21:36:35 +02002625 if (!rdev->ops->del_virtual_intf)
2626 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002627
Johannes Berg84efbb82012-06-16 00:00:26 +02002628 /*
2629 * If we remove a wireless device without a netdev then clear
2630 * user_ptr[1] so that nl80211_post_doit won't dereference it
2631 * to check if it needs to do dev_put(). Otherwise it crashes
2632 * since the wdev has been freed, unlike with a netdev where
2633 * we need the dev_put() for the netdev to really be freed.
2634 */
2635 if (!wdev->netdev)
2636 info->user_ptr[1] = NULL;
2637
Hila Gonene35e4d22012-06-27 17:19:42 +03002638 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002639}
2640
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002641static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2642{
2643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2644 struct net_device *dev = info->user_ptr[1];
2645 u16 noack_map;
2646
2647 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2648 return -EINVAL;
2649
2650 if (!rdev->ops->set_noack_map)
2651 return -EOPNOTSUPP;
2652
2653 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2654
Hila Gonene35e4d22012-06-27 17:19:42 +03002655 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002656}
2657
Johannes Berg41ade002007-12-19 02:03:29 +01002658struct get_key_cookie {
2659 struct sk_buff *msg;
2660 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002661 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002662};
2663
2664static void get_key_callback(void *c, struct key_params *params)
2665{
Johannes Bergb9454e82009-07-08 13:29:08 +02002666 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002667 struct get_key_cookie *cookie = c;
2668
David S. Miller9360ffd2012-03-29 04:41:26 -04002669 if ((params->key &&
2670 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2671 params->key_len, params->key)) ||
2672 (params->seq &&
2673 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2674 params->seq_len, params->seq)) ||
2675 (params->cipher &&
2676 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2677 params->cipher)))
2678 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002679
Johannes Bergb9454e82009-07-08 13:29:08 +02002680 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2681 if (!key)
2682 goto nla_put_failure;
2683
David S. Miller9360ffd2012-03-29 04:41:26 -04002684 if ((params->key &&
2685 nla_put(cookie->msg, NL80211_KEY_DATA,
2686 params->key_len, params->key)) ||
2687 (params->seq &&
2688 nla_put(cookie->msg, NL80211_KEY_SEQ,
2689 params->seq_len, params->seq)) ||
2690 (params->cipher &&
2691 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2692 params->cipher)))
2693 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002694
David S. Miller9360ffd2012-03-29 04:41:26 -04002695 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2696 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002697
2698 nla_nest_end(cookie->msg, key);
2699
Johannes Berg41ade002007-12-19 02:03:29 +01002700 return;
2701 nla_put_failure:
2702 cookie->error = 1;
2703}
2704
2705static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2706{
Johannes Berg4c476992010-10-04 21:36:35 +02002707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002708 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002709 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002710 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002711 const u8 *mac_addr = NULL;
2712 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002713 struct get_key_cookie cookie = {
2714 .error = 0,
2715 };
2716 void *hdr;
2717 struct sk_buff *msg;
2718
2719 if (info->attrs[NL80211_ATTR_KEY_IDX])
2720 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2721
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002722 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002723 return -EINVAL;
2724
2725 if (info->attrs[NL80211_ATTR_MAC])
2726 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2727
Johannes Berge31b8212010-10-05 19:39:30 +02002728 pairwise = !!mac_addr;
2729 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2730 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2731 if (kt >= NUM_NL80211_KEYTYPES)
2732 return -EINVAL;
2733 if (kt != NL80211_KEYTYPE_GROUP &&
2734 kt != NL80211_KEYTYPE_PAIRWISE)
2735 return -EINVAL;
2736 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2737 }
2738
Johannes Berg4c476992010-10-04 21:36:35 +02002739 if (!rdev->ops->get_key)
2740 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002743 if (!msg)
2744 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002745
Eric W. Biederman15e47302012-09-07 20:12:54 +00002746 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002747 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002748 if (!hdr)
Johannes Berg9fe271a2013-10-25 11:15:12 +02002749 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002752 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002753
David S. Miller9360ffd2012-03-29 04:41:26 -04002754 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2755 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2756 goto nla_put_failure;
2757 if (mac_addr &&
2758 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2759 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002760
Johannes Berge31b8212010-10-05 19:39:30 +02002761 if (pairwise && mac_addr &&
2762 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2763 return -ENOENT;
2764
Hila Gonene35e4d22012-06-27 17:19:42 +03002765 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2766 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002767
2768 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002769 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002770
2771 if (cookie.error)
2772 goto nla_put_failure;
2773
2774 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002775 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002776
2777 nla_put_failure:
2778 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002779 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002780 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002781 return err;
2782}
2783
2784static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2785{
Johannes Berg4c476992010-10-04 21:36:35 +02002786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002787 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002788 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002789 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Bergb9454e82009-07-08 13:29:08 +02002791 err = nl80211_parse_key(info, &key);
2792 if (err)
2793 return err;
2794
2795 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002796 return -EINVAL;
2797
Johannes Bergb9454e82009-07-08 13:29:08 +02002798 /* only support setting default key */
2799 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002800 return -EINVAL;
2801
Johannes Bergfffd0932009-07-08 14:22:54 +02002802 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002803
2804 if (key.def) {
2805 if (!rdev->ops->set_default_key) {
2806 err = -EOPNOTSUPP;
2807 goto out;
2808 }
2809
2810 err = nl80211_key_allowed(dev->ieee80211_ptr);
2811 if (err)
2812 goto out;
2813
Hila Gonene35e4d22012-06-27 17:19:42 +03002814 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002815 key.def_uni, key.def_multi);
2816
2817 if (err)
2818 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002819
Johannes Berg3d23e342009-09-29 23:27:28 +02002820#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002821 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002822#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002823 } else {
2824 if (key.def_uni || !key.def_multi) {
2825 err = -EINVAL;
2826 goto out;
2827 }
2828
2829 if (!rdev->ops->set_default_mgmt_key) {
2830 err = -EOPNOTSUPP;
2831 goto out;
2832 }
2833
2834 err = nl80211_key_allowed(dev->ieee80211_ptr);
2835 if (err)
2836 goto out;
2837
Hila Gonene35e4d22012-06-27 17:19:42 +03002838 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002839 if (err)
2840 goto out;
2841
2842#ifdef CONFIG_CFG80211_WEXT
2843 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2844#endif
2845 }
2846
2847 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002848 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002849
Johannes Berg41ade002007-12-19 02:03:29 +01002850 return err;
2851}
2852
2853static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2854{
Johannes Berg4c476992010-10-04 21:36:35 +02002855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002856 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002857 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002858 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002859 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002860
Johannes Bergb9454e82009-07-08 13:29:08 +02002861 err = nl80211_parse_key(info, &key);
2862 if (err)
2863 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002864
Johannes Bergb9454e82009-07-08 13:29:08 +02002865 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002866 return -EINVAL;
2867
Johannes Berg41ade002007-12-19 02:03:29 +01002868 if (info->attrs[NL80211_ATTR_MAC])
2869 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2870
Johannes Berge31b8212010-10-05 19:39:30 +02002871 if (key.type == -1) {
2872 if (mac_addr)
2873 key.type = NL80211_KEYTYPE_PAIRWISE;
2874 else
2875 key.type = NL80211_KEYTYPE_GROUP;
2876 }
2877
2878 /* for now */
2879 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2880 key.type != NL80211_KEYTYPE_GROUP)
2881 return -EINVAL;
2882
Johannes Berg4c476992010-10-04 21:36:35 +02002883 if (!rdev->ops->add_key)
2884 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002885
Johannes Berge31b8212010-10-05 19:39:30 +02002886 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2887 key.type == NL80211_KEYTYPE_PAIRWISE,
2888 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002889 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002890
2891 wdev_lock(dev->ieee80211_ptr);
2892 err = nl80211_key_allowed(dev->ieee80211_ptr);
2893 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002894 err = rdev_add_key(rdev, dev, key.idx,
2895 key.type == NL80211_KEYTYPE_PAIRWISE,
2896 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002897 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002898
Johannes Berg41ade002007-12-19 02:03:29 +01002899 return err;
2900}
2901
2902static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2903{
Johannes Berg4c476992010-10-04 21:36:35 +02002904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002905 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002906 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002907 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002908 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002909
Johannes Bergb9454e82009-07-08 13:29:08 +02002910 err = nl80211_parse_key(info, &key);
2911 if (err)
2912 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002913
2914 if (info->attrs[NL80211_ATTR_MAC])
2915 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2916
Johannes Berge31b8212010-10-05 19:39:30 +02002917 if (key.type == -1) {
2918 if (mac_addr)
2919 key.type = NL80211_KEYTYPE_PAIRWISE;
2920 else
2921 key.type = NL80211_KEYTYPE_GROUP;
2922 }
2923
2924 /* for now */
2925 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2926 key.type != NL80211_KEYTYPE_GROUP)
2927 return -EINVAL;
2928
Johannes Berg4c476992010-10-04 21:36:35 +02002929 if (!rdev->ops->del_key)
2930 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002931
Johannes Bergfffd0932009-07-08 14:22:54 +02002932 wdev_lock(dev->ieee80211_ptr);
2933 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002934
2935 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2936 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2937 err = -ENOENT;
2938
Johannes Bergfffd0932009-07-08 14:22:54 +02002939 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002940 err = rdev_del_key(rdev, dev, key.idx,
2941 key.type == NL80211_KEYTYPE_PAIRWISE,
2942 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002943
Johannes Berg3d23e342009-09-29 23:27:28 +02002944#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002945 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002946 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002947 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002948 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002949 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2950 }
2951#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002952 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002953
Johannes Berg41ade002007-12-19 02:03:29 +01002954 return err;
2955}
2956
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302957/* This function returns an error or the number of nested attributes */
2958static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2959{
2960 struct nlattr *attr;
2961 int n_entries = 0, tmp;
2962
2963 nla_for_each_nested(attr, nl_attr, tmp) {
2964 if (nla_len(attr) != ETH_ALEN)
2965 return -EINVAL;
2966
2967 n_entries++;
2968 }
2969
2970 return n_entries;
2971}
2972
2973/*
2974 * This function parses ACL information and allocates memory for ACL data.
2975 * On successful return, the calling function is responsible to free the
2976 * ACL buffer returned by this function.
2977 */
2978static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2979 struct genl_info *info)
2980{
2981 enum nl80211_acl_policy acl_policy;
2982 struct nlattr *attr;
2983 struct cfg80211_acl_data *acl;
2984 int i = 0, n_entries, tmp;
2985
2986 if (!wiphy->max_acl_mac_addrs)
2987 return ERR_PTR(-EOPNOTSUPP);
2988
2989 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2990 return ERR_PTR(-EINVAL);
2991
2992 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2993 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2994 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2995 return ERR_PTR(-EINVAL);
2996
2997 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2998 return ERR_PTR(-EINVAL);
2999
3000 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3001 if (n_entries < 0)
3002 return ERR_PTR(n_entries);
3003
3004 if (n_entries > wiphy->max_acl_mac_addrs)
3005 return ERR_PTR(-ENOTSUPP);
3006
3007 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3008 GFP_KERNEL);
3009 if (!acl)
3010 return ERR_PTR(-ENOMEM);
3011
3012 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3013 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3014 i++;
3015 }
3016
3017 acl->n_acl_entries = n_entries;
3018 acl->acl_policy = acl_policy;
3019
3020 return acl;
3021}
3022
3023static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3024{
3025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3026 struct net_device *dev = info->user_ptr[1];
3027 struct cfg80211_acl_data *acl;
3028 int err;
3029
3030 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3031 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3032 return -EOPNOTSUPP;
3033
3034 if (!dev->ieee80211_ptr->beacon_interval)
3035 return -EINVAL;
3036
3037 acl = parse_acl_data(&rdev->wiphy, info);
3038 if (IS_ERR(acl))
3039 return PTR_ERR(acl);
3040
3041 err = rdev_set_mac_acl(rdev, dev, acl);
3042
3043 kfree(acl);
3044
3045 return err;
3046}
3047
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003048static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01003049 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003050{
Johannes Berg88600202012-02-13 15:17:18 +01003051 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003052
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003053 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3054 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3055 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3056 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003057 return -EINVAL;
3058
Johannes Berg88600202012-02-13 15:17:18 +01003059 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003060
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003061 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3062 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3063 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003064 if (!bcn->head_len)
3065 return -EINVAL;
3066 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003067 }
3068
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003069 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3070 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3071 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003072 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003073 }
3074
Johannes Berg4c476992010-10-04 21:36:35 +02003075 if (!haveinfo)
3076 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003077
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003078 if (attrs[NL80211_ATTR_IE]) {
3079 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3080 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003081 }
3082
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003083 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003084 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003085 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003086 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003087 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003088 }
3089
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003090 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003091 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003092 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003093 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003094 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003095 }
3096
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003097 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3098 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3099 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003100 }
3101
Johannes Berg88600202012-02-13 15:17:18 +01003102 return 0;
3103}
3104
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003105static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3106 struct cfg80211_ap_settings *params)
3107{
3108 struct wireless_dev *wdev;
3109 bool ret = false;
3110
Johannes Berg89a54e42012-06-15 14:33:17 +02003111 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003112 if (wdev->iftype != NL80211_IFTYPE_AP &&
3113 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3114 continue;
3115
Johannes Berg683b6d32012-11-08 21:25:48 +01003116 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003117 continue;
3118
Johannes Berg683b6d32012-11-08 21:25:48 +01003119 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003120 ret = true;
3121 break;
3122 }
3123
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003124 return ret;
3125}
3126
Jouni Malinene39e5b52012-09-30 19:29:39 +03003127static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3128 enum nl80211_auth_type auth_type,
3129 enum nl80211_commands cmd)
3130{
3131 if (auth_type > NL80211_AUTHTYPE_MAX)
3132 return false;
3133
3134 switch (cmd) {
3135 case NL80211_CMD_AUTHENTICATE:
3136 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3137 auth_type == NL80211_AUTHTYPE_SAE)
3138 return false;
3139 return true;
3140 case NL80211_CMD_CONNECT:
3141 case NL80211_CMD_START_AP:
3142 /* SAE not supported yet */
3143 if (auth_type == NL80211_AUTHTYPE_SAE)
3144 return false;
3145 return true;
3146 default:
3147 return false;
3148 }
3149}
3150
Johannes Berg88600202012-02-13 15:17:18 +01003151static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3152{
3153 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3154 struct net_device *dev = info->user_ptr[1];
3155 struct wireless_dev *wdev = dev->ieee80211_ptr;
3156 struct cfg80211_ap_settings params;
3157 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003158 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003159
3160 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3161 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3162 return -EOPNOTSUPP;
3163
3164 if (!rdev->ops->start_ap)
3165 return -EOPNOTSUPP;
3166
3167 if (wdev->beacon_interval)
3168 return -EALREADY;
3169
3170 memset(&params, 0, sizeof(params));
3171
3172 /* these are required for START_AP */
3173 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3174 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3175 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3176 return -EINVAL;
3177
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003178 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003179 if (err)
3180 return err;
3181
3182 params.beacon_interval =
3183 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3184 params.dtim_period =
3185 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3186
3187 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3188 if (err)
3189 return err;
3190
3191 /*
3192 * In theory, some of these attributes should be required here
3193 * but since they were not used when the command was originally
3194 * added, keep them optional for old user space programs to let
3195 * them continue to work with drivers that do not need the
3196 * additional information -- drivers must check!
3197 */
3198 if (info->attrs[NL80211_ATTR_SSID]) {
3199 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3200 params.ssid_len =
3201 nla_len(info->attrs[NL80211_ATTR_SSID]);
3202 if (params.ssid_len == 0 ||
3203 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3204 return -EINVAL;
3205 }
3206
3207 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3208 params.hidden_ssid = nla_get_u32(
3209 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3210 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3211 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3212 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3213 return -EINVAL;
3214 }
3215
3216 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3217
3218 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3219 params.auth_type = nla_get_u32(
3220 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003221 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3222 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003223 return -EINVAL;
3224 } else
3225 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3226
3227 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3228 NL80211_MAX_NR_CIPHER_SUITES);
3229 if (err)
3230 return err;
3231
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303232 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3233 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3234 return -EOPNOTSUPP;
3235 params.inactivity_timeout = nla_get_u16(
3236 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3237 }
3238
Johannes Berg53cabad2012-11-14 15:17:28 +01003239 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3240 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3241 return -EINVAL;
3242 params.p2p_ctwindow =
3243 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3244 if (params.p2p_ctwindow > 127)
3245 return -EINVAL;
3246 if (params.p2p_ctwindow != 0 &&
3247 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3248 return -EINVAL;
3249 }
3250
3251 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3252 u8 tmp;
3253
3254 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3255 return -EINVAL;
3256 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3257 if (tmp > 1)
3258 return -EINVAL;
3259 params.p2p_opp_ps = tmp;
3260 if (params.p2p_opp_ps != 0 &&
3261 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3262 return -EINVAL;
3263 }
3264
Johannes Bergaa430da2012-05-16 23:50:18 +02003265 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003266 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3267 if (err)
3268 return err;
3269 } else if (wdev->preset_chandef.chan) {
3270 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003271 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003272 return -EINVAL;
3273
Ilan Peer174e0cd2014-02-23 09:13:01 +02003274 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
3275 wdev->iftype))
Johannes Bergaa430da2012-05-16 23:50:18 +02003276 return -EINVAL;
3277
Luciano Coelho2beb6da2014-02-18 11:40:36 +02003278 err = cfg80211_chandef_dfs_required(wdev->wiphy,
3279 &params.chandef,
3280 NL80211_IFTYPE_AP);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003281 if (err < 0)
3282 return err;
Luciano Coelho2beb6da2014-02-18 11:40:36 +02003283
3284 if (err > 0) {
Simon Wunderlich04f39042013-02-08 18:16:19 +01003285 params.radar_required = true;
Luciano Coelho2beb6da2014-02-18 11:40:36 +02003286 radar_detect_width = BIT(params.chandef.width);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003287 }
3288
Simon Wunderlich04f39042013-02-08 18:16:19 +01003289 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3290 params.chandef.chan,
3291 CHAN_MODE_SHARED,
3292 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003293 if (err)
3294 return err;
3295
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303296 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3297 params.acl = parse_acl_data(&rdev->wiphy, info);
3298 if (IS_ERR(params.acl))
3299 return PTR_ERR(params.acl);
3300 }
3301
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003302 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003303 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003304 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003305 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003306 wdev->beacon_interval = params.beacon_interval;
Michal Kazior9e0e2962014-01-29 14:22:27 +01003307 wdev->chandef = params.chandef;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003308 wdev->ssid_len = params.ssid_len;
3309 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003310 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003311 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303312
3313 kfree(params.acl);
3314
Johannes Berg56d18932011-05-09 18:41:15 +02003315 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003316}
3317
Johannes Berg88600202012-02-13 15:17:18 +01003318static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3319{
3320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3321 struct net_device *dev = info->user_ptr[1];
3322 struct wireless_dev *wdev = dev->ieee80211_ptr;
3323 struct cfg80211_beacon_data params;
3324 int err;
3325
3326 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3327 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3328 return -EOPNOTSUPP;
3329
3330 if (!rdev->ops->change_beacon)
3331 return -EOPNOTSUPP;
3332
3333 if (!wdev->beacon_interval)
3334 return -EINVAL;
3335
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003336 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003337 if (err)
3338 return err;
3339
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003340 wdev_lock(wdev);
3341 err = rdev_change_beacon(rdev, dev, &params);
3342 wdev_unlock(wdev);
3343
3344 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003345}
3346
3347static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003348{
Johannes Berg4c476992010-10-04 21:36:35 +02003349 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3350 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003351
Ilan Peer7c8d5e02014-02-25 15:33:38 +02003352 return cfg80211_stop_ap(rdev, dev, false);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003353}
3354
Johannes Berg5727ef12007-12-19 02:03:34 +01003355static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3356 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3357 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3358 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003359 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003360 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003361 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003362};
3363
Johannes Bergeccb8e82009-05-11 21:57:56 +03003364static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003365 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003366 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003367{
3368 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003369 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003370 int flag;
3371
Johannes Bergeccb8e82009-05-11 21:57:56 +03003372 /*
3373 * Try parsing the new attribute first so userspace
3374 * can specify both for older kernels.
3375 */
3376 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3377 if (nla) {
3378 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003379
Johannes Bergeccb8e82009-05-11 21:57:56 +03003380 sta_flags = nla_data(nla);
3381 params->sta_flags_mask = sta_flags->mask;
3382 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003383 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003384 if ((params->sta_flags_mask |
3385 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3386 return -EINVAL;
3387 return 0;
3388 }
3389
3390 /* if present, parse the old attribute */
3391
3392 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003393 if (!nla)
3394 return 0;
3395
3396 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3397 nla, sta_flags_policy))
3398 return -EINVAL;
3399
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003400 /*
3401 * Only allow certain flags for interface types so that
3402 * other attributes are silently ignored. Remember that
3403 * this is backward compatibility code with old userspace
3404 * and shouldn't be hit in other cases anyway.
3405 */
3406 switch (iftype) {
3407 case NL80211_IFTYPE_AP:
3408 case NL80211_IFTYPE_AP_VLAN:
3409 case NL80211_IFTYPE_P2P_GO:
3410 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3411 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3412 BIT(NL80211_STA_FLAG_WME) |
3413 BIT(NL80211_STA_FLAG_MFP);
3414 break;
3415 case NL80211_IFTYPE_P2P_CLIENT:
3416 case NL80211_IFTYPE_STATION:
3417 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3418 BIT(NL80211_STA_FLAG_TDLS_PEER);
3419 break;
3420 case NL80211_IFTYPE_MESH_POINT:
3421 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3422 BIT(NL80211_STA_FLAG_MFP) |
3423 BIT(NL80211_STA_FLAG_AUTHORIZED);
3424 default:
3425 return -EINVAL;
3426 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003427
Johannes Berg3383b5a2012-05-10 20:14:43 +02003428 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3429 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003430 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003431
Johannes Berg3383b5a2012-05-10 20:14:43 +02003432 /* no longer support new API additions in old API */
3433 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3434 return -EINVAL;
3435 }
3436 }
3437
Johannes Berg5727ef12007-12-19 02:03:34 +01003438 return 0;
3439}
3440
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003441static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3442 int attr)
3443{
3444 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003445 u32 bitrate;
3446 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003447
3448 rate = nla_nest_start(msg, attr);
3449 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003450 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003451
3452 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3453 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003454 /* report 16-bit bitrate only if we can */
3455 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003456 if (bitrate > 0 &&
3457 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3458 return false;
3459 if (bitrate_compat > 0 &&
3460 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3461 return false;
3462
3463 if (info->flags & RATE_INFO_FLAGS_MCS) {
3464 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3465 return false;
3466 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3467 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3468 return false;
3469 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3470 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3471 return false;
3472 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3473 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3474 return false;
3475 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3476 return false;
3477 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3478 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3479 return false;
3480 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3481 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3482 return false;
3483 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3484 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3485 return false;
3486 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3487 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3488 return false;
3489 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3490 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3491 return false;
3492 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003493
3494 nla_nest_end(msg, rate);
3495 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003496}
3497
Felix Fietkau119363c2013-04-22 16:29:30 +02003498static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3499 int id)
3500{
3501 void *attr;
3502 int i = 0;
3503
3504 if (!mask)
3505 return true;
3506
3507 attr = nla_nest_start(msg, id);
3508 if (!attr)
3509 return false;
3510
3511 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3512 if (!(mask & BIT(i)))
3513 continue;
3514
3515 if (nla_put_u8(msg, i, signal[i]))
3516 return false;
3517 }
3518
3519 nla_nest_end(msg, attr);
3520
3521 return true;
3522}
3523
Eric W. Biederman15e47302012-09-07 20:12:54 +00003524static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003525 int flags,
3526 struct cfg80211_registered_device *rdev,
3527 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003528 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003529{
3530 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003531 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003532
Eric W. Biederman15e47302012-09-07 20:12:54 +00003533 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003534 if (!hdr)
3535 return -1;
3536
David S. Miller9360ffd2012-03-29 04:41:26 -04003537 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3538 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3539 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3540 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003541
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003542 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3543 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003544 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003545 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3546 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3547 sinfo->connected_time))
3548 goto nla_put_failure;
3549 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3550 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3551 sinfo->inactive_time))
3552 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003553 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3554 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003555 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003556 (u32)sinfo->rx_bytes))
3557 goto nla_put_failure;
3558 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003559 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003560 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3561 (u32)sinfo->tx_bytes))
3562 goto nla_put_failure;
3563 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3564 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003565 sinfo->rx_bytes))
3566 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003567 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3568 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003569 sinfo->tx_bytes))
3570 goto nla_put_failure;
3571 if ((sinfo->filled & STATION_INFO_LLID) &&
3572 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3573 goto nla_put_failure;
3574 if ((sinfo->filled & STATION_INFO_PLID) &&
3575 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3576 goto nla_put_failure;
3577 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3578 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3579 sinfo->plink_state))
3580 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003581 switch (rdev->wiphy.signal_type) {
3582 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003583 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3584 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3585 sinfo->signal))
3586 goto nla_put_failure;
3587 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3588 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3589 sinfo->signal_avg))
3590 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003591 break;
3592 default:
3593 break;
3594 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003595 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3596 if (!nl80211_put_signal(msg, sinfo->chains,
3597 sinfo->chain_signal,
3598 NL80211_STA_INFO_CHAIN_SIGNAL))
3599 goto nla_put_failure;
3600 }
3601 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3602 if (!nl80211_put_signal(msg, sinfo->chains,
3603 sinfo->chain_signal_avg,
3604 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3605 goto nla_put_failure;
3606 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003607 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003608 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3609 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003610 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003611 }
3612 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3613 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3614 NL80211_STA_INFO_RX_BITRATE))
3615 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003616 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003617 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3618 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3619 sinfo->rx_packets))
3620 goto nla_put_failure;
3621 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3622 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3623 sinfo->tx_packets))
3624 goto nla_put_failure;
3625 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3626 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3627 sinfo->tx_retries))
3628 goto nla_put_failure;
3629 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3630 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3631 sinfo->tx_failed))
3632 goto nla_put_failure;
3633 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3634 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3635 sinfo->beacon_loss_count))
3636 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003637 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3638 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3639 sinfo->local_pm))
3640 goto nla_put_failure;
3641 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3642 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3643 sinfo->peer_pm))
3644 goto nla_put_failure;
3645 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3646 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3647 sinfo->nonpeer_pm))
3648 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003649 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3650 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3651 if (!bss_param)
3652 goto nla_put_failure;
3653
David S. Miller9360ffd2012-03-29 04:41:26 -04003654 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3655 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3656 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3657 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3658 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3659 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3660 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3661 sinfo->bss_param.dtim_period) ||
3662 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3663 sinfo->bss_param.beacon_interval))
3664 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003665
3666 nla_nest_end(msg, bss_param);
3667 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003668 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3669 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3670 sizeof(struct nl80211_sta_flag_update),
3671 &sinfo->sta_flags))
3672 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003673 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3674 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3675 sinfo->t_offset))
3676 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003677 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003678
David S. Miller9360ffd2012-03-29 04:41:26 -04003679 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3680 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3681 sinfo->assoc_req_ies))
3682 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003683
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003684 return genlmsg_end(msg, hdr);
3685
3686 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003687 genlmsg_cancel(msg, hdr);
3688 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003689}
3690
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003691static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003692 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003693{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003694 struct station_info sinfo;
3695 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003696 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003697 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003698 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003699 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003700
Johannes Berg97990a02013-04-19 01:02:55 +02003701 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003702 if (err)
3703 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003704
Johannes Berg97990a02013-04-19 01:02:55 +02003705 if (!wdev->netdev) {
3706 err = -EINVAL;
3707 goto out_err;
3708 }
3709
Johannes Bergbba95fe2008-07-29 13:22:51 +02003710 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003711 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003712 goto out_err;
3713 }
3714
Johannes Bergbba95fe2008-07-29 13:22:51 +02003715 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003716 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003717 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003718 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003719 if (err == -ENOENT)
3720 break;
3721 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003722 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003723
3724 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003725 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003726 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003727 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003728 &sinfo) < 0)
3729 goto out;
3730
3731 sta_idx++;
3732 }
3733
3734
3735 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003736 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003737 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003738 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003739 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003740
3741 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003742}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003743
Johannes Berg5727ef12007-12-19 02:03:34 +01003744static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3745{
Johannes Berg4c476992010-10-04 21:36:35 +02003746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3747 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003748 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003749 struct sk_buff *msg;
3750 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003751 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003752
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003753 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003754
3755 if (!info->attrs[NL80211_ATTR_MAC])
3756 return -EINVAL;
3757
3758 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3759
Johannes Berg4c476992010-10-04 21:36:35 +02003760 if (!rdev->ops->get_station)
3761 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003762
Hila Gonene35e4d22012-06-27 17:19:42 +03003763 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003764 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003765 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003766
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003767 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003768 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003769 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003770
Eric W. Biederman15e47302012-09-07 20:12:54 +00003771 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003772 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003773 nlmsg_free(msg);
3774 return -ENOBUFS;
3775 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003776
Johannes Berg4c476992010-10-04 21:36:35 +02003777 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003778}
3779
Johannes Berg77ee7c82013-02-15 00:48:33 +01003780int cfg80211_check_station_change(struct wiphy *wiphy,
3781 struct station_parameters *params,
3782 enum cfg80211_station_type statype)
3783{
3784 if (params->listen_interval != -1)
3785 return -EINVAL;
3786 if (params->aid)
3787 return -EINVAL;
3788
3789 /* When you run into this, adjust the code below for the new flag */
3790 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3791
3792 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003793 case CFG80211_STA_MESH_PEER_KERNEL:
3794 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003795 /*
3796 * No ignoring the TDLS flag here -- the userspace mesh
3797 * code doesn't have the bug of including TDLS in the
3798 * mask everywhere.
3799 */
3800 if (params->sta_flags_mask &
3801 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3802 BIT(NL80211_STA_FLAG_MFP) |
3803 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3804 return -EINVAL;
3805 break;
3806 case CFG80211_STA_TDLS_PEER_SETUP:
3807 case CFG80211_STA_TDLS_PEER_ACTIVE:
3808 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3809 return -EINVAL;
3810 /* ignore since it can't change */
3811 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3812 break;
3813 default:
3814 /* disallow mesh-specific things */
3815 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3816 return -EINVAL;
3817 if (params->local_pm)
3818 return -EINVAL;
3819 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3820 return -EINVAL;
3821 }
3822
3823 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3824 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3825 /* TDLS can't be set, ... */
3826 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3827 return -EINVAL;
3828 /*
3829 * ... but don't bother the driver with it. This works around
3830 * a hostapd/wpa_supplicant issue -- it always includes the
3831 * TLDS_PEER flag in the mask even for AP mode.
3832 */
3833 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3834 }
3835
3836 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3837 /* reject other things that can't change */
3838 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3839 return -EINVAL;
3840 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3841 return -EINVAL;
3842 if (params->supported_rates)
3843 return -EINVAL;
3844 if (params->ext_capab || params->ht_capa || params->vht_capa)
3845 return -EINVAL;
3846 }
3847
3848 if (statype != CFG80211_STA_AP_CLIENT) {
3849 if (params->vlan)
3850 return -EINVAL;
3851 }
3852
3853 switch (statype) {
3854 case CFG80211_STA_AP_MLME_CLIENT:
3855 /* Use this only for authorizing/unauthorizing a station */
3856 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3857 return -EOPNOTSUPP;
3858 break;
3859 case CFG80211_STA_AP_CLIENT:
3860 /* accept only the listed bits */
3861 if (params->sta_flags_mask &
3862 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3863 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3864 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3865 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3866 BIT(NL80211_STA_FLAG_WME) |
3867 BIT(NL80211_STA_FLAG_MFP)))
3868 return -EINVAL;
3869
3870 /* but authenticated/associated only if driver handles it */
3871 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3872 params->sta_flags_mask &
3873 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3874 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3875 return -EINVAL;
3876 break;
3877 case CFG80211_STA_IBSS:
3878 case CFG80211_STA_AP_STA:
3879 /* reject any changes other than AUTHORIZED */
3880 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3881 return -EINVAL;
3882 break;
3883 case CFG80211_STA_TDLS_PEER_SETUP:
3884 /* reject any changes other than AUTHORIZED or WME */
3885 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3886 BIT(NL80211_STA_FLAG_WME)))
3887 return -EINVAL;
3888 /* force (at least) rates when authorizing */
3889 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3890 !params->supported_rates)
3891 return -EINVAL;
3892 break;
3893 case CFG80211_STA_TDLS_PEER_ACTIVE:
3894 /* reject any changes */
3895 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003896 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003897 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3898 return -EINVAL;
3899 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003900 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003901 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3902 return -EINVAL;
3903 break;
3904 }
3905
3906 return 0;
3907}
3908EXPORT_SYMBOL(cfg80211_check_station_change);
3909
Johannes Berg5727ef12007-12-19 02:03:34 +01003910/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003911 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003912 */
Johannes Berg80b99892011-11-18 16:23:01 +01003913static struct net_device *get_vlan(struct genl_info *info,
3914 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003915{
Johannes Berg463d0182009-07-14 00:33:35 +02003916 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003917 struct net_device *v;
3918 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003919
Johannes Berg80b99892011-11-18 16:23:01 +01003920 if (!vlanattr)
3921 return NULL;
3922
3923 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3924 if (!v)
3925 return ERR_PTR(-ENODEV);
3926
3927 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3928 ret = -EINVAL;
3929 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003930 }
Johannes Berg80b99892011-11-18 16:23:01 +01003931
Johannes Berg77ee7c82013-02-15 00:48:33 +01003932 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3933 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3934 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3935 ret = -EINVAL;
3936 goto error;
3937 }
3938
Johannes Berg80b99892011-11-18 16:23:01 +01003939 if (!netif_running(v)) {
3940 ret = -ENETDOWN;
3941 goto error;
3942 }
3943
3944 return v;
3945 error:
3946 dev_put(v);
3947 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003948}
3949
Johannes Berg94e860f2014-01-20 23:58:15 +01003950static const struct nla_policy
3951nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
Jouni Malinendf881292013-02-14 21:10:54 +02003952 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3953 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3954};
3955
Johannes Bergff276692013-02-15 00:09:01 +01003956static int nl80211_parse_sta_wme(struct genl_info *info,
3957 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003958{
Jouni Malinendf881292013-02-14 21:10:54 +02003959 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3960 struct nlattr *nla;
3961 int err;
3962
Jouni Malinendf881292013-02-14 21:10:54 +02003963 /* parse WME attributes if present */
3964 if (!info->attrs[NL80211_ATTR_STA_WME])
3965 return 0;
3966
3967 nla = info->attrs[NL80211_ATTR_STA_WME];
3968 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3969 nl80211_sta_wme_policy);
3970 if (err)
3971 return err;
3972
3973 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3974 params->uapsd_queues = nla_get_u8(
3975 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3976 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3977 return -EINVAL;
3978
3979 if (tb[NL80211_STA_WME_MAX_SP])
3980 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3981
3982 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3983 return -EINVAL;
3984
3985 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3986
3987 return 0;
3988}
3989
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303990static int nl80211_parse_sta_channel_info(struct genl_info *info,
3991 struct station_parameters *params)
3992{
3993 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3994 params->supported_channels =
3995 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3996 params->supported_channels_len =
3997 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3998 /*
3999 * Need to include at least one (first channel, number of
4000 * channels) tuple for each subband, and must have proper
4001 * tuples for the rest of the data as well.
4002 */
4003 if (params->supported_channels_len < 2)
4004 return -EINVAL;
4005 if (params->supported_channels_len % 2)
4006 return -EINVAL;
4007 }
4008
4009 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4010 params->supported_oper_classes =
4011 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4012 params->supported_oper_classes_len =
4013 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4014 /*
4015 * The value of the Length field of the Supported Operating
4016 * Classes element is between 2 and 253.
4017 */
4018 if (params->supported_oper_classes_len < 2 ||
4019 params->supported_oper_classes_len > 253)
4020 return -EINVAL;
4021 }
4022 return 0;
4023}
4024
Johannes Bergff276692013-02-15 00:09:01 +01004025static int nl80211_set_station_tdls(struct genl_info *info,
4026 struct station_parameters *params)
4027{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304028 int err;
Johannes Bergff276692013-02-15 00:09:01 +01004029 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004030 if (info->attrs[NL80211_ATTR_PEER_AID])
4031 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01004032 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4033 params->ht_capa =
4034 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4035 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4036 params->vht_capa =
4037 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4038
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304039 err = nl80211_parse_sta_channel_info(info, params);
4040 if (err)
4041 return err;
4042
Johannes Bergff276692013-02-15 00:09:01 +01004043 return nl80211_parse_sta_wme(info, params);
4044}
4045
Johannes Berg5727ef12007-12-19 02:03:34 +01004046static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4047{
Johannes Berg4c476992010-10-04 21:36:35 +02004048 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004049 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004050 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004051 u8 *mac_addr;
4052 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01004053
4054 memset(&params, 0, sizeof(params));
4055
4056 params.listen_interval = -1;
4057
Johannes Berg77ee7c82013-02-15 00:48:33 +01004058 if (!rdev->ops->change_station)
4059 return -EOPNOTSUPP;
4060
Johannes Berg5727ef12007-12-19 02:03:34 +01004061 if (info->attrs[NL80211_ATTR_STA_AID])
4062 return -EINVAL;
4063
4064 if (!info->attrs[NL80211_ATTR_MAC])
4065 return -EINVAL;
4066
4067 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4068
4069 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4070 params.supported_rates =
4071 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4072 params.supported_rates_len =
4073 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4074 }
4075
Jouni Malinen9d62a982013-02-14 21:10:13 +02004076 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4077 params.capability =
4078 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4079 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4080 }
4081
4082 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4083 params.ext_capab =
4084 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4085 params.ext_capab_len =
4086 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4087 }
4088
Jouni Malinendf881292013-02-14 21:10:54 +02004089 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004090 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03004091
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004092 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004093 return -EINVAL;
4094
Johannes Bergf8bacc22013-02-14 23:27:01 +01004095 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004096 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004097 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4098 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4099 return -EINVAL;
4100 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004101
Johannes Bergf8bacc22013-02-14 23:27:01 +01004102 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004103 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004104 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4105 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4106 return -EINVAL;
4107 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4108 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004109
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004110 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4111 enum nl80211_mesh_power_mode pm = nla_get_u32(
4112 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4113
4114 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4115 pm > NL80211_MESH_POWER_MAX)
4116 return -EINVAL;
4117
4118 params.local_pm = pm;
4119 }
4120
Johannes Berg77ee7c82013-02-15 00:48:33 +01004121 /* Include parameters for TDLS peer (will check later) */
4122 err = nl80211_set_station_tdls(info, &params);
4123 if (err)
4124 return err;
4125
4126 params.vlan = get_vlan(info, rdev);
4127 if (IS_ERR(params.vlan))
4128 return PTR_ERR(params.vlan);
4129
Johannes Berga97f4422009-06-18 17:23:43 +02004130 switch (dev->ieee80211_ptr->iftype) {
4131 case NL80211_IFTYPE_AP:
4132 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004133 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004134 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004135 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004136 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004137 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004138 break;
4139 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004140 err = -EOPNOTSUPP;
4141 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004142 }
4143
Johannes Berg77ee7c82013-02-15 00:48:33 +01004144 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004145 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004146
Johannes Berg77ee7c82013-02-15 00:48:33 +01004147 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004148 if (params.vlan)
4149 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004150
Johannes Berg5727ef12007-12-19 02:03:34 +01004151 return err;
4152}
4153
4154static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4155{
Johannes Berg4c476992010-10-04 21:36:35 +02004156 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004157 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004158 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004159 struct station_parameters params;
4160 u8 *mac_addr = NULL;
4161
4162 memset(&params, 0, sizeof(params));
4163
Johannes Berg984c3112013-02-14 23:43:25 +01004164 if (!rdev->ops->add_station)
4165 return -EOPNOTSUPP;
4166
Johannes Berg5727ef12007-12-19 02:03:34 +01004167 if (!info->attrs[NL80211_ATTR_MAC])
4168 return -EINVAL;
4169
Johannes Berg5727ef12007-12-19 02:03:34 +01004170 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4171 return -EINVAL;
4172
4173 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4174 return -EINVAL;
4175
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004176 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4177 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004178 return -EINVAL;
4179
Johannes Berg5727ef12007-12-19 02:03:34 +01004180 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4181 params.supported_rates =
4182 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4183 params.supported_rates_len =
4184 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4185 params.listen_interval =
4186 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004187
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004188 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004189 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004190 else
4191 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004192 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4193 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004194
Jouni Malinen9d62a982013-02-14 21:10:13 +02004195 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4196 params.capability =
4197 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4198 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4199 }
4200
4201 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4202 params.ext_capab =
4203 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4204 params.ext_capab_len =
4205 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4206 }
4207
Jouni Malinen36aedc92008-08-25 11:58:58 +03004208 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4209 params.ht_capa =
4210 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004211
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004212 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4213 params.vht_capa =
4214 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4215
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004216 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4217 params.opmode_notif_used = true;
4218 params.opmode_notif =
4219 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4220 }
4221
Johannes Bergf8bacc22013-02-14 23:27:01 +01004222 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004223 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004224 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4225 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4226 return -EINVAL;
4227 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004228
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304229 err = nl80211_parse_sta_channel_info(info, &params);
4230 if (err)
4231 return err;
4232
Johannes Bergff276692013-02-15 00:09:01 +01004233 err = nl80211_parse_sta_wme(info, &params);
4234 if (err)
4235 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004236
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004237 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004238 return -EINVAL;
4239
Johannes Berg77ee7c82013-02-15 00:48:33 +01004240 /* When you run into this, adjust the code below for the new flag */
4241 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4242
Johannes Bergbdd90d52011-12-14 12:20:27 +01004243 switch (dev->ieee80211_ptr->iftype) {
4244 case NL80211_IFTYPE_AP:
4245 case NL80211_IFTYPE_AP_VLAN:
4246 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004247 /* ignore WME attributes if iface/sta is not capable */
4248 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4249 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4250 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004251
Johannes Bergbdd90d52011-12-14 12:20:27 +01004252 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004253 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4254 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004255 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004256 /* but don't bother the driver with it */
4257 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004258
Johannes Bergd582cff2012-10-26 17:53:44 +02004259 /* allow authenticated/associated only if driver handles it */
4260 if (!(rdev->wiphy.features &
4261 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4262 params.sta_flags_mask &
4263 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4264 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4265 return -EINVAL;
4266
Johannes Bergbdd90d52011-12-14 12:20:27 +01004267 /* must be last in here for error handling */
4268 params.vlan = get_vlan(info, rdev);
4269 if (IS_ERR(params.vlan))
4270 return PTR_ERR(params.vlan);
4271 break;
4272 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004273 /* ignore uAPSD data */
4274 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4275
Johannes Bergd582cff2012-10-26 17:53:44 +02004276 /* associated is disallowed */
4277 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4278 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004279 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004280 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4281 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004282 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004283 break;
4284 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004285 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004286 /* ignore uAPSD data */
4287 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4288
Johannes Berg77ee7c82013-02-15 00:48:33 +01004289 /* these are disallowed */
4290 if (params.sta_flags_mask &
4291 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4292 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004293 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004294 /* Only TDLS peers can be added */
4295 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4296 return -EINVAL;
4297 /* Can only add if TDLS ... */
4298 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4299 return -EOPNOTSUPP;
4300 /* ... with external setup is supported */
4301 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4302 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004303 /*
4304 * Older wpa_supplicant versions always mark the TDLS peer
4305 * as authorized, but it shouldn't yet be.
4306 */
4307 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004308 break;
4309 default:
4310 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004311 }
4312
Johannes Bergbdd90d52011-12-14 12:20:27 +01004313 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004314
Hila Gonene35e4d22012-06-27 17:19:42 +03004315 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004316
Johannes Berg5727ef12007-12-19 02:03:34 +01004317 if (params.vlan)
4318 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004319 return err;
4320}
4321
4322static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4323{
Johannes Berg4c476992010-10-04 21:36:35 +02004324 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4325 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004326 u8 *mac_addr = NULL;
4327
4328 if (info->attrs[NL80211_ATTR_MAC])
4329 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4330
Johannes Berge80cf852009-05-11 14:43:13 +02004331 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004333 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004334 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4335 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004336
Johannes Berg4c476992010-10-04 21:36:35 +02004337 if (!rdev->ops->del_station)
4338 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004339
Hila Gonene35e4d22012-06-27 17:19:42 +03004340 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004341}
4342
Eric W. Biederman15e47302012-09-07 20:12:54 +00004343static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004344 int flags, struct net_device *dev,
4345 u8 *dst, u8 *next_hop,
4346 struct mpath_info *pinfo)
4347{
4348 void *hdr;
4349 struct nlattr *pinfoattr;
4350
Eric W. Biederman15e47302012-09-07 20:12:54 +00004351 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004352 if (!hdr)
4353 return -1;
4354
David S. Miller9360ffd2012-03-29 04:41:26 -04004355 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4356 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4357 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4358 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4359 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004360
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004361 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4362 if (!pinfoattr)
4363 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004364 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4365 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4366 pinfo->frame_qlen))
4367 goto nla_put_failure;
4368 if (((pinfo->filled & MPATH_INFO_SN) &&
4369 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4370 ((pinfo->filled & MPATH_INFO_METRIC) &&
4371 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4372 pinfo->metric)) ||
4373 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4374 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4375 pinfo->exptime)) ||
4376 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4377 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4378 pinfo->flags)) ||
4379 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4380 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4381 pinfo->discovery_timeout)) ||
4382 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4383 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4384 pinfo->discovery_retries)))
4385 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004386
4387 nla_nest_end(msg, pinfoattr);
4388
4389 return genlmsg_end(msg, hdr);
4390
4391 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004392 genlmsg_cancel(msg, hdr);
4393 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004394}
4395
4396static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004397 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004398{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004399 struct mpath_info pinfo;
4400 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004401 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004402 u8 dst[ETH_ALEN];
4403 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004404 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004405 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004406
Johannes Berg97990a02013-04-19 01:02:55 +02004407 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004408 if (err)
4409 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004410
4411 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004412 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004413 goto out_err;
4414 }
4415
Johannes Berg97990a02013-04-19 01:02:55 +02004416 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004417 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004418 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004419 }
4420
Johannes Bergbba95fe2008-07-29 13:22:51 +02004421 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004422 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4423 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004424 if (err == -ENOENT)
4425 break;
4426 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004427 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004428
Eric W. Biederman15e47302012-09-07 20:12:54 +00004429 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004430 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004431 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004432 &pinfo) < 0)
4433 goto out;
4434
4435 path_idx++;
4436 }
4437
4438
4439 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004440 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004441 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004442 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004443 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004444 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004445}
4446
4447static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4448{
Johannes Berg4c476992010-10-04 21:36:35 +02004449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004450 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004451 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004452 struct mpath_info pinfo;
4453 struct sk_buff *msg;
4454 u8 *dst = NULL;
4455 u8 next_hop[ETH_ALEN];
4456
4457 memset(&pinfo, 0, sizeof(pinfo));
4458
4459 if (!info->attrs[NL80211_ATTR_MAC])
4460 return -EINVAL;
4461
4462 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4463
Johannes Berg4c476992010-10-04 21:36:35 +02004464 if (!rdev->ops->get_mpath)
4465 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004466
Johannes Berg4c476992010-10-04 21:36:35 +02004467 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4468 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004469
Hila Gonene35e4d22012-06-27 17:19:42 +03004470 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004471 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004472 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004473
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004475 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004476 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004477
Eric W. Biederman15e47302012-09-07 20:12:54 +00004478 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004479 dev, dst, next_hop, &pinfo) < 0) {
4480 nlmsg_free(msg);
4481 return -ENOBUFS;
4482 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004483
Johannes Berg4c476992010-10-04 21:36:35 +02004484 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004485}
4486
4487static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4488{
Johannes Berg4c476992010-10-04 21:36:35 +02004489 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4490 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004491 u8 *dst = NULL;
4492 u8 *next_hop = NULL;
4493
4494 if (!info->attrs[NL80211_ATTR_MAC])
4495 return -EINVAL;
4496
4497 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4498 return -EINVAL;
4499
4500 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4501 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4502
Johannes Berg4c476992010-10-04 21:36:35 +02004503 if (!rdev->ops->change_mpath)
4504 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004505
Johannes Berg4c476992010-10-04 21:36:35 +02004506 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4507 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004508
Hila Gonene35e4d22012-06-27 17:19:42 +03004509 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004510}
Johannes Berg4c476992010-10-04 21:36:35 +02004511
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004512static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4513{
Johannes Berg4c476992010-10-04 21:36:35 +02004514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4515 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004516 u8 *dst = NULL;
4517 u8 *next_hop = NULL;
4518
4519 if (!info->attrs[NL80211_ATTR_MAC])
4520 return -EINVAL;
4521
4522 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4523 return -EINVAL;
4524
4525 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4526 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4527
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (!rdev->ops->add_mpath)
4529 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004530
Johannes Berg4c476992010-10-04 21:36:35 +02004531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4532 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004533
Hila Gonene35e4d22012-06-27 17:19:42 +03004534 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004535}
4536
4537static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4538{
Johannes Berg4c476992010-10-04 21:36:35 +02004539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4540 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004541 u8 *dst = NULL;
4542
4543 if (info->attrs[NL80211_ATTR_MAC])
4544 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4545
Johannes Berg4c476992010-10-04 21:36:35 +02004546 if (!rdev->ops->del_mpath)
4547 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004548
Hila Gonene35e4d22012-06-27 17:19:42 +03004549 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004550}
4551
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004552static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4553{
Johannes Berg4c476992010-10-04 21:36:35 +02004554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4555 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004556 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004557 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004558 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004559
4560 memset(&params, 0, sizeof(params));
4561 /* default to not changing parameters */
4562 params.use_cts_prot = -1;
4563 params.use_short_preamble = -1;
4564 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004565 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004566 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004567 params.p2p_ctwindow = -1;
4568 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004569
4570 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4571 params.use_cts_prot =
4572 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4573 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4574 params.use_short_preamble =
4575 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4576 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4577 params.use_short_slot_time =
4578 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004579 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4580 params.basic_rates =
4581 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4582 params.basic_rates_len =
4583 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4584 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004585 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4586 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004587 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4588 params.ht_opmode =
4589 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004590
Johannes Berg53cabad2012-11-14 15:17:28 +01004591 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4592 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4593 return -EINVAL;
4594 params.p2p_ctwindow =
4595 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4596 if (params.p2p_ctwindow < 0)
4597 return -EINVAL;
4598 if (params.p2p_ctwindow != 0 &&
4599 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4600 return -EINVAL;
4601 }
4602
4603 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4604 u8 tmp;
4605
4606 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4607 return -EINVAL;
4608 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4609 if (tmp > 1)
4610 return -EINVAL;
4611 params.p2p_opp_ps = tmp;
4612 if (params.p2p_opp_ps &&
4613 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4614 return -EINVAL;
4615 }
4616
Johannes Berg4c476992010-10-04 21:36:35 +02004617 if (!rdev->ops->change_bss)
4618 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004619
Johannes Berg074ac8d2010-09-16 14:58:22 +02004620 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004621 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4622 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004623
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004624 wdev_lock(wdev);
4625 err = rdev_change_bss(rdev, dev, &params);
4626 wdev_unlock(wdev);
4627
4628 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004629}
4630
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004631static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004632 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4633 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4634 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4635 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4636 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4637 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004638 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004639};
4640
4641static int parse_reg_rule(struct nlattr *tb[],
4642 struct ieee80211_reg_rule *reg_rule)
4643{
4644 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4645 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4646
4647 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4648 return -EINVAL;
4649 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4650 return -EINVAL;
4651 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4652 return -EINVAL;
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004653 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4654 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004655 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4656 return -EINVAL;
4657
4658 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4659
4660 freq_range->start_freq_khz =
4661 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4662 freq_range->end_freq_khz =
4663 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
Janusz Dziedzicb0dfd2e2014-02-20 13:52:16 +01004664 freq_range->max_bandwidth_khz =
4665 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004666
4667 power_rule->max_eirp =
4668 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4669
4670 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4671 power_rule->max_antenna_gain =
4672 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4673
Janusz Dziedzic089027e2014-02-21 19:46:12 +01004674 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4675 reg_rule->dfs_cac_ms =
4676 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4677
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004678 return 0;
4679}
4680
4681static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4682{
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004683 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004684 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004685
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004686 /*
4687 * You should only get this when cfg80211 hasn't yet initialized
4688 * completely when built-in to the kernel right between the time
4689 * window between nl80211_init() and regulatory_init(), if that is
4690 * even possible.
4691 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004692 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004693 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004694
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004695 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4696 user_reg_hint_type =
4697 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4698 else
4699 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4700
4701 switch (user_reg_hint_type) {
4702 case NL80211_USER_REG_HINT_USER:
4703 case NL80211_USER_REG_HINT_CELL_BASE:
Ilan Peer52616f22014-02-25 16:26:00 +02004704 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4705 return -EINVAL;
4706
4707 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4708 return regulatory_hint_user(data, user_reg_hint_type);
4709 case NL80211_USER_REG_HINT_INDOOR:
4710 return regulatory_hint_indoor_user();
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004711 default:
4712 return -EINVAL;
4713 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004714}
4715
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004716static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004717 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004718{
Johannes Berg4c476992010-10-04 21:36:35 +02004719 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004720 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004721 struct wireless_dev *wdev = dev->ieee80211_ptr;
4722 struct mesh_config cur_params;
4723 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004724 void *hdr;
4725 struct nlattr *pinfoattr;
4726 struct sk_buff *msg;
4727
Johannes Berg29cbe682010-12-03 09:20:44 +01004728 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4729 return -EOPNOTSUPP;
4730
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004731 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004732 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004733
Johannes Berg29cbe682010-12-03 09:20:44 +01004734 wdev_lock(wdev);
4735 /* If not connected, get default parameters */
4736 if (!wdev->mesh_id_len)
4737 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4738 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004739 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004740 wdev_unlock(wdev);
4741
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004742 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004743 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004744
4745 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004747 if (!msg)
4748 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004749 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004750 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004751 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004752 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004753 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004754 if (!pinfoattr)
4755 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004756 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4757 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4758 cur_params.dot11MeshRetryTimeout) ||
4759 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4760 cur_params.dot11MeshConfirmTimeout) ||
4761 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4762 cur_params.dot11MeshHoldingTimeout) ||
4763 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4764 cur_params.dot11MeshMaxPeerLinks) ||
4765 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4766 cur_params.dot11MeshMaxRetries) ||
4767 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4768 cur_params.dot11MeshTTL) ||
4769 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4770 cur_params.element_ttl) ||
4771 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4772 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004773 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4774 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004775 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4776 cur_params.dot11MeshHWMPmaxPREQretries) ||
4777 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4778 cur_params.path_refresh_time) ||
4779 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4780 cur_params.min_discovery_timeout) ||
4781 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4782 cur_params.dot11MeshHWMPactivePathTimeout) ||
4783 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4784 cur_params.dot11MeshHWMPpreqMinInterval) ||
4785 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4786 cur_params.dot11MeshHWMPperrMinInterval) ||
4787 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4788 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4789 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4790 cur_params.dot11MeshHWMPRootMode) ||
4791 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4792 cur_params.dot11MeshHWMPRannInterval) ||
4793 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4794 cur_params.dot11MeshGateAnnouncementProtocol) ||
4795 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4796 cur_params.dot11MeshForwarding) ||
4797 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004798 cur_params.rssi_threshold) ||
4799 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004800 cur_params.ht_opmode) ||
4801 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4802 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4803 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004804 cur_params.dot11MeshHWMProotInterval) ||
4805 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004806 cur_params.dot11MeshHWMPconfirmationInterval) ||
4807 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4808 cur_params.power_mode) ||
4809 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004810 cur_params.dot11MeshAwakeWindowDuration) ||
4811 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4812 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004813 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004814 nla_nest_end(msg, pinfoattr);
4815 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004816 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004817
Johannes Berg3b858752009-03-12 09:55:09 +01004818 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004819 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004820 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004821 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004822 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004823}
4824
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004825static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004826 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4827 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4828 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4829 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4830 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4831 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004832 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004833 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004834 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004835 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4836 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4837 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4838 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4839 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004840 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004841 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004842 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004843 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004844 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004845 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004846 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4847 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004848 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4849 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004850 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004851 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4852 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004853 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004854};
4855
Javier Cardonac80d5452010-12-16 17:37:49 -08004856static const struct nla_policy
4857 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004858 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004859 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4860 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004861 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004862 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004863 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004864 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004865 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004866 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004867};
4868
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004869static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004870 struct mesh_config *cfg,
4871 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004872{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004873 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004874 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004875
Marco Porschea54fba2013-01-07 16:04:48 +01004876#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4877do { \
4878 if (tb[attr]) { \
4879 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4880 return -EINVAL; \
4881 cfg->param = fn(tb[attr]); \
4882 mask |= (1 << (attr - 1)); \
4883 } \
4884} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004885
4886
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004887 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004888 return -EINVAL;
4889 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004890 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004891 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004892 return -EINVAL;
4893
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004894 /* This makes sure that there aren't more than 32 mesh config
4895 * parameters (otherwise our bitfield scheme would not work.) */
4896 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4897
4898 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004899 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004900 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4901 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004902 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004903 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4904 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004905 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004906 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4907 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004908 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004909 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4910 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004911 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004912 mask, NL80211_MESHCONF_MAX_RETRIES,
4913 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004914 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004915 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004917 mask, NL80211_MESHCONF_ELEMENT_TTL,
4918 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004920 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4921 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4923 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004924 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4925 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004927 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4928 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004930 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4931 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004933 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4934 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004935 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4936 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004937 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4938 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004939 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004940 1, 65535, mask,
4941 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004942 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004944 1, 65535, mask,
4945 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004946 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004947 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004948 dot11MeshHWMPnetDiameterTraversalTime,
4949 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004950 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4951 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004952 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4953 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4954 nla_get_u8);
4955 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4956 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004957 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004958 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004959 dot11MeshGateAnnouncementProtocol, 0, 1,
4960 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004961 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004962 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004963 mask, NL80211_MESHCONF_FORWARDING,
4964 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004965 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004966 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004967 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004968 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004969 mask, NL80211_MESHCONF_HT_OPMODE,
4970 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004971 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004972 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004973 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4974 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004975 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004976 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4977 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004978 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004979 dot11MeshHWMPconfirmationInterval,
4980 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004981 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4982 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004983 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4984 NL80211_MESH_POWER_ACTIVE,
4985 NL80211_MESH_POWER_MAX,
4986 mask, NL80211_MESHCONF_POWER_MODE,
4987 nla_get_u32);
4988 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4989 0, 65535, mask,
4990 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004991 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4992 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4993 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004994 if (mask_out)
4995 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004996
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004997 return 0;
4998
4999#undef FILL_IN_MESH_PARAM_IF_SET
5000}
5001
Javier Cardonac80d5452010-12-16 17:37:49 -08005002static int nl80211_parse_mesh_setup(struct genl_info *info,
5003 struct mesh_setup *setup)
5004{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08005006 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5007
5008 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5009 return -EINVAL;
5010 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5011 info->attrs[NL80211_ATTR_MESH_SETUP],
5012 nl80211_mesh_setup_params_policy))
5013 return -EINVAL;
5014
Javier Cardonad299a1f2012-03-31 11:31:33 -07005015 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5016 setup->sync_method =
5017 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5018 IEEE80211_SYNC_METHOD_VENDOR :
5019 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5020
Javier Cardonac80d5452010-12-16 17:37:49 -08005021 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5022 setup->path_sel_proto =
5023 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5024 IEEE80211_PATH_PROTOCOL_VENDOR :
5025 IEEE80211_PATH_PROTOCOL_HWMP;
5026
5027 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5028 setup->path_metric =
5029 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5030 IEEE80211_PATH_METRIC_VENDOR :
5031 IEEE80211_PATH_METRIC_AIRTIME;
5032
Javier Cardona581a8b02011-04-07 15:08:27 -07005033
5034 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08005035 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07005036 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08005037 if (!is_valid_ie_attr(ieattr))
5038 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07005039 setup->ie = nla_data(ieattr);
5040 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08005041 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005042 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5043 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5044 return -EINVAL;
5045 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07005046 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5047 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08005048 if (setup->is_secure)
5049 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08005050
Colleen Twitty6e16d902013-05-08 11:45:59 -07005051 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5052 if (!setup->user_mpm)
5053 return -EINVAL;
5054 setup->auth_id =
5055 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5056 }
5057
Javier Cardonac80d5452010-12-16 17:37:49 -08005058 return 0;
5059}
5060
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005061static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01005062 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005063{
5064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5065 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005066 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005067 struct mesh_config cfg;
5068 u32 mask;
5069 int err;
5070
Johannes Berg29cbe682010-12-03 09:20:44 +01005071 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5072 return -EOPNOTSUPP;
5073
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005074 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005075 return -EOPNOTSUPP;
5076
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005077 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005078 if (err)
5079 return err;
5080
Johannes Berg29cbe682010-12-03 09:20:44 +01005081 wdev_lock(wdev);
5082 if (!wdev->mesh_id_len)
5083 err = -ENOLINK;
5084
5085 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005086 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005087
5088 wdev_unlock(wdev);
5089
5090 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005091}
5092
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005093static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5094{
Johannes Berg458f4f92012-12-06 15:47:38 +01005095 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005096 struct sk_buff *msg;
5097 void *hdr = NULL;
5098 struct nlattr *nl_reg_rules;
5099 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005100
5101 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005102 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005103
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005104 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005105 if (!msg)
5106 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005107
Eric W. Biederman15e47302012-09-07 20:12:54 +00005108 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005109 NL80211_CMD_GET_REG);
5110 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005111 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005112
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005113 if (reg_last_request_cell_base() &&
5114 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5115 NL80211_USER_REG_HINT_CELL_BASE))
5116 goto nla_put_failure;
5117
Johannes Berg458f4f92012-12-06 15:47:38 +01005118 rcu_read_lock();
5119 regdom = rcu_dereference(cfg80211_regdomain);
5120
5121 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5122 (regdom->dfs_region &&
5123 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5124 goto nla_put_failure_rcu;
5125
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005126 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5127 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005128 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005129
Johannes Berg458f4f92012-12-06 15:47:38 +01005130 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005131 struct nlattr *nl_reg_rule;
5132 const struct ieee80211_reg_rule *reg_rule;
5133 const struct ieee80211_freq_range *freq_range;
5134 const struct ieee80211_power_rule *power_rule;
Janusz Dziedzic97524822014-01-30 09:52:20 +01005135 unsigned int max_bandwidth_khz;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005136
Johannes Berg458f4f92012-12-06 15:47:38 +01005137 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005138 freq_range = &reg_rule->freq_range;
5139 power_rule = &reg_rule->power_rule;
5140
5141 nl_reg_rule = nla_nest_start(msg, i);
5142 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005143 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005144
Janusz Dziedzic97524822014-01-30 09:52:20 +01005145 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5146 if (!max_bandwidth_khz)
5147 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5148 reg_rule);
5149
David S. Miller9360ffd2012-03-29 04:41:26 -04005150 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5151 reg_rule->flags) ||
5152 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5153 freq_range->start_freq_khz) ||
5154 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5155 freq_range->end_freq_khz) ||
5156 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
Janusz Dziedzic97524822014-01-30 09:52:20 +01005157 max_bandwidth_khz) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005158 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5159 power_rule->max_antenna_gain) ||
5160 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
Janusz Dziedzic089027e2014-02-21 19:46:12 +01005161 power_rule->max_eirp) ||
5162 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5163 reg_rule->dfs_cac_ms))
Johannes Berg458f4f92012-12-06 15:47:38 +01005164 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005165
5166 nla_nest_end(msg, nl_reg_rule);
5167 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005168 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005169
5170 nla_nest_end(msg, nl_reg_rules);
5171
5172 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005173 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005174
Johannes Berg458f4f92012-12-06 15:47:38 +01005175nla_put_failure_rcu:
5176 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005177nla_put_failure:
5178 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005179put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005180 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005181 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005182}
5183
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005184static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5185{
5186 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5187 struct nlattr *nl_reg_rule;
5188 char *alpha2 = NULL;
5189 int rem_reg_rules = 0, r = 0;
5190 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005191 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005192 struct ieee80211_regdomain *rd = NULL;
5193
5194 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5195 return -EINVAL;
5196
5197 if (!info->attrs[NL80211_ATTR_REG_RULES])
5198 return -EINVAL;
5199
5200 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5201
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005202 if (info->attrs[NL80211_ATTR_DFS_REGION])
5203 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5204
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005205 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005206 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005207 num_rules++;
5208 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005209 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005210 }
5211
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005212 if (!reg_is_valid_request(alpha2))
5213 return -EINVAL;
5214
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005215 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005216 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005217
5218 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005219 if (!rd)
5220 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005221
5222 rd->n_reg_rules = num_rules;
5223 rd->alpha2[0] = alpha2[0];
5224 rd->alpha2[1] = alpha2[1];
5225
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005226 /*
5227 * Disable DFS master mode if the DFS region was
5228 * not supported or known on this kernel.
5229 */
5230 if (reg_supported_dfs_region(dfs_region))
5231 rd->dfs_region = dfs_region;
5232
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005233 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005234 rem_reg_rules) {
Johannes Bergae811e22014-01-24 10:17:47 +01005235 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5236 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5237 reg_rule_policy);
5238 if (r)
5239 goto bad_reg;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005240 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5241 if (r)
5242 goto bad_reg;
5243
5244 rule_idx++;
5245
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005246 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5247 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005248 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005249 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005250 }
5251
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005252 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005253 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005254 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005255
Johannes Bergd2372b32008-10-24 20:32:20 +02005256 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005257 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005258 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005259}
5260
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005261static int validate_scan_freqs(struct nlattr *freqs)
5262{
5263 struct nlattr *attr1, *attr2;
5264 int n_channels = 0, tmp1, tmp2;
5265
5266 nla_for_each_nested(attr1, freqs, tmp1) {
5267 n_channels++;
5268 /*
5269 * Some hardware has a limited channel list for
5270 * scanning, and it is pretty much nonsensical
5271 * to scan for a channel twice, so disallow that
5272 * and don't require drivers to check that the
5273 * channel list they get isn't longer than what
5274 * they can scan, as long as they can scan all
5275 * the channels they registered at once.
5276 */
5277 nla_for_each_nested(attr2, freqs, tmp2)
5278 if (attr1 != attr2 &&
5279 nla_get_u32(attr1) == nla_get_u32(attr2))
5280 return 0;
5281 }
5282
5283 return n_channels;
5284}
5285
Johannes Berg2a519312009-02-10 21:25:55 +01005286static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5287{
Johannes Berg4c476992010-10-04 21:36:35 +02005288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005289 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005290 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005291 struct nlattr *attr;
5292 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005293 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005294 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005295
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005296 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5297 return -EINVAL;
5298
Johannes Berg79c97e92009-07-07 03:56:12 +02005299 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005300
Johannes Berg4c476992010-10-04 21:36:35 +02005301 if (!rdev->ops->scan)
5302 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005303
Johannes Bergf9d15d12014-01-22 11:14:19 +02005304 if (rdev->scan_req || rdev->scan_msg) {
Johannes Bergf9f47522013-03-19 15:04:07 +01005305 err = -EBUSY;
5306 goto unlock;
5307 }
Johannes Berg2a519312009-02-10 21:25:55 +01005308
5309 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005310 n_channels = validate_scan_freqs(
5311 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005312 if (!n_channels) {
5313 err = -EINVAL;
5314 goto unlock;
5315 }
Johannes Berg2a519312009-02-10 21:25:55 +01005316 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005317 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01005318 }
5319
5320 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5321 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5322 n_ssids++;
5323
Johannes Bergf9f47522013-03-19 15:04:07 +01005324 if (n_ssids > wiphy->max_scan_ssids) {
5325 err = -EINVAL;
5326 goto unlock;
5327 }
Johannes Berg2a519312009-02-10 21:25:55 +01005328
Jouni Malinen70692ad2009-02-16 19:39:13 +02005329 if (info->attrs[NL80211_ATTR_IE])
5330 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5331 else
5332 ie_len = 0;
5333
Johannes Bergf9f47522013-03-19 15:04:07 +01005334 if (ie_len > wiphy->max_scan_ie_len) {
5335 err = -EINVAL;
5336 goto unlock;
5337 }
Johannes Berg18a83652009-03-31 12:12:05 +02005338
Johannes Berg2a519312009-02-10 21:25:55 +01005339 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005340 + sizeof(*request->ssids) * n_ssids
5341 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005342 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005343 if (!request) {
5344 err = -ENOMEM;
5345 goto unlock;
5346 }
Johannes Berg2a519312009-02-10 21:25:55 +01005347
Johannes Berg2a519312009-02-10 21:25:55 +01005348 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005349 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005350 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005351 if (ie_len) {
5352 if (request->ssids)
5353 request->ie = (void *)(request->ssids + n_ssids);
5354 else
5355 request->ie = (void *)(request->channels + n_channels);
5356 }
Johannes Berg2a519312009-02-10 21:25:55 +01005357
Johannes Berg584991d2009-11-02 13:32:03 +01005358 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005359 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5360 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005361 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005362 struct ieee80211_channel *chan;
5363
5364 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5365
5366 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005367 err = -EINVAL;
5368 goto out_free;
5369 }
Johannes Berg584991d2009-11-02 13:32:03 +01005370
5371 /* ignore disabled channels */
5372 if (chan->flags & IEEE80211_CHAN_DISABLED)
5373 continue;
5374
5375 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005376 i++;
5377 }
5378 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005379 enum ieee80211_band band;
5380
Johannes Berg2a519312009-02-10 21:25:55 +01005381 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005382 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5383 int j;
5384 if (!wiphy->bands[band])
5385 continue;
5386 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005387 struct ieee80211_channel *chan;
5388
5389 chan = &wiphy->bands[band]->channels[j];
5390
5391 if (chan->flags & IEEE80211_CHAN_DISABLED)
5392 continue;
5393
5394 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005395 i++;
5396 }
5397 }
5398 }
5399
Johannes Berg584991d2009-11-02 13:32:03 +01005400 if (!i) {
5401 err = -EINVAL;
5402 goto out_free;
5403 }
5404
5405 request->n_channels = i;
5406
Johannes Berg2a519312009-02-10 21:25:55 +01005407 i = 0;
5408 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5409 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005410 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005411 err = -EINVAL;
5412 goto out_free;
5413 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005414 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005415 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005416 i++;
5417 }
5418 }
5419
Jouni Malinen70692ad2009-02-16 19:39:13 +02005420 if (info->attrs[NL80211_ATTR_IE]) {
5421 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005422 memcpy((void *)request->ie,
5423 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005424 request->ie_len);
5425 }
5426
Johannes Berg34850ab2011-07-18 18:08:35 +02005427 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005428 if (wiphy->bands[i])
5429 request->rates[i] =
5430 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005431
5432 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5433 nla_for_each_nested(attr,
5434 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5435 tmp) {
5436 enum ieee80211_band band = nla_type(attr);
5437
Dan Carpenter84404622011-07-29 11:52:18 +03005438 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005439 err = -EINVAL;
5440 goto out_free;
5441 }
Felix Fietkau1b09cd82013-11-20 19:40:41 +01005442
5443 if (!wiphy->bands[band])
5444 continue;
5445
Johannes Berg34850ab2011-07-18 18:08:35 +02005446 err = ieee80211_get_ratemask(wiphy->bands[band],
5447 nla_data(attr),
5448 nla_len(attr),
5449 &request->rates[band]);
5450 if (err)
5451 goto out_free;
5452 }
5453 }
5454
Sam Leffler46856bb2012-10-11 21:03:32 -07005455 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005456 request->flags = nla_get_u32(
5457 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005458 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5459 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005460 err = -EOPNOTSUPP;
5461 goto out_free;
5462 }
5463 }
Sam Lefflered4737712012-10-11 21:03:31 -07005464
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305465 request->no_cck =
5466 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5467
Johannes Bergfd014282012-06-18 19:17:03 +02005468 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005469 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005470 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005471
Johannes Berg79c97e92009-07-07 03:56:12 +02005472 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005473 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005474
Johannes Berg463d0182009-07-14 00:33:35 +02005475 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005476 nl80211_send_scan_start(rdev, wdev);
5477 if (wdev->netdev)
5478 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005479 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005480 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005481 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005482 kfree(request);
5483 }
Johannes Berg3b858752009-03-12 09:55:09 +01005484
Johannes Bergf9f47522013-03-19 15:04:07 +01005485 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005486 return err;
5487}
5488
Luciano Coelho807f8a82011-05-11 17:09:35 +03005489static int nl80211_start_sched_scan(struct sk_buff *skb,
5490 struct genl_info *info)
5491{
5492 struct cfg80211_sched_scan_request *request;
5493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5494 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005495 struct nlattr *attr;
5496 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005497 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005498 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005499 enum ieee80211_band band;
5500 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005501 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Johannes Bergea73cbc2014-01-24 10:53:53 +01005502 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005503
5504 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5505 !rdev->ops->sched_scan_start)
5506 return -EOPNOTSUPP;
5507
5508 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5509 return -EINVAL;
5510
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005511 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5512 return -EINVAL;
5513
5514 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5515 if (interval == 0)
5516 return -EINVAL;
5517
Luciano Coelho807f8a82011-05-11 17:09:35 +03005518 wiphy = &rdev->wiphy;
5519
5520 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5521 n_channels = validate_scan_freqs(
5522 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5523 if (!n_channels)
5524 return -EINVAL;
5525 } else {
Ilan Peerbdfbec22014-01-09 11:37:23 +02005526 n_channels = ieee80211_get_num_supported_channels(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005527 }
5528
5529 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5530 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5531 tmp)
5532 n_ssids++;
5533
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005534 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005535 return -EINVAL;
5536
Johannes Bergea73cbc2014-01-24 10:53:53 +01005537 /*
5538 * First, count the number of 'real' matchsets. Due to an issue with
5539 * the old implementation, matchsets containing only the RSSI attribute
5540 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5541 * RSSI for all matchsets, rather than their own matchset for reporting
5542 * all APs with a strong RSSI. This is needed to be compatible with
5543 * older userspace that treated a matchset with only the RSSI as the
5544 * global RSSI for all other matchsets - if there are other matchsets.
5545 */
5546 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005547 nla_for_each_nested(attr,
5548 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
Johannes Bergea73cbc2014-01-24 10:53:53 +01005549 tmp) {
5550 struct nlattr *rssi;
5551
5552 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5553 nla_data(attr), nla_len(attr),
5554 nl80211_match_policy);
5555 if (err)
5556 return err;
5557 /* add other standalone attributes here */
5558 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5559 n_match_sets++;
5560 continue;
5561 }
5562 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5563 if (rssi)
5564 default_match_rssi = nla_get_s32(rssi);
5565 }
5566 }
5567
5568 /* However, if there's no other matchset, add the RSSI one */
5569 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5570 n_match_sets = 1;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005571
5572 if (n_match_sets > wiphy->max_match_sets)
5573 return -EINVAL;
5574
Luciano Coelho807f8a82011-05-11 17:09:35 +03005575 if (info->attrs[NL80211_ATTR_IE])
5576 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5577 else
5578 ie_len = 0;
5579
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005580 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005581 return -EINVAL;
5582
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005583 if (rdev->sched_scan_req) {
5584 err = -EINPROGRESS;
5585 goto out;
5586 }
5587
Luciano Coelho807f8a82011-05-11 17:09:35 +03005588 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005589 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005590 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005591 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005592 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005593 if (!request) {
5594 err = -ENOMEM;
5595 goto out;
5596 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005597
5598 if (n_ssids)
5599 request->ssids = (void *)&request->channels[n_channels];
5600 request->n_ssids = n_ssids;
5601 if (ie_len) {
5602 if (request->ssids)
5603 request->ie = (void *)(request->ssids + n_ssids);
5604 else
5605 request->ie = (void *)(request->channels + n_channels);
5606 }
5607
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005608 if (n_match_sets) {
5609 if (request->ie)
5610 request->match_sets = (void *)(request->ie + ie_len);
5611 else if (request->ssids)
5612 request->match_sets =
5613 (void *)(request->ssids + n_ssids);
5614 else
5615 request->match_sets =
5616 (void *)(request->channels + n_channels);
5617 }
5618 request->n_match_sets = n_match_sets;
5619
Luciano Coelho807f8a82011-05-11 17:09:35 +03005620 i = 0;
5621 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5622 /* user specified, bail out if channel not found */
5623 nla_for_each_nested(attr,
5624 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5625 tmp) {
5626 struct ieee80211_channel *chan;
5627
5628 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5629
5630 if (!chan) {
5631 err = -EINVAL;
5632 goto out_free;
5633 }
5634
5635 /* ignore disabled channels */
5636 if (chan->flags & IEEE80211_CHAN_DISABLED)
5637 continue;
5638
5639 request->channels[i] = chan;
5640 i++;
5641 }
5642 } else {
5643 /* all channels */
5644 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5645 int j;
5646 if (!wiphy->bands[band])
5647 continue;
5648 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5649 struct ieee80211_channel *chan;
5650
5651 chan = &wiphy->bands[band]->channels[j];
5652
5653 if (chan->flags & IEEE80211_CHAN_DISABLED)
5654 continue;
5655
5656 request->channels[i] = chan;
5657 i++;
5658 }
5659 }
5660 }
5661
5662 if (!i) {
5663 err = -EINVAL;
5664 goto out_free;
5665 }
5666
5667 request->n_channels = i;
5668
5669 i = 0;
5670 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5671 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5672 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005673 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005674 err = -EINVAL;
5675 goto out_free;
5676 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005677 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005678 memcpy(request->ssids[i].ssid, nla_data(attr),
5679 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005680 i++;
5681 }
5682 }
5683
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005684 i = 0;
5685 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5686 nla_for_each_nested(attr,
5687 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5688 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005689 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005690
Johannes Bergae811e22014-01-24 10:17:47 +01005691 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5692 nla_data(attr), nla_len(attr),
5693 nl80211_match_policy);
5694 if (err)
5695 goto out_free;
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005696 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005697 if (ssid) {
Johannes Bergea73cbc2014-01-24 10:53:53 +01005698 if (WARN_ON(i >= n_match_sets)) {
5699 /* this indicates a programming error,
5700 * the loop above should have verified
5701 * things properly
5702 */
5703 err = -EINVAL;
5704 goto out_free;
5705 }
5706
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005707 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5708 err = -EINVAL;
5709 goto out_free;
5710 }
5711 memcpy(request->match_sets[i].ssid.ssid,
5712 nla_data(ssid), nla_len(ssid));
5713 request->match_sets[i].ssid.ssid_len =
5714 nla_len(ssid);
Johannes Bergea73cbc2014-01-24 10:53:53 +01005715 /* special attribute - old implemenation w/a */
5716 request->match_sets[i].rssi_thold =
5717 default_match_rssi;
5718 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5719 if (rssi)
5720 request->match_sets[i].rssi_thold =
5721 nla_get_s32(rssi);
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005722 }
5723 i++;
5724 }
Johannes Bergea73cbc2014-01-24 10:53:53 +01005725
5726 /* there was no other matchset, so the RSSI one is alone */
5727 if (i == 0)
5728 request->match_sets[0].rssi_thold = default_match_rssi;
5729
5730 request->min_rssi_thold = INT_MAX;
5731 for (i = 0; i < n_match_sets; i++)
5732 request->min_rssi_thold =
5733 min(request->match_sets[i].rssi_thold,
5734 request->min_rssi_thold);
5735 } else {
5736 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005737 }
5738
Johannes Berg9900e482014-02-04 21:01:25 +01005739 if (ie_len) {
5740 request->ie_len = ie_len;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005741 memcpy((void *)request->ie,
5742 nla_data(info->attrs[NL80211_ATTR_IE]),
5743 request->ie_len);
5744 }
5745
Sam Leffler46856bb2012-10-11 21:03:32 -07005746 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005747 request->flags = nla_get_u32(
5748 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005749 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5750 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005751 err = -EOPNOTSUPP;
5752 goto out_free;
5753 }
5754 }
Sam Lefflered4737712012-10-11 21:03:31 -07005755
Luciano Coelho807f8a82011-05-11 17:09:35 +03005756 request->dev = dev;
5757 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005758 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005759 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005760
Hila Gonene35e4d22012-06-27 17:19:42 +03005761 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005762 if (!err) {
5763 rdev->sched_scan_req = request;
5764 nl80211_send_sched_scan(rdev, dev,
5765 NL80211_CMD_START_SCHED_SCAN);
5766 goto out;
5767 }
5768
5769out_free:
5770 kfree(request);
5771out:
5772 return err;
5773}
5774
5775static int nl80211_stop_sched_scan(struct sk_buff *skb,
5776 struct genl_info *info)
5777{
5778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5779
5780 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5781 !rdev->ops->sched_scan_stop)
5782 return -EOPNOTSUPP;
5783
Johannes Berg5fe231e2013-05-08 21:45:15 +02005784 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005785}
5786
Simon Wunderlich04f39042013-02-08 18:16:19 +01005787static int nl80211_start_radar_detection(struct sk_buff *skb,
5788 struct genl_info *info)
5789{
5790 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5791 struct net_device *dev = info->user_ptr[1];
5792 struct wireless_dev *wdev = dev->ieee80211_ptr;
5793 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005794 enum nl80211_dfs_regions dfs_region;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005795 unsigned int cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005796 int err;
5797
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005798 dfs_region = reg_get_dfs_region(wdev->wiphy);
5799 if (dfs_region == NL80211_DFS_UNSET)
5800 return -EINVAL;
5801
Simon Wunderlich04f39042013-02-08 18:16:19 +01005802 err = nl80211_parse_chandef(rdev, info, &chandef);
5803 if (err)
5804 return err;
5805
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005806 if (netif_carrier_ok(dev))
5807 return -EBUSY;
5808
Simon Wunderlich04f39042013-02-08 18:16:19 +01005809 if (wdev->cac_started)
5810 return -EBUSY;
5811
Luciano Coelho2beb6da2014-02-18 11:40:36 +02005812 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
5813 NL80211_IFTYPE_UNSPECIFIED);
Simon Wunderlich04f39042013-02-08 18:16:19 +01005814 if (err < 0)
5815 return err;
5816
5817 if (err == 0)
5818 return -EINVAL;
5819
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005820 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005821 return -EINVAL;
5822
5823 if (!rdev->ops->start_radar_detection)
5824 return -EOPNOTSUPP;
5825
Simon Wunderlich04f39042013-02-08 18:16:19 +01005826 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5827 chandef.chan, CHAN_MODE_SHARED,
5828 BIT(chandef.width));
5829 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005830 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005831
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005832 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
5833 if (WARN_ON(!cac_time_ms))
5834 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
5835
5836 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
5837 cac_time_ms);
Simon Wunderlich04f39042013-02-08 18:16:19 +01005838 if (!err) {
Michal Kazior9e0e2962014-01-29 14:22:27 +01005839 wdev->chandef = chandef;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005840 wdev->cac_started = true;
5841 wdev->cac_start_time = jiffies;
Janusz Dziedzic31559f32014-02-21 19:46:13 +01005842 wdev->cac_time_ms = cac_time_ms;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005843 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005844 return err;
5845}
5846
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005847static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5848{
5849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5850 struct net_device *dev = info->user_ptr[1];
5851 struct wireless_dev *wdev = dev->ieee80211_ptr;
5852 struct cfg80211_csa_settings params;
5853 /* csa_attrs is defined static to avoid waste of stack size - this
5854 * function is called under RTNL lock, so this should not be a problem.
5855 */
5856 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5857 u8 radar_detect_width = 0;
5858 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005859 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005860
5861 if (!rdev->ops->channel_switch ||
5862 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5863 return -EOPNOTSUPP;
5864
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005865 switch (dev->ieee80211_ptr->iftype) {
5866 case NL80211_IFTYPE_AP:
5867 case NL80211_IFTYPE_P2P_GO:
5868 need_new_beacon = true;
5869
5870 /* useless if AP is not running */
5871 if (!wdev->beacon_interval)
Johannes Berg1ff79df2014-01-22 10:05:27 +01005872 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005873 break;
5874 case NL80211_IFTYPE_ADHOC:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005875 if (!wdev->ssid_len)
5876 return -ENOTCONN;
5877 break;
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005878 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ff79df2014-01-22 10:05:27 +01005879 if (!wdev->mesh_id_len)
5880 return -ENOTCONN;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005881 break;
5882 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005883 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005884 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005885
5886 memset(&params, 0, sizeof(params));
5887
5888 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5889 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5890 return -EINVAL;
5891
5892 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005893 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005894 return -EINVAL;
5895
5896 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5897
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005898 if (!need_new_beacon)
5899 goto skip_beacons;
5900
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005901 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5902 if (err)
5903 return err;
5904
5905 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5906 info->attrs[NL80211_ATTR_CSA_IES],
5907 nl80211_policy);
5908 if (err)
5909 return err;
5910
5911 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5912 if (err)
5913 return err;
5914
5915 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5916 return -EINVAL;
5917
5918 params.counter_offset_beacon =
5919 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5920 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5921 return -EINVAL;
5922
5923 /* sanity check - counters should be the same */
5924 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5925 params.count)
5926 return -EINVAL;
5927
5928 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5929 params.counter_offset_presp =
5930 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5931 if (params.counter_offset_presp >=
5932 params.beacon_csa.probe_resp_len)
5933 return -EINVAL;
5934
5935 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5936 params.count)
5937 return -EINVAL;
5938 }
5939
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005940skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005941 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5942 if (err)
5943 return err;
5944
Ilan Peer174e0cd2014-02-23 09:13:01 +02005945 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
5946 wdev->iftype))
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005947 return -EINVAL;
5948
Luciano Coelho2beb6da2014-02-18 11:40:36 +02005949 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5950 &params.chandef,
5951 wdev->iftype);
5952 if (err < 0)
5953 return err;
5954
5955 if (err > 0) {
5956 radar_detect_width = BIT(params.chandef.width);
5957 params.radar_required = true;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005958 }
5959
5960 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5961 params.chandef.chan,
5962 CHAN_MODE_SHARED,
5963 radar_detect_width);
5964 if (err)
5965 return err;
5966
5967 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5968 params.block_tx = true;
5969
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005970 wdev_lock(wdev);
5971 err = rdev_channel_switch(rdev, dev, &params);
5972 wdev_unlock(wdev);
5973
5974 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005975}
5976
Johannes Berg9720bb32011-06-21 09:45:33 +02005977static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5978 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005979 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005980 struct wireless_dev *wdev,
5981 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005982{
Johannes Berg48ab9052009-07-10 18:42:31 +02005983 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005984 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005985 void *hdr;
5986 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005987 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005988
5989 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005990
Eric W. Biederman15e47302012-09-07 20:12:54 +00005991 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005992 NL80211_CMD_NEW_SCAN_RESULTS);
5993 if (!hdr)
5994 return -1;
5995
Johannes Berg9720bb32011-06-21 09:45:33 +02005996 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5997
Johannes Berg97990a02013-04-19 01:02:55 +02005998 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5999 goto nla_put_failure;
6000 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04006001 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6002 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02006003 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6004 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006005
6006 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6007 if (!bss)
6008 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04006009 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01006010 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04006011 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01006012
6013 rcu_read_lock();
6014 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006015 if (ies) {
6016 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6017 goto fail_unlock_rcu;
6018 tsf = true;
6019 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6020 ies->len, ies->data))
6021 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006022 }
6023 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01006024 if (ies) {
6025 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6026 goto fail_unlock_rcu;
6027 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6028 ies->len, ies->data))
6029 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01006030 }
6031 rcu_read_unlock();
6032
David S. Miller9360ffd2012-03-29 04:41:26 -04006033 if (res->beacon_interval &&
6034 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6035 goto nla_put_failure;
6036 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6037 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02006038 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04006039 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6040 jiffies_to_msecs(jiffies - intbss->ts)))
6041 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006042
Johannes Berg77965c92009-02-18 18:45:06 +01006043 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01006044 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04006045 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6046 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006047 break;
6048 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006049 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6050 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01006051 break;
6052 default:
6053 break;
6054 }
6055
Johannes Berg48ab9052009-07-10 18:42:31 +02006056 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02006057 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02006058 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04006059 if (intbss == wdev->current_bss &&
6060 nla_put_u32(msg, NL80211_BSS_STATUS,
6061 NL80211_BSS_STATUS_ASSOCIATED))
6062 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006063 break;
6064 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04006065 if (intbss == wdev->current_bss &&
6066 nla_put_u32(msg, NL80211_BSS_STATUS,
6067 NL80211_BSS_STATUS_IBSS_JOINED))
6068 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02006069 break;
6070 default:
6071 break;
6072 }
6073
Johannes Berg2a519312009-02-10 21:25:55 +01006074 nla_nest_end(msg, bss);
6075
6076 return genlmsg_end(msg, hdr);
6077
Johannes Berg8cef2c92013-02-05 16:54:31 +01006078 fail_unlock_rcu:
6079 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01006080 nla_put_failure:
6081 genlmsg_cancel(msg, hdr);
6082 return -EMSGSIZE;
6083}
6084
Johannes Berg97990a02013-04-19 01:02:55 +02006085static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01006086{
Johannes Berg48ab9052009-07-10 18:42:31 +02006087 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01006088 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02006089 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02006090 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01006091 int err;
6092
Johannes Berg97990a02013-04-19 01:02:55 +02006093 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006094 if (err)
6095 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01006096
Johannes Berg48ab9052009-07-10 18:42:31 +02006097 wdev_lock(wdev);
6098 spin_lock_bh(&rdev->bss_lock);
6099 cfg80211_bss_expire(rdev);
6100
Johannes Berg9720bb32011-06-21 09:45:33 +02006101 cb->seq = rdev->bss_generation;
6102
Johannes Berg48ab9052009-07-10 18:42:31 +02006103 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01006104 if (++idx <= start)
6105 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02006106 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01006107 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02006108 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006109 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02006110 break;
Johannes Berg2a519312009-02-10 21:25:55 +01006111 }
6112 }
6113
Johannes Berg48ab9052009-07-10 18:42:31 +02006114 spin_unlock_bh(&rdev->bss_lock);
6115 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006116
Johannes Berg97990a02013-04-19 01:02:55 +02006117 cb->args[2] = idx;
6118 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006119
Johannes Berg67748892010-10-04 21:14:06 +02006120 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01006121}
6122
Eric W. Biederman15e47302012-09-07 20:12:54 +00006123static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01006124 int flags, struct net_device *dev,
6125 struct survey_info *survey)
6126{
6127 void *hdr;
6128 struct nlattr *infoattr;
6129
Eric W. Biederman15e47302012-09-07 20:12:54 +00006130 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01006131 NL80211_CMD_NEW_SURVEY_RESULTS);
6132 if (!hdr)
6133 return -ENOMEM;
6134
David S. Miller9360ffd2012-03-29 04:41:26 -04006135 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6136 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006137
6138 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6139 if (!infoattr)
6140 goto nla_put_failure;
6141
David S. Miller9360ffd2012-03-29 04:41:26 -04006142 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6143 survey->channel->center_freq))
6144 goto nla_put_failure;
6145
6146 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6147 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6148 goto nla_put_failure;
6149 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6150 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6151 goto nla_put_failure;
6152 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6153 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6154 survey->channel_time))
6155 goto nla_put_failure;
6156 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6157 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6158 survey->channel_time_busy))
6159 goto nla_put_failure;
6160 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6161 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6162 survey->channel_time_ext_busy))
6163 goto nla_put_failure;
6164 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6165 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6166 survey->channel_time_rx))
6167 goto nla_put_failure;
6168 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6169 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6170 survey->channel_time_tx))
6171 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006172
6173 nla_nest_end(msg, infoattr);
6174
6175 return genlmsg_end(msg, hdr);
6176
6177 nla_put_failure:
6178 genlmsg_cancel(msg, hdr);
6179 return -EMSGSIZE;
6180}
6181
6182static int nl80211_dump_survey(struct sk_buff *skb,
6183 struct netlink_callback *cb)
6184{
6185 struct survey_info survey;
6186 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006187 struct wireless_dev *wdev;
6188 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006189 int res;
6190
Johannes Berg97990a02013-04-19 01:02:55 +02006191 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006192 if (res)
6193 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006194
Johannes Berg97990a02013-04-19 01:02:55 +02006195 if (!wdev->netdev) {
6196 res = -EINVAL;
6197 goto out_err;
6198 }
6199
Holger Schurig61fa7132009-11-11 12:25:40 +01006200 if (!dev->ops->dump_survey) {
6201 res = -EOPNOTSUPP;
6202 goto out_err;
6203 }
6204
6205 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006206 struct ieee80211_channel *chan;
6207
Johannes Berg97990a02013-04-19 01:02:55 +02006208 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006209 if (res == -ENOENT)
6210 break;
6211 if (res)
6212 goto out_err;
6213
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006214 /* Survey without a channel doesn't make sense */
6215 if (!survey.channel) {
6216 res = -EINVAL;
6217 goto out;
6218 }
6219
6220 chan = ieee80211_get_channel(&dev->wiphy,
6221 survey.channel->center_freq);
6222 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6223 survey_idx++;
6224 continue;
6225 }
6226
Holger Schurig61fa7132009-11-11 12:25:40 +01006227 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006228 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006229 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006230 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006231 goto out;
6232 survey_idx++;
6233 }
6234
6235 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006236 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006237 res = skb->len;
6238 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006239 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006240 return res;
6241}
6242
Samuel Ortizb23aa672009-07-01 21:26:54 +02006243static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6244{
6245 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6246 NL80211_WPA_VERSION_2));
6247}
6248
Jouni Malinen636a5d32009-03-19 13:39:22 +02006249static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6250{
Johannes Berg4c476992010-10-04 21:36:35 +02006251 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6252 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006253 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006254 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6255 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006256 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006257 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006258 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006259
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006260 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6261 return -EINVAL;
6262
6263 if (!info->attrs[NL80211_ATTR_MAC])
6264 return -EINVAL;
6265
Jouni Malinen17780922009-03-27 20:52:47 +02006266 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6267 return -EINVAL;
6268
Johannes Berg19957bb2009-07-02 17:20:43 +02006269 if (!info->attrs[NL80211_ATTR_SSID])
6270 return -EINVAL;
6271
6272 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6273 return -EINVAL;
6274
Johannes Bergfffd0932009-07-08 14:22:54 +02006275 err = nl80211_parse_key(info, &key);
6276 if (err)
6277 return err;
6278
6279 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006280 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6281 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006282 if (!key.p.key || !key.p.key_len)
6283 return -EINVAL;
6284 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6285 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6286 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6287 key.p.key_len != WLAN_KEY_LEN_WEP104))
6288 return -EINVAL;
6289 if (key.idx > 4)
6290 return -EINVAL;
6291 } else {
6292 key.p.key_len = 0;
6293 key.p.key = NULL;
6294 }
6295
Johannes Bergafea0b72010-08-10 09:46:42 +02006296 if (key.idx >= 0) {
6297 int i;
6298 bool ok = false;
6299 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6300 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6301 ok = true;
6302 break;
6303 }
6304 }
Johannes Berg4c476992010-10-04 21:36:35 +02006305 if (!ok)
6306 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006307 }
6308
Johannes Berg4c476992010-10-04 21:36:35 +02006309 if (!rdev->ops->auth)
6310 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006311
Johannes Berg074ac8d2010-09-16 14:58:22 +02006312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6314 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006315
Johannes Berg19957bb2009-07-02 17:20:43 +02006316 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen664834d2014-01-15 00:01:44 +02006317 chan = nl80211_get_valid_chan(&rdev->wiphy,
6318 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6319 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006320 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006321
Johannes Berg19957bb2009-07-02 17:20:43 +02006322 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6323 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6324
6325 if (info->attrs[NL80211_ATTR_IE]) {
6326 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6327 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6328 }
6329
6330 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006331 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006332 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006333
Jouni Malinene39e5b52012-09-30 19:29:39 +03006334 if (auth_type == NL80211_AUTHTYPE_SAE &&
6335 !info->attrs[NL80211_ATTR_SAE_DATA])
6336 return -EINVAL;
6337
6338 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6339 if (auth_type != NL80211_AUTHTYPE_SAE)
6340 return -EINVAL;
6341 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6342 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6343 /* need to include at least Auth Transaction and Status Code */
6344 if (sae_data_len < 4)
6345 return -EINVAL;
6346 }
6347
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006348 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6349
Johannes Berg95de8172012-01-20 13:55:25 +01006350 /*
6351 * Since we no longer track auth state, ignore
6352 * requests to only change local state.
6353 */
6354 if (local_state_change)
6355 return 0;
6356
Johannes Berg91bf9b22013-05-15 17:44:01 +02006357 wdev_lock(dev->ieee80211_ptr);
6358 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6359 ssid, ssid_len, ie, ie_len,
6360 key.p.key, key.p.key_len, key.idx,
6361 sae_data, sae_data_len);
6362 wdev_unlock(dev->ieee80211_ptr);
6363 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006364}
6365
Johannes Bergc0692b82010-08-27 14:26:53 +03006366static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6367 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006368 struct cfg80211_crypto_settings *settings,
6369 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006370{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006371 memset(settings, 0, sizeof(*settings));
6372
Samuel Ortizb23aa672009-07-01 21:26:54 +02006373 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6374
Johannes Bergc0692b82010-08-27 14:26:53 +03006375 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6376 u16 proto;
6377 proto = nla_get_u16(
6378 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6379 settings->control_port_ethertype = cpu_to_be16(proto);
6380 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6381 proto != ETH_P_PAE)
6382 return -EINVAL;
6383 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6384 settings->control_port_no_encrypt = true;
6385 } else
6386 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6387
Samuel Ortizb23aa672009-07-01 21:26:54 +02006388 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6389 void *data;
6390 int len, i;
6391
6392 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6393 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6394 settings->n_ciphers_pairwise = len / sizeof(u32);
6395
6396 if (len % sizeof(u32))
6397 return -EINVAL;
6398
Johannes Berg3dc27d22009-07-02 21:36:37 +02006399 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006400 return -EINVAL;
6401
6402 memcpy(settings->ciphers_pairwise, data, len);
6403
6404 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006405 if (!cfg80211_supported_cipher_suite(
6406 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006407 settings->ciphers_pairwise[i]))
6408 return -EINVAL;
6409 }
6410
6411 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6412 settings->cipher_group =
6413 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006414 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6415 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006416 return -EINVAL;
6417 }
6418
6419 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6420 settings->wpa_versions =
6421 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6422 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6423 return -EINVAL;
6424 }
6425
6426 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6427 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006428 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006429
6430 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6431 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6432 settings->n_akm_suites = len / sizeof(u32);
6433
6434 if (len % sizeof(u32))
6435 return -EINVAL;
6436
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006437 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6438 return -EINVAL;
6439
Samuel Ortizb23aa672009-07-01 21:26:54 +02006440 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006441 }
6442
6443 return 0;
6444}
6445
Jouni Malinen636a5d32009-03-19 13:39:22 +02006446static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6447{
Johannes Berg4c476992010-10-04 21:36:35 +02006448 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6449 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006450 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006451 struct cfg80211_assoc_request req = {};
6452 const u8 *bssid, *ssid;
6453 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006454
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006455 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6456 return -EINVAL;
6457
6458 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006459 !info->attrs[NL80211_ATTR_SSID] ||
6460 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006461 return -EINVAL;
6462
Johannes Berg4c476992010-10-04 21:36:35 +02006463 if (!rdev->ops->assoc)
6464 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006465
Johannes Berg074ac8d2010-09-16 14:58:22 +02006466 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006467 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6468 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006469
Johannes Berg19957bb2009-07-02 17:20:43 +02006470 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006471
Jouni Malinen664834d2014-01-15 00:01:44 +02006472 chan = nl80211_get_valid_chan(&rdev->wiphy,
6473 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6474 if (!chan)
Johannes Berg4c476992010-10-04 21:36:35 +02006475 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006476
Johannes Berg19957bb2009-07-02 17:20:43 +02006477 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6478 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006479
6480 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006481 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6482 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006483 }
6484
Jouni Malinendc6382c2009-05-06 22:09:37 +03006485 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006486 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006487 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006488 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006489 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006490 else if (mfp != NL80211_MFP_NO)
6491 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006492 }
6493
Johannes Berg3e5d7642009-07-07 14:37:26 +02006494 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006495 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006496
Ben Greear7e7c8922011-11-18 11:31:59 -08006497 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006498 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006499
6500 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006501 memcpy(&req.ht_capa_mask,
6502 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6503 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006504
6505 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006506 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006507 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006508 memcpy(&req.ht_capa,
6509 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6510 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006511 }
6512
Johannes Bergee2aca32013-02-21 17:36:01 +01006513 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006514 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006515
6516 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006517 memcpy(&req.vht_capa_mask,
6518 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6519 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006520
6521 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006522 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006523 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006524 memcpy(&req.vht_capa,
6525 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6526 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006527 }
6528
Johannes Bergf62fab72013-02-21 20:09:09 +01006529 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006530 if (!err) {
6531 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006532 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6533 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006534 wdev_unlock(dev->ieee80211_ptr);
6535 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006536
Jouni Malinen636a5d32009-03-19 13:39:22 +02006537 return err;
6538}
6539
6540static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6541{
Johannes Berg4c476992010-10-04 21:36:35 +02006542 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6543 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006544 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006545 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006546 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006547 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006548
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006549 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6550 return -EINVAL;
6551
6552 if (!info->attrs[NL80211_ATTR_MAC])
6553 return -EINVAL;
6554
6555 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6556 return -EINVAL;
6557
Johannes Berg4c476992010-10-04 21:36:35 +02006558 if (!rdev->ops->deauth)
6559 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006560
Johannes Berg074ac8d2010-09-16 14:58:22 +02006561 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006562 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6563 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006564
Johannes Berg19957bb2009-07-02 17:20:43 +02006565 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006566
Johannes Berg19957bb2009-07-02 17:20:43 +02006567 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6568 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006569 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006570 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006571 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006572
6573 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006574 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6575 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006576 }
6577
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006578 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6579
Johannes Berg91bf9b22013-05-15 17:44:01 +02006580 wdev_lock(dev->ieee80211_ptr);
6581 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6582 local_state_change);
6583 wdev_unlock(dev->ieee80211_ptr);
6584 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006585}
6586
6587static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6588{
Johannes Berg4c476992010-10-04 21:36:35 +02006589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6590 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006591 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006592 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006593 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006594 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006595
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006596 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6597 return -EINVAL;
6598
6599 if (!info->attrs[NL80211_ATTR_MAC])
6600 return -EINVAL;
6601
6602 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6603 return -EINVAL;
6604
Johannes Berg4c476992010-10-04 21:36:35 +02006605 if (!rdev->ops->disassoc)
6606 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006607
Johannes Berg074ac8d2010-09-16 14:58:22 +02006608 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006609 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6610 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006611
Johannes Berg19957bb2009-07-02 17:20:43 +02006612 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006613
Johannes Berg19957bb2009-07-02 17:20:43 +02006614 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6615 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006616 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006617 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006618 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006619
6620 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006621 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6622 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006623 }
6624
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006625 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6626
Johannes Berg91bf9b22013-05-15 17:44:01 +02006627 wdev_lock(dev->ieee80211_ptr);
6628 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6629 local_state_change);
6630 wdev_unlock(dev->ieee80211_ptr);
6631 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006632}
6633
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006634static bool
6635nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6636 int mcast_rate[IEEE80211_NUM_BANDS],
6637 int rateval)
6638{
6639 struct wiphy *wiphy = &rdev->wiphy;
6640 bool found = false;
6641 int band, i;
6642
6643 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6644 struct ieee80211_supported_band *sband;
6645
6646 sband = wiphy->bands[band];
6647 if (!sband)
6648 continue;
6649
6650 for (i = 0; i < sband->n_bitrates; i++) {
6651 if (sband->bitrates[i].bitrate == rateval) {
6652 mcast_rate[band] = i + 1;
6653 found = true;
6654 break;
6655 }
6656 }
6657 }
6658
6659 return found;
6660}
6661
Johannes Berg04a773a2009-04-19 21:24:32 +02006662static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6663{
Johannes Berg4c476992010-10-04 21:36:35 +02006664 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6665 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006666 struct cfg80211_ibss_params ibss;
6667 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006668 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006669 int err;
6670
Johannes Berg8e30bc52009-04-22 17:45:38 +02006671 memset(&ibss, 0, sizeof(ibss));
6672
Johannes Berg04a773a2009-04-19 21:24:32 +02006673 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6674 return -EINVAL;
6675
Johannes Berg683b6d32012-11-08 21:25:48 +01006676 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006677 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6678 return -EINVAL;
6679
Johannes Berg8e30bc52009-04-22 17:45:38 +02006680 ibss.beacon_interval = 100;
6681
6682 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6683 ibss.beacon_interval =
6684 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6685 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6686 return -EINVAL;
6687 }
6688
Johannes Berg4c476992010-10-04 21:36:35 +02006689 if (!rdev->ops->join_ibss)
6690 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006691
Johannes Berg4c476992010-10-04 21:36:35 +02006692 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6693 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006694
Johannes Berg79c97e92009-07-07 03:56:12 +02006695 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006696
Johannes Berg39193492011-09-16 13:45:25 +02006697 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006698 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006699
6700 if (!is_valid_ether_addr(ibss.bssid))
6701 return -EINVAL;
6702 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006703 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6704 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6705
6706 if (info->attrs[NL80211_ATTR_IE]) {
6707 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6708 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6709 }
6710
Johannes Berg683b6d32012-11-08 21:25:48 +01006711 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6712 if (err)
6713 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006714
Ilan Peer174e0cd2014-02-23 09:13:01 +02006715 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
6716 NL80211_IFTYPE_ADHOC))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006717 return -EINVAL;
6718
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006719 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006720 case NL80211_CHAN_WIDTH_5:
6721 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006722 case NL80211_CHAN_WIDTH_20_NOHT:
6723 break;
6724 case NL80211_CHAN_WIDTH_20:
6725 case NL80211_CHAN_WIDTH_40:
6726 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6727 break;
6728 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006729 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006730 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006731
Johannes Berg04a773a2009-04-19 21:24:32 +02006732 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006733 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006734
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006735 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6736 u8 *rates =
6737 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6738 int n_rates =
6739 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6740 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006741 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006742
Johannes Berg34850ab2011-07-18 18:08:35 +02006743 err = ieee80211_get_ratemask(sband, rates, n_rates,
6744 &ibss.basic_rates);
6745 if (err)
6746 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006747 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006748
Simon Wunderlich803768f2013-06-28 10:39:58 +02006749 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6750 memcpy(&ibss.ht_capa_mask,
6751 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6752 sizeof(ibss.ht_capa_mask));
6753
6754 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6755 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6756 return -EINVAL;
6757 memcpy(&ibss.ht_capa,
6758 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6759 sizeof(ibss.ht_capa));
6760 }
6761
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006762 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6763 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6764 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6765 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006766
Johannes Berg4c476992010-10-04 21:36:35 +02006767 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306768 bool no_ht = false;
6769
Johannes Berg4c476992010-10-04 21:36:35 +02006770 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306771 info->attrs[NL80211_ATTR_KEYS],
6772 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006773 if (IS_ERR(connkeys))
6774 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306775
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006776 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6777 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306778 kfree(connkeys);
6779 return -EINVAL;
6780 }
Johannes Berg4c476992010-10-04 21:36:35 +02006781 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006782
Antonio Quartulli267335d2012-01-31 20:25:47 +01006783 ibss.control_port =
6784 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6785
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006786 ibss.userspace_handles_dfs =
6787 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6788
Johannes Berg4c476992010-10-04 21:36:35 +02006789 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006790 if (err)
6791 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006792 return err;
6793}
6794
6795static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6796{
Johannes Berg4c476992010-10-04 21:36:35 +02006797 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6798 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006799
Johannes Berg4c476992010-10-04 21:36:35 +02006800 if (!rdev->ops->leave_ibss)
6801 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006802
Johannes Berg4c476992010-10-04 21:36:35 +02006803 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6804 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006805
Johannes Berg4c476992010-10-04 21:36:35 +02006806 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006807}
6808
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006809static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6810{
6811 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6812 struct net_device *dev = info->user_ptr[1];
6813 int mcast_rate[IEEE80211_NUM_BANDS];
6814 u32 nla_rate;
6815 int err;
6816
6817 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6818 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6819 return -EOPNOTSUPP;
6820
6821 if (!rdev->ops->set_mcast_rate)
6822 return -EOPNOTSUPP;
6823
6824 memset(mcast_rate, 0, sizeof(mcast_rate));
6825
6826 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6827 return -EINVAL;
6828
6829 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6830 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6831 return -EINVAL;
6832
6833 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6834
6835 return err;
6836}
6837
Johannes Bergad7e7182013-11-13 13:37:47 +01006838static struct sk_buff *
6839__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6840 int approxlen, u32 portid, u32 seq,
6841 enum nl80211_commands cmd,
Johannes Berg567ffc32013-12-18 14:43:31 +01006842 enum nl80211_attrs attr,
6843 const struct nl80211_vendor_cmd_info *info,
6844 gfp_t gfp)
Johannes Bergad7e7182013-11-13 13:37:47 +01006845{
6846 struct sk_buff *skb;
6847 void *hdr;
6848 struct nlattr *data;
6849
6850 skb = nlmsg_new(approxlen + 100, gfp);
6851 if (!skb)
6852 return NULL;
6853
6854 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6855 if (!hdr) {
6856 kfree_skb(skb);
6857 return NULL;
6858 }
6859
6860 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6861 goto nla_put_failure;
Johannes Berg567ffc32013-12-18 14:43:31 +01006862
6863 if (info) {
6864 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6865 info->vendor_id))
6866 goto nla_put_failure;
6867 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6868 info->subcmd))
6869 goto nla_put_failure;
6870 }
6871
Johannes Bergad7e7182013-11-13 13:37:47 +01006872 data = nla_nest_start(skb, attr);
6873
6874 ((void **)skb->cb)[0] = rdev;
6875 ((void **)skb->cb)[1] = hdr;
6876 ((void **)skb->cb)[2] = data;
6877
6878 return skb;
6879
6880 nla_put_failure:
6881 kfree_skb(skb);
6882 return NULL;
6883}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006884
Johannes Berge03ad6e2014-01-01 17:22:30 +01006885struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6886 enum nl80211_commands cmd,
6887 enum nl80211_attrs attr,
6888 int vendor_event_idx,
6889 int approxlen, gfp_t gfp)
6890{
6891 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6892 const struct nl80211_vendor_cmd_info *info;
6893
6894 switch (cmd) {
6895 case NL80211_CMD_TESTMODE:
6896 if (WARN_ON(vendor_event_idx != -1))
6897 return NULL;
6898 info = NULL;
6899 break;
6900 case NL80211_CMD_VENDOR:
6901 if (WARN_ON(vendor_event_idx < 0 ||
6902 vendor_event_idx >= wiphy->n_vendor_events))
6903 return NULL;
6904 info = &wiphy->vendor_events[vendor_event_idx];
6905 break;
6906 default:
6907 WARN_ON(1);
6908 return NULL;
6909 }
6910
6911 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6912 cmd, attr, info, gfp);
6913}
6914EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6915
6916void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6917{
6918 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6919 void *hdr = ((void **)skb->cb)[1];
6920 struct nlattr *data = ((void **)skb->cb)[2];
6921 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6922
6923 nla_nest_end(skb, data);
6924 genlmsg_end(skb, hdr);
6925
6926 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6927 mcgrp = NL80211_MCGRP_VENDOR;
6928
6929 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6930 mcgrp, gfp);
6931}
6932EXPORT_SYMBOL(__cfg80211_send_event_skb);
6933
Johannes Bergaff89a92009-07-01 21:26:51 +02006934#ifdef CONFIG_NL80211_TESTMODE
Johannes Bergaff89a92009-07-01 21:26:51 +02006935static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6936{
Johannes Berg4c476992010-10-04 21:36:35 +02006937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006938 struct wireless_dev *wdev =
6939 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006940 int err;
6941
David Spinadelfc73f112013-07-31 18:04:15 +03006942 if (!rdev->ops->testmode_cmd)
6943 return -EOPNOTSUPP;
6944
6945 if (IS_ERR(wdev)) {
6946 err = PTR_ERR(wdev);
6947 if (err != -EINVAL)
6948 return err;
6949 wdev = NULL;
6950 } else if (wdev->wiphy != &rdev->wiphy) {
6951 return -EINVAL;
6952 }
6953
Johannes Bergaff89a92009-07-01 21:26:51 +02006954 if (!info->attrs[NL80211_ATTR_TESTDATA])
6955 return -EINVAL;
6956
Johannes Bergad7e7182013-11-13 13:37:47 +01006957 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006958 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006959 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6960 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006961 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006962
Johannes Bergaff89a92009-07-01 21:26:51 +02006963 return err;
6964}
6965
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006966static int nl80211_testmode_dump(struct sk_buff *skb,
6967 struct netlink_callback *cb)
6968{
Johannes Berg00918d32011-12-13 17:22:05 +01006969 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006970 int err;
6971 long phy_idx;
6972 void *data = NULL;
6973 int data_len = 0;
6974
Johannes Berg5fe231e2013-05-08 21:45:15 +02006975 rtnl_lock();
6976
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006977 if (cb->args[0]) {
6978 /*
6979 * 0 is a valid index, but not valid for args[0],
6980 * so we need to offset by 1.
6981 */
6982 phy_idx = cb->args[0] - 1;
6983 } else {
6984 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6985 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6986 nl80211_policy);
6987 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006988 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006989
Johannes Berg2bd7e352012-06-15 14:23:16 +02006990 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6991 nl80211_fam.attrbuf);
6992 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006993 err = PTR_ERR(rdev);
6994 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006995 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006996 phy_idx = rdev->wiphy_idx;
6997 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006998
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006999 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7000 cb->args[1] =
7001 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7002 }
7003
7004 if (cb->args[1]) {
7005 data = nla_data((void *)cb->args[1]);
7006 data_len = nla_len((void *)cb->args[1]);
7007 }
7008
Johannes Berg00918d32011-12-13 17:22:05 +01007009 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7010 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02007011 err = -ENOENT;
7012 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007013 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007014
Johannes Berg00918d32011-12-13 17:22:05 +01007015 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007016 err = -EOPNOTSUPP;
7017 goto out_err;
7018 }
7019
7020 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00007021 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007022 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7023 NL80211_CMD_TESTMODE);
7024 struct nlattr *tmdata;
7025
Dan Carpentercb35fba2013-08-14 14:50:01 +03007026 if (!hdr)
7027 break;
7028
David S. Miller9360ffd2012-03-29 04:41:26 -04007029 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007030 genlmsg_cancel(skb, hdr);
7031 break;
7032 }
7033
7034 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7035 if (!tmdata) {
7036 genlmsg_cancel(skb, hdr);
7037 break;
7038 }
Hila Gonene35e4d22012-06-27 17:19:42 +03007039 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007040 nla_nest_end(skb, tmdata);
7041
7042 if (err == -ENOBUFS || err == -ENOENT) {
7043 genlmsg_cancel(skb, hdr);
7044 break;
7045 } else if (err) {
7046 genlmsg_cancel(skb, hdr);
7047 goto out_err;
7048 }
7049
7050 genlmsg_end(skb, hdr);
7051 }
7052
7053 err = skb->len;
7054 /* see above */
7055 cb->args[0] = phy_idx + 1;
7056 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02007057 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007058 return err;
7059}
Johannes Bergaff89a92009-07-01 21:26:51 +02007060#endif
7061
Samuel Ortizb23aa672009-07-01 21:26:54 +02007062static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7063{
Johannes Berg4c476992010-10-04 21:36:35 +02007064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7065 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007066 struct cfg80211_connect_params connect;
7067 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02007068 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007069 int err;
7070
7071 memset(&connect, 0, sizeof(connect));
7072
7073 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7074 return -EINVAL;
7075
7076 if (!info->attrs[NL80211_ATTR_SSID] ||
7077 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7078 return -EINVAL;
7079
7080 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7081 connect.auth_type =
7082 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03007083 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7084 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02007085 return -EINVAL;
7086 } else
7087 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7088
7089 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7090
Johannes Bergc0692b82010-08-27 14:26:53 +03007091 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02007092 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007093 if (err)
7094 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007095
Johannes Berg074ac8d2010-09-16 14:58:22 +02007096 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007097 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7098 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007099
Johannes Berg79c97e92009-07-07 03:56:12 +02007100 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007101
Bala Shanmugam4486ea92012-03-07 17:27:12 +05307102 connect.bg_scan_period = -1;
7103 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7104 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7105 connect.bg_scan_period =
7106 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7107 }
7108
Samuel Ortizb23aa672009-07-01 21:26:54 +02007109 if (info->attrs[NL80211_ATTR_MAC])
7110 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen1df4a512014-01-15 00:00:47 +02007111 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7112 connect.bssid_hint =
7113 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007114 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7115 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7116
7117 if (info->attrs[NL80211_ATTR_IE]) {
7118 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7119 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7120 }
7121
Jouni Malinencee00a92013-01-15 17:15:57 +02007122 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7123 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7124 if (connect.mfp != NL80211_MFP_REQUIRED &&
7125 connect.mfp != NL80211_MFP_NO)
7126 return -EINVAL;
7127 } else {
7128 connect.mfp = NL80211_MFP_NO;
7129 }
7130
Samuel Ortizb23aa672009-07-01 21:26:54 +02007131 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007132 connect.channel = nl80211_get_valid_chan(
7133 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7134 if (!connect.channel)
Johannes Berg4c476992010-10-04 21:36:35 +02007135 return -EINVAL;
Jouni Malinen1df4a512014-01-15 00:00:47 +02007136 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
Jouni Malinen664834d2014-01-15 00:01:44 +02007137 connect.channel_hint = nl80211_get_valid_chan(
7138 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7139 if (!connect.channel_hint)
Jouni Malinen1df4a512014-01-15 00:00:47 +02007140 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007141 }
7142
Johannes Bergfffd0932009-07-08 14:22:54 +02007143 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7144 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05307145 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02007146 if (IS_ERR(connkeys))
7147 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02007148 }
7149
Ben Greear7e7c8922011-11-18 11:31:59 -08007150 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7151 connect.flags |= ASSOC_REQ_DISABLE_HT;
7152
7153 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7154 memcpy(&connect.ht_capa_mask,
7155 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7156 sizeof(connect.ht_capa_mask));
7157
7158 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007159 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7160 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08007161 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08007162 }
Ben Greear7e7c8922011-11-18 11:31:59 -08007163 memcpy(&connect.ht_capa,
7164 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7165 sizeof(connect.ht_capa));
7166 }
7167
Johannes Bergee2aca32013-02-21 17:36:01 +01007168 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7169 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7170
7171 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7172 memcpy(&connect.vht_capa_mask,
7173 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7174 sizeof(connect.vht_capa_mask));
7175
7176 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7177 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7178 kfree(connkeys);
7179 return -EINVAL;
7180 }
7181 memcpy(&connect.vht_capa,
7182 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7183 sizeof(connect.vht_capa));
7184 }
7185
Johannes Berg83739b02013-05-15 17:44:01 +02007186 wdev_lock(dev->ieee80211_ptr);
7187 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7188 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007189 if (err)
7190 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007191 return err;
7192}
7193
7194static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7195{
Johannes Berg4c476992010-10-04 21:36:35 +02007196 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7197 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007198 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007199 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007200
7201 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7202 reason = WLAN_REASON_DEAUTH_LEAVING;
7203 else
7204 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7205
7206 if (reason == 0)
7207 return -EINVAL;
7208
Johannes Berg074ac8d2010-09-16 14:58:22 +02007209 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007210 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7211 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007212
Johannes Berg83739b02013-05-15 17:44:01 +02007213 wdev_lock(dev->ieee80211_ptr);
7214 ret = cfg80211_disconnect(rdev, dev, reason, true);
7215 wdev_unlock(dev->ieee80211_ptr);
7216 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007217}
7218
Johannes Berg463d0182009-07-14 00:33:35 +02007219static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7220{
Johannes Berg4c476992010-10-04 21:36:35 +02007221 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007222 struct net *net;
7223 int err;
7224 u32 pid;
7225
7226 if (!info->attrs[NL80211_ATTR_PID])
7227 return -EINVAL;
7228
7229 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7230
Johannes Berg463d0182009-07-14 00:33:35 +02007231 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007232 if (IS_ERR(net))
7233 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007234
7235 err = 0;
7236
7237 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007238 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7239 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007240
Johannes Berg463d0182009-07-14 00:33:35 +02007241 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007242 return err;
7243}
7244
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007245static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7246{
Johannes Berg4c476992010-10-04 21:36:35 +02007247 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007248 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7249 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007250 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007251 struct cfg80211_pmksa pmksa;
7252
7253 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7254
7255 if (!info->attrs[NL80211_ATTR_MAC])
7256 return -EINVAL;
7257
7258 if (!info->attrs[NL80211_ATTR_PMKID])
7259 return -EINVAL;
7260
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007261 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7262 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7263
Johannes Berg074ac8d2010-09-16 14:58:22 +02007264 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007265 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7266 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007267
7268 switch (info->genlhdr->cmd) {
7269 case NL80211_CMD_SET_PMKSA:
7270 rdev_ops = rdev->ops->set_pmksa;
7271 break;
7272 case NL80211_CMD_DEL_PMKSA:
7273 rdev_ops = rdev->ops->del_pmksa;
7274 break;
7275 default:
7276 WARN_ON(1);
7277 break;
7278 }
7279
Johannes Berg4c476992010-10-04 21:36:35 +02007280 if (!rdev_ops)
7281 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007282
Johannes Berg4c476992010-10-04 21:36:35 +02007283 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007284}
7285
7286static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7287{
Johannes Berg4c476992010-10-04 21:36:35 +02007288 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7289 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007290
Johannes Berg074ac8d2010-09-16 14:58:22 +02007291 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007292 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7293 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007294
Johannes Berg4c476992010-10-04 21:36:35 +02007295 if (!rdev->ops->flush_pmksa)
7296 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007297
Hila Gonene35e4d22012-06-27 17:19:42 +03007298 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007299}
7300
Arik Nemtsov109086c2011-09-28 14:12:50 +03007301static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7302{
7303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7304 struct net_device *dev = info->user_ptr[1];
7305 u8 action_code, dialog_token;
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307306 u32 peer_capability = 0;
Arik Nemtsov109086c2011-09-28 14:12:50 +03007307 u16 status_code;
7308 u8 *peer;
7309
7310 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7311 !rdev->ops->tdls_mgmt)
7312 return -EOPNOTSUPP;
7313
7314 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7315 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7316 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7317 !info->attrs[NL80211_ATTR_IE] ||
7318 !info->attrs[NL80211_ATTR_MAC])
7319 return -EINVAL;
7320
7321 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7322 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7323 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7324 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307325 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7326 peer_capability =
7327 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007328
Hila Gonene35e4d22012-06-27 17:19:42 +03007329 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
Sunil Dutt Undekaridf942e72014-02-20 16:22:09 +05307330 dialog_token, status_code, peer_capability,
Hila Gonene35e4d22012-06-27 17:19:42 +03007331 nla_data(info->attrs[NL80211_ATTR_IE]),
7332 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007333}
7334
7335static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7336{
7337 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7338 struct net_device *dev = info->user_ptr[1];
7339 enum nl80211_tdls_operation operation;
7340 u8 *peer;
7341
7342 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7343 !rdev->ops->tdls_oper)
7344 return -EOPNOTSUPP;
7345
7346 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7347 !info->attrs[NL80211_ATTR_MAC])
7348 return -EINVAL;
7349
7350 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7351 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7352
Hila Gonene35e4d22012-06-27 17:19:42 +03007353 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007354}
7355
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007356static int nl80211_remain_on_channel(struct sk_buff *skb,
7357 struct genl_info *info)
7358{
Johannes Berg4c476992010-10-04 21:36:35 +02007359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007360 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007361 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007362 struct sk_buff *msg;
7363 void *hdr;
7364 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007365 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007366 int err;
7367
7368 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7369 !info->attrs[NL80211_ATTR_DURATION])
7370 return -EINVAL;
7371
7372 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7373
Johannes Berg7c4ef712011-11-18 15:33:48 +01007374 if (!rdev->ops->remain_on_channel ||
7375 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007376 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007377
Johannes Bergebf348f2012-06-01 12:50:54 +02007378 /*
7379 * We should be on that channel for at least a minimum amount of
7380 * time (10ms) but no longer than the driver supports.
7381 */
7382 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7383 duration > rdev->wiphy.max_remain_on_channel_duration)
7384 return -EINVAL;
7385
Johannes Berg683b6d32012-11-08 21:25:48 +01007386 err = nl80211_parse_chandef(rdev, info, &chandef);
7387 if (err)
7388 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007389
7390 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007391 if (!msg)
7392 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007393
Eric W. Biederman15e47302012-09-07 20:12:54 +00007394 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007395 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007396 if (!hdr) {
7397 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007398 goto free_msg;
7399 }
7400
Johannes Berg683b6d32012-11-08 21:25:48 +01007401 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7402 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007403
7404 if (err)
7405 goto free_msg;
7406
David S. Miller9360ffd2012-03-29 04:41:26 -04007407 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7408 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007409
7410 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007411
7412 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007413
7414 nla_put_failure:
7415 err = -ENOBUFS;
7416 free_msg:
7417 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007418 return err;
7419}
7420
7421static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7422 struct genl_info *info)
7423{
Johannes Berg4c476992010-10-04 21:36:35 +02007424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007425 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007426 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007427
7428 if (!info->attrs[NL80211_ATTR_COOKIE])
7429 return -EINVAL;
7430
Johannes Berg4c476992010-10-04 21:36:35 +02007431 if (!rdev->ops->cancel_remain_on_channel)
7432 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007433
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007434 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7435
Hila Gonene35e4d22012-06-27 17:19:42 +03007436 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007437}
7438
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007439static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7440 u8 *rates, u8 rates_len)
7441{
7442 u8 i;
7443 u32 mask = 0;
7444
7445 for (i = 0; i < rates_len; i++) {
7446 int rate = (rates[i] & 0x7f) * 5;
7447 int ridx;
7448 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7449 struct ieee80211_rate *srate =
7450 &sband->bitrates[ridx];
7451 if (rate == srate->bitrate) {
7452 mask |= 1 << ridx;
7453 break;
7454 }
7455 }
7456 if (ridx == sband->n_bitrates)
7457 return 0; /* rate not found */
7458 }
7459
7460 return mask;
7461}
7462
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007463static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7464 u8 *rates, u8 rates_len,
7465 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7466{
7467 u8 i;
7468
7469 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7470
7471 for (i = 0; i < rates_len; i++) {
7472 int ridx, rbit;
7473
7474 ridx = rates[i] / 8;
7475 rbit = BIT(rates[i] % 8);
7476
7477 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007478 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007479 return false;
7480
7481 /* check availability */
7482 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7483 mcs[ridx] |= rbit;
7484 else
7485 return false;
7486 }
7487
7488 return true;
7489}
7490
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007491static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7492{
7493 u16 mcs_mask = 0;
7494
7495 switch (vht_mcs_map) {
7496 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7497 break;
7498 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7499 mcs_mask = 0x00FF;
7500 break;
7501 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7502 mcs_mask = 0x01FF;
7503 break;
7504 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7505 mcs_mask = 0x03FF;
7506 break;
7507 default:
7508 break;
7509 }
7510
7511 return mcs_mask;
7512}
7513
7514static void vht_build_mcs_mask(u16 vht_mcs_map,
7515 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7516{
7517 u8 nss;
7518
7519 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7520 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7521 vht_mcs_map >>= 2;
7522 }
7523}
7524
7525static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7526 struct nl80211_txrate_vht *txrate,
7527 u16 mcs[NL80211_VHT_NSS_MAX])
7528{
7529 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7530 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7531 u8 i;
7532
7533 if (!sband->vht_cap.vht_supported)
7534 return false;
7535
7536 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7537
7538 /* Build vht_mcs_mask from VHT capabilities */
7539 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7540
7541 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7542 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7543 mcs[i] = txrate->mcs[i];
7544 else
7545 return false;
7546 }
7547
7548 return true;
7549}
7550
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007551static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007552 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7553 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007554 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7555 .len = NL80211_MAX_SUPP_HT_RATES },
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007556 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007557 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007558};
7559
7560static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7561 struct genl_info *info)
7562{
7563 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007565 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007566 int rem, i;
7567 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007568 struct nlattr *tx_rates;
7569 struct ieee80211_supported_band *sband;
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007570 u16 vht_tx_mcs_map;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007571
Johannes Berg4c476992010-10-04 21:36:35 +02007572 if (!rdev->ops->set_bitrate_mask)
7573 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007574
7575 memset(&mask, 0, sizeof(mask));
7576 /* Default to all rates enabled */
7577 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7578 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007579
7580 if (!sband)
7581 continue;
7582
7583 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007584 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007585 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007586 sizeof(mask.control[i].ht_mcs));
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007587
7588 if (!sband->vht_cap.vht_supported)
7589 continue;
7590
7591 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7592 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007593 }
7594
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007595 /* if no rates are given set it back to the defaults */
7596 if (!info->attrs[NL80211_ATTR_TX_RATES])
7597 goto out;
7598
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007599 /*
7600 * The nested attribute uses enum nl80211_band as the index. This maps
7601 * directly to the enum ieee80211_band values used in cfg80211.
7602 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007603 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Johannes Bergae811e22014-01-24 10:17:47 +01007604 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007605 enum ieee80211_band band = nla_type(tx_rates);
Johannes Bergae811e22014-01-24 10:17:47 +01007606 int err;
7607
Johannes Berg4c476992010-10-04 21:36:35 +02007608 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7609 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007610 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007611 if (sband == NULL)
7612 return -EINVAL;
Johannes Bergae811e22014-01-24 10:17:47 +01007613 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7614 nla_len(tx_rates), nl80211_txattr_policy);
7615 if (err)
7616 return err;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007617 if (tb[NL80211_TXRATE_LEGACY]) {
7618 mask.control[band].legacy = rateset_to_mask(
7619 sband,
7620 nla_data(tb[NL80211_TXRATE_LEGACY]),
7621 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307622 if ((mask.control[band].legacy == 0) &&
7623 nla_len(tb[NL80211_TXRATE_LEGACY]))
7624 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007625 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007626 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007627 if (!ht_rateset_to_mask(
7628 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007629 nla_data(tb[NL80211_TXRATE_HT]),
7630 nla_len(tb[NL80211_TXRATE_HT]),
7631 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007632 return -EINVAL;
7633 }
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007634 if (tb[NL80211_TXRATE_VHT]) {
7635 if (!vht_set_mcs_mask(
7636 sband,
7637 nla_data(tb[NL80211_TXRATE_VHT]),
7638 mask.control[band].vht_mcs))
7639 return -EINVAL;
7640 }
Janusz Dziedzic0b9323f2014-01-08 08:46:02 +01007641 if (tb[NL80211_TXRATE_GI]) {
7642 mask.control[band].gi =
7643 nla_get_u8(tb[NL80211_TXRATE_GI]);
7644 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7645 return -EINVAL;
7646 }
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007647
7648 if (mask.control[band].legacy == 0) {
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007649 /* don't allow empty legacy rates if HT or VHT
7650 * are not even supported.
7651 */
7652 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7653 rdev->wiphy.bands[band]->vht_cap.vht_supported))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007654 return -EINVAL;
7655
7656 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007657 if (mask.control[band].ht_mcs[i])
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007658 goto out;
7659
7660 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7661 if (mask.control[band].vht_mcs[i])
7662 goto out;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007663
7664 /* legacy and mcs rates may not be both empty */
Janusz Dziedzic204e35a2013-12-05 20:42:58 +01007665 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007666 }
7667 }
7668
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007669out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007670 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007671}
7672
Johannes Berg2e161f72010-08-12 15:38:38 +02007673static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007674{
Johannes Berg4c476992010-10-04 21:36:35 +02007675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007676 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007677 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007678
7679 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7680 return -EINVAL;
7681
Johannes Berg2e161f72010-08-12 15:38:38 +02007682 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7683 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007684
Johannes Berg71bbc992012-06-15 15:30:18 +02007685 switch (wdev->iftype) {
7686 case NL80211_IFTYPE_STATION:
7687 case NL80211_IFTYPE_ADHOC:
7688 case NL80211_IFTYPE_P2P_CLIENT:
7689 case NL80211_IFTYPE_AP:
7690 case NL80211_IFTYPE_AP_VLAN:
7691 case NL80211_IFTYPE_MESH_POINT:
7692 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007693 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007694 break;
7695 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007696 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007697 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007698
7699 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007700 if (!rdev->ops->mgmt_tx)
7701 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007702
Eric W. Biederman15e47302012-09-07 20:12:54 +00007703 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007704 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7705 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007706}
7707
Johannes Berg2e161f72010-08-12 15:38:38 +02007708static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007709{
Johannes Berg4c476992010-10-04 21:36:35 +02007710 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007711 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007712 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007713 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007714 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007715 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007716 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007717 struct cfg80211_mgmt_tx_params params = {
7718 .dont_wait_for_ack =
7719 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7720 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007721
Johannes Berg683b6d32012-11-08 21:25:48 +01007722 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007723 return -EINVAL;
7724
Johannes Berg4c476992010-10-04 21:36:35 +02007725 if (!rdev->ops->mgmt_tx)
7726 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007727
Johannes Berg71bbc992012-06-15 15:30:18 +02007728 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007729 case NL80211_IFTYPE_P2P_DEVICE:
7730 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7731 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007732 case NL80211_IFTYPE_STATION:
7733 case NL80211_IFTYPE_ADHOC:
7734 case NL80211_IFTYPE_P2P_CLIENT:
7735 case NL80211_IFTYPE_AP:
7736 case NL80211_IFTYPE_AP_VLAN:
7737 case NL80211_IFTYPE_MESH_POINT:
7738 case NL80211_IFTYPE_P2P_GO:
7739 break;
7740 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007741 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007742 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007743
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007744 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007745 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007746 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007747 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007748
7749 /*
7750 * We should wait on the channel for at least a minimum amount
7751 * of time (10ms) but no longer than the driver supports.
7752 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007753 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7754 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007755 return -EINVAL;
7756
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007757 }
7758
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007759 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007760
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007761 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007762 return -EINVAL;
7763
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007764 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307765
Antonio Quartulliea141b752013-06-11 14:20:03 +02007766 /* get the channel if any has been specified, otherwise pass NULL to
7767 * the driver. The latter will use the current one
7768 */
7769 chandef.chan = NULL;
7770 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7771 err = nl80211_parse_chandef(rdev, info, &chandef);
7772 if (err)
7773 return err;
7774 }
7775
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007776 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007777 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007778
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007779 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007780 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7781 if (!msg)
7782 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007783
Eric W. Biederman15e47302012-09-07 20:12:54 +00007784 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007785 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007786 if (!hdr) {
7787 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007788 goto free_msg;
7789 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007790 }
Johannes Berge247bd902011-11-04 11:18:21 +01007791
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007792 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7793 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7794 params.chan = chandef.chan;
7795 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007796 if (err)
7797 goto free_msg;
7798
Johannes Berge247bd902011-11-04 11:18:21 +01007799 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007800 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7801 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007802
Johannes Berge247bd902011-11-04 11:18:21 +01007803 genlmsg_end(msg, hdr);
7804 return genlmsg_reply(msg, info);
7805 }
7806
7807 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007808
7809 nla_put_failure:
7810 err = -ENOBUFS;
7811 free_msg:
7812 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007813 return err;
7814}
7815
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007816static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7817{
7818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007819 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007820 u64 cookie;
7821
7822 if (!info->attrs[NL80211_ATTR_COOKIE])
7823 return -EINVAL;
7824
7825 if (!rdev->ops->mgmt_tx_cancel_wait)
7826 return -EOPNOTSUPP;
7827
Johannes Berg71bbc992012-06-15 15:30:18 +02007828 switch (wdev->iftype) {
7829 case NL80211_IFTYPE_STATION:
7830 case NL80211_IFTYPE_ADHOC:
7831 case NL80211_IFTYPE_P2P_CLIENT:
7832 case NL80211_IFTYPE_AP:
7833 case NL80211_IFTYPE_AP_VLAN:
7834 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007835 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007836 break;
7837 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007838 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007839 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007840
7841 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7842
Hila Gonene35e4d22012-06-27 17:19:42 +03007843 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007844}
7845
Kalle Valoffb9eb32010-02-17 17:58:10 +02007846static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7847{
Johannes Berg4c476992010-10-04 21:36:35 +02007848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007849 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007850 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007851 u8 ps_state;
7852 bool state;
7853 int err;
7854
Johannes Berg4c476992010-10-04 21:36:35 +02007855 if (!info->attrs[NL80211_ATTR_PS_STATE])
7856 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007857
7858 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7859
Johannes Berg4c476992010-10-04 21:36:35 +02007860 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7861 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007862
7863 wdev = dev->ieee80211_ptr;
7864
Johannes Berg4c476992010-10-04 21:36:35 +02007865 if (!rdev->ops->set_power_mgmt)
7866 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007867
7868 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7869
7870 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007871 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007872
Hila Gonene35e4d22012-06-27 17:19:42 +03007873 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007874 if (!err)
7875 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007876 return err;
7877}
7878
7879static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7880{
Johannes Berg4c476992010-10-04 21:36:35 +02007881 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007882 enum nl80211_ps_state ps_state;
7883 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007884 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007885 struct sk_buff *msg;
7886 void *hdr;
7887 int err;
7888
Kalle Valoffb9eb32010-02-17 17:58:10 +02007889 wdev = dev->ieee80211_ptr;
7890
Johannes Berg4c476992010-10-04 21:36:35 +02007891 if (!rdev->ops->set_power_mgmt)
7892 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007893
7894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007895 if (!msg)
7896 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007897
Eric W. Biederman15e47302012-09-07 20:12:54 +00007898 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007899 NL80211_CMD_GET_POWER_SAVE);
7900 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007901 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007902 goto free_msg;
7903 }
7904
7905 if (wdev->ps)
7906 ps_state = NL80211_PS_ENABLED;
7907 else
7908 ps_state = NL80211_PS_DISABLED;
7909
David S. Miller9360ffd2012-03-29 04:41:26 -04007910 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7911 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007912
7913 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007914 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007915
Johannes Berg4c476992010-10-04 21:36:35 +02007916 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007917 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007918 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007919 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007920 return err;
7921}
7922
Johannes Berg94e860f2014-01-20 23:58:15 +01007923static const struct nla_policy
7924nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007925 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7926 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7927 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007928 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7929 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7930 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007931};
7932
Thomas Pedersen84f10702012-07-12 16:17:33 -07007933static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007934 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007935{
7936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007937 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007938 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007939
Johannes Bergd9d8b012012-11-26 12:51:52 +01007940 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007941 return -EINVAL;
7942
Thomas Pedersen84f10702012-07-12 16:17:33 -07007943 if (!rdev->ops->set_cqm_txe_config)
7944 return -EOPNOTSUPP;
7945
7946 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7947 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7948 return -EOPNOTSUPP;
7949
Hila Gonene35e4d22012-06-27 17:19:42 +03007950 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007951}
7952
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007953static int nl80211_set_cqm_rssi(struct genl_info *info,
7954 s32 threshold, u32 hysteresis)
7955{
Johannes Berg4c476992010-10-04 21:36:35 +02007956 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007957 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007958 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007959
7960 if (threshold > 0)
7961 return -EINVAL;
7962
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007963 /* disabling - hysteresis should also be zero then */
7964 if (threshold == 0)
7965 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007966
Johannes Berg4c476992010-10-04 21:36:35 +02007967 if (!rdev->ops->set_cqm_rssi_config)
7968 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007969
Johannes Berg074ac8d2010-09-16 14:58:22 +02007970 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007971 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7972 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007973
Hila Gonene35e4d22012-06-27 17:19:42 +03007974 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007975}
7976
7977static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7978{
7979 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7980 struct nlattr *cqm;
7981 int err;
7982
7983 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007984 if (!cqm)
7985 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007986
7987 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7988 nl80211_attr_cqm_policy);
7989 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007990 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007991
7992 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7993 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007994 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7995 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007996
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007997 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7998 }
7999
8000 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8001 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8002 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8003 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8004 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8005 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8006
8007 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8008 }
8009
8010 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008011}
8012
Johannes Berg29cbe682010-12-03 09:20:44 +01008013static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8014{
8015 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8016 struct net_device *dev = info->user_ptr[1];
8017 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08008018 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01008019 int err;
8020
8021 /* start with default */
8022 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08008023 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01008024
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008025 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01008026 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008027 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01008028 if (err)
8029 return err;
8030 }
8031
8032 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8033 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8034 return -EINVAL;
8035
Javier Cardonac80d5452010-12-16 17:37:49 -08008036 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8037 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8038
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08008039 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8040 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8041 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8042 return -EINVAL;
8043
Marco Porsch9bdbf042013-01-07 16:04:51 +01008044 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8045 setup.beacon_interval =
8046 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8047 if (setup.beacon_interval < 10 ||
8048 setup.beacon_interval > 10000)
8049 return -EINVAL;
8050 }
8051
8052 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8053 setup.dtim_period =
8054 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8055 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8056 return -EINVAL;
8057 }
8058
Javier Cardonac80d5452010-12-16 17:37:49 -08008059 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8060 /* parse additional setup parameters if given */
8061 err = nl80211_parse_mesh_setup(info, &setup);
8062 if (err)
8063 return err;
8064 }
8065
Thomas Pedersend37bb182013-03-04 13:06:13 -08008066 if (setup.user_mpm)
8067 cfg.auto_open_plinks = false;
8068
Johannes Bergcc1d2802012-05-16 23:50:20 +02008069 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01008070 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8071 if (err)
8072 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008073 } else {
8074 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01008075 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02008076 }
8077
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07008078 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8079 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8080 int n_rates =
8081 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8082 struct ieee80211_supported_band *sband;
8083
8084 if (!setup.chandef.chan)
8085 return -EINVAL;
8086
8087 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8088
8089 err = ieee80211_get_ratemask(sband, rates, n_rates,
8090 &setup.basic_rates);
8091 if (err)
8092 return err;
8093 }
8094
Javier Cardonac80d5452010-12-16 17:37:49 -08008095 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01008096}
8097
8098static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8099{
8100 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8101 struct net_device *dev = info->user_ptr[1];
8102
8103 return cfg80211_leave_mesh(rdev, dev);
8104}
8105
Johannes Bergdfb89c52012-06-27 09:23:48 +02008106#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008107static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8108 struct cfg80211_registered_device *rdev)
8109{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008110 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008111 struct nlattr *nl_pats, *nl_pat;
8112 int i, pat_len;
8113
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008114 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008115 return 0;
8116
8117 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8118 if (!nl_pats)
8119 return -ENOBUFS;
8120
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008121 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008122 nl_pat = nla_nest_start(msg, i + 1);
8123 if (!nl_pat)
8124 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008125 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008126 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008127 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008128 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8129 wowlan->patterns[i].pattern) ||
8130 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008131 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008132 return -ENOBUFS;
8133 nla_nest_end(msg, nl_pat);
8134 }
8135 nla_nest_end(msg, nl_pats);
8136
8137 return 0;
8138}
8139
Johannes Berg2a0e0472013-01-23 22:57:40 +01008140static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8141 struct cfg80211_wowlan_tcp *tcp)
8142{
8143 struct nlattr *nl_tcp;
8144
8145 if (!tcp)
8146 return 0;
8147
8148 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8149 if (!nl_tcp)
8150 return -ENOBUFS;
8151
8152 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8153 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8154 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8155 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8156 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8157 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8158 tcp->payload_len, tcp->payload) ||
8159 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8160 tcp->data_interval) ||
8161 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8162 tcp->wake_len, tcp->wake_data) ||
8163 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8164 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8165 return -ENOBUFS;
8166
8167 if (tcp->payload_seq.len &&
8168 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8169 sizeof(tcp->payload_seq), &tcp->payload_seq))
8170 return -ENOBUFS;
8171
8172 if (tcp->payload_tok.len &&
8173 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8174 sizeof(tcp->payload_tok) + tcp->tokens_size,
8175 &tcp->payload_tok))
8176 return -ENOBUFS;
8177
Johannes Berge248ad32013-05-16 10:24:28 +02008178 nla_nest_end(msg, nl_tcp);
8179
Johannes Berg2a0e0472013-01-23 22:57:40 +01008180 return 0;
8181}
8182
Johannes Bergff1b6e62011-05-04 15:37:28 +02008183static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8184{
8185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8186 struct sk_buff *msg;
8187 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008188 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008189
Johannes Berg964dc9e2013-06-03 17:25:34 +02008190 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008191 return -EOPNOTSUPP;
8192
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008193 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01008194 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008195 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8196 rdev->wiphy.wowlan_config->tcp->payload_len +
8197 rdev->wiphy.wowlan_config->tcp->wake_len +
8198 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008199 }
8200
8201 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008202 if (!msg)
8203 return -ENOMEM;
8204
Eric W. Biederman15e47302012-09-07 20:12:54 +00008205 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02008206 NL80211_CMD_GET_WOWLAN);
8207 if (!hdr)
8208 goto nla_put_failure;
8209
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008210 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02008211 struct nlattr *nl_wowlan;
8212
8213 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8214 if (!nl_wowlan)
8215 goto nla_put_failure;
8216
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008217 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008219 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008220 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008221 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008222 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008223 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008224 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008225 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008226 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008227 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008228 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008229 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04008230 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8231 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008232
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008233 if (nl80211_send_wowlan_patterns(msg, rdev))
8234 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01008235
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008236 if (nl80211_send_wowlan_tcp(msg,
8237 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01008238 goto nla_put_failure;
8239
Johannes Bergff1b6e62011-05-04 15:37:28 +02008240 nla_nest_end(msg, nl_wowlan);
8241 }
8242
8243 genlmsg_end(msg, hdr);
8244 return genlmsg_reply(msg, info);
8245
8246nla_put_failure:
8247 nlmsg_free(msg);
8248 return -ENOBUFS;
8249}
8250
Johannes Berg2a0e0472013-01-23 22:57:40 +01008251static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8252 struct nlattr *attr,
8253 struct cfg80211_wowlan *trig)
8254{
8255 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8256 struct cfg80211_wowlan_tcp *cfg;
8257 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8258 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8259 u32 size;
8260 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8261 int err, port;
8262
Johannes Berg964dc9e2013-06-03 17:25:34 +02008263 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008264 return -EINVAL;
8265
8266 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8267 nla_data(attr), nla_len(attr),
8268 nl80211_wowlan_tcp_policy);
8269 if (err)
8270 return err;
8271
8272 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8273 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8274 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8275 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8276 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8277 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8278 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8279 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8280 return -EINVAL;
8281
8282 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008283 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008284 return -EINVAL;
8285
8286 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008287 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008288 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008289 return -EINVAL;
8290
8291 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008292 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008293 return -EINVAL;
8294
8295 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8296 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8297 return -EINVAL;
8298
8299 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8300 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8301
8302 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8303 tokens_size = tokln - sizeof(*tok);
8304
8305 if (!tok->len || tokens_size % tok->len)
8306 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008307 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008308 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008309 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008310 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008311 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008312 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008313 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008314 return -EINVAL;
8315 if (tok->offset + tok->len > data_size)
8316 return -EINVAL;
8317 }
8318
8319 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8320 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008321 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008322 return -EINVAL;
8323 if (seq->len == 0 || seq->len > 4)
8324 return -EINVAL;
8325 if (seq->len + seq->offset > data_size)
8326 return -EINVAL;
8327 }
8328
8329 size = sizeof(*cfg);
8330 size += data_size;
8331 size += wake_size + wake_mask_size;
8332 size += tokens_size;
8333
8334 cfg = kzalloc(size, GFP_KERNEL);
8335 if (!cfg)
8336 return -ENOMEM;
8337 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8338 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8339 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8340 ETH_ALEN);
8341 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8342 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8343 else
8344 port = 0;
8345#ifdef CONFIG_INET
8346 /* allocate a socket and port for it and use it */
8347 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8348 IPPROTO_TCP, &cfg->sock, 1);
8349 if (err) {
8350 kfree(cfg);
8351 return err;
8352 }
8353 if (inet_csk_get_port(cfg->sock->sk, port)) {
8354 sock_release(cfg->sock);
8355 kfree(cfg);
8356 return -EADDRINUSE;
8357 }
8358 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8359#else
8360 if (!port) {
8361 kfree(cfg);
8362 return -EINVAL;
8363 }
8364 cfg->src_port = port;
8365#endif
8366
8367 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8368 cfg->payload_len = data_size;
8369 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8370 memcpy((void *)cfg->payload,
8371 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8372 data_size);
8373 if (seq)
8374 cfg->payload_seq = *seq;
8375 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8376 cfg->wake_len = wake_size;
8377 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8378 memcpy((void *)cfg->wake_data,
8379 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8380 wake_size);
8381 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8382 data_size + wake_size;
8383 memcpy((void *)cfg->wake_mask,
8384 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8385 wake_mask_size);
8386 if (tok) {
8387 cfg->tokens_size = tokens_size;
8388 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8389 }
8390
8391 trig->tcp = cfg;
8392
8393 return 0;
8394}
8395
Johannes Bergff1b6e62011-05-04 15:37:28 +02008396static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8397{
8398 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8399 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008400 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008401 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008402 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008403 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008404 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008405
Johannes Berg964dc9e2013-06-03 17:25:34 +02008406 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008407 return -EOPNOTSUPP;
8408
Johannes Bergae33bd82012-07-12 16:25:02 +02008409 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8410 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008411 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008412 goto set_wakeup;
8413 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008414
8415 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8416 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8417 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8418 nl80211_wowlan_policy);
8419 if (err)
8420 return err;
8421
8422 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8423 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8424 return -EINVAL;
8425 new_triggers.any = true;
8426 }
8427
8428 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8429 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8430 return -EINVAL;
8431 new_triggers.disconnect = true;
8432 }
8433
8434 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8435 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8436 return -EINVAL;
8437 new_triggers.magic_pkt = true;
8438 }
8439
Johannes Berg77dbbb12011-07-13 10:48:55 +02008440 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8441 return -EINVAL;
8442
8443 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8444 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8445 return -EINVAL;
8446 new_triggers.gtk_rekey_failure = true;
8447 }
8448
8449 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8450 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8451 return -EINVAL;
8452 new_triggers.eap_identity_req = true;
8453 }
8454
8455 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8456 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8457 return -EINVAL;
8458 new_triggers.four_way_handshake = true;
8459 }
8460
8461 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8462 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8463 return -EINVAL;
8464 new_triggers.rfkill_release = true;
8465 }
8466
Johannes Bergff1b6e62011-05-04 15:37:28 +02008467 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8468 struct nlattr *pat;
8469 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008470 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008471 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008472
8473 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8474 rem)
8475 n_patterns++;
8476 if (n_patterns > wowlan->n_patterns)
8477 return -EINVAL;
8478
8479 new_triggers.patterns = kcalloc(n_patterns,
8480 sizeof(new_triggers.patterns[0]),
8481 GFP_KERNEL);
8482 if (!new_triggers.patterns)
8483 return -ENOMEM;
8484
8485 new_triggers.n_patterns = n_patterns;
8486 i = 0;
8487
8488 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8489 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008490 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8491 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008492 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008493 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8494 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008495 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008496 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008497 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008498 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008499 goto error;
8500 if (pat_len > wowlan->pattern_max_len ||
8501 pat_len < wowlan->pattern_min_len)
8502 goto error;
8503
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008504 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008505 pkt_offset = 0;
8506 else
8507 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008508 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008509 if (pkt_offset > wowlan->max_pkt_offset)
8510 goto error;
8511 new_triggers.patterns[i].pkt_offset = pkt_offset;
8512
Johannes Bergff1b6e62011-05-04 15:37:28 +02008513 new_triggers.patterns[i].mask =
8514 kmalloc(mask_len + pat_len, GFP_KERNEL);
8515 if (!new_triggers.patterns[i].mask) {
8516 err = -ENOMEM;
8517 goto error;
8518 }
8519 new_triggers.patterns[i].pattern =
8520 new_triggers.patterns[i].mask + mask_len;
8521 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008522 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008523 mask_len);
8524 new_triggers.patterns[i].pattern_len = pat_len;
8525 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008526 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008527 pat_len);
8528 i++;
8529 }
8530 }
8531
Johannes Berg2a0e0472013-01-23 22:57:40 +01008532 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8533 err = nl80211_parse_wowlan_tcp(
8534 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8535 &new_triggers);
8536 if (err)
8537 goto error;
8538 }
8539
Johannes Bergae33bd82012-07-12 16:25:02 +02008540 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8541 if (!ntrig) {
8542 err = -ENOMEM;
8543 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008544 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008545 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008546 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008547
Johannes Bergae33bd82012-07-12 16:25:02 +02008548 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008549 if (rdev->ops->set_wakeup &&
8550 prev_enabled != !!rdev->wiphy.wowlan_config)
8551 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008552
Johannes Bergff1b6e62011-05-04 15:37:28 +02008553 return 0;
8554 error:
8555 for (i = 0; i < new_triggers.n_patterns; i++)
8556 kfree(new_triggers.patterns[i].mask);
8557 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008558 if (new_triggers.tcp && new_triggers.tcp->sock)
8559 sock_release(new_triggers.tcp->sock);
8560 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008561 return err;
8562}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008563#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008564
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008565static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8566 struct cfg80211_registered_device *rdev)
8567{
8568 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8569 int i, j, pat_len;
8570 struct cfg80211_coalesce_rules *rule;
8571
8572 if (!rdev->coalesce->n_rules)
8573 return 0;
8574
8575 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8576 if (!nl_rules)
8577 return -ENOBUFS;
8578
8579 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8580 nl_rule = nla_nest_start(msg, i + 1);
8581 if (!nl_rule)
8582 return -ENOBUFS;
8583
8584 rule = &rdev->coalesce->rules[i];
8585 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8586 rule->delay))
8587 return -ENOBUFS;
8588
8589 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8590 rule->condition))
8591 return -ENOBUFS;
8592
8593 nl_pats = nla_nest_start(msg,
8594 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8595 if (!nl_pats)
8596 return -ENOBUFS;
8597
8598 for (j = 0; j < rule->n_patterns; j++) {
8599 nl_pat = nla_nest_start(msg, j + 1);
8600 if (!nl_pat)
8601 return -ENOBUFS;
8602 pat_len = rule->patterns[j].pattern_len;
8603 if (nla_put(msg, NL80211_PKTPAT_MASK,
8604 DIV_ROUND_UP(pat_len, 8),
8605 rule->patterns[j].mask) ||
8606 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8607 rule->patterns[j].pattern) ||
8608 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8609 rule->patterns[j].pkt_offset))
8610 return -ENOBUFS;
8611 nla_nest_end(msg, nl_pat);
8612 }
8613 nla_nest_end(msg, nl_pats);
8614 nla_nest_end(msg, nl_rule);
8615 }
8616 nla_nest_end(msg, nl_rules);
8617
8618 return 0;
8619}
8620
8621static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8622{
8623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8624 struct sk_buff *msg;
8625 void *hdr;
8626
8627 if (!rdev->wiphy.coalesce)
8628 return -EOPNOTSUPP;
8629
8630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8631 if (!msg)
8632 return -ENOMEM;
8633
8634 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8635 NL80211_CMD_GET_COALESCE);
8636 if (!hdr)
8637 goto nla_put_failure;
8638
8639 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8640 goto nla_put_failure;
8641
8642 genlmsg_end(msg, hdr);
8643 return genlmsg_reply(msg, info);
8644
8645nla_put_failure:
8646 nlmsg_free(msg);
8647 return -ENOBUFS;
8648}
8649
8650void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8651{
8652 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8653 int i, j;
8654 struct cfg80211_coalesce_rules *rule;
8655
8656 if (!coalesce)
8657 return;
8658
8659 for (i = 0; i < coalesce->n_rules; i++) {
8660 rule = &coalesce->rules[i];
8661 for (j = 0; j < rule->n_patterns; j++)
8662 kfree(rule->patterns[j].mask);
8663 kfree(rule->patterns);
8664 }
8665 kfree(coalesce->rules);
8666 kfree(coalesce);
8667 rdev->coalesce = NULL;
8668}
8669
8670static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8671 struct nlattr *rule,
8672 struct cfg80211_coalesce_rules *new_rule)
8673{
8674 int err, i;
8675 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8676 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8677 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8678 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8679
8680 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8681 nla_len(rule), nl80211_coalesce_policy);
8682 if (err)
8683 return err;
8684
8685 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8686 new_rule->delay =
8687 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8688 if (new_rule->delay > coalesce->max_delay)
8689 return -EINVAL;
8690
8691 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8692 new_rule->condition =
8693 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8694 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8695 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8696 return -EINVAL;
8697
8698 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8699 return -EINVAL;
8700
8701 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8702 rem)
8703 n_patterns++;
8704 if (n_patterns > coalesce->n_patterns)
8705 return -EINVAL;
8706
8707 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8708 GFP_KERNEL);
8709 if (!new_rule->patterns)
8710 return -ENOMEM;
8711
8712 new_rule->n_patterns = n_patterns;
8713 i = 0;
8714
8715 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8716 rem) {
8717 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8718 nla_len(pat), NULL);
8719 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8720 !pat_tb[NL80211_PKTPAT_PATTERN])
8721 return -EINVAL;
8722 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8723 mask_len = DIV_ROUND_UP(pat_len, 8);
8724 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8725 return -EINVAL;
8726 if (pat_len > coalesce->pattern_max_len ||
8727 pat_len < coalesce->pattern_min_len)
8728 return -EINVAL;
8729
8730 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8731 pkt_offset = 0;
8732 else
8733 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8734 if (pkt_offset > coalesce->max_pkt_offset)
8735 return -EINVAL;
8736 new_rule->patterns[i].pkt_offset = pkt_offset;
8737
8738 new_rule->patterns[i].mask =
8739 kmalloc(mask_len + pat_len, GFP_KERNEL);
8740 if (!new_rule->patterns[i].mask)
8741 return -ENOMEM;
8742 new_rule->patterns[i].pattern =
8743 new_rule->patterns[i].mask + mask_len;
8744 memcpy(new_rule->patterns[i].mask,
8745 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8746 new_rule->patterns[i].pattern_len = pat_len;
8747 memcpy(new_rule->patterns[i].pattern,
8748 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8749 i++;
8750 }
8751
8752 return 0;
8753}
8754
8755static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8756{
8757 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8758 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8759 struct cfg80211_coalesce new_coalesce = {};
8760 struct cfg80211_coalesce *n_coalesce;
8761 int err, rem_rule, n_rules = 0, i, j;
8762 struct nlattr *rule;
8763 struct cfg80211_coalesce_rules *tmp_rule;
8764
8765 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8766 return -EOPNOTSUPP;
8767
8768 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8769 cfg80211_rdev_free_coalesce(rdev);
8770 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8771 return 0;
8772 }
8773
8774 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8775 rem_rule)
8776 n_rules++;
8777 if (n_rules > coalesce->n_rules)
8778 return -EINVAL;
8779
8780 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8781 GFP_KERNEL);
8782 if (!new_coalesce.rules)
8783 return -ENOMEM;
8784
8785 new_coalesce.n_rules = n_rules;
8786 i = 0;
8787
8788 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8789 rem_rule) {
8790 err = nl80211_parse_coalesce_rule(rdev, rule,
8791 &new_coalesce.rules[i]);
8792 if (err)
8793 goto error;
8794
8795 i++;
8796 }
8797
8798 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8799 if (err)
8800 goto error;
8801
8802 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8803 if (!n_coalesce) {
8804 err = -ENOMEM;
8805 goto error;
8806 }
8807 cfg80211_rdev_free_coalesce(rdev);
8808 rdev->coalesce = n_coalesce;
8809
8810 return 0;
8811error:
8812 for (i = 0; i < new_coalesce.n_rules; i++) {
8813 tmp_rule = &new_coalesce.rules[i];
8814 for (j = 0; j < tmp_rule->n_patterns; j++)
8815 kfree(tmp_rule->patterns[j].mask);
8816 kfree(tmp_rule->patterns);
8817 }
8818 kfree(new_coalesce.rules);
8819
8820 return err;
8821}
8822
Johannes Berge5497d72011-07-05 16:35:40 +02008823static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8824{
8825 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8826 struct net_device *dev = info->user_ptr[1];
8827 struct wireless_dev *wdev = dev->ieee80211_ptr;
8828 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8829 struct cfg80211_gtk_rekey_data rekey_data;
8830 int err;
8831
8832 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8833 return -EINVAL;
8834
8835 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8836 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8837 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8838 nl80211_rekey_policy);
8839 if (err)
8840 return err;
8841
8842 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8843 return -ERANGE;
8844 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8845 return -ERANGE;
8846 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8847 return -ERANGE;
8848
8849 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8850 NL80211_KEK_LEN);
8851 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8852 NL80211_KCK_LEN);
8853 memcpy(rekey_data.replay_ctr,
8854 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8855 NL80211_REPLAY_CTR_LEN);
8856
8857 wdev_lock(wdev);
8858 if (!wdev->current_bss) {
8859 err = -ENOTCONN;
8860 goto out;
8861 }
8862
8863 if (!rdev->ops->set_rekey_data) {
8864 err = -EOPNOTSUPP;
8865 goto out;
8866 }
8867
Hila Gonene35e4d22012-06-27 17:19:42 +03008868 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008869 out:
8870 wdev_unlock(wdev);
8871 return err;
8872}
8873
Johannes Berg28946da2011-11-04 11:18:12 +01008874static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8875 struct genl_info *info)
8876{
8877 struct net_device *dev = info->user_ptr[1];
8878 struct wireless_dev *wdev = dev->ieee80211_ptr;
8879
8880 if (wdev->iftype != NL80211_IFTYPE_AP &&
8881 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8882 return -EINVAL;
8883
Eric W. Biederman15e47302012-09-07 20:12:54 +00008884 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008885 return -EBUSY;
8886
Eric W. Biederman15e47302012-09-07 20:12:54 +00008887 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008888 return 0;
8889}
8890
Johannes Berg7f6cf312011-11-04 11:18:15 +01008891static int nl80211_probe_client(struct sk_buff *skb,
8892 struct genl_info *info)
8893{
8894 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8895 struct net_device *dev = info->user_ptr[1];
8896 struct wireless_dev *wdev = dev->ieee80211_ptr;
8897 struct sk_buff *msg;
8898 void *hdr;
8899 const u8 *addr;
8900 u64 cookie;
8901 int err;
8902
8903 if (wdev->iftype != NL80211_IFTYPE_AP &&
8904 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8905 return -EOPNOTSUPP;
8906
8907 if (!info->attrs[NL80211_ATTR_MAC])
8908 return -EINVAL;
8909
8910 if (!rdev->ops->probe_client)
8911 return -EOPNOTSUPP;
8912
8913 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8914 if (!msg)
8915 return -ENOMEM;
8916
Eric W. Biederman15e47302012-09-07 20:12:54 +00008917 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008918 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008919 if (!hdr) {
8920 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008921 goto free_msg;
8922 }
8923
8924 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8925
Hila Gonene35e4d22012-06-27 17:19:42 +03008926 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008927 if (err)
8928 goto free_msg;
8929
David S. Miller9360ffd2012-03-29 04:41:26 -04008930 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8931 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008932
8933 genlmsg_end(msg, hdr);
8934
8935 return genlmsg_reply(msg, info);
8936
8937 nla_put_failure:
8938 err = -ENOBUFS;
8939 free_msg:
8940 nlmsg_free(msg);
8941 return err;
8942}
8943
Johannes Berg5e760232011-11-04 11:18:17 +01008944static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8945{
8946 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008947 struct cfg80211_beacon_registration *reg, *nreg;
8948 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008949
8950 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8951 return -EOPNOTSUPP;
8952
Ben Greear37c73b52012-10-26 14:49:25 -07008953 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8954 if (!nreg)
8955 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008956
Ben Greear37c73b52012-10-26 14:49:25 -07008957 /* First, check if already registered. */
8958 spin_lock_bh(&rdev->beacon_registrations_lock);
8959 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8960 if (reg->nlportid == info->snd_portid) {
8961 rv = -EALREADY;
8962 goto out_err;
8963 }
8964 }
8965 /* Add it to the list */
8966 nreg->nlportid = info->snd_portid;
8967 list_add(&nreg->list, &rdev->beacon_registrations);
8968
8969 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008970
8971 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008972out_err:
8973 spin_unlock_bh(&rdev->beacon_registrations_lock);
8974 kfree(nreg);
8975 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008976}
8977
Johannes Berg98104fde2012-06-16 00:19:54 +02008978static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8979{
8980 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8981 struct wireless_dev *wdev = info->user_ptr[1];
8982 int err;
8983
8984 if (!rdev->ops->start_p2p_device)
8985 return -EOPNOTSUPP;
8986
8987 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8988 return -EOPNOTSUPP;
8989
8990 if (wdev->p2p_started)
8991 return 0;
8992
Johannes Berg98104fde2012-06-16 00:19:54 +02008993 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008994 if (err)
8995 return err;
8996
Johannes Bergeeb126e2012-10-23 15:16:50 +02008997 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008998 if (err)
8999 return err;
9000
9001 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02009002 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02009003
9004 return 0;
9005}
9006
9007static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9008{
9009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9010 struct wireless_dev *wdev = info->user_ptr[1];
9011
9012 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9013 return -EOPNOTSUPP;
9014
9015 if (!rdev->ops->stop_p2p_device)
9016 return -EOPNOTSUPP;
9017
Johannes Bergf9f47522013-03-19 15:04:07 +01009018 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009019
9020 return 0;
9021}
9022
Johannes Berg3713b4e2013-02-14 16:19:38 +01009023static int nl80211_get_protocol_features(struct sk_buff *skb,
9024 struct genl_info *info)
9025{
9026 void *hdr;
9027 struct sk_buff *msg;
9028
9029 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9030 if (!msg)
9031 return -ENOMEM;
9032
9033 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9034 NL80211_CMD_GET_PROTOCOL_FEATURES);
9035 if (!hdr)
9036 goto nla_put_failure;
9037
9038 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9039 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9040 goto nla_put_failure;
9041
9042 genlmsg_end(msg, hdr);
9043 return genlmsg_reply(msg, info);
9044
9045 nla_put_failure:
9046 kfree_skb(msg);
9047 return -ENOBUFS;
9048}
9049
Jouni Malinen355199e2013-02-27 17:14:27 +02009050static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9051{
9052 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9053 struct cfg80211_update_ft_ies_params ft_params;
9054 struct net_device *dev = info->user_ptr[1];
9055
9056 if (!rdev->ops->update_ft_ies)
9057 return -EOPNOTSUPP;
9058
9059 if (!info->attrs[NL80211_ATTR_MDID] ||
9060 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9061 return -EINVAL;
9062
9063 memset(&ft_params, 0, sizeof(ft_params));
9064 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9065 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9066 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9067
9068 return rdev_update_ft_ies(rdev, dev, &ft_params);
9069}
9070
Arend van Spriel5de17982013-04-18 15:49:00 +02009071static int nl80211_crit_protocol_start(struct sk_buff *skb,
9072 struct genl_info *info)
9073{
9074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9075 struct wireless_dev *wdev = info->user_ptr[1];
9076 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9077 u16 duration;
9078 int ret;
9079
9080 if (!rdev->ops->crit_proto_start)
9081 return -EOPNOTSUPP;
9082
9083 if (WARN_ON(!rdev->ops->crit_proto_stop))
9084 return -EINVAL;
9085
9086 if (rdev->crit_proto_nlportid)
9087 return -EBUSY;
9088
9089 /* determine protocol if provided */
9090 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9091 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9092
9093 if (proto >= NUM_NL80211_CRIT_PROTO)
9094 return -EINVAL;
9095
9096 /* timeout must be provided */
9097 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9098 return -EINVAL;
9099
9100 duration =
9101 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9102
9103 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9104 return -ERANGE;
9105
9106 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9107 if (!ret)
9108 rdev->crit_proto_nlportid = info->snd_portid;
9109
9110 return ret;
9111}
9112
9113static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9114 struct genl_info *info)
9115{
9116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9117 struct wireless_dev *wdev = info->user_ptr[1];
9118
9119 if (!rdev->ops->crit_proto_stop)
9120 return -EOPNOTSUPP;
9121
9122 if (rdev->crit_proto_nlportid) {
9123 rdev->crit_proto_nlportid = 0;
9124 rdev_crit_proto_stop(rdev, wdev);
9125 }
9126 return 0;
9127}
9128
Johannes Bergad7e7182013-11-13 13:37:47 +01009129static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9130{
9131 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9132 struct wireless_dev *wdev =
9133 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9134 int i, err;
9135 u32 vid, subcmd;
9136
9137 if (!rdev->wiphy.vendor_commands)
9138 return -EOPNOTSUPP;
9139
9140 if (IS_ERR(wdev)) {
9141 err = PTR_ERR(wdev);
9142 if (err != -EINVAL)
9143 return err;
9144 wdev = NULL;
9145 } else if (wdev->wiphy != &rdev->wiphy) {
9146 return -EINVAL;
9147 }
9148
9149 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9150 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9151 return -EINVAL;
9152
9153 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9154 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9155 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9156 const struct wiphy_vendor_command *vcmd;
9157 void *data = NULL;
9158 int len = 0;
9159
9160 vcmd = &rdev->wiphy.vendor_commands[i];
9161
9162 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9163 continue;
9164
9165 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9166 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9167 if (!wdev)
9168 return -EINVAL;
9169 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9170 !wdev->netdev)
9171 return -EINVAL;
9172
9173 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9174 if (wdev->netdev &&
9175 !netif_running(wdev->netdev))
9176 return -ENETDOWN;
9177 if (!wdev->netdev && !wdev->p2p_started)
9178 return -ENETDOWN;
9179 }
9180 } else {
9181 wdev = NULL;
9182 }
9183
9184 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9185 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9186 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9187 }
9188
9189 rdev->cur_cmd_info = info;
9190 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9191 data, len);
9192 rdev->cur_cmd_info = NULL;
9193 return err;
9194 }
9195
9196 return -EOPNOTSUPP;
9197}
9198
9199struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9200 enum nl80211_commands cmd,
9201 enum nl80211_attrs attr,
9202 int approxlen)
9203{
9204 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9205
9206 if (WARN_ON(!rdev->cur_cmd_info))
9207 return NULL;
9208
9209 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9210 rdev->cur_cmd_info->snd_portid,
9211 rdev->cur_cmd_info->snd_seq,
Johannes Berg567ffc32013-12-18 14:43:31 +01009212 cmd, attr, NULL, GFP_KERNEL);
Johannes Bergad7e7182013-11-13 13:37:47 +01009213}
9214EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9215
9216int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9217{
9218 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9219 void *hdr = ((void **)skb->cb)[1];
9220 struct nlattr *data = ((void **)skb->cb)[2];
9221
9222 if (WARN_ON(!rdev->cur_cmd_info)) {
9223 kfree_skb(skb);
9224 return -EINVAL;
9225 }
9226
9227 nla_nest_end(skb, data);
9228 genlmsg_end(skb, hdr);
9229 return genlmsg_reply(skb, rdev->cur_cmd_info);
9230}
9231EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9232
9233
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08009234static int nl80211_set_qos_map(struct sk_buff *skb,
9235 struct genl_info *info)
9236{
9237 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9238 struct cfg80211_qos_map *qos_map = NULL;
9239 struct net_device *dev = info->user_ptr[1];
9240 u8 *pos, len, num_des, des_len, des;
9241 int ret;
9242
9243 if (!rdev->ops->set_qos_map)
9244 return -EOPNOTSUPP;
9245
9246 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9247 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9248 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9249
9250 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9251 len > IEEE80211_QOS_MAP_LEN_MAX)
9252 return -EINVAL;
9253
9254 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9255 if (!qos_map)
9256 return -ENOMEM;
9257
9258 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9259 if (num_des) {
9260 des_len = num_des *
9261 sizeof(struct cfg80211_dscp_exception);
9262 memcpy(qos_map->dscp_exception, pos, des_len);
9263 qos_map->num_des = num_des;
9264 for (des = 0; des < num_des; des++) {
9265 if (qos_map->dscp_exception[des].up > 7) {
9266 kfree(qos_map);
9267 return -EINVAL;
9268 }
9269 }
9270 pos += des_len;
9271 }
9272 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9273 }
9274
9275 wdev_lock(dev->ieee80211_ptr);
9276 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9277 if (!ret)
9278 ret = rdev_set_qos_map(rdev, dev, qos_map);
9279 wdev_unlock(dev->ieee80211_ptr);
9280
9281 kfree(qos_map);
9282 return ret;
9283}
9284
Johannes Berg4c476992010-10-04 21:36:35 +02009285#define NL80211_FLAG_NEED_WIPHY 0x01
9286#define NL80211_FLAG_NEED_NETDEV 0x02
9287#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02009288#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9289#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9290 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02009291#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02009292/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02009293#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9294 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02009295
Johannes Bergf84f7712013-11-14 17:14:45 +01009296static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009297 struct genl_info *info)
9298{
9299 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02009300 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009301 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02009302 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9303
9304 if (rtnl)
9305 rtnl_lock();
9306
9307 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02009308 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02009309 if (IS_ERR(rdev)) {
9310 if (rtnl)
9311 rtnl_unlock();
9312 return PTR_ERR(rdev);
9313 }
9314 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02009315 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9316 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02009317 ASSERT_RTNL();
9318
Johannes Berg89a54e42012-06-15 14:33:17 +02009319 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9320 info->attrs);
9321 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009322 if (rtnl)
9323 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009324 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009325 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009326
Johannes Berg89a54e42012-06-15 14:33:17 +02009327 dev = wdev->netdev;
9328 rdev = wiphy_to_dev(wdev->wiphy);
9329
Johannes Berg1bf614e2012-06-15 15:23:36 +02009330 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9331 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009332 if (rtnl)
9333 rtnl_unlock();
9334 return -EINVAL;
9335 }
9336
9337 info->user_ptr[1] = dev;
9338 } else {
9339 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009340 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009341
Johannes Berg1bf614e2012-06-15 15:23:36 +02009342 if (dev) {
9343 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9344 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009345 if (rtnl)
9346 rtnl_unlock();
9347 return -ENETDOWN;
9348 }
9349
9350 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009351 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9352 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009353 if (rtnl)
9354 rtnl_unlock();
9355 return -ENETDOWN;
9356 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009357 }
9358
Johannes Berg4c476992010-10-04 21:36:35 +02009359 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009360 }
9361
9362 return 0;
9363}
9364
Johannes Bergf84f7712013-11-14 17:14:45 +01009365static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
Johannes Berg4c476992010-10-04 21:36:35 +02009366 struct genl_info *info)
9367{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009368 if (info->user_ptr[1]) {
9369 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9370 struct wireless_dev *wdev = info->user_ptr[1];
9371
9372 if (wdev->netdev)
9373 dev_put(wdev->netdev);
9374 } else {
9375 dev_put(info->user_ptr[1]);
9376 }
9377 }
Johannes Berg4c476992010-10-04 21:36:35 +02009378 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9379 rtnl_unlock();
9380}
9381
Johannes Berg4534de82013-11-14 17:14:46 +01009382static const struct genl_ops nl80211_ops[] = {
Johannes Berg55682962007-09-20 13:09:35 -04009383 {
9384 .cmd = NL80211_CMD_GET_WIPHY,
9385 .doit = nl80211_get_wiphy,
9386 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009387 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009388 .policy = nl80211_policy,
9389 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009390 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9391 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009392 },
9393 {
9394 .cmd = NL80211_CMD_SET_WIPHY,
9395 .doit = nl80211_set_wiphy,
9396 .policy = nl80211_policy,
9397 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009398 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009399 },
9400 {
9401 .cmd = NL80211_CMD_GET_INTERFACE,
9402 .doit = nl80211_get_interface,
9403 .dumpit = nl80211_dump_interface,
9404 .policy = nl80211_policy,
9405 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009406 .internal_flags = NL80211_FLAG_NEED_WDEV |
9407 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009408 },
9409 {
9410 .cmd = NL80211_CMD_SET_INTERFACE,
9411 .doit = nl80211_set_interface,
9412 .policy = nl80211_policy,
9413 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009414 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9415 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009416 },
9417 {
9418 .cmd = NL80211_CMD_NEW_INTERFACE,
9419 .doit = nl80211_new_interface,
9420 .policy = nl80211_policy,
9421 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009422 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9423 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009424 },
9425 {
9426 .cmd = NL80211_CMD_DEL_INTERFACE,
9427 .doit = nl80211_del_interface,
9428 .policy = nl80211_policy,
9429 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009430 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009431 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009432 },
Johannes Berg41ade002007-12-19 02:03:29 +01009433 {
9434 .cmd = NL80211_CMD_GET_KEY,
9435 .doit = nl80211_get_key,
9436 .policy = nl80211_policy,
9437 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009438 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009439 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009440 },
9441 {
9442 .cmd = NL80211_CMD_SET_KEY,
9443 .doit = nl80211_set_key,
9444 .policy = nl80211_policy,
9445 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009446 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009447 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009448 },
9449 {
9450 .cmd = NL80211_CMD_NEW_KEY,
9451 .doit = nl80211_new_key,
9452 .policy = nl80211_policy,
9453 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009454 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009455 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009456 },
9457 {
9458 .cmd = NL80211_CMD_DEL_KEY,
9459 .doit = nl80211_del_key,
9460 .policy = nl80211_policy,
9461 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009462 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009463 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009464 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009465 {
9466 .cmd = NL80211_CMD_SET_BEACON,
9467 .policy = nl80211_policy,
9468 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009469 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009470 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009471 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009472 },
9473 {
Johannes Berg88600202012-02-13 15:17:18 +01009474 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009475 .policy = nl80211_policy,
9476 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009477 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009479 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009480 },
9481 {
Johannes Berg88600202012-02-13 15:17:18 +01009482 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009483 .policy = nl80211_policy,
9484 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009485 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009487 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009488 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009489 {
9490 .cmd = NL80211_CMD_GET_STATION,
9491 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009492 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009493 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009494 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9495 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009496 },
9497 {
9498 .cmd = NL80211_CMD_SET_STATION,
9499 .doit = nl80211_set_station,
9500 .policy = nl80211_policy,
9501 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009503 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009504 },
9505 {
9506 .cmd = NL80211_CMD_NEW_STATION,
9507 .doit = nl80211_new_station,
9508 .policy = nl80211_policy,
9509 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009511 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009512 },
9513 {
9514 .cmd = NL80211_CMD_DEL_STATION,
9515 .doit = nl80211_del_station,
9516 .policy = nl80211_policy,
9517 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009519 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009520 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009521 {
9522 .cmd = NL80211_CMD_GET_MPATH,
9523 .doit = nl80211_get_mpath,
9524 .dumpit = nl80211_dump_mpath,
9525 .policy = nl80211_policy,
9526 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009527 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009528 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009529 },
9530 {
9531 .cmd = NL80211_CMD_SET_MPATH,
9532 .doit = nl80211_set_mpath,
9533 .policy = nl80211_policy,
9534 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009535 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009536 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009537 },
9538 {
9539 .cmd = NL80211_CMD_NEW_MPATH,
9540 .doit = nl80211_new_mpath,
9541 .policy = nl80211_policy,
9542 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009543 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009544 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009545 },
9546 {
9547 .cmd = NL80211_CMD_DEL_MPATH,
9548 .doit = nl80211_del_mpath,
9549 .policy = nl80211_policy,
9550 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009551 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009552 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009553 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009554 {
9555 .cmd = NL80211_CMD_SET_BSS,
9556 .doit = nl80211_set_bss,
9557 .policy = nl80211_policy,
9558 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009559 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009560 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009561 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009562 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009563 .cmd = NL80211_CMD_GET_REG,
9564 .doit = nl80211_get_reg,
9565 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009566 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009567 /* can be retrieved by unprivileged users */
9568 },
9569 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009570 .cmd = NL80211_CMD_SET_REG,
9571 .doit = nl80211_set_reg,
9572 .policy = nl80211_policy,
9573 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009574 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009575 },
9576 {
9577 .cmd = NL80211_CMD_REQ_SET_REG,
9578 .doit = nl80211_req_set_reg,
9579 .policy = nl80211_policy,
9580 .flags = GENL_ADMIN_PERM,
9581 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009582 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009583 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9584 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009585 .policy = nl80211_policy,
9586 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009587 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009588 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009589 },
9590 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009591 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9592 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009593 .policy = nl80211_policy,
9594 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009595 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009596 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009597 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009598 {
Johannes Berg2a519312009-02-10 21:25:55 +01009599 .cmd = NL80211_CMD_TRIGGER_SCAN,
9600 .doit = nl80211_trigger_scan,
9601 .policy = nl80211_policy,
9602 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009603 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009604 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009605 },
9606 {
9607 .cmd = NL80211_CMD_GET_SCAN,
9608 .policy = nl80211_policy,
9609 .dumpit = nl80211_dump_scan,
9610 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009611 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009612 .cmd = NL80211_CMD_START_SCHED_SCAN,
9613 .doit = nl80211_start_sched_scan,
9614 .policy = nl80211_policy,
9615 .flags = GENL_ADMIN_PERM,
9616 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9617 NL80211_FLAG_NEED_RTNL,
9618 },
9619 {
9620 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9621 .doit = nl80211_stop_sched_scan,
9622 .policy = nl80211_policy,
9623 .flags = GENL_ADMIN_PERM,
9624 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9625 NL80211_FLAG_NEED_RTNL,
9626 },
9627 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009628 .cmd = NL80211_CMD_AUTHENTICATE,
9629 .doit = nl80211_authenticate,
9630 .policy = nl80211_policy,
9631 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009632 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009633 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009634 },
9635 {
9636 .cmd = NL80211_CMD_ASSOCIATE,
9637 .doit = nl80211_associate,
9638 .policy = nl80211_policy,
9639 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009640 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009641 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009642 },
9643 {
9644 .cmd = NL80211_CMD_DEAUTHENTICATE,
9645 .doit = nl80211_deauthenticate,
9646 .policy = nl80211_policy,
9647 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009648 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009649 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009650 },
9651 {
9652 .cmd = NL80211_CMD_DISASSOCIATE,
9653 .doit = nl80211_disassociate,
9654 .policy = nl80211_policy,
9655 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009656 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009657 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009658 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009659 {
9660 .cmd = NL80211_CMD_JOIN_IBSS,
9661 .doit = nl80211_join_ibss,
9662 .policy = nl80211_policy,
9663 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009664 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009665 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009666 },
9667 {
9668 .cmd = NL80211_CMD_LEAVE_IBSS,
9669 .doit = nl80211_leave_ibss,
9670 .policy = nl80211_policy,
9671 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009672 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009673 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009674 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009675#ifdef CONFIG_NL80211_TESTMODE
9676 {
9677 .cmd = NL80211_CMD_TESTMODE,
9678 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009679 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009680 .policy = nl80211_policy,
9681 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009682 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9683 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009684 },
9685#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009686 {
9687 .cmd = NL80211_CMD_CONNECT,
9688 .doit = nl80211_connect,
9689 .policy = nl80211_policy,
9690 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009691 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009692 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009693 },
9694 {
9695 .cmd = NL80211_CMD_DISCONNECT,
9696 .doit = nl80211_disconnect,
9697 .policy = nl80211_policy,
9698 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009699 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009700 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009701 },
Johannes Berg463d0182009-07-14 00:33:35 +02009702 {
9703 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9704 .doit = nl80211_wiphy_netns,
9705 .policy = nl80211_policy,
9706 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009707 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9708 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009709 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009710 {
9711 .cmd = NL80211_CMD_GET_SURVEY,
9712 .policy = nl80211_policy,
9713 .dumpit = nl80211_dump_survey,
9714 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009715 {
9716 .cmd = NL80211_CMD_SET_PMKSA,
9717 .doit = nl80211_setdel_pmksa,
9718 .policy = nl80211_policy,
9719 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009721 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009722 },
9723 {
9724 .cmd = NL80211_CMD_DEL_PMKSA,
9725 .doit = nl80211_setdel_pmksa,
9726 .policy = nl80211_policy,
9727 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009729 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009730 },
9731 {
9732 .cmd = NL80211_CMD_FLUSH_PMKSA,
9733 .doit = nl80211_flush_pmksa,
9734 .policy = nl80211_policy,
9735 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009737 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009738 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009739 {
9740 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9741 .doit = nl80211_remain_on_channel,
9742 .policy = nl80211_policy,
9743 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009744 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009745 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009746 },
9747 {
9748 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9749 .doit = nl80211_cancel_remain_on_channel,
9750 .policy = nl80211_policy,
9751 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009752 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009753 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009754 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009755 {
9756 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9757 .doit = nl80211_set_tx_bitrate_mask,
9758 .policy = nl80211_policy,
9759 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009760 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9761 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009762 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009763 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009764 .cmd = NL80211_CMD_REGISTER_FRAME,
9765 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009766 .policy = nl80211_policy,
9767 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009768 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009769 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009770 },
9771 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009772 .cmd = NL80211_CMD_FRAME,
9773 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009774 .policy = nl80211_policy,
9775 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009776 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009777 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009778 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009779 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009780 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9781 .doit = nl80211_tx_mgmt_cancel_wait,
9782 .policy = nl80211_policy,
9783 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009784 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009785 NL80211_FLAG_NEED_RTNL,
9786 },
9787 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009788 .cmd = NL80211_CMD_SET_POWER_SAVE,
9789 .doit = nl80211_set_power_save,
9790 .policy = nl80211_policy,
9791 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009792 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9793 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009794 },
9795 {
9796 .cmd = NL80211_CMD_GET_POWER_SAVE,
9797 .doit = nl80211_get_power_save,
9798 .policy = nl80211_policy,
9799 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009800 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9801 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009802 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009803 {
9804 .cmd = NL80211_CMD_SET_CQM,
9805 .doit = nl80211_set_cqm,
9806 .policy = nl80211_policy,
9807 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009808 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9809 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009810 },
Johannes Bergf444de02010-05-05 15:25:02 +02009811 {
9812 .cmd = NL80211_CMD_SET_CHANNEL,
9813 .doit = nl80211_set_channel,
9814 .policy = nl80211_policy,
9815 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009816 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9817 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009818 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009819 {
9820 .cmd = NL80211_CMD_SET_WDS_PEER,
9821 .doit = nl80211_set_wds_peer,
9822 .policy = nl80211_policy,
9823 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009824 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9825 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009826 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009827 {
9828 .cmd = NL80211_CMD_JOIN_MESH,
9829 .doit = nl80211_join_mesh,
9830 .policy = nl80211_policy,
9831 .flags = GENL_ADMIN_PERM,
9832 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9833 NL80211_FLAG_NEED_RTNL,
9834 },
9835 {
9836 .cmd = NL80211_CMD_LEAVE_MESH,
9837 .doit = nl80211_leave_mesh,
9838 .policy = nl80211_policy,
9839 .flags = GENL_ADMIN_PERM,
9840 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9841 NL80211_FLAG_NEED_RTNL,
9842 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009843#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009844 {
9845 .cmd = NL80211_CMD_GET_WOWLAN,
9846 .doit = nl80211_get_wowlan,
9847 .policy = nl80211_policy,
9848 /* can be retrieved by unprivileged users */
9849 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9850 NL80211_FLAG_NEED_RTNL,
9851 },
9852 {
9853 .cmd = NL80211_CMD_SET_WOWLAN,
9854 .doit = nl80211_set_wowlan,
9855 .policy = nl80211_policy,
9856 .flags = GENL_ADMIN_PERM,
9857 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9858 NL80211_FLAG_NEED_RTNL,
9859 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009860#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009861 {
9862 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9863 .doit = nl80211_set_rekey_data,
9864 .policy = nl80211_policy,
9865 .flags = GENL_ADMIN_PERM,
9866 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9867 NL80211_FLAG_NEED_RTNL,
9868 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009869 {
9870 .cmd = NL80211_CMD_TDLS_MGMT,
9871 .doit = nl80211_tdls_mgmt,
9872 .policy = nl80211_policy,
9873 .flags = GENL_ADMIN_PERM,
9874 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9875 NL80211_FLAG_NEED_RTNL,
9876 },
9877 {
9878 .cmd = NL80211_CMD_TDLS_OPER,
9879 .doit = nl80211_tdls_oper,
9880 .policy = nl80211_policy,
9881 .flags = GENL_ADMIN_PERM,
9882 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9883 NL80211_FLAG_NEED_RTNL,
9884 },
Johannes Berg28946da2011-11-04 11:18:12 +01009885 {
9886 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9887 .doit = nl80211_register_unexpected_frame,
9888 .policy = nl80211_policy,
9889 .flags = GENL_ADMIN_PERM,
9890 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9891 NL80211_FLAG_NEED_RTNL,
9892 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009893 {
9894 .cmd = NL80211_CMD_PROBE_CLIENT,
9895 .doit = nl80211_probe_client,
9896 .policy = nl80211_policy,
9897 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009898 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009899 NL80211_FLAG_NEED_RTNL,
9900 },
Johannes Berg5e760232011-11-04 11:18:17 +01009901 {
9902 .cmd = NL80211_CMD_REGISTER_BEACONS,
9903 .doit = nl80211_register_beacons,
9904 .policy = nl80211_policy,
9905 .flags = GENL_ADMIN_PERM,
9906 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9907 NL80211_FLAG_NEED_RTNL,
9908 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009909 {
9910 .cmd = NL80211_CMD_SET_NOACK_MAP,
9911 .doit = nl80211_set_noack_map,
9912 .policy = nl80211_policy,
9913 .flags = GENL_ADMIN_PERM,
9914 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9915 NL80211_FLAG_NEED_RTNL,
9916 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009917 {
9918 .cmd = NL80211_CMD_START_P2P_DEVICE,
9919 .doit = nl80211_start_p2p_device,
9920 .policy = nl80211_policy,
9921 .flags = GENL_ADMIN_PERM,
9922 .internal_flags = NL80211_FLAG_NEED_WDEV |
9923 NL80211_FLAG_NEED_RTNL,
9924 },
9925 {
9926 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9927 .doit = nl80211_stop_p2p_device,
9928 .policy = nl80211_policy,
9929 .flags = GENL_ADMIN_PERM,
9930 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9931 NL80211_FLAG_NEED_RTNL,
9932 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009933 {
9934 .cmd = NL80211_CMD_SET_MCAST_RATE,
9935 .doit = nl80211_set_mcast_rate,
9936 .policy = nl80211_policy,
9937 .flags = GENL_ADMIN_PERM,
9938 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9939 NL80211_FLAG_NEED_RTNL,
9940 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309941 {
9942 .cmd = NL80211_CMD_SET_MAC_ACL,
9943 .doit = nl80211_set_mac_acl,
9944 .policy = nl80211_policy,
9945 .flags = GENL_ADMIN_PERM,
9946 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9947 NL80211_FLAG_NEED_RTNL,
9948 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009949 {
9950 .cmd = NL80211_CMD_RADAR_DETECT,
9951 .doit = nl80211_start_radar_detection,
9952 .policy = nl80211_policy,
9953 .flags = GENL_ADMIN_PERM,
9954 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9955 NL80211_FLAG_NEED_RTNL,
9956 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009957 {
9958 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9959 .doit = nl80211_get_protocol_features,
9960 .policy = nl80211_policy,
9961 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009962 {
9963 .cmd = NL80211_CMD_UPDATE_FT_IES,
9964 .doit = nl80211_update_ft_ies,
9965 .policy = nl80211_policy,
9966 .flags = GENL_ADMIN_PERM,
9967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9968 NL80211_FLAG_NEED_RTNL,
9969 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009970 {
9971 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9972 .doit = nl80211_crit_protocol_start,
9973 .policy = nl80211_policy,
9974 .flags = GENL_ADMIN_PERM,
9975 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9976 NL80211_FLAG_NEED_RTNL,
9977 },
9978 {
9979 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9980 .doit = nl80211_crit_protocol_stop,
9981 .policy = nl80211_policy,
9982 .flags = GENL_ADMIN_PERM,
9983 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9984 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009985 },
9986 {
9987 .cmd = NL80211_CMD_GET_COALESCE,
9988 .doit = nl80211_get_coalesce,
9989 .policy = nl80211_policy,
9990 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9991 NL80211_FLAG_NEED_RTNL,
9992 },
9993 {
9994 .cmd = NL80211_CMD_SET_COALESCE,
9995 .doit = nl80211_set_coalesce,
9996 .policy = nl80211_policy,
9997 .flags = GENL_ADMIN_PERM,
9998 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9999 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +020010000 },
10001 {
10002 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10003 .doit = nl80211_channel_switch,
10004 .policy = nl80211_policy,
10005 .flags = GENL_ADMIN_PERM,
10006 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10007 NL80211_FLAG_NEED_RTNL,
10008 },
Johannes Bergad7e7182013-11-13 13:37:47 +010010009 {
10010 .cmd = NL80211_CMD_VENDOR,
10011 .doit = nl80211_vendor_cmd,
10012 .policy = nl80211_policy,
10013 .flags = GENL_ADMIN_PERM,
10014 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10015 NL80211_FLAG_NEED_RTNL,
10016 },
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -080010017 {
10018 .cmd = NL80211_CMD_SET_QOS_MAP,
10019 .doit = nl80211_set_qos_map,
10020 .policy = nl80211_policy,
10021 .flags = GENL_ADMIN_PERM,
10022 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10023 NL80211_FLAG_NEED_RTNL,
10024 },
Johannes Berg55682962007-09-20 13:09:35 -040010025};
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010026
Johannes Berg55682962007-09-20 13:09:35 -040010027/* notification functions */
10028
10029void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
10030{
10031 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +020010032 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -040010033
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010034 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010035 if (!msg)
10036 return;
10037
Johannes Berg86e8cf92013-06-19 10:57:22 +020010038 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -040010039 nlmsg_free(msg);
10040 return;
10041 }
10042
Johannes Berg68eb5502013-11-19 15:19:38 +010010043 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010044 NL80211_MCGRP_CONFIG, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -040010045}
10046
Johannes Berg362a4152009-05-24 16:43:15 +020010047static int nl80211_add_scan_req(struct sk_buff *msg,
10048 struct cfg80211_registered_device *rdev)
10049{
10050 struct cfg80211_scan_request *req = rdev->scan_req;
10051 struct nlattr *nest;
10052 int i;
10053
10054 if (WARN_ON(!req))
10055 return 0;
10056
10057 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10058 if (!nest)
10059 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010060 for (i = 0; i < req->n_ssids; i++) {
10061 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10062 goto nla_put_failure;
10063 }
Johannes Berg362a4152009-05-24 16:43:15 +020010064 nla_nest_end(msg, nest);
10065
10066 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10067 if (!nest)
10068 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -040010069 for (i = 0; i < req->n_channels; i++) {
10070 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10071 goto nla_put_failure;
10072 }
Johannes Berg362a4152009-05-24 16:43:15 +020010073 nla_nest_end(msg, nest);
10074
David S. Miller9360ffd2012-03-29 04:41:26 -040010075 if (req->ie &&
10076 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10077 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +020010078
Johannes Bergae917c92013-10-25 11:05:22 +020010079 if (req->flags &&
10080 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10081 goto nla_put_failure;
Sam Lefflered4737712012-10-11 21:03:31 -070010082
Johannes Berg362a4152009-05-24 16:43:15 +020010083 return 0;
10084 nla_put_failure:
10085 return -ENOBUFS;
10086}
10087
Johannes Berga538e2d2009-06-16 19:56:42 +020010088static int nl80211_send_scan_msg(struct sk_buff *msg,
10089 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010090 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010091 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +020010092 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +010010093{
10094 void *hdr;
10095
Eric W. Biederman15e47302012-09-07 20:12:54 +000010096 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +010010097 if (!hdr)
10098 return -1;
10099
David S. Miller9360ffd2012-03-29 04:41:26 -040010100 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +020010101 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10102 wdev->netdev->ifindex)) ||
10103 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010104 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +010010105
Johannes Berg362a4152009-05-24 16:43:15 +020010106 /* ignore errors and send incomplete event anyway */
10107 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +010010108
10109 return genlmsg_end(msg, hdr);
10110
10111 nla_put_failure:
10112 genlmsg_cancel(msg, hdr);
10113 return -EMSGSIZE;
10114}
10115
Luciano Coelho807f8a82011-05-11 17:09:35 +030010116static int
10117nl80211_send_sched_scan_msg(struct sk_buff *msg,
10118 struct cfg80211_registered_device *rdev,
10119 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010120 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +030010121{
10122 void *hdr;
10123
Eric W. Biederman15e47302012-09-07 20:12:54 +000010124 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010125 if (!hdr)
10126 return -1;
10127
David S. Miller9360ffd2012-03-29 04:41:26 -040010128 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10129 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10130 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +030010131
10132 return genlmsg_end(msg, hdr);
10133
10134 nla_put_failure:
10135 genlmsg_cancel(msg, hdr);
10136 return -EMSGSIZE;
10137}
10138
Johannes Berga538e2d2009-06-16 19:56:42 +020010139void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +020010140 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +020010141{
10142 struct sk_buff *msg;
10143
Thomas Graf58050fc2012-06-28 03:57:45 +000010144 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010145 if (!msg)
10146 return;
10147
Johannes Bergfd014282012-06-18 19:17:03 +020010148 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +020010149 NL80211_CMD_TRIGGER_SCAN) < 0) {
10150 nlmsg_free(msg);
10151 return;
10152 }
10153
Johannes Berg68eb5502013-11-19 15:19:38 +010010154 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010155 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +020010156}
10157
Johannes Bergf9d15d12014-01-22 11:14:19 +020010158struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10159 struct wireless_dev *wdev, bool aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010010160{
10161 struct sk_buff *msg;
10162
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010164 if (!msg)
Johannes Bergf9d15d12014-01-22 11:14:19 +020010165 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010166
Johannes Bergfd014282012-06-18 19:17:03 +020010167 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Bergf9d15d12014-01-22 11:14:19 +020010168 aborted ? NL80211_CMD_SCAN_ABORTED :
10169 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +010010170 nlmsg_free(msg);
Johannes Bergf9d15d12014-01-22 11:14:19 +020010171 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010010172 }
10173
Johannes Bergf9d15d12014-01-22 11:14:19 +020010174 return msg;
Johannes Berg2a519312009-02-10 21:25:55 +010010175}
10176
Johannes Bergf9d15d12014-01-22 11:14:19 +020010177void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10178 struct sk_buff *msg)
Johannes Berg2a519312009-02-10 21:25:55 +010010179{
Johannes Berg2a519312009-02-10 21:25:55 +010010180 if (!msg)
10181 return;
10182
Johannes Berg68eb5502013-11-19 15:19:38 +010010183 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010184 NL80211_MCGRP_SCAN, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +010010185}
10186
Luciano Coelho807f8a82011-05-11 17:09:35 +030010187void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10188 struct net_device *netdev)
10189{
10190 struct sk_buff *msg;
10191
10192 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10193 if (!msg)
10194 return;
10195
10196 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10197 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10198 nlmsg_free(msg);
10199 return;
10200 }
10201
Johannes Berg68eb5502013-11-19 15:19:38 +010010202 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010203 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010204}
10205
10206void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10207 struct net_device *netdev, u32 cmd)
10208{
10209 struct sk_buff *msg;
10210
Thomas Graf58050fc2012-06-28 03:57:45 +000010211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010212 if (!msg)
10213 return;
10214
10215 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10216 nlmsg_free(msg);
10217 return;
10218 }
10219
Johannes Berg68eb5502013-11-19 15:19:38 +010010220 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010221 NL80211_MCGRP_SCAN, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +030010222}
10223
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010224/*
10225 * This can happen on global regulatory changes or device specific settings
10226 * based on custom world regulatory domains.
10227 */
10228void nl80211_send_reg_change_event(struct regulatory_request *request)
10229{
10230 struct sk_buff *msg;
10231 void *hdr;
10232
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010234 if (!msg)
10235 return;
10236
10237 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10238 if (!hdr) {
10239 nlmsg_free(msg);
10240 return;
10241 }
10242
10243 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -040010244 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10245 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010246
David S. Miller9360ffd2012-03-29 04:41:26 -040010247 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10248 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10249 NL80211_REGDOM_TYPE_WORLD))
10250 goto nla_put_failure;
10251 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10252 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10253 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10254 goto nla_put_failure;
10255 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10256 request->intersect) {
10257 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10258 NL80211_REGDOM_TYPE_INTERSECTION))
10259 goto nla_put_failure;
10260 } else {
10261 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10262 NL80211_REGDOM_TYPE_COUNTRY) ||
10263 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10264 request->alpha2))
10265 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010266 }
10267
Johannes Bergf4173762012-12-03 18:23:37 +010010268 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -040010269 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10270 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010271
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010272 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010273
Johannes Bergbc43b282009-07-25 10:54:13 +020010274 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010275 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010276 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Bergbc43b282009-07-25 10:54:13 +020010277 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010278
10279 return;
10280
10281nla_put_failure:
10282 genlmsg_cancel(msg, hdr);
10283 nlmsg_free(msg);
10284}
10285
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010286static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10287 struct net_device *netdev,
10288 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010289 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010290{
10291 struct sk_buff *msg;
10292 void *hdr;
10293
Johannes Berge6d6e342009-07-01 21:26:47 +020010294 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010295 if (!msg)
10296 return;
10297
10298 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10299 if (!hdr) {
10300 nlmsg_free(msg);
10301 return;
10302 }
10303
David S. Miller9360ffd2012-03-29 04:41:26 -040010304 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10305 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10306 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10307 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010308
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010309 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010310
Johannes Berg68eb5502013-11-19 15:19:38 +010010311 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010312 NL80211_MCGRP_MLME, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010313 return;
10314
10315 nla_put_failure:
10316 genlmsg_cancel(msg, hdr);
10317 nlmsg_free(msg);
10318}
10319
10320void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010321 struct net_device *netdev, const u8 *buf,
10322 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010323{
10324 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010325 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010326}
10327
10328void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10329 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010330 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010331{
Johannes Berge6d6e342009-07-01 21:26:47 +020010332 nl80211_send_mlme_event(rdev, netdev, buf, len,
10333 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010334}
10335
Jouni Malinen53b46b82009-03-27 20:53:56 +020010336void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010337 struct net_device *netdev, const u8 *buf,
10338 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010339{
10340 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010341 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010342}
10343
Jouni Malinen53b46b82009-03-27 20:53:56 +020010344void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10345 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010346 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010347{
10348 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010349 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010350}
10351
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010352void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10353 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010354{
Johannes Berg947add32013-02-22 22:05:20 +010010355 struct wireless_dev *wdev = dev->ieee80211_ptr;
10356 struct wiphy *wiphy = wdev->wiphy;
10357 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010358 const struct ieee80211_mgmt *mgmt = (void *)buf;
10359 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010360
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010361 if (WARN_ON(len < 2))
10362 return;
10363
10364 if (ieee80211_is_deauth(mgmt->frame_control))
10365 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10366 else
10367 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10368
10369 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10370 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010371}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010372EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010373
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010374static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10375 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010376 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010377{
10378 struct sk_buff *msg;
10379 void *hdr;
10380
Johannes Berge6d6e342009-07-01 21:26:47 +020010381 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010382 if (!msg)
10383 return;
10384
10385 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10386 if (!hdr) {
10387 nlmsg_free(msg);
10388 return;
10389 }
10390
David S. Miller9360ffd2012-03-29 04:41:26 -040010391 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10392 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10393 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10394 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10395 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010396
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010397 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010398
Johannes Berg68eb5502013-11-19 15:19:38 +010010399 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010400 NL80211_MCGRP_MLME, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010401 return;
10402
10403 nla_put_failure:
10404 genlmsg_cancel(msg, hdr);
10405 nlmsg_free(msg);
10406}
10407
10408void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010409 struct net_device *netdev, const u8 *addr,
10410 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010411{
10412 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010413 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010414}
10415
10416void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010417 struct net_device *netdev, const u8 *addr,
10418 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010419{
Johannes Berge6d6e342009-07-01 21:26:47 +020010420 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10421 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010422}
10423
Samuel Ortizb23aa672009-07-01 21:26:54 +020010424void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10425 struct net_device *netdev, const u8 *bssid,
10426 const u8 *req_ie, size_t req_ie_len,
10427 const u8 *resp_ie, size_t resp_ie_len,
10428 u16 status, gfp_t gfp)
10429{
10430 struct sk_buff *msg;
10431 void *hdr;
10432
Thomas Graf58050fc2012-06-28 03:57:45 +000010433 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010434 if (!msg)
10435 return;
10436
10437 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10438 if (!hdr) {
10439 nlmsg_free(msg);
10440 return;
10441 }
10442
David S. Miller9360ffd2012-03-29 04:41:26 -040010443 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10444 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10445 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10446 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10447 (req_ie &&
10448 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10449 (resp_ie &&
10450 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10451 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010452
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010453 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010454
Johannes Berg68eb5502013-11-19 15:19:38 +010010455 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010456 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010457 return;
10458
10459 nla_put_failure:
10460 genlmsg_cancel(msg, hdr);
10461 nlmsg_free(msg);
10462
10463}
10464
10465void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10466 struct net_device *netdev, const u8 *bssid,
10467 const u8 *req_ie, size_t req_ie_len,
10468 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10469{
10470 struct sk_buff *msg;
10471 void *hdr;
10472
Thomas Graf58050fc2012-06-28 03:57:45 +000010473 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010474 if (!msg)
10475 return;
10476
10477 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10478 if (!hdr) {
10479 nlmsg_free(msg);
10480 return;
10481 }
10482
David S. Miller9360ffd2012-03-29 04:41:26 -040010483 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10484 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10485 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10486 (req_ie &&
10487 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10488 (resp_ie &&
10489 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10490 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010491
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010492 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010493
Johannes Berg68eb5502013-11-19 15:19:38 +010010494 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010495 NL80211_MCGRP_MLME, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010496 return;
10497
10498 nla_put_failure:
10499 genlmsg_cancel(msg, hdr);
10500 nlmsg_free(msg);
10501
10502}
10503
10504void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10505 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010506 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010507{
10508 struct sk_buff *msg;
10509 void *hdr;
10510
Thomas Graf58050fc2012-06-28 03:57:45 +000010511 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010512 if (!msg)
10513 return;
10514
10515 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10516 if (!hdr) {
10517 nlmsg_free(msg);
10518 return;
10519 }
10520
David S. Miller9360ffd2012-03-29 04:41:26 -040010521 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10522 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10523 (from_ap && reason &&
10524 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10525 (from_ap &&
10526 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10527 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10528 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010529
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010530 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010531
Johannes Berg68eb5502013-11-19 15:19:38 +010010532 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010533 NL80211_MCGRP_MLME, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010534 return;
10535
10536 nla_put_failure:
10537 genlmsg_cancel(msg, hdr);
10538 nlmsg_free(msg);
10539
10540}
10541
Johannes Berg04a773a2009-04-19 21:24:32 +020010542void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10543 struct net_device *netdev, const u8 *bssid,
10544 gfp_t gfp)
10545{
10546 struct sk_buff *msg;
10547 void *hdr;
10548
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010549 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010550 if (!msg)
10551 return;
10552
10553 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10554 if (!hdr) {
10555 nlmsg_free(msg);
10556 return;
10557 }
10558
David S. Miller9360ffd2012-03-29 04:41:26 -040010559 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10560 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10561 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10562 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010563
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010564 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010565
Johannes Berg68eb5502013-11-19 15:19:38 +010010566 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010567 NL80211_MCGRP_MLME, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010568 return;
10569
10570 nla_put_failure:
10571 genlmsg_cancel(msg, hdr);
10572 nlmsg_free(msg);
10573}
10574
Johannes Berg947add32013-02-22 22:05:20 +010010575void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10576 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010577{
Johannes Berg947add32013-02-22 22:05:20 +010010578 struct wireless_dev *wdev = dev->ieee80211_ptr;
10579 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010580 struct sk_buff *msg;
10581 void *hdr;
10582
Johannes Berg947add32013-02-22 22:05:20 +010010583 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10584 return;
10585
10586 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10587
Javier Cardonac93b5e72011-04-07 15:08:34 -070010588 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10589 if (!msg)
10590 return;
10591
10592 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10593 if (!hdr) {
10594 nlmsg_free(msg);
10595 return;
10596 }
10597
David S. Miller9360ffd2012-03-29 04:41:26 -040010598 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010599 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10600 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010601 (ie_len && ie &&
10602 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10603 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010604
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010605 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010606
Johannes Berg68eb5502013-11-19 15:19:38 +010010607 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010608 NL80211_MCGRP_MLME, gfp);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010609 return;
10610
10611 nla_put_failure:
10612 genlmsg_cancel(msg, hdr);
10613 nlmsg_free(msg);
10614}
Johannes Berg947add32013-02-22 22:05:20 +010010615EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010616
Jouni Malinena3b8b052009-03-27 21:59:49 +020010617void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10618 struct net_device *netdev, const u8 *addr,
10619 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010620 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010621{
10622 struct sk_buff *msg;
10623 void *hdr;
10624
Johannes Berge6d6e342009-07-01 21:26:47 +020010625 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010626 if (!msg)
10627 return;
10628
10629 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10630 if (!hdr) {
10631 nlmsg_free(msg);
10632 return;
10633 }
10634
David S. Miller9360ffd2012-03-29 04:41:26 -040010635 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10636 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10637 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10638 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10639 (key_id != -1 &&
10640 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10641 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10642 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010643
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010644 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010645
Johannes Berg68eb5502013-11-19 15:19:38 +010010646 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010647 NL80211_MCGRP_MLME, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010648 return;
10649
10650 nla_put_failure:
10651 genlmsg_cancel(msg, hdr);
10652 nlmsg_free(msg);
10653}
10654
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010655void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10656 struct ieee80211_channel *channel_before,
10657 struct ieee80211_channel *channel_after)
10658{
10659 struct sk_buff *msg;
10660 void *hdr;
10661 struct nlattr *nl_freq;
10662
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010663 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010664 if (!msg)
10665 return;
10666
10667 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10668 if (!hdr) {
10669 nlmsg_free(msg);
10670 return;
10671 }
10672
10673 /*
10674 * Since we are applying the beacon hint to a wiphy we know its
10675 * wiphy_idx is valid
10676 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010677 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10678 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010679
10680 /* Before */
10681 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10682 if (!nl_freq)
10683 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010684 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010685 goto nla_put_failure;
10686 nla_nest_end(msg, nl_freq);
10687
10688 /* After */
10689 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10690 if (!nl_freq)
10691 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010692 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010693 goto nla_put_failure;
10694 nla_nest_end(msg, nl_freq);
10695
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010696 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010697
Johannes Berg463d0182009-07-14 00:33:35 +020010698 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +010010699 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010700 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
Johannes Berg463d0182009-07-14 00:33:35 +020010701 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010702
10703 return;
10704
10705nla_put_failure:
10706 genlmsg_cancel(msg, hdr);
10707 nlmsg_free(msg);
10708}
10709
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010710static void nl80211_send_remain_on_chan_event(
10711 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010712 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010713 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010714 unsigned int duration, gfp_t gfp)
10715{
10716 struct sk_buff *msg;
10717 void *hdr;
10718
10719 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10720 if (!msg)
10721 return;
10722
10723 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10724 if (!hdr) {
10725 nlmsg_free(msg);
10726 return;
10727 }
10728
David S. Miller9360ffd2012-03-29 04:41:26 -040010729 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010730 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10731 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010732 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010733 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010734 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10735 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010736 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10737 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010738
David S. Miller9360ffd2012-03-29 04:41:26 -040010739 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10740 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10741 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010742
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010743 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010744
Johannes Berg68eb5502013-11-19 15:19:38 +010010745 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010746 NL80211_MCGRP_MLME, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010747 return;
10748
10749 nla_put_failure:
10750 genlmsg_cancel(msg, hdr);
10751 nlmsg_free(msg);
10752}
10753
Johannes Berg947add32013-02-22 22:05:20 +010010754void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10755 struct ieee80211_channel *chan,
10756 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010757{
Johannes Berg947add32013-02-22 22:05:20 +010010758 struct wiphy *wiphy = wdev->wiphy;
10759 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10760
10761 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010762 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010763 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010764 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010765}
Johannes Berg947add32013-02-22 22:05:20 +010010766EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010767
Johannes Berg947add32013-02-22 22:05:20 +010010768void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10769 struct ieee80211_channel *chan,
10770 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010771{
Johannes Berg947add32013-02-22 22:05:20 +010010772 struct wiphy *wiphy = wdev->wiphy;
10773 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10774
10775 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010776 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010777 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010778}
Johannes Berg947add32013-02-22 22:05:20 +010010779EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010780
Johannes Berg947add32013-02-22 22:05:20 +010010781void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10782 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010783{
Johannes Berg947add32013-02-22 22:05:20 +010010784 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10785 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010786 struct sk_buff *msg;
10787
Johannes Berg947add32013-02-22 22:05:20 +010010788 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10789
Thomas Graf58050fc2012-06-28 03:57:45 +000010790 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010791 if (!msg)
10792 return;
10793
John W. Linville66266b32012-03-15 13:25:41 -040010794 if (nl80211_send_station(msg, 0, 0, 0,
10795 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010796 nlmsg_free(msg);
10797 return;
10798 }
10799
Johannes Berg68eb5502013-11-19 15:19:38 +010010800 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010801 NL80211_MCGRP_MLME, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010802}
Johannes Berg947add32013-02-22 22:05:20 +010010803EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010804
Johannes Berg947add32013-02-22 22:05:20 +010010805void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010806{
Johannes Berg947add32013-02-22 22:05:20 +010010807 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10808 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010809 struct sk_buff *msg;
10810 void *hdr;
10811
Johannes Berg947add32013-02-22 22:05:20 +010010812 trace_cfg80211_del_sta(dev, mac_addr);
10813
Thomas Graf58050fc2012-06-28 03:57:45 +000010814 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010815 if (!msg)
10816 return;
10817
10818 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10819 if (!hdr) {
10820 nlmsg_free(msg);
10821 return;
10822 }
10823
David S. Miller9360ffd2012-03-29 04:41:26 -040010824 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10825 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10826 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010827
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010828 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010829
Johannes Berg68eb5502013-11-19 15:19:38 +010010830 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010831 NL80211_MCGRP_MLME, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010832 return;
10833
10834 nla_put_failure:
10835 genlmsg_cancel(msg, hdr);
10836 nlmsg_free(msg);
10837}
Johannes Berg947add32013-02-22 22:05:20 +010010838EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010839
Johannes Berg947add32013-02-22 22:05:20 +010010840void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10841 enum nl80211_connect_failed_reason reason,
10842 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010843{
Johannes Berg947add32013-02-22 22:05:20 +010010844 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10845 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010846 struct sk_buff *msg;
10847 void *hdr;
10848
10849 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10850 if (!msg)
10851 return;
10852
10853 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10854 if (!hdr) {
10855 nlmsg_free(msg);
10856 return;
10857 }
10858
10859 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10860 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10861 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10862 goto nla_put_failure;
10863
10864 genlmsg_end(msg, hdr);
10865
Johannes Berg68eb5502013-11-19 15:19:38 +010010866 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010010867 NL80211_MCGRP_MLME, gfp);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010868 return;
10869
10870 nla_put_failure:
10871 genlmsg_cancel(msg, hdr);
10872 nlmsg_free(msg);
10873}
Johannes Berg947add32013-02-22 22:05:20 +010010874EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010875
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010876static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10877 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010878{
10879 struct wireless_dev *wdev = dev->ieee80211_ptr;
10880 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10881 struct sk_buff *msg;
10882 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010883 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010884
Eric W. Biederman15e47302012-09-07 20:12:54 +000010885 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010886 return false;
10887
10888 msg = nlmsg_new(100, gfp);
10889 if (!msg)
10890 return true;
10891
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010892 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010893 if (!hdr) {
10894 nlmsg_free(msg);
10895 return true;
10896 }
10897
David S. Miller9360ffd2012-03-29 04:41:26 -040010898 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10899 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10900 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10901 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010902
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010903 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010904 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010905 return true;
10906
10907 nla_put_failure:
10908 genlmsg_cancel(msg, hdr);
10909 nlmsg_free(msg);
10910 return true;
10911}
10912
Johannes Berg947add32013-02-22 22:05:20 +010010913bool cfg80211_rx_spurious_frame(struct net_device *dev,
10914 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010915{
Johannes Berg947add32013-02-22 22:05:20 +010010916 struct wireless_dev *wdev = dev->ieee80211_ptr;
10917 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010918
Johannes Berg947add32013-02-22 22:05:20 +010010919 trace_cfg80211_rx_spurious_frame(dev, addr);
10920
10921 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10922 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10923 trace_cfg80211_return_bool(false);
10924 return false;
10925 }
10926 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10927 addr, gfp);
10928 trace_cfg80211_return_bool(ret);
10929 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010930}
Johannes Berg947add32013-02-22 22:05:20 +010010931EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10932
10933bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10934 const u8 *addr, gfp_t gfp)
10935{
10936 struct wireless_dev *wdev = dev->ieee80211_ptr;
10937 bool ret;
10938
10939 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10940
10941 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10942 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10943 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10944 trace_cfg80211_return_bool(false);
10945 return false;
10946 }
10947 ret = __nl80211_unexpected_frame(dev,
10948 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10949 addr, gfp);
10950 trace_cfg80211_return_bool(ret);
10951 return ret;
10952}
10953EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010954
Johannes Berg2e161f72010-08-12 15:38:38 +020010955int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010956 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010957 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010958 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010959{
Johannes Berg71bbc992012-06-15 15:30:18 +020010960 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010961 struct sk_buff *msg;
10962 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010963
10964 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10965 if (!msg)
10966 return -ENOMEM;
10967
Johannes Berg2e161f72010-08-12 15:38:38 +020010968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010969 if (!hdr) {
10970 nlmsg_free(msg);
10971 return -ENOMEM;
10972 }
10973
David S. Miller9360ffd2012-03-29 04:41:26 -040010974 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010975 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10976 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010977 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010978 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10979 (sig_dbm &&
10980 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010981 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10982 (flags &&
10983 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010984 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010985
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010986 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010987
Eric W. Biederman15e47302012-09-07 20:12:54 +000010988 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010989
10990 nla_put_failure:
10991 genlmsg_cancel(msg, hdr);
10992 nlmsg_free(msg);
10993 return -ENOBUFS;
10994}
10995
Johannes Berg947add32013-02-22 22:05:20 +010010996void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10997 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010998{
Johannes Berg947add32013-02-22 22:05:20 +010010999 struct wiphy *wiphy = wdev->wiphy;
11000 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020011001 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020011002 struct sk_buff *msg;
11003 void *hdr;
11004
Johannes Berg947add32013-02-22 22:05:20 +010011005 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11006
Jouni Malinen026331c2010-02-15 12:53:10 +020011007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11008 if (!msg)
11009 return;
11010
Johannes Berg2e161f72010-08-12 15:38:38 +020011011 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020011012 if (!hdr) {
11013 nlmsg_free(msg);
11014 return;
11015 }
11016
David S. Miller9360ffd2012-03-29 04:41:26 -040011017 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020011018 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11019 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030011020 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011021 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11022 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11023 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11024 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020011025
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011026 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020011027
Johannes Berg68eb5502013-11-19 15:19:38 +010011028 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011029 NL80211_MCGRP_MLME, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020011030 return;
11031
11032 nla_put_failure:
11033 genlmsg_cancel(msg, hdr);
11034 nlmsg_free(msg);
11035}
Johannes Berg947add32013-02-22 22:05:20 +010011036EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020011037
Johannes Berg947add32013-02-22 22:05:20 +010011038void cfg80211_cqm_rssi_notify(struct net_device *dev,
11039 enum nl80211_cqm_rssi_threshold_event rssi_event,
11040 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011041{
Johannes Berg947add32013-02-22 22:05:20 +010011042 struct wireless_dev *wdev = dev->ieee80211_ptr;
11043 struct wiphy *wiphy = wdev->wiphy;
11044 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011045 struct sk_buff *msg;
11046 struct nlattr *pinfoattr;
11047 void *hdr;
11048
Johannes Berg947add32013-02-22 22:05:20 +010011049 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11050
Thomas Graf58050fc2012-06-28 03:57:45 +000011051 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011052 if (!msg)
11053 return;
11054
11055 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11056 if (!hdr) {
11057 nlmsg_free(msg);
11058 return;
11059 }
11060
David S. Miller9360ffd2012-03-29 04:41:26 -040011061 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011062 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040011063 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011064
11065 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11066 if (!pinfoattr)
11067 goto nla_put_failure;
11068
David S. Miller9360ffd2012-03-29 04:41:26 -040011069 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11070 rssi_event))
11071 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011072
11073 nla_nest_end(msg, pinfoattr);
11074
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011075 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011076
Johannes Berg68eb5502013-11-19 15:19:38 +010011077 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011078 NL80211_MCGRP_MLME, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011079 return;
11080
11081 nla_put_failure:
11082 genlmsg_cancel(msg, hdr);
11083 nlmsg_free(msg);
11084}
Johannes Berg947add32013-02-22 22:05:20 +010011085EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020011086
Johannes Berg947add32013-02-22 22:05:20 +010011087static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11088 struct net_device *netdev, const u8 *bssid,
11089 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020011090{
11091 struct sk_buff *msg;
11092 struct nlattr *rekey_attr;
11093 void *hdr;
11094
Thomas Graf58050fc2012-06-28 03:57:45 +000011095 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011096 if (!msg)
11097 return;
11098
11099 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11100 if (!hdr) {
11101 nlmsg_free(msg);
11102 return;
11103 }
11104
David S. Miller9360ffd2012-03-29 04:41:26 -040011105 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11106 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11107 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11108 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011109
11110 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11111 if (!rekey_attr)
11112 goto nla_put_failure;
11113
David S. Miller9360ffd2012-03-29 04:41:26 -040011114 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11115 NL80211_REPLAY_CTR_LEN, replay_ctr))
11116 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020011117
11118 nla_nest_end(msg, rekey_attr);
11119
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011120 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020011121
Johannes Berg68eb5502013-11-19 15:19:38 +010011122 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011123 NL80211_MCGRP_MLME, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020011124 return;
11125
11126 nla_put_failure:
11127 genlmsg_cancel(msg, hdr);
11128 nlmsg_free(msg);
11129}
11130
Johannes Berg947add32013-02-22 22:05:20 +010011131void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11132 const u8 *replay_ctr, gfp_t gfp)
11133{
11134 struct wireless_dev *wdev = dev->ieee80211_ptr;
11135 struct wiphy *wiphy = wdev->wiphy;
11136 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11137
11138 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11139 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11140}
11141EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11142
11143static void
11144nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11145 struct net_device *netdev, int index,
11146 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011147{
11148 struct sk_buff *msg;
11149 struct nlattr *attr;
11150 void *hdr;
11151
Thomas Graf58050fc2012-06-28 03:57:45 +000011152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011153 if (!msg)
11154 return;
11155
11156 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11157 if (!hdr) {
11158 nlmsg_free(msg);
11159 return;
11160 }
11161
David S. Miller9360ffd2012-03-29 04:41:26 -040011162 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11163 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11164 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011165
11166 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11167 if (!attr)
11168 goto nla_put_failure;
11169
David S. Miller9360ffd2012-03-29 04:41:26 -040011170 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11171 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11172 (preauth &&
11173 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11174 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011175
11176 nla_nest_end(msg, attr);
11177
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011178 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011179
Johannes Berg68eb5502013-11-19 15:19:38 +010011180 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011181 NL80211_MCGRP_MLME, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030011182 return;
11183
11184 nla_put_failure:
11185 genlmsg_cancel(msg, hdr);
11186 nlmsg_free(msg);
11187}
11188
Johannes Berg947add32013-02-22 22:05:20 +010011189void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11190 const u8 *bssid, bool preauth, gfp_t gfp)
11191{
11192 struct wireless_dev *wdev = dev->ieee80211_ptr;
11193 struct wiphy *wiphy = wdev->wiphy;
11194 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11195
11196 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11197 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11198}
11199EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11200
11201static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11202 struct net_device *netdev,
11203 struct cfg80211_chan_def *chandef,
11204 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070011205{
11206 struct sk_buff *msg;
11207 void *hdr;
11208
Thomas Graf58050fc2012-06-28 03:57:45 +000011209 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011210 if (!msg)
11211 return;
11212
11213 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11214 if (!hdr) {
11215 nlmsg_free(msg);
11216 return;
11217 }
11218
Johannes Berg683b6d32012-11-08 21:25:48 +010011219 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11220 goto nla_put_failure;
11221
11222 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040011223 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070011224
11225 genlmsg_end(msg, hdr);
11226
Johannes Berg68eb5502013-11-19 15:19:38 +010011227 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011228 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070011229 return;
11230
11231 nla_put_failure:
11232 genlmsg_cancel(msg, hdr);
11233 nlmsg_free(msg);
11234}
11235
Johannes Berg947add32013-02-22 22:05:20 +010011236void cfg80211_ch_switch_notify(struct net_device *dev,
11237 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070011238{
Johannes Berg947add32013-02-22 22:05:20 +010011239 struct wireless_dev *wdev = dev->ieee80211_ptr;
11240 struct wiphy *wiphy = wdev->wiphy;
11241 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11242
Simon Wunderliche487eae2013-11-21 18:19:51 +010011243 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010011244
Simon Wunderliche487eae2013-11-21 18:19:51 +010011245 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010011246
11247 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020011248 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070011249 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11250 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010011251 return;
Johannes Berg947add32013-02-22 22:05:20 +010011252
Michal Kazior9e0e2962014-01-29 14:22:27 +010011253 wdev->chandef = *chandef;
Janusz Dziedzic96f55f12014-01-24 14:29:21 +010011254 wdev->preset_chandef = *chandef;
Johannes Berg947add32013-02-22 22:05:20 +010011255 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010011256}
11257EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11258
11259void cfg80211_cqm_txe_notify(struct net_device *dev,
11260 const u8 *peer, u32 num_packets,
11261 u32 rate, u32 intvl, gfp_t gfp)
11262{
11263 struct wireless_dev *wdev = dev->ieee80211_ptr;
11264 struct wiphy *wiphy = wdev->wiphy;
11265 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011266 struct sk_buff *msg;
11267 struct nlattr *pinfoattr;
11268 void *hdr;
11269
11270 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11271 if (!msg)
11272 return;
11273
11274 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11275 if (!hdr) {
11276 nlmsg_free(msg);
11277 return;
11278 }
11279
11280 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011281 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070011282 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11283 goto nla_put_failure;
11284
11285 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11286 if (!pinfoattr)
11287 goto nla_put_failure;
11288
11289 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11290 goto nla_put_failure;
11291
11292 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11293 goto nla_put_failure;
11294
11295 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11296 goto nla_put_failure;
11297
11298 nla_nest_end(msg, pinfoattr);
11299
11300 genlmsg_end(msg, hdr);
11301
Johannes Berg68eb5502013-11-19 15:19:38 +010011302 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011303 NL80211_MCGRP_MLME, gfp);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011304 return;
11305
11306 nla_put_failure:
11307 genlmsg_cancel(msg, hdr);
11308 nlmsg_free(msg);
11309}
Johannes Berg947add32013-02-22 22:05:20 +010011310EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011311
11312void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011313nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011314 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011315 enum nl80211_radar_event event,
11316 struct net_device *netdev, gfp_t gfp)
11317{
11318 struct sk_buff *msg;
11319 void *hdr;
11320
11321 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11322 if (!msg)
11323 return;
11324
11325 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11326 if (!hdr) {
11327 nlmsg_free(msg);
11328 return;
11329 }
11330
11331 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11332 goto nla_put_failure;
11333
11334 /* NOP and radar events don't need a netdev parameter */
11335 if (netdev) {
11336 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11337
11338 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11339 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11340 goto nla_put_failure;
11341 }
11342
11343 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11344 goto nla_put_failure;
11345
11346 if (nl80211_send_chandef(msg, chandef))
11347 goto nla_put_failure;
11348
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011349 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011350
Johannes Berg68eb5502013-11-19 15:19:38 +010011351 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011352 NL80211_MCGRP_MLME, gfp);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011353 return;
11354
11355 nla_put_failure:
11356 genlmsg_cancel(msg, hdr);
11357 nlmsg_free(msg);
11358}
11359
Johannes Berg947add32013-02-22 22:05:20 +010011360void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11361 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011362{
Johannes Berg947add32013-02-22 22:05:20 +010011363 struct wireless_dev *wdev = dev->ieee80211_ptr;
11364 struct wiphy *wiphy = wdev->wiphy;
11365 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011366 struct sk_buff *msg;
11367 struct nlattr *pinfoattr;
11368 void *hdr;
11369
Johannes Berg947add32013-02-22 22:05:20 +010011370 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11371
Thomas Graf58050fc2012-06-28 03:57:45 +000011372 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011373 if (!msg)
11374 return;
11375
11376 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11377 if (!hdr) {
11378 nlmsg_free(msg);
11379 return;
11380 }
11381
David S. Miller9360ffd2012-03-29 04:41:26 -040011382 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011383 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011384 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11385 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011386
11387 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11388 if (!pinfoattr)
11389 goto nla_put_failure;
11390
David S. Miller9360ffd2012-03-29 04:41:26 -040011391 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11392 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011393
11394 nla_nest_end(msg, pinfoattr);
11395
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011396 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011397
Johannes Berg68eb5502013-11-19 15:19:38 +010011398 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011399 NL80211_MCGRP_MLME, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011400 return;
11401
11402 nla_put_failure:
11403 genlmsg_cancel(msg, hdr);
11404 nlmsg_free(msg);
11405}
Johannes Berg947add32013-02-22 22:05:20 +010011406EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011407
Johannes Berg7f6cf312011-11-04 11:18:15 +010011408void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11409 u64 cookie, bool acked, gfp_t gfp)
11410{
11411 struct wireless_dev *wdev = dev->ieee80211_ptr;
11412 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11413 struct sk_buff *msg;
11414 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011415
Beni Lev4ee3e062012-08-27 12:49:39 +030011416 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11417
Thomas Graf58050fc2012-06-28 03:57:45 +000011418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011419
Johannes Berg7f6cf312011-11-04 11:18:15 +010011420 if (!msg)
11421 return;
11422
11423 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11424 if (!hdr) {
11425 nlmsg_free(msg);
11426 return;
11427 }
11428
David S. Miller9360ffd2012-03-29 04:41:26 -040011429 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11430 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11431 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11432 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11433 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11434 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011435
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011436 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011437
Johannes Berg68eb5502013-11-19 15:19:38 +010011438 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011439 NL80211_MCGRP_MLME, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011440 return;
11441
11442 nla_put_failure:
11443 genlmsg_cancel(msg, hdr);
11444 nlmsg_free(msg);
11445}
11446EXPORT_SYMBOL(cfg80211_probe_status);
11447
Johannes Berg5e760232011-11-04 11:18:17 +010011448void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11449 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011450 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010011451{
11452 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11453 struct sk_buff *msg;
11454 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011455 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010011456
Beni Lev4ee3e062012-08-27 12:49:39 +030011457 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11458
Ben Greear37c73b52012-10-26 14:49:25 -070011459 spin_lock_bh(&rdev->beacon_registrations_lock);
11460 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11461 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11462 if (!msg) {
11463 spin_unlock_bh(&rdev->beacon_registrations_lock);
11464 return;
11465 }
Johannes Berg5e760232011-11-04 11:18:17 +010011466
Ben Greear37c73b52012-10-26 14:49:25 -070011467 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11468 if (!hdr)
11469 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010011470
Ben Greear37c73b52012-10-26 14:49:25 -070011471 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11472 (freq &&
11473 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11474 (sig_dbm &&
11475 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11476 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11477 goto nla_put_failure;
11478
11479 genlmsg_end(msg, hdr);
11480
11481 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010011482 }
Ben Greear37c73b52012-10-26 14:49:25 -070011483 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011484 return;
11485
11486 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011487 spin_unlock_bh(&rdev->beacon_registrations_lock);
11488 if (hdr)
11489 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010011490 nlmsg_free(msg);
11491}
11492EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11493
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011494#ifdef CONFIG_PM
11495void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11496 struct cfg80211_wowlan_wakeup *wakeup,
11497 gfp_t gfp)
11498{
11499 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11500 struct sk_buff *msg;
11501 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011502 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011503
11504 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11505
11506 if (wakeup)
11507 size += wakeup->packet_present_len;
11508
11509 msg = nlmsg_new(size, gfp);
11510 if (!msg)
11511 return;
11512
11513 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11514 if (!hdr)
11515 goto free_msg;
11516
11517 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11518 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11519 goto free_msg;
11520
11521 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11522 wdev->netdev->ifindex))
11523 goto free_msg;
11524
11525 if (wakeup) {
11526 struct nlattr *reasons;
11527
11528 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
Johannes Berg7fa322c2013-10-25 11:16:58 +020011529 if (!reasons)
11530 goto free_msg;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011531
11532 if (wakeup->disconnect &&
11533 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11534 goto free_msg;
11535 if (wakeup->magic_pkt &&
11536 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11537 goto free_msg;
11538 if (wakeup->gtk_rekey_failure &&
11539 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11540 goto free_msg;
11541 if (wakeup->eap_identity_req &&
11542 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11543 goto free_msg;
11544 if (wakeup->four_way_handshake &&
11545 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11546 goto free_msg;
11547 if (wakeup->rfkill_release &&
11548 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11549 goto free_msg;
11550
11551 if (wakeup->pattern_idx >= 0 &&
11552 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11553 wakeup->pattern_idx))
11554 goto free_msg;
11555
Johannes Bergae917c92013-10-25 11:05:22 +020011556 if (wakeup->tcp_match &&
11557 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11558 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011559
Johannes Bergae917c92013-10-25 11:05:22 +020011560 if (wakeup->tcp_connlost &&
11561 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11562 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011563
Johannes Bergae917c92013-10-25 11:05:22 +020011564 if (wakeup->tcp_nomoretokens &&
11565 nla_put_flag(msg,
11566 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11567 goto free_msg;
Johannes Berg2a0e0472013-01-23 22:57:40 +010011568
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011569 if (wakeup->packet) {
11570 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11571 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11572
11573 if (!wakeup->packet_80211) {
11574 pkt_attr =
11575 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11576 len_attr =
11577 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11578 }
11579
11580 if (wakeup->packet_len &&
11581 nla_put_u32(msg, len_attr, wakeup->packet_len))
11582 goto free_msg;
11583
11584 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11585 wakeup->packet))
11586 goto free_msg;
11587 }
11588
11589 nla_nest_end(msg, reasons);
11590 }
11591
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011592 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011593
Johannes Berg68eb5502013-11-19 15:19:38 +010011594 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011595 NL80211_MCGRP_MLME, gfp);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011596 return;
11597
11598 free_msg:
11599 nlmsg_free(msg);
11600}
11601EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11602#endif
11603
Jouni Malinen3475b092012-11-16 22:49:57 +020011604void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11605 enum nl80211_tdls_operation oper,
11606 u16 reason_code, gfp_t gfp)
11607{
11608 struct wireless_dev *wdev = dev->ieee80211_ptr;
11609 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11610 struct sk_buff *msg;
11611 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011612
11613 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11614 reason_code);
11615
11616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11617 if (!msg)
11618 return;
11619
11620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11621 if (!hdr) {
11622 nlmsg_free(msg);
11623 return;
11624 }
11625
11626 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11627 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11628 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11629 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11630 (reason_code > 0 &&
11631 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11632 goto nla_put_failure;
11633
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011634 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011635
Johannes Berg68eb5502013-11-19 15:19:38 +010011636 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011637 NL80211_MCGRP_MLME, gfp);
Jouni Malinen3475b092012-11-16 22:49:57 +020011638 return;
11639
11640 nla_put_failure:
11641 genlmsg_cancel(msg, hdr);
11642 nlmsg_free(msg);
11643}
11644EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11645
Jouni Malinen026331c2010-02-15 12:53:10 +020011646static int nl80211_netlink_notify(struct notifier_block * nb,
11647 unsigned long state,
11648 void *_notify)
11649{
11650 struct netlink_notify *notify = _notify;
11651 struct cfg80211_registered_device *rdev;
11652 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011653 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011654
11655 if (state != NETLINK_URELEASE)
11656 return NOTIFY_DONE;
11657
11658 rcu_read_lock();
11659
Johannes Berg5e760232011-11-04 11:18:17 +010011660 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg78f22b62014-03-24 17:57:27 +010011661 bool schedule_destroy_work = false;
11662
11663 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
Eric W. Biederman15e47302012-09-07 20:12:54 +000011664 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011665
Johannes Berg78f22b62014-03-24 17:57:27 +010011666 if (wdev->owner_nlportid == notify->portid)
11667 schedule_destroy_work = true;
11668 }
11669
Ben Greear37c73b52012-10-26 14:49:25 -070011670 spin_lock_bh(&rdev->beacon_registrations_lock);
11671 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11672 list) {
11673 if (reg->nlportid == notify->portid) {
11674 list_del(&reg->list);
11675 kfree(reg);
11676 break;
11677 }
11678 }
11679 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg78f22b62014-03-24 17:57:27 +010011680
11681 if (schedule_destroy_work) {
11682 struct cfg80211_iface_destroy *destroy;
11683
11684 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
11685 if (destroy) {
11686 destroy->nlportid = notify->portid;
11687 spin_lock(&rdev->destroy_list_lock);
11688 list_add(&destroy->list, &rdev->destroy_list);
11689 spin_unlock(&rdev->destroy_list_lock);
11690 schedule_work(&rdev->destroy_work);
11691 }
11692 }
Johannes Berg5e760232011-11-04 11:18:17 +010011693 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011694
11695 rcu_read_unlock();
11696
11697 return NOTIFY_DONE;
11698}
11699
11700static struct notifier_block nl80211_netlink_notifier = {
11701 .notifier_call = nl80211_netlink_notify,
11702};
11703
Jouni Malinen355199e2013-02-27 17:14:27 +020011704void cfg80211_ft_event(struct net_device *netdev,
11705 struct cfg80211_ft_event_params *ft_event)
11706{
11707 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11708 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11709 struct sk_buff *msg;
11710 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011711
11712 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11713
11714 if (!ft_event->target_ap)
11715 return;
11716
11717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11718 if (!msg)
11719 return;
11720
11721 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
Johannes Bergae917c92013-10-25 11:05:22 +020011722 if (!hdr)
11723 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011724
Johannes Bergae917c92013-10-25 11:05:22 +020011725 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11726 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11727 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11728 goto out;
11729
11730 if (ft_event->ies &&
11731 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11732 goto out;
11733 if (ft_event->ric_ies &&
11734 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11735 ft_event->ric_ies))
11736 goto out;
Jouni Malinen355199e2013-02-27 17:14:27 +020011737
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011738 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011739
Johannes Berg68eb5502013-11-19 15:19:38 +010011740 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +010011741 NL80211_MCGRP_MLME, GFP_KERNEL);
Johannes Bergae917c92013-10-25 11:05:22 +020011742 return;
11743 out:
11744 nlmsg_free(msg);
Jouni Malinen355199e2013-02-27 17:14:27 +020011745}
11746EXPORT_SYMBOL(cfg80211_ft_event);
11747
Arend van Spriel5de17982013-04-18 15:49:00 +020011748void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11749{
11750 struct cfg80211_registered_device *rdev;
11751 struct sk_buff *msg;
11752 void *hdr;
11753 u32 nlportid;
11754
11755 rdev = wiphy_to_dev(wdev->wiphy);
11756 if (!rdev->crit_proto_nlportid)
11757 return;
11758
11759 nlportid = rdev->crit_proto_nlportid;
11760 rdev->crit_proto_nlportid = 0;
11761
11762 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11763 if (!msg)
11764 return;
11765
11766 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11767 if (!hdr)
11768 goto nla_put_failure;
11769
11770 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11771 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11772 goto nla_put_failure;
11773
11774 genlmsg_end(msg, hdr);
11775
11776 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11777 return;
11778
11779 nla_put_failure:
11780 if (hdr)
11781 genlmsg_cancel(msg, hdr);
11782 nlmsg_free(msg);
11783
11784}
11785EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11786
Johannes Berg348baf02014-01-24 14:06:29 +010011787void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11788{
11789 struct wiphy *wiphy = wdev->wiphy;
11790 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11791 struct sk_buff *msg;
11792 void *hdr;
11793
11794 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11795 if (!msg)
11796 return;
11797
11798 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11799 if (!hdr)
11800 goto out;
11801
11802 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11803 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11804 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11805 goto out;
11806
11807 genlmsg_end(msg, hdr);
11808
11809 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11810 NL80211_MCGRP_MLME, GFP_KERNEL);
11811 return;
11812 out:
11813 nlmsg_free(msg);
11814}
11815
Johannes Berg55682962007-09-20 13:09:35 -040011816/* initialisation/exit functions */
11817
11818int nl80211_init(void)
11819{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011820 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011821
Johannes Berg2a94fe42013-11-19 15:19:39 +010011822 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11823 nl80211_mcgrps);
Johannes Berg55682962007-09-20 13:09:35 -040011824 if (err)
11825 return err;
11826
Jouni Malinen026331c2010-02-15 12:53:10 +020011827 err = netlink_register_notifier(&nl80211_netlink_notifier);
11828 if (err)
11829 goto err_out;
11830
Johannes Berg55682962007-09-20 13:09:35 -040011831 return 0;
11832 err_out:
11833 genl_unregister_family(&nl80211_fam);
11834 return err;
11835}
11836
11837void nl80211_exit(void)
11838{
Jouni Malinen026331c2010-02-15 12:53:10 +020011839 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011840 genl_unregister_family(&nl80211_fam);
11841}