blob: 5f10f7acfa060c47f5d626e979267f87c7bf6435 [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 Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 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 Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400104}
105
Johannes Berga9455402012-06-15 13:32:49 +0200106static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200108{
Johannes Berg7fee4772012-06-15 14:09:58 +0200109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200111
112 assert_cfg80211_lock();
113
Johannes Berg878d9ec2012-06-15 14:18:32 +0200114 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200117 return ERR_PTR(-EINVAL);
118
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200120 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200122
Johannes Berg89a54e42012-06-15 14:33:17 +0200123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
Johannes Berg878d9ec2012-06-15 14:18:32 +0200149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200151 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
Johannes Berga9455402012-06-15 13:32:49 +0200171 }
172
Johannes Berg4f7eff12012-06-15 14:14:22 +0200173 if (!rdev)
174 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200175
Johannes Berg4f7eff12012-06-15 14:14:22 +0200176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
Johannes Berg55682962007-09-20 13:09:35 -0400221/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700225 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100227
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100243
Eliad Pellere007b852011-11-24 18:13:56 +0200244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100246
Johannes Bergb9454e82009-07-08 13:29:08 +0200247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800271 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300273
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300283
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700286
Johannes Berg6c739412011-11-03 09:27:01 +0100287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100374 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
375 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
376 .len = NL80211_VHT_CAPABILITY_LEN,
377 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200378 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
379 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
380 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400381};
382
Johannes Berge31b8212010-10-05 19:39:30 +0200383/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000384static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200385 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200386 [NL80211_KEY_IDX] = { .type = NLA_U8 },
387 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200388 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200389 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
390 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200391 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100392 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
393};
394
395/* policy for the key default flags */
396static const struct nla_policy
397nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
398 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
399 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200400};
401
Johannes Bergff1b6e62011-05-04 15:37:28 +0200402/* policy for WoWLAN attributes */
403static const struct nla_policy
404nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
405 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
406 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
407 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
408 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200409 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
410 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100413 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
414};
415
416static const struct nla_policy
417nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
418 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
419 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
420 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
421 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
422 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
423 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
425 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
426 },
427 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
428 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
429 },
430 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
431 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200433};
434
Johannes Berge5497d72011-07-05 16:35:40 +0200435/* policy for GTK rekey offload attributes */
436static const struct nla_policy
437nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
438 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
439 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
440 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
441};
442
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300443static const struct nla_policy
444nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200445 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300446 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700447 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300448};
449
Johannes Berg97990a02013-04-19 01:02:55 +0200450static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
451 struct netlink_callback *cb,
452 struct cfg80211_registered_device **rdev,
453 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100454{
Johannes Berg67748892010-10-04 21:14:06 +0200455 int err;
456
Johannes Berg67748892010-10-04 21:14:06 +0200457 rtnl_lock();
Johannes Berg97990a02013-04-19 01:02:55 +0200458 mutex_lock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200459
Johannes Berg97990a02013-04-19 01:02:55 +0200460 if (!cb->args[0]) {
461 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
462 nl80211_fam.attrbuf, nl80211_fam.maxattr,
463 nl80211_policy);
464 if (err)
465 goto out_unlock;
466
467 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
468 nl80211_fam.attrbuf);
469 if (IS_ERR(*wdev)) {
470 err = PTR_ERR(*wdev);
471 goto out_unlock;
472 }
473 *rdev = wiphy_to_dev((*wdev)->wiphy);
474 cb->args[0] = (*rdev)->wiphy_idx;
475 cb->args[1] = (*wdev)->identifier;
476 } else {
477 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
478 struct wireless_dev *tmp;
479
480 if (!wiphy) {
481 err = -ENODEV;
482 goto out_unlock;
483 }
484 *rdev = wiphy_to_dev(wiphy);
485 *wdev = NULL;
486
487 mutex_lock(&(*rdev)->devlist_mtx);
488 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
489 if (tmp->identifier == cb->args[1]) {
490 *wdev = tmp;
491 break;
492 }
493 }
494 mutex_unlock(&(*rdev)->devlist_mtx);
495
496 if (!*wdev) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
Johannes Berg67748892010-10-04 21:14:06 +0200500 }
501
Johannes Berg97990a02013-04-19 01:02:55 +0200502 cfg80211_lock_rdev(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200503
Johannes Berg97990a02013-04-19 01:02:55 +0200504 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200505 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200506 out_unlock:
507 mutex_unlock(&cfg80211_mutex);
Johannes Berg67748892010-10-04 21:14:06 +0200508 rtnl_unlock();
509 return err;
510}
511
Johannes Berg97990a02013-04-19 01:02:55 +0200512static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200513{
514 cfg80211_unlock_rdev(rdev);
515 rtnl_unlock();
516}
517
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100518/* IE validation */
519static bool is_valid_ie_attr(const struct nlattr *attr)
520{
521 const u8 *pos;
522 int len;
523
524 if (!attr)
525 return true;
526
527 pos = nla_data(attr);
528 len = nla_len(attr);
529
530 while (len) {
531 u8 elemlen;
532
533 if (len < 2)
534 return false;
535 len -= 2;
536
537 elemlen = pos[1];
538 if (elemlen > len)
539 return false;
540
541 len -= elemlen;
542 pos += 2 + elemlen;
543 }
544
545 return true;
546}
547
Johannes Berg55682962007-09-20 13:09:35 -0400548/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000549static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400550 int flags, u8 cmd)
551{
552 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000553 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400554}
555
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400556static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100557 struct ieee80211_channel *chan,
558 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400559{
David S. Miller9360ffd2012-03-29 04:41:26 -0400560 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
561 chan->center_freq))
562 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400563
David S. Miller9360ffd2012-03-29 04:41:26 -0400564 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
565 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
566 goto nla_put_failure;
567 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
572 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100573 if (chan->flags & IEEE80211_CHAN_RADAR) {
574 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
575 goto nla_put_failure;
576 if (large) {
577 u32 time;
578
579 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
580
581 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
582 chan->dfs_state))
583 goto nla_put_failure;
584 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
585 time))
586 goto nla_put_failure;
587 }
588 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400589
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100590 if (large) {
591 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
592 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
593 goto nla_put_failure;
594 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
595 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
596 goto nla_put_failure;
597 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
598 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
599 goto nla_put_failure;
600 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
602 goto nla_put_failure;
603 }
604
David S. Miller9360ffd2012-03-29 04:41:26 -0400605 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
606 DBM_TO_MBM(chan->max_power)))
607 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400608
609 return 0;
610
611 nla_put_failure:
612 return -ENOBUFS;
613}
614
Johannes Berg55682962007-09-20 13:09:35 -0400615/* netlink command implementations */
616
Johannes Bergb9454e82009-07-08 13:29:08 +0200617struct key_parse {
618 struct key_params p;
619 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200620 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200621 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100622 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200623};
624
625static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
626{
627 struct nlattr *tb[NL80211_KEY_MAX + 1];
628 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
629 nl80211_key_policy);
630 if (err)
631 return err;
632
633 k->def = !!tb[NL80211_KEY_DEFAULT];
634 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
635
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100636 if (k->def) {
637 k->def_uni = true;
638 k->def_multi = true;
639 }
640 if (k->defmgmt)
641 k->def_multi = true;
642
Johannes Bergb9454e82009-07-08 13:29:08 +0200643 if (tb[NL80211_KEY_IDX])
644 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
645
646 if (tb[NL80211_KEY_DATA]) {
647 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
648 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
649 }
650
651 if (tb[NL80211_KEY_SEQ]) {
652 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
653 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
654 }
655
656 if (tb[NL80211_KEY_CIPHER])
657 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
658
Johannes Berge31b8212010-10-05 19:39:30 +0200659 if (tb[NL80211_KEY_TYPE]) {
660 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
661 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
662 return -EINVAL;
663 }
664
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100665 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
666 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100667 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
668 tb[NL80211_KEY_DEFAULT_TYPES],
669 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100670 if (err)
671 return err;
672
673 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
674 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
675 }
676
Johannes Bergb9454e82009-07-08 13:29:08 +0200677 return 0;
678}
679
680static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
681{
682 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
683 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
684 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
685 }
686
687 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
688 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
689 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
690 }
691
692 if (info->attrs[NL80211_ATTR_KEY_IDX])
693 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
694
695 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
696 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
697
698 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
699 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
700
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100701 if (k->def) {
702 k->def_uni = true;
703 k->def_multi = true;
704 }
705 if (k->defmgmt)
706 k->def_multi = true;
707
Johannes Berge31b8212010-10-05 19:39:30 +0200708 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
709 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
710 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
711 return -EINVAL;
712 }
713
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
715 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
716 int err = nla_parse_nested(
717 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
718 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
719 nl80211_key_default_policy);
720 if (err)
721 return err;
722
723 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
724 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
725 }
726
Johannes Bergb9454e82009-07-08 13:29:08 +0200727 return 0;
728}
729
730static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
731{
732 int err;
733
734 memset(k, 0, sizeof(*k));
735 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200736 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200737
738 if (info->attrs[NL80211_ATTR_KEY])
739 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
740 else
741 err = nl80211_parse_key_old(info, k);
742
743 if (err)
744 return err;
745
746 if (k->def && k->defmgmt)
747 return -EINVAL;
748
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100749 if (k->defmgmt) {
750 if (k->def_uni || !k->def_multi)
751 return -EINVAL;
752 }
753
Johannes Bergb9454e82009-07-08 13:29:08 +0200754 if (k->idx != -1) {
755 if (k->defmgmt) {
756 if (k->idx < 4 || k->idx > 5)
757 return -EINVAL;
758 } else if (k->def) {
759 if (k->idx < 0 || k->idx > 3)
760 return -EINVAL;
761 } else {
762 if (k->idx < 0 || k->idx > 5)
763 return -EINVAL;
764 }
765 }
766
767 return 0;
768}
769
Johannes Bergfffd0932009-07-08 14:22:54 +0200770static struct cfg80211_cached_keys *
771nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530772 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200773{
774 struct key_parse parse;
775 struct nlattr *key;
776 struct cfg80211_cached_keys *result;
777 int rem, err, def = 0;
778
779 result = kzalloc(sizeof(*result), GFP_KERNEL);
780 if (!result)
781 return ERR_PTR(-ENOMEM);
782
783 result->def = -1;
784 result->defmgmt = -1;
785
786 nla_for_each_nested(key, keys, rem) {
787 memset(&parse, 0, sizeof(parse));
788 parse.idx = -1;
789
790 err = nl80211_parse_key_new(key, &parse);
791 if (err)
792 goto error;
793 err = -EINVAL;
794 if (!parse.p.key)
795 goto error;
796 if (parse.idx < 0 || parse.idx > 4)
797 goto error;
798 if (parse.def) {
799 if (def)
800 goto error;
801 def = 1;
802 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100803 if (!parse.def_uni || !parse.def_multi)
804 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200805 } else if (parse.defmgmt)
806 goto error;
807 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200808 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200809 if (err)
810 goto error;
811 result->params[parse.idx].cipher = parse.p.cipher;
812 result->params[parse.idx].key_len = parse.p.key_len;
813 result->params[parse.idx].key = result->data[parse.idx];
814 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530815
816 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
817 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
818 if (no_ht)
819 *no_ht = true;
820 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200821 }
822
823 return result;
824 error:
825 kfree(result);
826 return ERR_PTR(err);
827}
828
829static int nl80211_key_allowed(struct wireless_dev *wdev)
830{
831 ASSERT_WDEV_LOCK(wdev);
832
Johannes Bergfffd0932009-07-08 14:22:54 +0200833 switch (wdev->iftype) {
834 case NL80211_IFTYPE_AP:
835 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200836 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700837 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200838 break;
839 case NL80211_IFTYPE_ADHOC:
840 if (!wdev->current_bss)
841 return -ENOLINK;
842 break;
843 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200845 if (wdev->sme_state != CFG80211_SME_CONNECTED)
846 return -ENOLINK;
847 break;
848 default:
849 return -EINVAL;
850 }
851
852 return 0;
853}
854
Johannes Berg7527a782011-05-13 10:58:57 +0200855static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
856{
857 struct nlattr *nl_modes = nla_nest_start(msg, attr);
858 int i;
859
860 if (!nl_modes)
861 goto nla_put_failure;
862
863 i = 0;
864 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400865 if ((ifmodes & 1) && nla_put_flag(msg, i))
866 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200867 ifmodes >>= 1;
868 i++;
869 }
870
871 nla_nest_end(msg, nl_modes);
872 return 0;
873
874nla_put_failure:
875 return -ENOBUFS;
876}
877
878static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100879 struct sk_buff *msg,
880 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200881{
882 struct nlattr *nl_combis;
883 int i, j;
884
885 nl_combis = nla_nest_start(msg,
886 NL80211_ATTR_INTERFACE_COMBINATIONS);
887 if (!nl_combis)
888 goto nla_put_failure;
889
890 for (i = 0; i < wiphy->n_iface_combinations; i++) {
891 const struct ieee80211_iface_combination *c;
892 struct nlattr *nl_combi, *nl_limits;
893
894 c = &wiphy->iface_combinations[i];
895
896 nl_combi = nla_nest_start(msg, i + 1);
897 if (!nl_combi)
898 goto nla_put_failure;
899
900 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
901 if (!nl_limits)
902 goto nla_put_failure;
903
904 for (j = 0; j < c->n_limits; j++) {
905 struct nlattr *nl_limit;
906
907 nl_limit = nla_nest_start(msg, j + 1);
908 if (!nl_limit)
909 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400910 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
911 c->limits[j].max))
912 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200913 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
914 c->limits[j].types))
915 goto nla_put_failure;
916 nla_nest_end(msg, nl_limit);
917 }
918
919 nla_nest_end(msg, nl_limits);
920
David S. Miller9360ffd2012-03-29 04:41:26 -0400921 if (c->beacon_int_infra_match &&
922 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
923 goto nla_put_failure;
924 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
925 c->num_different_channels) ||
926 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
927 c->max_interfaces))
928 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100929 if (large &&
930 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
931 c->radar_detect_widths))
932 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200933
934 nla_nest_end(msg, nl_combi);
935 }
936
937 nla_nest_end(msg, nl_combis);
938
939 return 0;
940nla_put_failure:
941 return -ENOBUFS;
942}
943
Johannes Berg3713b4e2013-02-14 16:19:38 +0100944#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100945static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
946 struct sk_buff *msg)
947{
948 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
949 struct nlattr *nl_tcp;
950
951 if (!tcp)
952 return 0;
953
954 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
955 if (!nl_tcp)
956 return -ENOBUFS;
957
958 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
959 tcp->data_payload_max))
960 return -ENOBUFS;
961
962 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
963 tcp->data_payload_max))
964 return -ENOBUFS;
965
966 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
967 return -ENOBUFS;
968
969 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
970 sizeof(*tcp->tok), tcp->tok))
971 return -ENOBUFS;
972
973 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
974 tcp->data_interval_max))
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
978 tcp->wake_payload_max))
979 return -ENOBUFS;
980
981 nla_nest_end(msg, nl_tcp);
982 return 0;
983}
984
Johannes Berg3713b4e2013-02-14 16:19:38 +0100985static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100986 struct cfg80211_registered_device *dev,
987 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100988{
989 struct nlattr *nl_wowlan;
990
991 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
992 return 0;
993
994 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
995 if (!nl_wowlan)
996 return -ENOBUFS;
997
998 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
999 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1000 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1001 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1002 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1003 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1004 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1005 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1006 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1007 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1008 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1009 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1010 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1011 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1012 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1013 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1014 return -ENOBUFS;
1015
1016 if (dev->wiphy.wowlan.n_patterns) {
1017 struct nl80211_wowlan_pattern_support pat = {
1018 .max_patterns = dev->wiphy.wowlan.n_patterns,
1019 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
1020 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
1021 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
1022 };
1023
1024 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1025 sizeof(pat), &pat))
1026 return -ENOBUFS;
1027 }
1028
Johannes Bergb56cf722013-02-20 01:02:38 +01001029 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1030 return -ENOBUFS;
1031
Johannes Berg3713b4e2013-02-14 16:19:38 +01001032 nla_nest_end(msg, nl_wowlan);
1033
1034 return 0;
1035}
1036#endif
1037
1038static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1039 struct ieee80211_supported_band *sband)
1040{
1041 struct nlattr *nl_rates, *nl_rate;
1042 struct ieee80211_rate *rate;
1043 int i;
1044
1045 /* add HT info */
1046 if (sband->ht_cap.ht_supported &&
1047 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1048 sizeof(sband->ht_cap.mcs),
1049 &sband->ht_cap.mcs) ||
1050 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1051 sband->ht_cap.cap) ||
1052 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1053 sband->ht_cap.ampdu_factor) ||
1054 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1055 sband->ht_cap.ampdu_density)))
1056 return -ENOBUFS;
1057
1058 /* add VHT info */
1059 if (sband->vht_cap.vht_supported &&
1060 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1061 sizeof(sband->vht_cap.vht_mcs),
1062 &sband->vht_cap.vht_mcs) ||
1063 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1064 sband->vht_cap.cap)))
1065 return -ENOBUFS;
1066
1067 /* add bitrates */
1068 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1069 if (!nl_rates)
1070 return -ENOBUFS;
1071
1072 for (i = 0; i < sband->n_bitrates; i++) {
1073 nl_rate = nla_nest_start(msg, i);
1074 if (!nl_rate)
1075 return -ENOBUFS;
1076
1077 rate = &sband->bitrates[i];
1078 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1079 rate->bitrate))
1080 return -ENOBUFS;
1081 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1082 nla_put_flag(msg,
1083 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1084 return -ENOBUFS;
1085
1086 nla_nest_end(msg, nl_rate);
1087 }
1088
1089 nla_nest_end(msg, nl_rates);
1090
1091 return 0;
1092}
1093
1094static int
1095nl80211_send_mgmt_stypes(struct sk_buff *msg,
1096 const struct ieee80211_txrx_stypes *mgmt_stypes)
1097{
1098 u16 stypes;
1099 struct nlattr *nl_ftypes, *nl_ifs;
1100 enum nl80211_iftype ift;
1101 int i;
1102
1103 if (!mgmt_stypes)
1104 return 0;
1105
1106 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1107 if (!nl_ifs)
1108 return -ENOBUFS;
1109
1110 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1111 nl_ftypes = nla_nest_start(msg, ift);
1112 if (!nl_ftypes)
1113 return -ENOBUFS;
1114 i = 0;
1115 stypes = mgmt_stypes[ift].tx;
1116 while (stypes) {
1117 if ((stypes & 1) &&
1118 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1119 (i << 4) | IEEE80211_FTYPE_MGMT))
1120 return -ENOBUFS;
1121 stypes >>= 1;
1122 i++;
1123 }
1124 nla_nest_end(msg, nl_ftypes);
1125 }
1126
1127 nla_nest_end(msg, nl_ifs);
1128
1129 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1130 if (!nl_ifs)
1131 return -ENOBUFS;
1132
1133 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1134 nl_ftypes = nla_nest_start(msg, ift);
1135 if (!nl_ftypes)
1136 return -ENOBUFS;
1137 i = 0;
1138 stypes = mgmt_stypes[ift].rx;
1139 while (stypes) {
1140 if ((stypes & 1) &&
1141 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1142 (i << 4) | IEEE80211_FTYPE_MGMT))
1143 return -ENOBUFS;
1144 stypes >>= 1;
1145 i++;
1146 }
1147 nla_nest_end(msg, nl_ftypes);
1148 }
1149 nla_nest_end(msg, nl_ifs);
1150
1151 return 0;
1152}
1153
1154static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1155 struct sk_buff *msg, u32 portid, u32 seq,
1156 int flags, bool split, long *split_start,
1157 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001158{
1159 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001160 struct nlattr *nl_bands, *nl_band;
1161 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001162 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001163 enum ieee80211_band band;
1164 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001165 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001166 const struct ieee80211_txrx_stypes *mgmt_stypes =
1167 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001168 long start = 0, start_chan = 0, start_band = 0;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001169 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001170
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001172 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001173 return -ENOBUFS;
1174
1175 /* allow always using the variables */
1176 if (!split) {
1177 split_start = &start;
1178 band_start = &start_band;
1179 chan_start = &start_chan;
1180 }
Johannes Berg55682962007-09-20 13:09:35 -04001181
David S. Miller9360ffd2012-03-29 04:41:26 -04001182 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001183 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1184 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001185 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001186 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001187 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001188
Johannes Berg3713b4e2013-02-14 16:19:38 +01001189 switch (*split_start) {
1190 case 0:
1191 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1192 dev->wiphy.retry_short) ||
1193 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1194 dev->wiphy.retry_long) ||
1195 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1196 dev->wiphy.frag_threshold) ||
1197 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1198 dev->wiphy.rts_threshold) ||
1199 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1200 dev->wiphy.coverage_class) ||
1201 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1202 dev->wiphy.max_scan_ssids) ||
1203 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1204 dev->wiphy.max_sched_scan_ssids) ||
1205 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1206 dev->wiphy.max_scan_ie_len) ||
1207 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1208 dev->wiphy.max_sched_scan_ie_len) ||
1209 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1210 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001211 goto nla_put_failure;
1212
Johannes Berg3713b4e2013-02-14 16:19:38 +01001213 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1214 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1215 goto nla_put_failure;
1216 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1217 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1218 goto nla_put_failure;
1219 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1220 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1221 goto nla_put_failure;
1222 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1223 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1224 goto nla_put_failure;
1225 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1226 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1227 goto nla_put_failure;
1228 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1229 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001231
Johannes Berg3713b4e2013-02-14 16:19:38 +01001232 (*split_start)++;
1233 if (split)
1234 break;
1235 case 1:
1236 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1237 sizeof(u32) * dev->wiphy.n_cipher_suites,
1238 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001239 goto nla_put_failure;
1240
Johannes Berg3713b4e2013-02-14 16:19:38 +01001241 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1242 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001243 goto nla_put_failure;
1244
Johannes Berg3713b4e2013-02-14 16:19:38 +01001245 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1246 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1247 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001248
Johannes Berg3713b4e2013-02-14 16:19:38 +01001249 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1250 dev->wiphy.available_antennas_tx) ||
1251 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1252 dev->wiphy.available_antennas_rx))
1253 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001254
Johannes Berg3713b4e2013-02-14 16:19:38 +01001255 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1256 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1257 dev->wiphy.probe_resp_offload))
1258 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001259
Johannes Berg3713b4e2013-02-14 16:19:38 +01001260 if ((dev->wiphy.available_antennas_tx ||
1261 dev->wiphy.available_antennas_rx) &&
1262 dev->ops->get_antenna) {
1263 u32 tx_ant = 0, rx_ant = 0;
1264 int res;
1265 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1266 if (!res) {
1267 if (nla_put_u32(msg,
1268 NL80211_ATTR_WIPHY_ANTENNA_TX,
1269 tx_ant) ||
1270 nla_put_u32(msg,
1271 NL80211_ATTR_WIPHY_ANTENNA_RX,
1272 rx_ant))
1273 goto nla_put_failure;
1274 }
Johannes Bergee688b002008-01-24 19:38:39 +01001275 }
1276
Johannes Berg3713b4e2013-02-14 16:19:38 +01001277 (*split_start)++;
1278 if (split)
1279 break;
1280 case 2:
1281 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1282 dev->wiphy.interface_modes))
1283 goto nla_put_failure;
1284 (*split_start)++;
1285 if (split)
1286 break;
1287 case 3:
1288 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1289 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001290 goto nla_put_failure;
1291
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1293 struct ieee80211_supported_band *sband;
1294
1295 sband = dev->wiphy.bands[band];
1296
1297 if (!sband)
1298 continue;
1299
1300 nl_band = nla_nest_start(msg, band);
1301 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001302 goto nla_put_failure;
1303
Johannes Berg3713b4e2013-02-14 16:19:38 +01001304 switch (*chan_start) {
1305 case 0:
1306 if (nl80211_send_band_rateinfo(msg, sband))
1307 goto nla_put_failure;
1308 (*chan_start)++;
1309 if (split)
1310 break;
1311 default:
1312 /* add frequencies */
1313 nl_freqs = nla_nest_start(
1314 msg, NL80211_BAND_ATTR_FREQS);
1315 if (!nl_freqs)
1316 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001317
Johannes Berg3713b4e2013-02-14 16:19:38 +01001318 for (i = *chan_start - 1;
1319 i < sband->n_channels;
1320 i++) {
1321 nl_freq = nla_nest_start(msg, i);
1322 if (!nl_freq)
1323 goto nla_put_failure;
1324
1325 chan = &sband->channels[i];
1326
Johannes Bergcdc89b92013-02-18 23:54:36 +01001327 if (nl80211_msg_put_channel(msg, chan,
1328 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001329 goto nla_put_failure;
1330
1331 nla_nest_end(msg, nl_freq);
1332 if (split)
1333 break;
1334 }
1335 if (i < sband->n_channels)
1336 *chan_start = i + 2;
1337 else
1338 *chan_start = 0;
1339 nla_nest_end(msg, nl_freqs);
1340 }
1341
1342 nla_nest_end(msg, nl_band);
1343
1344 if (split) {
1345 /* start again here */
1346 if (*chan_start)
1347 band--;
1348 break;
1349 }
Johannes Bergee688b002008-01-24 19:38:39 +01001350 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001352
Johannes Berg3713b4e2013-02-14 16:19:38 +01001353 if (band < IEEE80211_NUM_BANDS)
1354 *band_start = band + 1;
1355 else
1356 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001357
Johannes Berg3713b4e2013-02-14 16:19:38 +01001358 /* if bands & channels are done, continue outside */
1359 if (*band_start == 0 && *chan_start == 0)
1360 (*split_start)++;
1361 if (split)
1362 break;
1363 case 4:
1364 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1365 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001366 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001367
1368 i = 0;
1369#define CMD(op, n) \
1370 do { \
1371 if (dev->ops->op) { \
1372 i++; \
1373 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1374 goto nla_put_failure; \
1375 } \
1376 } while (0)
1377
1378 CMD(add_virtual_intf, NEW_INTERFACE);
1379 CMD(change_virtual_intf, SET_INTERFACE);
1380 CMD(add_key, NEW_KEY);
1381 CMD(start_ap, START_AP);
1382 CMD(add_station, NEW_STATION);
1383 CMD(add_mpath, NEW_MPATH);
1384 CMD(update_mesh_config, SET_MESH_CONFIG);
1385 CMD(change_bss, SET_BSS);
1386 CMD(auth, AUTHENTICATE);
1387 CMD(assoc, ASSOCIATE);
1388 CMD(deauth, DEAUTHENTICATE);
1389 CMD(disassoc, DISASSOCIATE);
1390 CMD(join_ibss, JOIN_IBSS);
1391 CMD(join_mesh, JOIN_MESH);
1392 CMD(set_pmksa, SET_PMKSA);
1393 CMD(del_pmksa, DEL_PMKSA);
1394 CMD(flush_pmksa, FLUSH_PMKSA);
1395 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1396 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1397 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1398 CMD(mgmt_tx, FRAME);
1399 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1400 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1401 i++;
1402 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1403 goto nla_put_failure;
1404 }
1405 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1406 dev->ops->join_mesh) {
1407 i++;
1408 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1409 goto nla_put_failure;
1410 }
1411 CMD(set_wds_peer, SET_WDS_PEER);
1412 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1413 CMD(tdls_mgmt, TDLS_MGMT);
1414 CMD(tdls_oper, TDLS_OPER);
1415 }
1416 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1417 CMD(sched_scan_start, START_SCHED_SCAN);
1418 CMD(probe_client, PROBE_CLIENT);
1419 CMD(set_noack_map, SET_NOACK_MAP);
1420 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1421 i++;
1422 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1423 goto nla_put_failure;
1424 }
1425 CMD(start_p2p_device, START_P2P_DEVICE);
1426 CMD(set_mcast_rate, SET_MCAST_RATE);
Arend van Spriel5de17982013-04-18 15:49:00 +02001427 if (split) {
1428 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1429 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1430 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001431
Kalle Valo4745fc02011-11-17 19:06:10 +02001432#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001433 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001434#endif
1435
Johannes Berg8fdc6212009-03-14 09:34:01 +01001436#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001437
Johannes Berg3713b4e2013-02-14 16:19:38 +01001438 if (dev->ops->connect || dev->ops->auth) {
1439 i++;
1440 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001441 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001442 }
1443
Johannes Berg3713b4e2013-02-14 16:19:38 +01001444 if (dev->ops->disconnect || dev->ops->deauth) {
1445 i++;
1446 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1447 goto nla_put_failure;
1448 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001449
Johannes Berg3713b4e2013-02-14 16:19:38 +01001450 nla_nest_end(msg, nl_cmds);
1451 (*split_start)++;
1452 if (split)
1453 break;
1454 case 5:
1455 if (dev->ops->remain_on_channel &&
1456 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1457 nla_put_u32(msg,
1458 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1459 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001460 goto nla_put_failure;
1461
Johannes Berg3713b4e2013-02-14 16:19:38 +01001462 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1463 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1464 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001465
Johannes Berg3713b4e2013-02-14 16:19:38 +01001466 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1467 goto nla_put_failure;
1468 (*split_start)++;
1469 if (split)
1470 break;
1471 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001472#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001473 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001474 goto nla_put_failure;
1475 (*split_start)++;
1476 if (split)
1477 break;
1478#else
1479 (*split_start)++;
1480#endif
1481 case 7:
1482 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1483 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001484 goto nla_put_failure;
1485
Johannes Bergcdc89b92013-02-18 23:54:36 +01001486 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001487 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001488
Johannes Berg3713b4e2013-02-14 16:19:38 +01001489 (*split_start)++;
1490 if (split)
1491 break;
1492 case 8:
1493 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1494 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1495 dev->wiphy.ap_sme_capa))
1496 goto nla_put_failure;
1497
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001498 features = dev->wiphy.features;
1499 /*
1500 * We can only add the per-channel limit information if the
1501 * dump is split, otherwise it makes it too big. Therefore
1502 * only advertise it in that case.
1503 */
1504 if (split)
1505 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1506 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001507 goto nla_put_failure;
1508
1509 if (dev->wiphy.ht_capa_mod_mask &&
1510 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1511 sizeof(*dev->wiphy.ht_capa_mod_mask),
1512 dev->wiphy.ht_capa_mod_mask))
1513 goto nla_put_failure;
1514
1515 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1516 dev->wiphy.max_acl_mac_addrs &&
1517 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1518 dev->wiphy.max_acl_mac_addrs))
1519 goto nla_put_failure;
1520
1521 /*
1522 * Any information below this point is only available to
1523 * applications that can deal with it being split. This
1524 * helps ensure that newly added capabilities don't break
1525 * older tools by overrunning their buffers.
1526 *
1527 * We still increment split_start so that in the split
1528 * case we'll continue with more data in the next round,
1529 * but break unconditionally so unsplit data stops here.
1530 */
1531 (*split_start)++;
1532 break;
1533 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001534 if (dev->wiphy.extended_capabilities &&
1535 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1536 dev->wiphy.extended_capabilities_len,
1537 dev->wiphy.extended_capabilities) ||
1538 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1539 dev->wiphy.extended_capabilities_len,
1540 dev->wiphy.extended_capabilities_mask)))
1541 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001542
Johannes Bergee2aca32013-02-21 17:36:01 +01001543 if (dev->wiphy.vht_capa_mod_mask &&
1544 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1545 sizeof(*dev->wiphy.vht_capa_mod_mask),
1546 dev->wiphy.vht_capa_mod_mask))
1547 goto nla_put_failure;
1548
Johannes Berg3713b4e2013-02-14 16:19:38 +01001549 /* done */
1550 *split_start = 0;
1551 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001552 }
Johannes Berg55682962007-09-20 13:09:35 -04001553 return genlmsg_end(msg, hdr);
1554
1555 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001556 genlmsg_cancel(msg, hdr);
1557 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001558}
1559
1560static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1561{
Johannes Berg645e77d2013-03-01 14:03:49 +01001562 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001563 int start = cb->args[0];
1564 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001565 s64 filter_wiphy = -1;
1566 bool split = false;
1567 struct nlattr **tb = nl80211_fam.attrbuf;
1568 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001569
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001570 mutex_lock(&cfg80211_mutex);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001571 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1572 tb, nl80211_fam.maxattr, nl80211_policy);
1573 if (res == 0) {
1574 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1575 if (tb[NL80211_ATTR_WIPHY])
1576 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1577 if (tb[NL80211_ATTR_WDEV])
1578 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1579 if (tb[NL80211_ATTR_IFINDEX]) {
1580 struct net_device *netdev;
1581 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1582
1583 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1584 if (!netdev) {
1585 mutex_unlock(&cfg80211_mutex);
1586 return -ENODEV;
1587 }
1588 if (netdev->ieee80211_ptr) {
1589 dev = wiphy_to_dev(
1590 netdev->ieee80211_ptr->wiphy);
1591 filter_wiphy = dev->wiphy_idx;
1592 }
1593 dev_put(netdev);
1594 }
1595 }
1596
Johannes Berg79c97e92009-07-07 03:56:12 +02001597 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001598 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1599 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001600 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001601 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001602 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1603 continue;
1604 /* attempt to fit multiple wiphy data chunks into the skb */
1605 do {
1606 ret = nl80211_send_wiphy(dev, skb,
1607 NETLINK_CB(cb->skb).portid,
1608 cb->nlh->nlmsg_seq,
1609 NLM_F_MULTI,
1610 split, &cb->args[1],
1611 &cb->args[2],
1612 &cb->args[3]);
1613 if (ret < 0) {
1614 /*
1615 * If sending the wiphy data didn't fit (ENOBUFS
1616 * or EMSGSIZE returned), this SKB is still
1617 * empty (so it's not too big because another
1618 * wiphy dataset is already in the skb) and
1619 * we've not tried to adjust the dump allocation
1620 * yet ... then adjust the alloc size to be
1621 * bigger, and return 1 but with the empty skb.
1622 * This results in an empty message being RX'ed
1623 * in userspace, but that is ignored.
1624 *
1625 * We can then retry with the larger buffer.
1626 */
1627 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1628 !skb->len &&
1629 cb->min_dump_alloc < 4096) {
1630 cb->min_dump_alloc = 4096;
1631 mutex_unlock(&cfg80211_mutex);
1632 return 1;
1633 }
1634 idx--;
1635 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001636 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001637 } while (cb->args[1] > 0);
1638 break;
Johannes Berg55682962007-09-20 13:09:35 -04001639 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001640 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001641
1642 cb->args[0] = idx;
1643
1644 return skb->len;
1645}
1646
1647static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1648{
1649 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001650 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001651
Johannes Berg645e77d2013-03-01 14:03:49 +01001652 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001653 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001654 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001655
Johannes Berg3713b4e2013-02-14 16:19:38 +01001656 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1657 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001658 nlmsg_free(msg);
1659 return -ENOBUFS;
1660 }
Johannes Berg55682962007-09-20 13:09:35 -04001661
Johannes Berg134e6372009-07-10 09:51:34 +00001662 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001663}
1664
Jouni Malinen31888482008-10-30 16:59:24 +02001665static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1666 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1667 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1668 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1669 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1670 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1671};
1672
1673static int parse_txq_params(struct nlattr *tb[],
1674 struct ieee80211_txq_params *txq_params)
1675{
Johannes Berga3304b02012-03-28 11:04:24 +02001676 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001677 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1678 !tb[NL80211_TXQ_ATTR_AIFS])
1679 return -EINVAL;
1680
Johannes Berga3304b02012-03-28 11:04:24 +02001681 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001682 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1683 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1684 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1685 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1686
Johannes Berga3304b02012-03-28 11:04:24 +02001687 if (txq_params->ac >= NL80211_NUM_ACS)
1688 return -EINVAL;
1689
Jouni Malinen31888482008-10-30 16:59:24 +02001690 return 0;
1691}
1692
Johannes Bergf444de02010-05-05 15:25:02 +02001693static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1694{
1695 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001696 * You can only set the channel explicitly for WDS interfaces,
1697 * all others have their channel managed via their respective
1698 * "establish a connection" command (connect, join, ...)
1699 *
1700 * For AP/GO and mesh mode, the channel can be set with the
1701 * channel userspace API, but is only stored and passed to the
1702 * low-level driver when the AP starts or the mesh is joined.
1703 * This is for backward compatibility, userspace can also give
1704 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001705 *
1706 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001707 * whatever else is going on, so they have their own special
1708 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001709 */
1710 return !wdev ||
1711 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001712 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001713 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1714 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001715}
1716
Johannes Berg683b6d32012-11-08 21:25:48 +01001717static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1718 struct genl_info *info,
1719 struct cfg80211_chan_def *chandef)
1720{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301721 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001722
1723 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1724 return -EINVAL;
1725
1726 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1727
1728 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001729 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1730 chandef->center_freq1 = control_freq;
1731 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001732
1733 /* Primary channel not allowed */
1734 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1735 return -EINVAL;
1736
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001737 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1738 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001739
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001740 chantype = nla_get_u32(
1741 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1742
1743 switch (chantype) {
1744 case NL80211_CHAN_NO_HT:
1745 case NL80211_CHAN_HT20:
1746 case NL80211_CHAN_HT40PLUS:
1747 case NL80211_CHAN_HT40MINUS:
1748 cfg80211_chandef_create(chandef, chandef->chan,
1749 chantype);
1750 break;
1751 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001752 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001753 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001754 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1755 chandef->width =
1756 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1757 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1758 chandef->center_freq1 =
1759 nla_get_u32(
1760 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1761 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1762 chandef->center_freq2 =
1763 nla_get_u32(
1764 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1765 }
1766
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001767 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001768 return -EINVAL;
1769
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001770 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1771 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001772 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001773
Johannes Berg683b6d32012-11-08 21:25:48 +01001774 return 0;
1775}
1776
Johannes Bergf444de02010-05-05 15:25:02 +02001777static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1778 struct wireless_dev *wdev,
1779 struct genl_info *info)
1780{
Johannes Berg683b6d32012-11-08 21:25:48 +01001781 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001782 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001783 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1784
1785 if (wdev)
1786 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001787
Johannes Bergf444de02010-05-05 15:25:02 +02001788 if (!nl80211_can_set_dev_channel(wdev))
1789 return -EOPNOTSUPP;
1790
Johannes Berg683b6d32012-11-08 21:25:48 +01001791 result = nl80211_parse_chandef(rdev, info, &chandef);
1792 if (result)
1793 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001794
1795 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001796 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001797 case NL80211_IFTYPE_AP:
1798 case NL80211_IFTYPE_P2P_GO:
1799 if (wdev->beacon_interval) {
1800 result = -EBUSY;
1801 break;
1802 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001803 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001804 result = -EINVAL;
1805 break;
1806 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001807 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001808 result = 0;
1809 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001810 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001811 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001812 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001813 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001814 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001815 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001816 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001817 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001818 }
1819 mutex_unlock(&rdev->devlist_mtx);
1820
1821 return result;
1822}
1823
1824static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1825{
Johannes Berg4c476992010-10-04 21:36:35 +02001826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1827 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001828
Johannes Berg4c476992010-10-04 21:36:35 +02001829 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001830}
1831
Bill Jordane8347eb2010-10-01 13:54:28 -04001832static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1833{
Johannes Berg43b19952010-10-07 13:10:30 +02001834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1835 struct net_device *dev = info->user_ptr[1];
1836 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001837 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001838
1839 if (!info->attrs[NL80211_ATTR_MAC])
1840 return -EINVAL;
1841
Johannes Berg43b19952010-10-07 13:10:30 +02001842 if (netif_running(dev))
1843 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001844
Johannes Berg43b19952010-10-07 13:10:30 +02001845 if (!rdev->ops->set_wds_peer)
1846 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001847
Johannes Berg43b19952010-10-07 13:10:30 +02001848 if (wdev->iftype != NL80211_IFTYPE_WDS)
1849 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001850
1851 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001852 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001853}
1854
1855
Johannes Berg55682962007-09-20 13:09:35 -04001856static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1857{
1858 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001859 struct net_device *netdev = NULL;
1860 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001861 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001862 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001863 u32 changed;
1864 u8 retry_short = 0, retry_long = 0;
1865 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001866 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001867
Johannes Bergf444de02010-05-05 15:25:02 +02001868 /*
1869 * Try to find the wiphy and netdev. Normally this
1870 * function shouldn't need the netdev, but this is
1871 * done for backward compatibility -- previously
1872 * setting the channel was done per wiphy, but now
1873 * it is per netdev. Previous userland like hostapd
1874 * also passed a netdev to set_wiphy, so that it is
1875 * possible to let that go to the right netdev!
1876 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001877 mutex_lock(&cfg80211_mutex);
1878
Johannes Bergf444de02010-05-05 15:25:02 +02001879 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1880 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1881
1882 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1883 if (netdev && netdev->ieee80211_ptr) {
1884 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1885 mutex_lock(&rdev->mtx);
1886 } else
1887 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001888 }
1889
Johannes Bergf444de02010-05-05 15:25:02 +02001890 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001891 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1892 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001893 if (IS_ERR(rdev)) {
1894 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001895 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001896 }
1897 wdev = NULL;
1898 netdev = NULL;
1899 result = 0;
1900
1901 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001902 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001903 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001904
1905 /*
1906 * end workaround code, by now the rdev is available
1907 * and locked, and wdev may or may not be NULL.
1908 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001909
1910 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001911 result = cfg80211_dev_rename(
1912 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001913
1914 mutex_unlock(&cfg80211_mutex);
1915
1916 if (result)
1917 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001918
Jouni Malinen31888482008-10-30 16:59:24 +02001919 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1920 struct ieee80211_txq_params txq_params;
1921 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1922
1923 if (!rdev->ops->set_txq_params) {
1924 result = -EOPNOTSUPP;
1925 goto bad_res;
1926 }
1927
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001928 if (!netdev) {
1929 result = -EINVAL;
1930 goto bad_res;
1931 }
1932
Johannes Berg133a3ff2011-11-03 14:50:13 +01001933 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1934 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1935 result = -EINVAL;
1936 goto bad_res;
1937 }
1938
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001939 if (!netif_running(netdev)) {
1940 result = -ENETDOWN;
1941 goto bad_res;
1942 }
1943
Jouni Malinen31888482008-10-30 16:59:24 +02001944 nla_for_each_nested(nl_txq_params,
1945 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1946 rem_txq_params) {
1947 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1948 nla_data(nl_txq_params),
1949 nla_len(nl_txq_params),
1950 txq_params_policy);
1951 result = parse_txq_params(tb, &txq_params);
1952 if (result)
1953 goto bad_res;
1954
Hila Gonene35e4d22012-06-27 17:19:42 +03001955 result = rdev_set_txq_params(rdev, netdev,
1956 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001957 if (result)
1958 goto bad_res;
1959 }
1960 }
1961
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001962 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001963 result = __nl80211_set_channel(rdev,
1964 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1965 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001966 if (result)
1967 goto bad_res;
1968 }
1969
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001970 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001971 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001972 enum nl80211_tx_power_setting type;
1973 int idx, mbm = 0;
1974
Johannes Bergc8442112012-10-24 10:17:18 +02001975 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1976 txp_wdev = NULL;
1977
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001978 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001979 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001980 goto bad_res;
1981 }
1982
1983 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1984 type = nla_get_u32(info->attrs[idx]);
1985
1986 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1987 (type != NL80211_TX_POWER_AUTOMATIC)) {
1988 result = -EINVAL;
1989 goto bad_res;
1990 }
1991
1992 if (type != NL80211_TX_POWER_AUTOMATIC) {
1993 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1994 mbm = nla_get_u32(info->attrs[idx]);
1995 }
1996
Johannes Bergc8442112012-10-24 10:17:18 +02001997 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001998 if (result)
1999 goto bad_res;
2000 }
2001
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002002 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2003 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2004 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002005 if ((!rdev->wiphy.available_antennas_tx &&
2006 !rdev->wiphy.available_antennas_rx) ||
2007 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002008 result = -EOPNOTSUPP;
2009 goto bad_res;
2010 }
2011
2012 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2013 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2014
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002015 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002016 * available antenna masks, except for the "all" mask */
2017 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2018 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002019 result = -EINVAL;
2020 goto bad_res;
2021 }
2022
Bruno Randolf7f531e02010-12-16 11:30:22 +09002023 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2024 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002025
Hila Gonene35e4d22012-06-27 17:19:42 +03002026 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002027 if (result)
2028 goto bad_res;
2029 }
2030
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002031 changed = 0;
2032
2033 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2034 retry_short = nla_get_u8(
2035 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2036 if (retry_short == 0) {
2037 result = -EINVAL;
2038 goto bad_res;
2039 }
2040 changed |= WIPHY_PARAM_RETRY_SHORT;
2041 }
2042
2043 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2044 retry_long = nla_get_u8(
2045 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2046 if (retry_long == 0) {
2047 result = -EINVAL;
2048 goto bad_res;
2049 }
2050 changed |= WIPHY_PARAM_RETRY_LONG;
2051 }
2052
2053 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2054 frag_threshold = nla_get_u32(
2055 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2056 if (frag_threshold < 256) {
2057 result = -EINVAL;
2058 goto bad_res;
2059 }
2060 if (frag_threshold != (u32) -1) {
2061 /*
2062 * Fragments (apart from the last one) are required to
2063 * have even length. Make the fragmentation code
2064 * simpler by stripping LSB should someone try to use
2065 * odd threshold value.
2066 */
2067 frag_threshold &= ~0x1;
2068 }
2069 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2070 }
2071
2072 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2073 rts_threshold = nla_get_u32(
2074 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2075 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2076 }
2077
Lukáš Turek81077e82009-12-21 22:50:47 +01002078 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2079 coverage_class = nla_get_u8(
2080 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2081 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2082 }
2083
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002084 if (changed) {
2085 u8 old_retry_short, old_retry_long;
2086 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002087 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002088
2089 if (!rdev->ops->set_wiphy_params) {
2090 result = -EOPNOTSUPP;
2091 goto bad_res;
2092 }
2093
2094 old_retry_short = rdev->wiphy.retry_short;
2095 old_retry_long = rdev->wiphy.retry_long;
2096 old_frag_threshold = rdev->wiphy.frag_threshold;
2097 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002098 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002099
2100 if (changed & WIPHY_PARAM_RETRY_SHORT)
2101 rdev->wiphy.retry_short = retry_short;
2102 if (changed & WIPHY_PARAM_RETRY_LONG)
2103 rdev->wiphy.retry_long = retry_long;
2104 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2105 rdev->wiphy.frag_threshold = frag_threshold;
2106 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2107 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002108 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2109 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002110
Hila Gonene35e4d22012-06-27 17:19:42 +03002111 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002112 if (result) {
2113 rdev->wiphy.retry_short = old_retry_short;
2114 rdev->wiphy.retry_long = old_retry_long;
2115 rdev->wiphy.frag_threshold = old_frag_threshold;
2116 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002117 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002118 }
2119 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002120
Johannes Berg306d6112008-12-08 12:39:04 +01002121 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01002122 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02002123 if (netdev)
2124 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002125 return result;
2126}
2127
Johannes Berg71bbc992012-06-15 15:30:18 +02002128static inline u64 wdev_id(struct wireless_dev *wdev)
2129{
2130 return (u64)wdev->identifier |
2131 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2132}
Johannes Berg55682962007-09-20 13:09:35 -04002133
Johannes Berg683b6d32012-11-08 21:25:48 +01002134static int nl80211_send_chandef(struct sk_buff *msg,
2135 struct cfg80211_chan_def *chandef)
2136{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002137 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002138
Johannes Berg683b6d32012-11-08 21:25:48 +01002139 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2140 chandef->chan->center_freq))
2141 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002142 switch (chandef->width) {
2143 case NL80211_CHAN_WIDTH_20_NOHT:
2144 case NL80211_CHAN_WIDTH_20:
2145 case NL80211_CHAN_WIDTH_40:
2146 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2147 cfg80211_get_chandef_type(chandef)))
2148 return -ENOBUFS;
2149 break;
2150 default:
2151 break;
2152 }
2153 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2154 return -ENOBUFS;
2155 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2156 return -ENOBUFS;
2157 if (chandef->center_freq2 &&
2158 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002159 return -ENOBUFS;
2160 return 0;
2161}
2162
Eric W. Biederman15e47302012-09-07 20:12:54 +00002163static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002164 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002165 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002166{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002167 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002168 void *hdr;
2169
Eric W. Biederman15e47302012-09-07 20:12:54 +00002170 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002171 if (!hdr)
2172 return -1;
2173
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002174 if (dev &&
2175 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002176 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002177 goto nla_put_failure;
2178
2179 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2180 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002181 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002183 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2184 rdev->devlist_generation ^
2185 (cfg80211_rdev_list_generation << 2)))
2186 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002187
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002188 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002189 int ret;
2190 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002191
Johannes Berg683b6d32012-11-08 21:25:48 +01002192 ret = rdev_get_channel(rdev, wdev, &chandef);
2193 if (ret == 0) {
2194 if (nl80211_send_chandef(msg, &chandef))
2195 goto nla_put_failure;
2196 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002197 }
2198
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002199 if (wdev->ssid_len) {
2200 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2201 goto nla_put_failure;
2202 }
2203
Johannes Berg55682962007-09-20 13:09:35 -04002204 return genlmsg_end(msg, hdr);
2205
2206 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002207 genlmsg_cancel(msg, hdr);
2208 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002209}
2210
2211static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2212{
2213 int wp_idx = 0;
2214 int if_idx = 0;
2215 int wp_start = cb->args[0];
2216 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002217 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002218 struct wireless_dev *wdev;
2219
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002220 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02002221 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2222 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002223 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002224 if (wp_idx < wp_start) {
2225 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002226 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002227 }
Johannes Berg55682962007-09-20 13:09:35 -04002228 if_idx = 0;
2229
Johannes Bergf5ea9122009-08-07 16:17:38 +02002230 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02002231 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002232 if (if_idx < if_start) {
2233 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002234 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002236 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002237 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002238 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02002239 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002240 goto out;
2241 }
2242 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002243 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02002244 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002245
2246 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002247 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002248 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002249 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04002250
2251 cb->args[0] = wp_idx;
2252 cb->args[1] = if_idx;
2253
2254 return skb->len;
2255}
2256
2257static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2258{
2259 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002260 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002261 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002262
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002264 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002265 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002266
Eric W. Biederman15e47302012-09-07 20:12:54 +00002267 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002268 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002269 nlmsg_free(msg);
2270 return -ENOBUFS;
2271 }
Johannes Berg55682962007-09-20 13:09:35 -04002272
Johannes Berg134e6372009-07-10 09:51:34 +00002273 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002274}
2275
Michael Wu66f7ac52008-01-31 19:48:22 +01002276static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2277 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2278 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2279 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2280 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2281 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2282};
2283
2284static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2285{
2286 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2287 int flag;
2288
2289 *mntrflags = 0;
2290
2291 if (!nla)
2292 return -EINVAL;
2293
2294 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2295 nla, mntr_flags_policy))
2296 return -EINVAL;
2297
2298 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2299 if (flags[flag])
2300 *mntrflags |= (1<<flag);
2301
2302 return 0;
2303}
2304
Johannes Berg9bc383d2009-11-19 11:55:19 +01002305static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002306 struct net_device *netdev, u8 use_4addr,
2307 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002308{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002309 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002310 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002311 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002312 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002313 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002314
2315 switch (iftype) {
2316 case NL80211_IFTYPE_AP_VLAN:
2317 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2318 return 0;
2319 break;
2320 case NL80211_IFTYPE_STATION:
2321 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2322 return 0;
2323 break;
2324 default:
2325 break;
2326 }
2327
2328 return -EOPNOTSUPP;
2329}
2330
Johannes Berg55682962007-09-20 13:09:35 -04002331static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2332{
Johannes Berg4c476992010-10-04 21:36:35 +02002333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002334 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002335 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002336 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002337 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002338 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002339 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002340
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341 memset(&params, 0, sizeof(params));
2342
Johannes Berg04a773a2009-04-19 21:24:32 +02002343 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002344
Johannes Berg723b0382008-09-16 20:22:09 +02002345 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002346 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002347 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002348 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002349 if (ntype > NL80211_IFTYPE_MAX)
2350 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002351 }
2352
Johannes Berg92ffe052008-09-16 20:39:36 +02002353 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002354 struct wireless_dev *wdev = dev->ieee80211_ptr;
2355
Johannes Berg4c476992010-10-04 21:36:35 +02002356 if (ntype != NL80211_IFTYPE_MESH_POINT)
2357 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002358 if (netif_running(dev))
2359 return -EBUSY;
2360
2361 wdev_lock(wdev);
2362 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2363 IEEE80211_MAX_MESH_ID_LEN);
2364 wdev->mesh_id_up_len =
2365 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2366 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2367 wdev->mesh_id_up_len);
2368 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369 }
2370
Felix Fietkau8b787642009-11-10 18:53:10 +01002371 if (info->attrs[NL80211_ATTR_4ADDR]) {
2372 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2373 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002374 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002375 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002376 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002377 } else {
2378 params.use_4addr = -1;
2379 }
2380
Johannes Berg92ffe052008-09-16 20:39:36 +02002381 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002382 if (ntype != NL80211_IFTYPE_MONITOR)
2383 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002384 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2385 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002386 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002387 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002388
2389 flags = &_flags;
2390 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002391 }
Johannes Berg3b858752009-03-12 09:55:09 +01002392
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002393 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002394 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002395 else
2396 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002397
Johannes Berg9bc383d2009-11-19 11:55:19 +01002398 if (!err && params.use_4addr != -1)
2399 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2400
Johannes Berg55682962007-09-20 13:09:35 -04002401 return err;
2402}
2403
2404static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2405{
Johannes Berg4c476992010-10-04 21:36:35 +02002406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002407 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002408 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002409 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002410 int err;
2411 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002412 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002413
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414 memset(&params, 0, sizeof(params));
2415
Johannes Berg55682962007-09-20 13:09:35 -04002416 if (!info->attrs[NL80211_ATTR_IFNAME])
2417 return -EINVAL;
2418
2419 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2420 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2421 if (type > NL80211_IFTYPE_MAX)
2422 return -EINVAL;
2423 }
2424
Johannes Berg79c97e92009-07-07 03:56:12 +02002425 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002426 !(rdev->wiphy.interface_modes & (1 << type)))
2427 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002428
Arend van Spriel1c18f142013-01-08 10:17:27 +01002429 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2430 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2431 ETH_ALEN);
2432 if (!is_valid_ether_addr(params.macaddr))
2433 return -EADDRNOTAVAIL;
2434 }
2435
Johannes Berg9bc383d2009-11-19 11:55:19 +01002436 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002437 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002438 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002439 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002440 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002441 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002442
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2444 if (!msg)
2445 return -ENOMEM;
2446
Michael Wu66f7ac52008-01-31 19:48:22 +01002447 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2448 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2449 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002450 wdev = rdev_add_virtual_intf(rdev,
2451 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2452 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002453 if (IS_ERR(wdev)) {
2454 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002455 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002456 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002457
Johannes Berg98104fde2012-06-16 00:19:54 +02002458 switch (type) {
2459 case NL80211_IFTYPE_MESH_POINT:
2460 if (!info->attrs[NL80211_ATTR_MESH_ID])
2461 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002462 wdev_lock(wdev);
2463 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2464 IEEE80211_MAX_MESH_ID_LEN);
2465 wdev->mesh_id_up_len =
2466 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2467 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2468 wdev->mesh_id_up_len);
2469 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002470 break;
2471 case NL80211_IFTYPE_P2P_DEVICE:
2472 /*
2473 * P2P Device doesn't have a netdev, so doesn't go
2474 * through the netdev notifier and must be added here
2475 */
2476 mutex_init(&wdev->mtx);
2477 INIT_LIST_HEAD(&wdev->event_list);
2478 spin_lock_init(&wdev->event_lock);
2479 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2480 spin_lock_init(&wdev->mgmt_registrations_lock);
2481
2482 mutex_lock(&rdev->devlist_mtx);
2483 wdev->identifier = ++rdev->wdev_id;
2484 list_add_rcu(&wdev->list, &rdev->wdev_list);
2485 rdev->devlist_generation++;
2486 mutex_unlock(&rdev->devlist_mtx);
2487 break;
2488 default:
2489 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002490 }
2491
Eric W. Biederman15e47302012-09-07 20:12:54 +00002492 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002493 rdev, wdev) < 0) {
2494 nlmsg_free(msg);
2495 return -ENOBUFS;
2496 }
2497
2498 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002499}
2500
2501static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2502{
Johannes Berg4c476992010-10-04 21:36:35 +02002503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002504 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002505
Johannes Berg4c476992010-10-04 21:36:35 +02002506 if (!rdev->ops->del_virtual_intf)
2507 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002508
Johannes Berg84efbb82012-06-16 00:00:26 +02002509 /*
2510 * If we remove a wireless device without a netdev then clear
2511 * user_ptr[1] so that nl80211_post_doit won't dereference it
2512 * to check if it needs to do dev_put(). Otherwise it crashes
2513 * since the wdev has been freed, unlike with a netdev where
2514 * we need the dev_put() for the netdev to really be freed.
2515 */
2516 if (!wdev->netdev)
2517 info->user_ptr[1] = NULL;
2518
Hila Gonene35e4d22012-06-27 17:19:42 +03002519 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002520}
2521
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002522static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2523{
2524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2525 struct net_device *dev = info->user_ptr[1];
2526 u16 noack_map;
2527
2528 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2529 return -EINVAL;
2530
2531 if (!rdev->ops->set_noack_map)
2532 return -EOPNOTSUPP;
2533
2534 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2535
Hila Gonene35e4d22012-06-27 17:19:42 +03002536 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002537}
2538
Johannes Berg41ade002007-12-19 02:03:29 +01002539struct get_key_cookie {
2540 struct sk_buff *msg;
2541 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002542 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002543};
2544
2545static void get_key_callback(void *c, struct key_params *params)
2546{
Johannes Bergb9454e82009-07-08 13:29:08 +02002547 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002548 struct get_key_cookie *cookie = c;
2549
David S. Miller9360ffd2012-03-29 04:41:26 -04002550 if ((params->key &&
2551 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2552 params->key_len, params->key)) ||
2553 (params->seq &&
2554 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2555 params->seq_len, params->seq)) ||
2556 (params->cipher &&
2557 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2558 params->cipher)))
2559 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002560
Johannes Bergb9454e82009-07-08 13:29:08 +02002561 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2562 if (!key)
2563 goto nla_put_failure;
2564
David S. Miller9360ffd2012-03-29 04:41:26 -04002565 if ((params->key &&
2566 nla_put(cookie->msg, NL80211_KEY_DATA,
2567 params->key_len, params->key)) ||
2568 (params->seq &&
2569 nla_put(cookie->msg, NL80211_KEY_SEQ,
2570 params->seq_len, params->seq)) ||
2571 (params->cipher &&
2572 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2573 params->cipher)))
2574 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002575
David S. Miller9360ffd2012-03-29 04:41:26 -04002576 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2577 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002578
2579 nla_nest_end(cookie->msg, key);
2580
Johannes Berg41ade002007-12-19 02:03:29 +01002581 return;
2582 nla_put_failure:
2583 cookie->error = 1;
2584}
2585
2586static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2587{
Johannes Berg4c476992010-10-04 21:36:35 +02002588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002589 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002590 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002591 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002592 const u8 *mac_addr = NULL;
2593 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002594 struct get_key_cookie cookie = {
2595 .error = 0,
2596 };
2597 void *hdr;
2598 struct sk_buff *msg;
2599
2600 if (info->attrs[NL80211_ATTR_KEY_IDX])
2601 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2602
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002603 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002604 return -EINVAL;
2605
2606 if (info->attrs[NL80211_ATTR_MAC])
2607 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2608
Johannes Berge31b8212010-10-05 19:39:30 +02002609 pairwise = !!mac_addr;
2610 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2611 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2612 if (kt >= NUM_NL80211_KEYTYPES)
2613 return -EINVAL;
2614 if (kt != NL80211_KEYTYPE_GROUP &&
2615 kt != NL80211_KEYTYPE_PAIRWISE)
2616 return -EINVAL;
2617 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2618 }
2619
Johannes Berg4c476992010-10-04 21:36:35 +02002620 if (!rdev->ops->get_key)
2621 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002622
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002624 if (!msg)
2625 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002626
Eric W. Biederman15e47302012-09-07 20:12:54 +00002627 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002628 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002629 if (IS_ERR(hdr))
2630 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002631
2632 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002633 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002634
David S. Miller9360ffd2012-03-29 04:41:26 -04002635 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2636 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2637 goto nla_put_failure;
2638 if (mac_addr &&
2639 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2640 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002641
Johannes Berge31b8212010-10-05 19:39:30 +02002642 if (pairwise && mac_addr &&
2643 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2644 return -ENOENT;
2645
Hila Gonene35e4d22012-06-27 17:19:42 +03002646 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2647 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002648
2649 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002650 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002651
2652 if (cookie.error)
2653 goto nla_put_failure;
2654
2655 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002656 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002657
2658 nla_put_failure:
2659 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002660 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002661 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002662 return err;
2663}
2664
2665static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2666{
Johannes Berg4c476992010-10-04 21:36:35 +02002667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002668 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002669 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002670 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002671
Johannes Bergb9454e82009-07-08 13:29:08 +02002672 err = nl80211_parse_key(info, &key);
2673 if (err)
2674 return err;
2675
2676 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002677 return -EINVAL;
2678
Johannes Bergb9454e82009-07-08 13:29:08 +02002679 /* only support setting default key */
2680 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002681 return -EINVAL;
2682
Johannes Bergfffd0932009-07-08 14:22:54 +02002683 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002684
2685 if (key.def) {
2686 if (!rdev->ops->set_default_key) {
2687 err = -EOPNOTSUPP;
2688 goto out;
2689 }
2690
2691 err = nl80211_key_allowed(dev->ieee80211_ptr);
2692 if (err)
2693 goto out;
2694
Hila Gonene35e4d22012-06-27 17:19:42 +03002695 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002696 key.def_uni, key.def_multi);
2697
2698 if (err)
2699 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002700
Johannes Berg3d23e342009-09-29 23:27:28 +02002701#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002702 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002703#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002704 } else {
2705 if (key.def_uni || !key.def_multi) {
2706 err = -EINVAL;
2707 goto out;
2708 }
2709
2710 if (!rdev->ops->set_default_mgmt_key) {
2711 err = -EOPNOTSUPP;
2712 goto out;
2713 }
2714
2715 err = nl80211_key_allowed(dev->ieee80211_ptr);
2716 if (err)
2717 goto out;
2718
Hila Gonene35e4d22012-06-27 17:19:42 +03002719 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002720 if (err)
2721 goto out;
2722
2723#ifdef CONFIG_CFG80211_WEXT
2724 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2725#endif
2726 }
2727
2728 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002729 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002730
Johannes Berg41ade002007-12-19 02:03:29 +01002731 return err;
2732}
2733
2734static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2735{
Johannes Berg4c476992010-10-04 21:36:35 +02002736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002737 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002738 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002739 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002740 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002741
Johannes Bergb9454e82009-07-08 13:29:08 +02002742 err = nl80211_parse_key(info, &key);
2743 if (err)
2744 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002745
Johannes Bergb9454e82009-07-08 13:29:08 +02002746 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002747 return -EINVAL;
2748
Johannes Berg41ade002007-12-19 02:03:29 +01002749 if (info->attrs[NL80211_ATTR_MAC])
2750 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2751
Johannes Berge31b8212010-10-05 19:39:30 +02002752 if (key.type == -1) {
2753 if (mac_addr)
2754 key.type = NL80211_KEYTYPE_PAIRWISE;
2755 else
2756 key.type = NL80211_KEYTYPE_GROUP;
2757 }
2758
2759 /* for now */
2760 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2761 key.type != NL80211_KEYTYPE_GROUP)
2762 return -EINVAL;
2763
Johannes Berg4c476992010-10-04 21:36:35 +02002764 if (!rdev->ops->add_key)
2765 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002766
Johannes Berge31b8212010-10-05 19:39:30 +02002767 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2768 key.type == NL80211_KEYTYPE_PAIRWISE,
2769 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002770 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002771
2772 wdev_lock(dev->ieee80211_ptr);
2773 err = nl80211_key_allowed(dev->ieee80211_ptr);
2774 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002775 err = rdev_add_key(rdev, dev, key.idx,
2776 key.type == NL80211_KEYTYPE_PAIRWISE,
2777 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002778 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002779
Johannes Berg41ade002007-12-19 02:03:29 +01002780 return err;
2781}
2782
2783static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2784{
Johannes Berg4c476992010-10-04 21:36:35 +02002785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002786 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002787 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002788 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002789 struct key_parse key;
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;
Johannes Berg41ade002007-12-19 02:03:29 +01002794
2795 if (info->attrs[NL80211_ATTR_MAC])
2796 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2797
Johannes Berge31b8212010-10-05 19:39:30 +02002798 if (key.type == -1) {
2799 if (mac_addr)
2800 key.type = NL80211_KEYTYPE_PAIRWISE;
2801 else
2802 key.type = NL80211_KEYTYPE_GROUP;
2803 }
2804
2805 /* for now */
2806 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2807 key.type != NL80211_KEYTYPE_GROUP)
2808 return -EINVAL;
2809
Johannes Berg4c476992010-10-04 21:36:35 +02002810 if (!rdev->ops->del_key)
2811 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002812
Johannes Bergfffd0932009-07-08 14:22:54 +02002813 wdev_lock(dev->ieee80211_ptr);
2814 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002815
2816 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2817 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2818 err = -ENOENT;
2819
Johannes Bergfffd0932009-07-08 14:22:54 +02002820 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002821 err = rdev_del_key(rdev, dev, key.idx,
2822 key.type == NL80211_KEYTYPE_PAIRWISE,
2823 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002824
Johannes Berg3d23e342009-09-29 23:27:28 +02002825#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002826 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002827 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002828 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002829 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002830 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2831 }
2832#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002833 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002834
Johannes Berg41ade002007-12-19 02:03:29 +01002835 return err;
2836}
2837
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302838/* This function returns an error or the number of nested attributes */
2839static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2840{
2841 struct nlattr *attr;
2842 int n_entries = 0, tmp;
2843
2844 nla_for_each_nested(attr, nl_attr, tmp) {
2845 if (nla_len(attr) != ETH_ALEN)
2846 return -EINVAL;
2847
2848 n_entries++;
2849 }
2850
2851 return n_entries;
2852}
2853
2854/*
2855 * This function parses ACL information and allocates memory for ACL data.
2856 * On successful return, the calling function is responsible to free the
2857 * ACL buffer returned by this function.
2858 */
2859static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2860 struct genl_info *info)
2861{
2862 enum nl80211_acl_policy acl_policy;
2863 struct nlattr *attr;
2864 struct cfg80211_acl_data *acl;
2865 int i = 0, n_entries, tmp;
2866
2867 if (!wiphy->max_acl_mac_addrs)
2868 return ERR_PTR(-EOPNOTSUPP);
2869
2870 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2871 return ERR_PTR(-EINVAL);
2872
2873 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2874 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2875 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2876 return ERR_PTR(-EINVAL);
2877
2878 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2879 return ERR_PTR(-EINVAL);
2880
2881 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2882 if (n_entries < 0)
2883 return ERR_PTR(n_entries);
2884
2885 if (n_entries > wiphy->max_acl_mac_addrs)
2886 return ERR_PTR(-ENOTSUPP);
2887
2888 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2889 GFP_KERNEL);
2890 if (!acl)
2891 return ERR_PTR(-ENOMEM);
2892
2893 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2894 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2895 i++;
2896 }
2897
2898 acl->n_acl_entries = n_entries;
2899 acl->acl_policy = acl_policy;
2900
2901 return acl;
2902}
2903
2904static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2905{
2906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2907 struct net_device *dev = info->user_ptr[1];
2908 struct cfg80211_acl_data *acl;
2909 int err;
2910
2911 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2912 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2913 return -EOPNOTSUPP;
2914
2915 if (!dev->ieee80211_ptr->beacon_interval)
2916 return -EINVAL;
2917
2918 acl = parse_acl_data(&rdev->wiphy, info);
2919 if (IS_ERR(acl))
2920 return PTR_ERR(acl);
2921
2922 err = rdev_set_mac_acl(rdev, dev, acl);
2923
2924 kfree(acl);
2925
2926 return err;
2927}
2928
Johannes Berg88600202012-02-13 15:17:18 +01002929static int nl80211_parse_beacon(struct genl_info *info,
2930 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002931{
Johannes Berg88600202012-02-13 15:17:18 +01002932 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002933
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002934 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2935 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2936 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2937 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002938 return -EINVAL;
2939
Johannes Berg88600202012-02-13 15:17:18 +01002940 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002941
Johannes Berged1b6cc2007-12-19 02:03:32 +01002942 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002943 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2944 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2945 if (!bcn->head_len)
2946 return -EINVAL;
2947 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002948 }
2949
2950 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002951 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2952 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002953 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002954 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002955 }
2956
Johannes Berg4c476992010-10-04 21:36:35 +02002957 if (!haveinfo)
2958 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002959
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002960 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002961 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2962 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002963 }
2964
2965 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002966 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002967 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002968 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002969 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2970 }
2971
2972 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002973 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002974 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002975 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002976 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2977 }
2978
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002979 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002980 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002981 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002982 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002983 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2984 }
2985
Johannes Berg88600202012-02-13 15:17:18 +01002986 return 0;
2987}
2988
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002989static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2990 struct cfg80211_ap_settings *params)
2991{
2992 struct wireless_dev *wdev;
2993 bool ret = false;
2994
2995 mutex_lock(&rdev->devlist_mtx);
2996
Johannes Berg89a54e42012-06-15 14:33:17 +02002997 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002998 if (wdev->iftype != NL80211_IFTYPE_AP &&
2999 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3000 continue;
3001
Johannes Berg683b6d32012-11-08 21:25:48 +01003002 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003003 continue;
3004
Johannes Berg683b6d32012-11-08 21:25:48 +01003005 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003006 ret = true;
3007 break;
3008 }
3009
3010 mutex_unlock(&rdev->devlist_mtx);
3011
3012 return ret;
3013}
3014
Jouni Malinene39e5b52012-09-30 19:29:39 +03003015static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3016 enum nl80211_auth_type auth_type,
3017 enum nl80211_commands cmd)
3018{
3019 if (auth_type > NL80211_AUTHTYPE_MAX)
3020 return false;
3021
3022 switch (cmd) {
3023 case NL80211_CMD_AUTHENTICATE:
3024 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3025 auth_type == NL80211_AUTHTYPE_SAE)
3026 return false;
3027 return true;
3028 case NL80211_CMD_CONNECT:
3029 case NL80211_CMD_START_AP:
3030 /* SAE not supported yet */
3031 if (auth_type == NL80211_AUTHTYPE_SAE)
3032 return false;
3033 return true;
3034 default:
3035 return false;
3036 }
3037}
3038
Johannes Berg88600202012-02-13 15:17:18 +01003039static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3040{
3041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3042 struct net_device *dev = info->user_ptr[1];
3043 struct wireless_dev *wdev = dev->ieee80211_ptr;
3044 struct cfg80211_ap_settings params;
3045 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003046 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003047
3048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3050 return -EOPNOTSUPP;
3051
3052 if (!rdev->ops->start_ap)
3053 return -EOPNOTSUPP;
3054
3055 if (wdev->beacon_interval)
3056 return -EALREADY;
3057
3058 memset(&params, 0, sizeof(params));
3059
3060 /* these are required for START_AP */
3061 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3062 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3063 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3064 return -EINVAL;
3065
3066 err = nl80211_parse_beacon(info, &params.beacon);
3067 if (err)
3068 return err;
3069
3070 params.beacon_interval =
3071 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3072 params.dtim_period =
3073 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3074
3075 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3076 if (err)
3077 return err;
3078
3079 /*
3080 * In theory, some of these attributes should be required here
3081 * but since they were not used when the command was originally
3082 * added, keep them optional for old user space programs to let
3083 * them continue to work with drivers that do not need the
3084 * additional information -- drivers must check!
3085 */
3086 if (info->attrs[NL80211_ATTR_SSID]) {
3087 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3088 params.ssid_len =
3089 nla_len(info->attrs[NL80211_ATTR_SSID]);
3090 if (params.ssid_len == 0 ||
3091 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3092 return -EINVAL;
3093 }
3094
3095 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3096 params.hidden_ssid = nla_get_u32(
3097 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3098 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3099 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3100 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3101 return -EINVAL;
3102 }
3103
3104 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3105
3106 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3107 params.auth_type = nla_get_u32(
3108 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003109 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3110 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003111 return -EINVAL;
3112 } else
3113 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3114
3115 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3116 NL80211_MAX_NR_CIPHER_SUITES);
3117 if (err)
3118 return err;
3119
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303120 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3121 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3122 return -EOPNOTSUPP;
3123 params.inactivity_timeout = nla_get_u16(
3124 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3125 }
3126
Johannes Berg53cabad2012-11-14 15:17:28 +01003127 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3129 return -EINVAL;
3130 params.p2p_ctwindow =
3131 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3132 if (params.p2p_ctwindow > 127)
3133 return -EINVAL;
3134 if (params.p2p_ctwindow != 0 &&
3135 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3136 return -EINVAL;
3137 }
3138
3139 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3140 u8 tmp;
3141
3142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3143 return -EINVAL;
3144 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3145 if (tmp > 1)
3146 return -EINVAL;
3147 params.p2p_opp_ps = tmp;
3148 if (params.p2p_opp_ps != 0 &&
3149 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3150 return -EINVAL;
3151 }
3152
Johannes Bergaa430da2012-05-16 23:50:18 +02003153 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003154 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3155 if (err)
3156 return err;
3157 } else if (wdev->preset_chandef.chan) {
3158 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003159 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003160 return -EINVAL;
3161
Johannes Berg683b6d32012-11-08 21:25:48 +01003162 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003163 return -EINVAL;
3164
Simon Wunderlich04f39042013-02-08 18:16:19 +01003165 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3166 if (err < 0)
3167 return err;
3168 if (err) {
3169 radar_detect_width = BIT(params.chandef.width);
3170 params.radar_required = true;
3171 }
3172
Michal Kaziore4e32452012-06-29 12:47:08 +02003173 mutex_lock(&rdev->devlist_mtx);
Simon Wunderlich04f39042013-02-08 18:16:19 +01003174 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3175 params.chandef.chan,
3176 CHAN_MODE_SHARED,
3177 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003178 mutex_unlock(&rdev->devlist_mtx);
3179
3180 if (err)
3181 return err;
3182
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303183 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3184 params.acl = parse_acl_data(&rdev->wiphy, info);
3185 if (IS_ERR(params.acl))
3186 return PTR_ERR(params.acl);
3187 }
3188
Hila Gonene35e4d22012-06-27 17:19:42 +03003189 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003190 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003191 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003192 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003193 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003194 wdev->ssid_len = params.ssid_len;
3195 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003196 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303197
3198 kfree(params.acl);
3199
Johannes Berg56d18932011-05-09 18:41:15 +02003200 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003201}
3202
Johannes Berg88600202012-02-13 15:17:18 +01003203static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3204{
3205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3206 struct net_device *dev = info->user_ptr[1];
3207 struct wireless_dev *wdev = dev->ieee80211_ptr;
3208 struct cfg80211_beacon_data params;
3209 int err;
3210
3211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3213 return -EOPNOTSUPP;
3214
3215 if (!rdev->ops->change_beacon)
3216 return -EOPNOTSUPP;
3217
3218 if (!wdev->beacon_interval)
3219 return -EINVAL;
3220
3221 err = nl80211_parse_beacon(info, &params);
3222 if (err)
3223 return err;
3224
Hila Gonene35e4d22012-06-27 17:19:42 +03003225 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003226}
3227
3228static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003229{
Johannes Berg4c476992010-10-04 21:36:35 +02003230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3231 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003232
Michal Kazior60771782012-06-29 12:46:56 +02003233 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003234}
3235
Johannes Berg5727ef12007-12-19 02:03:34 +01003236static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3237 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3238 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3239 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003240 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003241 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003242 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003243};
3244
Johannes Bergeccb8e82009-05-11 21:57:56 +03003245static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003246 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003247 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003248{
3249 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003250 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003251 int flag;
3252
Johannes Bergeccb8e82009-05-11 21:57:56 +03003253 /*
3254 * Try parsing the new attribute first so userspace
3255 * can specify both for older kernels.
3256 */
3257 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3258 if (nla) {
3259 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003260
Johannes Bergeccb8e82009-05-11 21:57:56 +03003261 sta_flags = nla_data(nla);
3262 params->sta_flags_mask = sta_flags->mask;
3263 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003264 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003265 if ((params->sta_flags_mask |
3266 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3267 return -EINVAL;
3268 return 0;
3269 }
3270
3271 /* if present, parse the old attribute */
3272
3273 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003274 if (!nla)
3275 return 0;
3276
3277 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3278 nla, sta_flags_policy))
3279 return -EINVAL;
3280
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003281 /*
3282 * Only allow certain flags for interface types so that
3283 * other attributes are silently ignored. Remember that
3284 * this is backward compatibility code with old userspace
3285 * and shouldn't be hit in other cases anyway.
3286 */
3287 switch (iftype) {
3288 case NL80211_IFTYPE_AP:
3289 case NL80211_IFTYPE_AP_VLAN:
3290 case NL80211_IFTYPE_P2P_GO:
3291 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3292 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3293 BIT(NL80211_STA_FLAG_WME) |
3294 BIT(NL80211_STA_FLAG_MFP);
3295 break;
3296 case NL80211_IFTYPE_P2P_CLIENT:
3297 case NL80211_IFTYPE_STATION:
3298 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3299 BIT(NL80211_STA_FLAG_TDLS_PEER);
3300 break;
3301 case NL80211_IFTYPE_MESH_POINT:
3302 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3303 BIT(NL80211_STA_FLAG_MFP) |
3304 BIT(NL80211_STA_FLAG_AUTHORIZED);
3305 default:
3306 return -EINVAL;
3307 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003308
Johannes Berg3383b5a2012-05-10 20:14:43 +02003309 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3310 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003311 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003312
Johannes Berg3383b5a2012-05-10 20:14:43 +02003313 /* no longer support new API additions in old API */
3314 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3315 return -EINVAL;
3316 }
3317 }
3318
Johannes Berg5727ef12007-12-19 02:03:34 +01003319 return 0;
3320}
3321
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003322static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3323 int attr)
3324{
3325 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003326 u32 bitrate;
3327 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003328
3329 rate = nla_nest_start(msg, attr);
3330 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003331 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003332
3333 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3334 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003335 /* report 16-bit bitrate only if we can */
3336 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003337 if (bitrate > 0 &&
3338 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3339 return false;
3340 if (bitrate_compat > 0 &&
3341 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3342 return false;
3343
3344 if (info->flags & RATE_INFO_FLAGS_MCS) {
3345 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3346 return false;
3347 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3348 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3349 return false;
3350 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3351 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3352 return false;
3353 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3354 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3355 return false;
3356 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3357 return false;
3358 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3359 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3360 return false;
3361 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3362 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3363 return false;
3364 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3365 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3366 return false;
3367 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3368 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3369 return false;
3370 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3371 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3372 return false;
3373 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003374
3375 nla_nest_end(msg, rate);
3376 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003377}
3378
Felix Fietkau119363c2013-04-22 16:29:30 +02003379static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3380 int id)
3381{
3382 void *attr;
3383 int i = 0;
3384
3385 if (!mask)
3386 return true;
3387
3388 attr = nla_nest_start(msg, id);
3389 if (!attr)
3390 return false;
3391
3392 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3393 if (!(mask & BIT(i)))
3394 continue;
3395
3396 if (nla_put_u8(msg, i, signal[i]))
3397 return false;
3398 }
3399
3400 nla_nest_end(msg, attr);
3401
3402 return true;
3403}
3404
Eric W. Biederman15e47302012-09-07 20:12:54 +00003405static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003406 int flags,
3407 struct cfg80211_registered_device *rdev,
3408 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003409 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003410{
3411 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003412 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003413
Eric W. Biederman15e47302012-09-07 20:12:54 +00003414 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003415 if (!hdr)
3416 return -1;
3417
David S. Miller9360ffd2012-03-29 04:41:26 -04003418 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3419 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3420 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3421 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003422
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003423 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3424 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003425 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003426 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3427 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3428 sinfo->connected_time))
3429 goto nla_put_failure;
3430 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3431 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3432 sinfo->inactive_time))
3433 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003434 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3435 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003436 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003437 (u32)sinfo->rx_bytes))
3438 goto nla_put_failure;
3439 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3440 NL80211_STA_INFO_TX_BYTES64)) &&
3441 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3442 (u32)sinfo->tx_bytes))
3443 goto nla_put_failure;
3444 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3445 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003446 sinfo->rx_bytes))
3447 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003448 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3449 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003450 sinfo->tx_bytes))
3451 goto nla_put_failure;
3452 if ((sinfo->filled & STATION_INFO_LLID) &&
3453 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3454 goto nla_put_failure;
3455 if ((sinfo->filled & STATION_INFO_PLID) &&
3456 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3457 goto nla_put_failure;
3458 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3459 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3460 sinfo->plink_state))
3461 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003462 switch (rdev->wiphy.signal_type) {
3463 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003464 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3465 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3466 sinfo->signal))
3467 goto nla_put_failure;
3468 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3469 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3470 sinfo->signal_avg))
3471 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003472 break;
3473 default:
3474 break;
3475 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003476 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3477 if (!nl80211_put_signal(msg, sinfo->chains,
3478 sinfo->chain_signal,
3479 NL80211_STA_INFO_CHAIN_SIGNAL))
3480 goto nla_put_failure;
3481 }
3482 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3483 if (!nl80211_put_signal(msg, sinfo->chains,
3484 sinfo->chain_signal_avg,
3485 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3486 goto nla_put_failure;
3487 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003488 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003489 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3490 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003491 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003492 }
3493 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3494 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3495 NL80211_STA_INFO_RX_BITRATE))
3496 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003497 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003498 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3499 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3500 sinfo->rx_packets))
3501 goto nla_put_failure;
3502 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3503 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3504 sinfo->tx_packets))
3505 goto nla_put_failure;
3506 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3507 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3508 sinfo->tx_retries))
3509 goto nla_put_failure;
3510 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3511 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3512 sinfo->tx_failed))
3513 goto nla_put_failure;
3514 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3515 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3516 sinfo->beacon_loss_count))
3517 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003518 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3519 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3520 sinfo->local_pm))
3521 goto nla_put_failure;
3522 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3523 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3524 sinfo->peer_pm))
3525 goto nla_put_failure;
3526 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3527 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3528 sinfo->nonpeer_pm))
3529 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003530 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3531 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3532 if (!bss_param)
3533 goto nla_put_failure;
3534
David S. Miller9360ffd2012-03-29 04:41:26 -04003535 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3536 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3537 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3538 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3539 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3540 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3541 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3542 sinfo->bss_param.dtim_period) ||
3543 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3544 sinfo->bss_param.beacon_interval))
3545 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003546
3547 nla_nest_end(msg, bss_param);
3548 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003549 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3550 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3551 sizeof(struct nl80211_sta_flag_update),
3552 &sinfo->sta_flags))
3553 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003554 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3555 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3556 sinfo->t_offset))
3557 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003558 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003559
David S. Miller9360ffd2012-03-29 04:41:26 -04003560 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3561 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3562 sinfo->assoc_req_ies))
3563 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003564
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003565 return genlmsg_end(msg, hdr);
3566
3567 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003568 genlmsg_cancel(msg, hdr);
3569 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003570}
3571
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003572static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003573 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003574{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003575 struct station_info sinfo;
3576 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003577 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003578 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003579 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003580 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003581
Johannes Berg97990a02013-04-19 01:02:55 +02003582 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003583 if (err)
3584 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003585
Johannes Berg97990a02013-04-19 01:02:55 +02003586 if (!wdev->netdev) {
3587 err = -EINVAL;
3588 goto out_err;
3589 }
3590
Johannes Bergbba95fe2008-07-29 13:22:51 +02003591 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003592 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003593 goto out_err;
3594 }
3595
Johannes Bergbba95fe2008-07-29 13:22:51 +02003596 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003597 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003598 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003599 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003600 if (err == -ENOENT)
3601 break;
3602 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003603 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003604
3605 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003606 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003607 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003608 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003609 &sinfo) < 0)
3610 goto out;
3611
3612 sta_idx++;
3613 }
3614
3615
3616 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003617 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003618 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003619 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003620 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003621
3622 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003623}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003624
Johannes Berg5727ef12007-12-19 02:03:34 +01003625static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3626{
Johannes Berg4c476992010-10-04 21:36:35 +02003627 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3628 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003629 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003630 struct sk_buff *msg;
3631 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003632 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003633
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003634 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003635
3636 if (!info->attrs[NL80211_ATTR_MAC])
3637 return -EINVAL;
3638
3639 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3640
Johannes Berg4c476992010-10-04 21:36:35 +02003641 if (!rdev->ops->get_station)
3642 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003643
Hila Gonene35e4d22012-06-27 17:19:42 +03003644 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003645 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003646 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003647
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003649 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003650 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003651
Eric W. Biederman15e47302012-09-07 20:12:54 +00003652 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003653 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003654 nlmsg_free(msg);
3655 return -ENOBUFS;
3656 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003657
Johannes Berg4c476992010-10-04 21:36:35 +02003658 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003659}
3660
Johannes Berg77ee7c82013-02-15 00:48:33 +01003661int cfg80211_check_station_change(struct wiphy *wiphy,
3662 struct station_parameters *params,
3663 enum cfg80211_station_type statype)
3664{
3665 if (params->listen_interval != -1)
3666 return -EINVAL;
3667 if (params->aid)
3668 return -EINVAL;
3669
3670 /* When you run into this, adjust the code below for the new flag */
3671 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3672
3673 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003674 case CFG80211_STA_MESH_PEER_KERNEL:
3675 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003676 /*
3677 * No ignoring the TDLS flag here -- the userspace mesh
3678 * code doesn't have the bug of including TDLS in the
3679 * mask everywhere.
3680 */
3681 if (params->sta_flags_mask &
3682 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3683 BIT(NL80211_STA_FLAG_MFP) |
3684 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3685 return -EINVAL;
3686 break;
3687 case CFG80211_STA_TDLS_PEER_SETUP:
3688 case CFG80211_STA_TDLS_PEER_ACTIVE:
3689 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3690 return -EINVAL;
3691 /* ignore since it can't change */
3692 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3693 break;
3694 default:
3695 /* disallow mesh-specific things */
3696 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3697 return -EINVAL;
3698 if (params->local_pm)
3699 return -EINVAL;
3700 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3701 return -EINVAL;
3702 }
3703
3704 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3705 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3706 /* TDLS can't be set, ... */
3707 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3708 return -EINVAL;
3709 /*
3710 * ... but don't bother the driver with it. This works around
3711 * a hostapd/wpa_supplicant issue -- it always includes the
3712 * TLDS_PEER flag in the mask even for AP mode.
3713 */
3714 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3715 }
3716
3717 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3718 /* reject other things that can't change */
3719 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3720 return -EINVAL;
3721 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3722 return -EINVAL;
3723 if (params->supported_rates)
3724 return -EINVAL;
3725 if (params->ext_capab || params->ht_capa || params->vht_capa)
3726 return -EINVAL;
3727 }
3728
3729 if (statype != CFG80211_STA_AP_CLIENT) {
3730 if (params->vlan)
3731 return -EINVAL;
3732 }
3733
3734 switch (statype) {
3735 case CFG80211_STA_AP_MLME_CLIENT:
3736 /* Use this only for authorizing/unauthorizing a station */
3737 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3738 return -EOPNOTSUPP;
3739 break;
3740 case CFG80211_STA_AP_CLIENT:
3741 /* accept only the listed bits */
3742 if (params->sta_flags_mask &
3743 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3744 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3745 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3746 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3747 BIT(NL80211_STA_FLAG_WME) |
3748 BIT(NL80211_STA_FLAG_MFP)))
3749 return -EINVAL;
3750
3751 /* but authenticated/associated only if driver handles it */
3752 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3753 params->sta_flags_mask &
3754 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3755 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3756 return -EINVAL;
3757 break;
3758 case CFG80211_STA_IBSS:
3759 case CFG80211_STA_AP_STA:
3760 /* reject any changes other than AUTHORIZED */
3761 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3762 return -EINVAL;
3763 break;
3764 case CFG80211_STA_TDLS_PEER_SETUP:
3765 /* reject any changes other than AUTHORIZED or WME */
3766 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3767 BIT(NL80211_STA_FLAG_WME)))
3768 return -EINVAL;
3769 /* force (at least) rates when authorizing */
3770 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3771 !params->supported_rates)
3772 return -EINVAL;
3773 break;
3774 case CFG80211_STA_TDLS_PEER_ACTIVE:
3775 /* reject any changes */
3776 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003777 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003778 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3779 return -EINVAL;
3780 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003781 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003782 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3783 return -EINVAL;
3784 break;
3785 }
3786
3787 return 0;
3788}
3789EXPORT_SYMBOL(cfg80211_check_station_change);
3790
Johannes Berg5727ef12007-12-19 02:03:34 +01003791/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003792 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003793 */
Johannes Berg80b99892011-11-18 16:23:01 +01003794static struct net_device *get_vlan(struct genl_info *info,
3795 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003796{
Johannes Berg463d0182009-07-14 00:33:35 +02003797 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003798 struct net_device *v;
3799 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003800
Johannes Berg80b99892011-11-18 16:23:01 +01003801 if (!vlanattr)
3802 return NULL;
3803
3804 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3805 if (!v)
3806 return ERR_PTR(-ENODEV);
3807
3808 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3809 ret = -EINVAL;
3810 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003811 }
Johannes Berg80b99892011-11-18 16:23:01 +01003812
Johannes Berg77ee7c82013-02-15 00:48:33 +01003813 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3814 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3815 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3816 ret = -EINVAL;
3817 goto error;
3818 }
3819
Johannes Berg80b99892011-11-18 16:23:01 +01003820 if (!netif_running(v)) {
3821 ret = -ENETDOWN;
3822 goto error;
3823 }
3824
3825 return v;
3826 error:
3827 dev_put(v);
3828 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003829}
3830
Jouni Malinendf881292013-02-14 21:10:54 +02003831static struct nla_policy
3832nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3833 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3834 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3835};
3836
Johannes Bergff276692013-02-15 00:09:01 +01003837static int nl80211_parse_sta_wme(struct genl_info *info,
3838 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003839{
Jouni Malinendf881292013-02-14 21:10:54 +02003840 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3841 struct nlattr *nla;
3842 int err;
3843
Jouni Malinendf881292013-02-14 21:10:54 +02003844 /* parse WME attributes if present */
3845 if (!info->attrs[NL80211_ATTR_STA_WME])
3846 return 0;
3847
3848 nla = info->attrs[NL80211_ATTR_STA_WME];
3849 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3850 nl80211_sta_wme_policy);
3851 if (err)
3852 return err;
3853
3854 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3855 params->uapsd_queues = nla_get_u8(
3856 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3857 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3858 return -EINVAL;
3859
3860 if (tb[NL80211_STA_WME_MAX_SP])
3861 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3862
3863 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3864 return -EINVAL;
3865
3866 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3867
3868 return 0;
3869}
3870
Johannes Bergff276692013-02-15 00:09:01 +01003871static int nl80211_set_station_tdls(struct genl_info *info,
3872 struct station_parameters *params)
3873{
3874 /* Dummy STA entry gets updated once the peer capabilities are known */
3875 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3876 params->ht_capa =
3877 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3878 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3879 params->vht_capa =
3880 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3881
3882 return nl80211_parse_sta_wme(info, params);
3883}
3884
Johannes Berg5727ef12007-12-19 02:03:34 +01003885static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3886{
Johannes Berg4c476992010-10-04 21:36:35 +02003887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003888 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003889 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003890 u8 *mac_addr;
3891 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003892
3893 memset(&params, 0, sizeof(params));
3894
3895 params.listen_interval = -1;
3896
Johannes Berg77ee7c82013-02-15 00:48:33 +01003897 if (!rdev->ops->change_station)
3898 return -EOPNOTSUPP;
3899
Johannes Berg5727ef12007-12-19 02:03:34 +01003900 if (info->attrs[NL80211_ATTR_STA_AID])
3901 return -EINVAL;
3902
3903 if (!info->attrs[NL80211_ATTR_MAC])
3904 return -EINVAL;
3905
3906 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3907
3908 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3909 params.supported_rates =
3910 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3911 params.supported_rates_len =
3912 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3913 }
3914
Jouni Malinen9d62a982013-02-14 21:10:13 +02003915 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3916 params.capability =
3917 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3918 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3919 }
3920
3921 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3922 params.ext_capab =
3923 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3924 params.ext_capab_len =
3925 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3926 }
3927
Jouni Malinendf881292013-02-14 21:10:54 +02003928 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003929 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03003930
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003931 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003932 return -EINVAL;
3933
Johannes Bergf8bacc22013-02-14 23:27:01 +01003934 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003935 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003936 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3937 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3938 return -EINVAL;
3939 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003940
Johannes Bergf8bacc22013-02-14 23:27:01 +01003941 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003942 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003943 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3944 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3945 return -EINVAL;
3946 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3947 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003948
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003949 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3950 enum nl80211_mesh_power_mode pm = nla_get_u32(
3951 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3952
3953 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3954 pm > NL80211_MESH_POWER_MAX)
3955 return -EINVAL;
3956
3957 params.local_pm = pm;
3958 }
3959
Johannes Berg77ee7c82013-02-15 00:48:33 +01003960 /* Include parameters for TDLS peer (will check later) */
3961 err = nl80211_set_station_tdls(info, &params);
3962 if (err)
3963 return err;
3964
3965 params.vlan = get_vlan(info, rdev);
3966 if (IS_ERR(params.vlan))
3967 return PTR_ERR(params.vlan);
3968
Johannes Berga97f4422009-06-18 17:23:43 +02003969 switch (dev->ieee80211_ptr->iftype) {
3970 case NL80211_IFTYPE_AP:
3971 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003972 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003973 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003974 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003975 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003976 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003977 break;
3978 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003979 err = -EOPNOTSUPP;
3980 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003981 }
3982
Johannes Berg77ee7c82013-02-15 00:48:33 +01003983 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003984 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003985
Johannes Berg77ee7c82013-02-15 00:48:33 +01003986 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003987 if (params.vlan)
3988 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003989
Johannes Berg5727ef12007-12-19 02:03:34 +01003990 return err;
3991}
3992
3993static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3994{
Johannes Berg4c476992010-10-04 21:36:35 +02003995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003996 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003997 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003998 struct station_parameters params;
3999 u8 *mac_addr = NULL;
4000
4001 memset(&params, 0, sizeof(params));
4002
Johannes Berg984c3112013-02-14 23:43:25 +01004003 if (!rdev->ops->add_station)
4004 return -EOPNOTSUPP;
4005
Johannes Berg5727ef12007-12-19 02:03:34 +01004006 if (!info->attrs[NL80211_ATTR_MAC])
4007 return -EINVAL;
4008
Johannes Berg5727ef12007-12-19 02:03:34 +01004009 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4010 return -EINVAL;
4011
4012 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4013 return -EINVAL;
4014
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004015 if (!info->attrs[NL80211_ATTR_STA_AID])
4016 return -EINVAL;
4017
Johannes Berg5727ef12007-12-19 02:03:34 +01004018 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4019 params.supported_rates =
4020 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4021 params.supported_rates_len =
4022 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4023 params.listen_interval =
4024 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004025
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004026 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4027 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4028 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004029
Jouni Malinen9d62a982013-02-14 21:10:13 +02004030 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4031 params.capability =
4032 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4033 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4034 }
4035
4036 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4037 params.ext_capab =
4038 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4039 params.ext_capab_len =
4040 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4041 }
4042
Jouni Malinen36aedc902008-08-25 11:58:58 +03004043 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4044 params.ht_capa =
4045 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004046
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004047 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4048 params.vht_capa =
4049 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4050
Johannes Bergf8bacc22013-02-14 23:27:01 +01004051 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004052 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004053 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4054 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4055 return -EINVAL;
4056 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004057
Johannes Bergff276692013-02-15 00:09:01 +01004058 err = nl80211_parse_sta_wme(info, &params);
4059 if (err)
4060 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004061
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004062 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004063 return -EINVAL;
4064
Johannes Berg77ee7c82013-02-15 00:48:33 +01004065 /* When you run into this, adjust the code below for the new flag */
4066 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4067
Johannes Bergbdd90d52011-12-14 12:20:27 +01004068 switch (dev->ieee80211_ptr->iftype) {
4069 case NL80211_IFTYPE_AP:
4070 case NL80211_IFTYPE_AP_VLAN:
4071 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004072 /* ignore WME attributes if iface/sta is not capable */
4073 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4074 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4075 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004076
Johannes Bergbdd90d52011-12-14 12:20:27 +01004077 /* TDLS peers cannot be added */
4078 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004079 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004080 /* but don't bother the driver with it */
4081 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004082
Johannes Bergd582cff2012-10-26 17:53:44 +02004083 /* allow authenticated/associated only if driver handles it */
4084 if (!(rdev->wiphy.features &
4085 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4086 params.sta_flags_mask &
4087 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4088 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4089 return -EINVAL;
4090
Johannes Bergbdd90d52011-12-14 12:20:27 +01004091 /* must be last in here for error handling */
4092 params.vlan = get_vlan(info, rdev);
4093 if (IS_ERR(params.vlan))
4094 return PTR_ERR(params.vlan);
4095 break;
4096 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004097 /* ignore uAPSD data */
4098 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4099
Johannes Bergd582cff2012-10-26 17:53:44 +02004100 /* associated is disallowed */
4101 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4102 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004103 /* TDLS peers cannot be added */
4104 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004105 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004106 break;
4107 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004108 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004109 /* ignore uAPSD data */
4110 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4111
Johannes Berg77ee7c82013-02-15 00:48:33 +01004112 /* these are disallowed */
4113 if (params.sta_flags_mask &
4114 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4115 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004116 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004117 /* Only TDLS peers can be added */
4118 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4119 return -EINVAL;
4120 /* Can only add if TDLS ... */
4121 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4122 return -EOPNOTSUPP;
4123 /* ... with external setup is supported */
4124 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4125 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004126 /*
4127 * Older wpa_supplicant versions always mark the TDLS peer
4128 * as authorized, but it shouldn't yet be.
4129 */
4130 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004131 break;
4132 default:
4133 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004134 }
4135
Johannes Bergbdd90d52011-12-14 12:20:27 +01004136 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004137
Hila Gonene35e4d22012-06-27 17:19:42 +03004138 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004139
Johannes Berg5727ef12007-12-19 02:03:34 +01004140 if (params.vlan)
4141 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004142 return err;
4143}
4144
4145static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4146{
Johannes Berg4c476992010-10-04 21:36:35 +02004147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4148 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004149 u8 *mac_addr = NULL;
4150
4151 if (info->attrs[NL80211_ATTR_MAC])
4152 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4153
Johannes Berge80cf852009-05-11 14:43:13 +02004154 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004155 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004156 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004157 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4158 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004159
Johannes Berg4c476992010-10-04 21:36:35 +02004160 if (!rdev->ops->del_station)
4161 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004162
Hila Gonene35e4d22012-06-27 17:19:42 +03004163 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004164}
4165
Eric W. Biederman15e47302012-09-07 20:12:54 +00004166static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004167 int flags, struct net_device *dev,
4168 u8 *dst, u8 *next_hop,
4169 struct mpath_info *pinfo)
4170{
4171 void *hdr;
4172 struct nlattr *pinfoattr;
4173
Eric W. Biederman15e47302012-09-07 20:12:54 +00004174 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004175 if (!hdr)
4176 return -1;
4177
David S. Miller9360ffd2012-03-29 04:41:26 -04004178 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4179 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4180 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4181 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4182 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004183
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004184 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4185 if (!pinfoattr)
4186 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004187 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4188 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4189 pinfo->frame_qlen))
4190 goto nla_put_failure;
4191 if (((pinfo->filled & MPATH_INFO_SN) &&
4192 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4193 ((pinfo->filled & MPATH_INFO_METRIC) &&
4194 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4195 pinfo->metric)) ||
4196 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4197 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4198 pinfo->exptime)) ||
4199 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4200 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4201 pinfo->flags)) ||
4202 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4203 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4204 pinfo->discovery_timeout)) ||
4205 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4206 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4207 pinfo->discovery_retries)))
4208 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004209
4210 nla_nest_end(msg, pinfoattr);
4211
4212 return genlmsg_end(msg, hdr);
4213
4214 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004215 genlmsg_cancel(msg, hdr);
4216 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004217}
4218
4219static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004220 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004221{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004222 struct mpath_info pinfo;
4223 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004224 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004225 u8 dst[ETH_ALEN];
4226 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004227 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004228 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004229
Johannes Berg97990a02013-04-19 01:02:55 +02004230 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004231 if (err)
4232 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004233
4234 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004235 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004236 goto out_err;
4237 }
4238
Johannes Berg97990a02013-04-19 01:02:55 +02004239 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004240 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004241 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004242 }
4243
Johannes Bergbba95fe2008-07-29 13:22:51 +02004244 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004245 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4246 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004247 if (err == -ENOENT)
4248 break;
4249 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004250 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004251
Eric W. Biederman15e47302012-09-07 20:12:54 +00004252 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004253 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004254 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004255 &pinfo) < 0)
4256 goto out;
4257
4258 path_idx++;
4259 }
4260
4261
4262 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004263 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004264 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004265 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004266 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004267 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004268}
4269
4270static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4271{
Johannes Berg4c476992010-10-04 21:36:35 +02004272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004273 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004274 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004275 struct mpath_info pinfo;
4276 struct sk_buff *msg;
4277 u8 *dst = NULL;
4278 u8 next_hop[ETH_ALEN];
4279
4280 memset(&pinfo, 0, sizeof(pinfo));
4281
4282 if (!info->attrs[NL80211_ATTR_MAC])
4283 return -EINVAL;
4284
4285 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4286
Johannes Berg4c476992010-10-04 21:36:35 +02004287 if (!rdev->ops->get_mpath)
4288 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004289
Johannes Berg4c476992010-10-04 21:36:35 +02004290 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4291 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004292
Hila Gonene35e4d22012-06-27 17:19:42 +03004293 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004294 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004295 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004296
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004298 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004299 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004300
Eric W. Biederman15e47302012-09-07 20:12:54 +00004301 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004302 dev, dst, next_hop, &pinfo) < 0) {
4303 nlmsg_free(msg);
4304 return -ENOBUFS;
4305 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004306
Johannes Berg4c476992010-10-04 21:36:35 +02004307 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004308}
4309
4310static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4311{
Johannes Berg4c476992010-10-04 21:36:35 +02004312 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4313 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004314 u8 *dst = NULL;
4315 u8 *next_hop = NULL;
4316
4317 if (!info->attrs[NL80211_ATTR_MAC])
4318 return -EINVAL;
4319
4320 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4321 return -EINVAL;
4322
4323 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4324 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4325
Johannes Berg4c476992010-10-04 21:36:35 +02004326 if (!rdev->ops->change_mpath)
4327 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004328
Johannes Berg4c476992010-10-04 21:36:35 +02004329 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4330 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331
Hila Gonene35e4d22012-06-27 17:19:42 +03004332 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004333}
Johannes Berg4c476992010-10-04 21:36:35 +02004334
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4336{
Johannes Berg4c476992010-10-04 21:36:35 +02004337 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4338 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004339 u8 *dst = NULL;
4340 u8 *next_hop = NULL;
4341
4342 if (!info->attrs[NL80211_ATTR_MAC])
4343 return -EINVAL;
4344
4345 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4346 return -EINVAL;
4347
4348 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4349 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4350
Johannes Berg4c476992010-10-04 21:36:35 +02004351 if (!rdev->ops->add_mpath)
4352 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004353
Johannes Berg4c476992010-10-04 21:36:35 +02004354 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4355 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004356
Hila Gonene35e4d22012-06-27 17:19:42 +03004357 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004358}
4359
4360static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4361{
Johannes Berg4c476992010-10-04 21:36:35 +02004362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4363 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004364 u8 *dst = NULL;
4365
4366 if (info->attrs[NL80211_ATTR_MAC])
4367 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4368
Johannes Berg4c476992010-10-04 21:36:35 +02004369 if (!rdev->ops->del_mpath)
4370 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004371
Hila Gonene35e4d22012-06-27 17:19:42 +03004372 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004373}
4374
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004375static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4376{
Johannes Berg4c476992010-10-04 21:36:35 +02004377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4378 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004379 struct bss_parameters params;
4380
4381 memset(&params, 0, sizeof(params));
4382 /* default to not changing parameters */
4383 params.use_cts_prot = -1;
4384 params.use_short_preamble = -1;
4385 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004386 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004387 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004388 params.p2p_ctwindow = -1;
4389 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004390
4391 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4392 params.use_cts_prot =
4393 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4394 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4395 params.use_short_preamble =
4396 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4397 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4398 params.use_short_slot_time =
4399 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004400 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4401 params.basic_rates =
4402 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4403 params.basic_rates_len =
4404 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4405 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004406 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4407 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004408 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4409 params.ht_opmode =
4410 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004411
Johannes Berg53cabad2012-11-14 15:17:28 +01004412 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4413 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4414 return -EINVAL;
4415 params.p2p_ctwindow =
4416 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4417 if (params.p2p_ctwindow < 0)
4418 return -EINVAL;
4419 if (params.p2p_ctwindow != 0 &&
4420 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4421 return -EINVAL;
4422 }
4423
4424 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4425 u8 tmp;
4426
4427 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4428 return -EINVAL;
4429 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4430 if (tmp > 1)
4431 return -EINVAL;
4432 params.p2p_opp_ps = tmp;
4433 if (params.p2p_opp_ps &&
4434 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4435 return -EINVAL;
4436 }
4437
Johannes Berg4c476992010-10-04 21:36:35 +02004438 if (!rdev->ops->change_bss)
4439 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004440
Johannes Berg074ac8d2010-09-16 14:58:22 +02004441 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004442 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4443 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004444
Hila Gonene35e4d22012-06-27 17:19:42 +03004445 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004446}
4447
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004448static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004449 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4450 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4451 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4452 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4453 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4454 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4455};
4456
4457static int parse_reg_rule(struct nlattr *tb[],
4458 struct ieee80211_reg_rule *reg_rule)
4459{
4460 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4461 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4462
4463 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4464 return -EINVAL;
4465 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4466 return -EINVAL;
4467 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4468 return -EINVAL;
4469 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4470 return -EINVAL;
4471 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4472 return -EINVAL;
4473
4474 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4475
4476 freq_range->start_freq_khz =
4477 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4478 freq_range->end_freq_khz =
4479 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4480 freq_range->max_bandwidth_khz =
4481 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4482
4483 power_rule->max_eirp =
4484 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4485
4486 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4487 power_rule->max_antenna_gain =
4488 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4489
4490 return 0;
4491}
4492
4493static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4494{
4495 int r;
4496 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004497 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004498
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004499 /*
4500 * You should only get this when cfg80211 hasn't yet initialized
4501 * completely when built-in to the kernel right between the time
4502 * window between nl80211_init() and regulatory_init(), if that is
4503 * even possible.
4504 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004505 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004506 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004507
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004508 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4509 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004510
4511 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4512
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004513 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4514 user_reg_hint_type =
4515 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4516 else
4517 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4518
4519 switch (user_reg_hint_type) {
4520 case NL80211_USER_REG_HINT_USER:
4521 case NL80211_USER_REG_HINT_CELL_BASE:
4522 break;
4523 default:
4524 return -EINVAL;
4525 }
4526
4527 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004528
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004529 return r;
4530}
4531
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004532static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004533 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004534{
Johannes Berg4c476992010-10-04 21:36:35 +02004535 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004536 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004537 struct wireless_dev *wdev = dev->ieee80211_ptr;
4538 struct mesh_config cur_params;
4539 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004540 void *hdr;
4541 struct nlattr *pinfoattr;
4542 struct sk_buff *msg;
4543
Johannes Berg29cbe682010-12-03 09:20:44 +01004544 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4545 return -EOPNOTSUPP;
4546
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004547 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004548 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004549
Johannes Berg29cbe682010-12-03 09:20:44 +01004550 wdev_lock(wdev);
4551 /* If not connected, get default parameters */
4552 if (!wdev->mesh_id_len)
4553 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4554 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004555 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004556 wdev_unlock(wdev);
4557
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004558 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004559 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004560
4561 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004563 if (!msg)
4564 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004565 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004566 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004567 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004568 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004569 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004570 if (!pinfoattr)
4571 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004572 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4573 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4574 cur_params.dot11MeshRetryTimeout) ||
4575 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4576 cur_params.dot11MeshConfirmTimeout) ||
4577 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4578 cur_params.dot11MeshHoldingTimeout) ||
4579 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4580 cur_params.dot11MeshMaxPeerLinks) ||
4581 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4582 cur_params.dot11MeshMaxRetries) ||
4583 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4584 cur_params.dot11MeshTTL) ||
4585 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4586 cur_params.element_ttl) ||
4587 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4588 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004589 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4590 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004591 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4592 cur_params.dot11MeshHWMPmaxPREQretries) ||
4593 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4594 cur_params.path_refresh_time) ||
4595 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4596 cur_params.min_discovery_timeout) ||
4597 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4598 cur_params.dot11MeshHWMPactivePathTimeout) ||
4599 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4600 cur_params.dot11MeshHWMPpreqMinInterval) ||
4601 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4602 cur_params.dot11MeshHWMPperrMinInterval) ||
4603 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4604 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4605 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4606 cur_params.dot11MeshHWMPRootMode) ||
4607 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4608 cur_params.dot11MeshHWMPRannInterval) ||
4609 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4610 cur_params.dot11MeshGateAnnouncementProtocol) ||
4611 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4612 cur_params.dot11MeshForwarding) ||
4613 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004614 cur_params.rssi_threshold) ||
4615 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004616 cur_params.ht_opmode) ||
4617 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4618 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4619 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004620 cur_params.dot11MeshHWMProotInterval) ||
4621 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004622 cur_params.dot11MeshHWMPconfirmationInterval) ||
4623 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4624 cur_params.power_mode) ||
4625 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4626 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004627 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004628 nla_nest_end(msg, pinfoattr);
4629 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004630 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004631
Johannes Berg3b858752009-03-12 09:55:09 +01004632 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004633 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004634 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004635 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004636 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004637}
4638
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004639static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004640 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4641 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4642 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4643 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4644 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4645 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004646 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004647 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004648 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004649 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4650 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4651 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4652 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4653 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004654 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004655 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004656 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004657 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004658 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004659 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004660 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4661 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004662 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4663 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004664 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004665 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4666 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004667};
4668
Javier Cardonac80d5452010-12-16 17:37:49 -08004669static const struct nla_policy
4670 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004671 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004672 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4673 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004674 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004675 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004676 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004677 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004678 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004679 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004680};
4681
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004682static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004683 struct mesh_config *cfg,
4684 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004685{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004686 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004687 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004688
Marco Porschea54fba2013-01-07 16:04:48 +01004689#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4690do { \
4691 if (tb[attr]) { \
4692 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4693 return -EINVAL; \
4694 cfg->param = fn(tb[attr]); \
4695 mask |= (1 << (attr - 1)); \
4696 } \
4697} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004698
4699
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004700 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004701 return -EINVAL;
4702 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004703 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004704 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004705 return -EINVAL;
4706
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004707 /* This makes sure that there aren't more than 32 mesh config
4708 * parameters (otherwise our bitfield scheme would not work.) */
4709 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4710
4711 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004712 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004713 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4714 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004716 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4717 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004718 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004719 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4720 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004721 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004722 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4723 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004724 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004725 mask, NL80211_MESHCONF_MAX_RETRIES,
4726 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004727 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004728 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004729 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004730 mask, NL80211_MESHCONF_ELEMENT_TTL,
4731 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004732 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004733 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4734 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4736 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004737 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4738 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004739 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004740 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4741 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004742 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004743 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4744 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004746 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4747 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004748 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4749 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004750 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4751 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004752 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004753 1, 65535, mask,
4754 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004755 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004756 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004757 1, 65535, mask,
4758 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004759 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004760 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004761 dot11MeshHWMPnetDiameterTraversalTime,
4762 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004763 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4764 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004765 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4766 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4767 nla_get_u8);
4768 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4769 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004770 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004771 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004772 dot11MeshGateAnnouncementProtocol, 0, 1,
4773 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004774 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004775 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004776 mask, NL80211_MESHCONF_FORWARDING,
4777 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004778 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004779 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4780 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004781 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004782 mask, NL80211_MESHCONF_HT_OPMODE,
4783 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004784 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004785 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004786 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4787 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004788 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004789 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4790 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004791 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004792 dot11MeshHWMPconfirmationInterval,
4793 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004794 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4795 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004796 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4797 NL80211_MESH_POWER_ACTIVE,
4798 NL80211_MESH_POWER_MAX,
4799 mask, NL80211_MESHCONF_POWER_MODE,
4800 nla_get_u32);
4801 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4802 0, 65535, mask,
4803 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004804 if (mask_out)
4805 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004806
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004807 return 0;
4808
4809#undef FILL_IN_MESH_PARAM_IF_SET
4810}
4811
Javier Cardonac80d5452010-12-16 17:37:49 -08004812static int nl80211_parse_mesh_setup(struct genl_info *info,
4813 struct mesh_setup *setup)
4814{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004815 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004816 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4817
4818 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4819 return -EINVAL;
4820 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4821 info->attrs[NL80211_ATTR_MESH_SETUP],
4822 nl80211_mesh_setup_params_policy))
4823 return -EINVAL;
4824
Javier Cardonad299a1f2012-03-31 11:31:33 -07004825 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4826 setup->sync_method =
4827 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4828 IEEE80211_SYNC_METHOD_VENDOR :
4829 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4830
Javier Cardonac80d5452010-12-16 17:37:49 -08004831 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4832 setup->path_sel_proto =
4833 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4834 IEEE80211_PATH_PROTOCOL_VENDOR :
4835 IEEE80211_PATH_PROTOCOL_HWMP;
4836
4837 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4838 setup->path_metric =
4839 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4840 IEEE80211_PATH_METRIC_VENDOR :
4841 IEEE80211_PATH_METRIC_AIRTIME;
4842
Javier Cardona581a8b02011-04-07 15:08:27 -07004843
4844 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004845 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004846 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004847 if (!is_valid_ie_attr(ieattr))
4848 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004849 setup->ie = nla_data(ieattr);
4850 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004851 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004852 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4853 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4854 return -EINVAL;
4855 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004856 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4857 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004858 if (setup->is_secure)
4859 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004860
Colleen Twitty6e16d902013-05-08 11:45:59 -07004861 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
4862 if (!setup->user_mpm)
4863 return -EINVAL;
4864 setup->auth_id =
4865 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
4866 }
4867
Javier Cardonac80d5452010-12-16 17:37:49 -08004868 return 0;
4869}
4870
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004871static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004872 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004873{
4874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4875 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004876 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004877 struct mesh_config cfg;
4878 u32 mask;
4879 int err;
4880
Johannes Berg29cbe682010-12-03 09:20:44 +01004881 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4882 return -EOPNOTSUPP;
4883
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004884 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004885 return -EOPNOTSUPP;
4886
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004887 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004888 if (err)
4889 return err;
4890
Johannes Berg29cbe682010-12-03 09:20:44 +01004891 wdev_lock(wdev);
4892 if (!wdev->mesh_id_len)
4893 err = -ENOLINK;
4894
4895 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004896 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004897
4898 wdev_unlock(wdev);
4899
4900 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004901}
4902
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004903static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4904{
Johannes Berg458f4f92012-12-06 15:47:38 +01004905 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004906 struct sk_buff *msg;
4907 void *hdr = NULL;
4908 struct nlattr *nl_reg_rules;
4909 unsigned int i;
4910 int err = -EINVAL;
4911
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004912 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004913
4914 if (!cfg80211_regdomain)
4915 goto out;
4916
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004917 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004918 if (!msg) {
4919 err = -ENOBUFS;
4920 goto out;
4921 }
4922
Eric W. Biederman15e47302012-09-07 20:12:54 +00004923 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004924 NL80211_CMD_GET_REG);
4925 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004926 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004927
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004928 if (reg_last_request_cell_base() &&
4929 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4930 NL80211_USER_REG_HINT_CELL_BASE))
4931 goto nla_put_failure;
4932
Johannes Berg458f4f92012-12-06 15:47:38 +01004933 rcu_read_lock();
4934 regdom = rcu_dereference(cfg80211_regdomain);
4935
4936 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4937 (regdom->dfs_region &&
4938 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4939 goto nla_put_failure_rcu;
4940
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004941 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4942 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004943 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004944
Johannes Berg458f4f92012-12-06 15:47:38 +01004945 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004946 struct nlattr *nl_reg_rule;
4947 const struct ieee80211_reg_rule *reg_rule;
4948 const struct ieee80211_freq_range *freq_range;
4949 const struct ieee80211_power_rule *power_rule;
4950
Johannes Berg458f4f92012-12-06 15:47:38 +01004951 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004952 freq_range = &reg_rule->freq_range;
4953 power_rule = &reg_rule->power_rule;
4954
4955 nl_reg_rule = nla_nest_start(msg, i);
4956 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004957 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004958
David S. Miller9360ffd2012-03-29 04:41:26 -04004959 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4960 reg_rule->flags) ||
4961 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4962 freq_range->start_freq_khz) ||
4963 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4964 freq_range->end_freq_khz) ||
4965 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4966 freq_range->max_bandwidth_khz) ||
4967 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4968 power_rule->max_antenna_gain) ||
4969 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4970 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004971 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004972
4973 nla_nest_end(msg, nl_reg_rule);
4974 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004975 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004976
4977 nla_nest_end(msg, nl_reg_rules);
4978
4979 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004980 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004981 goto out;
4982
Johannes Berg458f4f92012-12-06 15:47:38 +01004983nla_put_failure_rcu:
4984 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004985nla_put_failure:
4986 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004987put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004988 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004989 err = -EMSGSIZE;
4990out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004991 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004992 return err;
4993}
4994
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004995static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4996{
4997 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4998 struct nlattr *nl_reg_rule;
4999 char *alpha2 = NULL;
5000 int rem_reg_rules = 0, r = 0;
5001 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005002 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005003 struct ieee80211_regdomain *rd = NULL;
5004
5005 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5006 return -EINVAL;
5007
5008 if (!info->attrs[NL80211_ATTR_REG_RULES])
5009 return -EINVAL;
5010
5011 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5012
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005013 if (info->attrs[NL80211_ATTR_DFS_REGION])
5014 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5015
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005016 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005017 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005018 num_rules++;
5019 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005020 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005021 }
5022
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005023 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005024 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005025
5026 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005027 if (!rd)
5028 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005029
5030 rd->n_reg_rules = num_rules;
5031 rd->alpha2[0] = alpha2[0];
5032 rd->alpha2[1] = alpha2[1];
5033
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005034 /*
5035 * Disable DFS master mode if the DFS region was
5036 * not supported or known on this kernel.
5037 */
5038 if (reg_supported_dfs_region(dfs_region))
5039 rd->dfs_region = dfs_region;
5040
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005041 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005042 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005043 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005044 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5045 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005046 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5047 if (r)
5048 goto bad_reg;
5049
5050 rule_idx++;
5051
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005052 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5053 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005054 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005055 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005056 }
5057
Johannes Berg6913b492012-12-04 00:48:59 +01005058 mutex_lock(&cfg80211_mutex);
5059
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005060 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005061 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005062 rd = NULL;
Johannes Berg6913b492012-12-04 00:48:59 +01005063 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005064
Johannes Bergd2372b32008-10-24 20:32:20 +02005065 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005066 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005067 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005068}
5069
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005070static int validate_scan_freqs(struct nlattr *freqs)
5071{
5072 struct nlattr *attr1, *attr2;
5073 int n_channels = 0, tmp1, tmp2;
5074
5075 nla_for_each_nested(attr1, freqs, tmp1) {
5076 n_channels++;
5077 /*
5078 * Some hardware has a limited channel list for
5079 * scanning, and it is pretty much nonsensical
5080 * to scan for a channel twice, so disallow that
5081 * and don't require drivers to check that the
5082 * channel list they get isn't longer than what
5083 * they can scan, as long as they can scan all
5084 * the channels they registered at once.
5085 */
5086 nla_for_each_nested(attr2, freqs, tmp2)
5087 if (attr1 != attr2 &&
5088 nla_get_u32(attr1) == nla_get_u32(attr2))
5089 return 0;
5090 }
5091
5092 return n_channels;
5093}
5094
Johannes Berg2a519312009-02-10 21:25:55 +01005095static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5096{
Johannes Berg4c476992010-10-04 21:36:35 +02005097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005098 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005099 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005100 struct nlattr *attr;
5101 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005102 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005103 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005104
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005105 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5106 return -EINVAL;
5107
Johannes Berg79c97e92009-07-07 03:56:12 +02005108 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005109
Johannes Berg4c476992010-10-04 21:36:35 +02005110 if (!rdev->ops->scan)
5111 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005112
Johannes Bergf9f47522013-03-19 15:04:07 +01005113 mutex_lock(&rdev->sched_scan_mtx);
5114 if (rdev->scan_req) {
5115 err = -EBUSY;
5116 goto unlock;
5117 }
Johannes Berg2a519312009-02-10 21:25:55 +01005118
5119 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005120 n_channels = validate_scan_freqs(
5121 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005122 if (!n_channels) {
5123 err = -EINVAL;
5124 goto unlock;
5125 }
Johannes Berg2a519312009-02-10 21:25:55 +01005126 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005127 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005128 n_channels = 0;
5129
Johannes Berg2a519312009-02-10 21:25:55 +01005130 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5131 if (wiphy->bands[band])
5132 n_channels += wiphy->bands[band]->n_channels;
5133 }
5134
5135 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5136 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5137 n_ssids++;
5138
Johannes Bergf9f47522013-03-19 15:04:07 +01005139 if (n_ssids > wiphy->max_scan_ssids) {
5140 err = -EINVAL;
5141 goto unlock;
5142 }
Johannes Berg2a519312009-02-10 21:25:55 +01005143
Jouni Malinen70692ad2009-02-16 19:39:13 +02005144 if (info->attrs[NL80211_ATTR_IE])
5145 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5146 else
5147 ie_len = 0;
5148
Johannes Bergf9f47522013-03-19 15:04:07 +01005149 if (ie_len > wiphy->max_scan_ie_len) {
5150 err = -EINVAL;
5151 goto unlock;
5152 }
Johannes Berg18a83652009-03-31 12:12:05 +02005153
Johannes Berg2a519312009-02-10 21:25:55 +01005154 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005155 + sizeof(*request->ssids) * n_ssids
5156 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005157 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005158 if (!request) {
5159 err = -ENOMEM;
5160 goto unlock;
5161 }
Johannes Berg2a519312009-02-10 21:25:55 +01005162
Johannes Berg2a519312009-02-10 21:25:55 +01005163 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005164 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005165 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005166 if (ie_len) {
5167 if (request->ssids)
5168 request->ie = (void *)(request->ssids + n_ssids);
5169 else
5170 request->ie = (void *)(request->channels + n_channels);
5171 }
Johannes Berg2a519312009-02-10 21:25:55 +01005172
Johannes Berg584991d2009-11-02 13:32:03 +01005173 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005174 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5175 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005176 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005177 struct ieee80211_channel *chan;
5178
5179 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5180
5181 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005182 err = -EINVAL;
5183 goto out_free;
5184 }
Johannes Berg584991d2009-11-02 13:32:03 +01005185
5186 /* ignore disabled channels */
5187 if (chan->flags & IEEE80211_CHAN_DISABLED)
5188 continue;
5189
5190 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005191 i++;
5192 }
5193 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005194 enum ieee80211_band band;
5195
Johannes Berg2a519312009-02-10 21:25:55 +01005196 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005197 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5198 int j;
5199 if (!wiphy->bands[band])
5200 continue;
5201 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005202 struct ieee80211_channel *chan;
5203
5204 chan = &wiphy->bands[band]->channels[j];
5205
5206 if (chan->flags & IEEE80211_CHAN_DISABLED)
5207 continue;
5208
5209 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005210 i++;
5211 }
5212 }
5213 }
5214
Johannes Berg584991d2009-11-02 13:32:03 +01005215 if (!i) {
5216 err = -EINVAL;
5217 goto out_free;
5218 }
5219
5220 request->n_channels = i;
5221
Johannes Berg2a519312009-02-10 21:25:55 +01005222 i = 0;
5223 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5224 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005225 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005226 err = -EINVAL;
5227 goto out_free;
5228 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005229 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005230 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005231 i++;
5232 }
5233 }
5234
Jouni Malinen70692ad2009-02-16 19:39:13 +02005235 if (info->attrs[NL80211_ATTR_IE]) {
5236 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005237 memcpy((void *)request->ie,
5238 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005239 request->ie_len);
5240 }
5241
Johannes Berg34850ab2011-07-18 18:08:35 +02005242 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005243 if (wiphy->bands[i])
5244 request->rates[i] =
5245 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005246
5247 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5248 nla_for_each_nested(attr,
5249 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5250 tmp) {
5251 enum ieee80211_band band = nla_type(attr);
5252
Dan Carpenter84404622011-07-29 11:52:18 +03005253 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005254 err = -EINVAL;
5255 goto out_free;
5256 }
5257 err = ieee80211_get_ratemask(wiphy->bands[band],
5258 nla_data(attr),
5259 nla_len(attr),
5260 &request->rates[band]);
5261 if (err)
5262 goto out_free;
5263 }
5264 }
5265
Sam Leffler46856bb2012-10-11 21:03:32 -07005266 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005267 request->flags = nla_get_u32(
5268 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005269 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5270 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5271 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5272 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005273 err = -EOPNOTSUPP;
5274 goto out_free;
5275 }
5276 }
Sam Lefflered4737712012-10-11 21:03:31 -07005277
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305278 request->no_cck =
5279 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5280
Johannes Bergfd014282012-06-18 19:17:03 +02005281 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005282 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005283 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005284
Johannes Berg79c97e92009-07-07 03:56:12 +02005285 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005286 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005287
Johannes Berg463d0182009-07-14 00:33:35 +02005288 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005289 nl80211_send_scan_start(rdev, wdev);
5290 if (wdev->netdev)
5291 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005292 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005293 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005294 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005295 kfree(request);
5296 }
Johannes Berg3b858752009-03-12 09:55:09 +01005297
Johannes Bergf9f47522013-03-19 15:04:07 +01005298 unlock:
5299 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01005300 return err;
5301}
5302
Luciano Coelho807f8a82011-05-11 17:09:35 +03005303static int nl80211_start_sched_scan(struct sk_buff *skb,
5304 struct genl_info *info)
5305{
5306 struct cfg80211_sched_scan_request *request;
5307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5308 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005309 struct nlattr *attr;
5310 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005311 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005312 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005313 enum ieee80211_band band;
5314 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005315 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005316
5317 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5318 !rdev->ops->sched_scan_start)
5319 return -EOPNOTSUPP;
5320
5321 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5322 return -EINVAL;
5323
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005324 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5325 return -EINVAL;
5326
5327 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5328 if (interval == 0)
5329 return -EINVAL;
5330
Luciano Coelho807f8a82011-05-11 17:09:35 +03005331 wiphy = &rdev->wiphy;
5332
5333 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5334 n_channels = validate_scan_freqs(
5335 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5336 if (!n_channels)
5337 return -EINVAL;
5338 } else {
5339 n_channels = 0;
5340
5341 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5342 if (wiphy->bands[band])
5343 n_channels += wiphy->bands[band]->n_channels;
5344 }
5345
5346 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5347 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5348 tmp)
5349 n_ssids++;
5350
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005351 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005352 return -EINVAL;
5353
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005354 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5355 nla_for_each_nested(attr,
5356 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5357 tmp)
5358 n_match_sets++;
5359
5360 if (n_match_sets > wiphy->max_match_sets)
5361 return -EINVAL;
5362
Luciano Coelho807f8a82011-05-11 17:09:35 +03005363 if (info->attrs[NL80211_ATTR_IE])
5364 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5365 else
5366 ie_len = 0;
5367
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005368 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005369 return -EINVAL;
5370
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005371 mutex_lock(&rdev->sched_scan_mtx);
5372
5373 if (rdev->sched_scan_req) {
5374 err = -EINPROGRESS;
5375 goto out;
5376 }
5377
Luciano Coelho807f8a82011-05-11 17:09:35 +03005378 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005379 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005380 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005381 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005382 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005383 if (!request) {
5384 err = -ENOMEM;
5385 goto out;
5386 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005387
5388 if (n_ssids)
5389 request->ssids = (void *)&request->channels[n_channels];
5390 request->n_ssids = n_ssids;
5391 if (ie_len) {
5392 if (request->ssids)
5393 request->ie = (void *)(request->ssids + n_ssids);
5394 else
5395 request->ie = (void *)(request->channels + n_channels);
5396 }
5397
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005398 if (n_match_sets) {
5399 if (request->ie)
5400 request->match_sets = (void *)(request->ie + ie_len);
5401 else if (request->ssids)
5402 request->match_sets =
5403 (void *)(request->ssids + n_ssids);
5404 else
5405 request->match_sets =
5406 (void *)(request->channels + n_channels);
5407 }
5408 request->n_match_sets = n_match_sets;
5409
Luciano Coelho807f8a82011-05-11 17:09:35 +03005410 i = 0;
5411 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5412 /* user specified, bail out if channel not found */
5413 nla_for_each_nested(attr,
5414 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5415 tmp) {
5416 struct ieee80211_channel *chan;
5417
5418 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5419
5420 if (!chan) {
5421 err = -EINVAL;
5422 goto out_free;
5423 }
5424
5425 /* ignore disabled channels */
5426 if (chan->flags & IEEE80211_CHAN_DISABLED)
5427 continue;
5428
5429 request->channels[i] = chan;
5430 i++;
5431 }
5432 } else {
5433 /* all channels */
5434 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5435 int j;
5436 if (!wiphy->bands[band])
5437 continue;
5438 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5439 struct ieee80211_channel *chan;
5440
5441 chan = &wiphy->bands[band]->channels[j];
5442
5443 if (chan->flags & IEEE80211_CHAN_DISABLED)
5444 continue;
5445
5446 request->channels[i] = chan;
5447 i++;
5448 }
5449 }
5450 }
5451
5452 if (!i) {
5453 err = -EINVAL;
5454 goto out_free;
5455 }
5456
5457 request->n_channels = i;
5458
5459 i = 0;
5460 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5461 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5462 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005463 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005464 err = -EINVAL;
5465 goto out_free;
5466 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005467 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005468 memcpy(request->ssids[i].ssid, nla_data(attr),
5469 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005470 i++;
5471 }
5472 }
5473
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005474 i = 0;
5475 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5476 nla_for_each_nested(attr,
5477 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5478 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005479 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005480
5481 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5482 nla_data(attr), nla_len(attr),
5483 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005484 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005485 if (ssid) {
5486 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5487 err = -EINVAL;
5488 goto out_free;
5489 }
5490 memcpy(request->match_sets[i].ssid.ssid,
5491 nla_data(ssid), nla_len(ssid));
5492 request->match_sets[i].ssid.ssid_len =
5493 nla_len(ssid);
5494 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005495 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5496 if (rssi)
5497 request->rssi_thold = nla_get_u32(rssi);
5498 else
5499 request->rssi_thold =
5500 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005501 i++;
5502 }
5503 }
5504
Luciano Coelho807f8a82011-05-11 17:09:35 +03005505 if (info->attrs[NL80211_ATTR_IE]) {
5506 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5507 memcpy((void *)request->ie,
5508 nla_data(info->attrs[NL80211_ATTR_IE]),
5509 request->ie_len);
5510 }
5511
Sam Leffler46856bb2012-10-11 21:03:32 -07005512 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005513 request->flags = nla_get_u32(
5514 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005515 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5516 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5517 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5518 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005519 err = -EOPNOTSUPP;
5520 goto out_free;
5521 }
5522 }
Sam Lefflered4737712012-10-11 21:03:31 -07005523
Luciano Coelho807f8a82011-05-11 17:09:35 +03005524 request->dev = dev;
5525 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005526 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005527 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005528
Hila Gonene35e4d22012-06-27 17:19:42 +03005529 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005530 if (!err) {
5531 rdev->sched_scan_req = request;
5532 nl80211_send_sched_scan(rdev, dev,
5533 NL80211_CMD_START_SCHED_SCAN);
5534 goto out;
5535 }
5536
5537out_free:
5538 kfree(request);
5539out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005540 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005541 return err;
5542}
5543
5544static int nl80211_stop_sched_scan(struct sk_buff *skb,
5545 struct genl_info *info)
5546{
5547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005548 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005549
5550 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5551 !rdev->ops->sched_scan_stop)
5552 return -EOPNOTSUPP;
5553
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005554 mutex_lock(&rdev->sched_scan_mtx);
5555 err = __cfg80211_stop_sched_scan(rdev, false);
5556 mutex_unlock(&rdev->sched_scan_mtx);
5557
5558 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005559}
5560
Simon Wunderlich04f39042013-02-08 18:16:19 +01005561static int nl80211_start_radar_detection(struct sk_buff *skb,
5562 struct genl_info *info)
5563{
5564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5565 struct net_device *dev = info->user_ptr[1];
5566 struct wireless_dev *wdev = dev->ieee80211_ptr;
5567 struct cfg80211_chan_def chandef;
5568 int err;
5569
5570 err = nl80211_parse_chandef(rdev, info, &chandef);
5571 if (err)
5572 return err;
5573
5574 if (wdev->cac_started)
5575 return -EBUSY;
5576
5577 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5578 if (err < 0)
5579 return err;
5580
5581 if (err == 0)
5582 return -EINVAL;
5583
5584 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5585 return -EINVAL;
5586
5587 if (!rdev->ops->start_radar_detection)
5588 return -EOPNOTSUPP;
5589
5590 mutex_lock(&rdev->devlist_mtx);
5591 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5592 chandef.chan, CHAN_MODE_SHARED,
5593 BIT(chandef.width));
5594 if (err)
5595 goto err_locked;
5596
5597 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5598 if (!err) {
5599 wdev->channel = chandef.chan;
5600 wdev->cac_started = true;
5601 wdev->cac_start_time = jiffies;
5602 }
5603err_locked:
5604 mutex_unlock(&rdev->devlist_mtx);
5605
5606 return err;
5607}
5608
Johannes Berg9720bb32011-06-21 09:45:33 +02005609static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5610 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005611 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005612 struct wireless_dev *wdev,
5613 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005614{
Johannes Berg48ab9052009-07-10 18:42:31 +02005615 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005616 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005617 void *hdr;
5618 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005619 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005620
5621 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005622
Eric W. Biederman15e47302012-09-07 20:12:54 +00005623 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005624 NL80211_CMD_NEW_SCAN_RESULTS);
5625 if (!hdr)
5626 return -1;
5627
Johannes Berg9720bb32011-06-21 09:45:33 +02005628 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5629
Johannes Berg97990a02013-04-19 01:02:55 +02005630 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5631 goto nla_put_failure;
5632 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005633 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5634 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005635 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5636 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005637
5638 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5639 if (!bss)
5640 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005641 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005642 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005643 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005644
5645 rcu_read_lock();
5646 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005647 if (ies) {
5648 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5649 goto fail_unlock_rcu;
5650 tsf = true;
5651 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5652 ies->len, ies->data))
5653 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005654 }
5655 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005656 if (ies) {
5657 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5658 goto fail_unlock_rcu;
5659 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5660 ies->len, ies->data))
5661 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005662 }
5663 rcu_read_unlock();
5664
David S. Miller9360ffd2012-03-29 04:41:26 -04005665 if (res->beacon_interval &&
5666 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5667 goto nla_put_failure;
5668 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5669 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5670 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5671 jiffies_to_msecs(jiffies - intbss->ts)))
5672 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005673
Johannes Berg77965c92009-02-18 18:45:06 +01005674 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005675 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005676 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5677 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005678 break;
5679 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005680 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5681 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005682 break;
5683 default:
5684 break;
5685 }
5686
Johannes Berg48ab9052009-07-10 18:42:31 +02005687 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005688 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005689 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005690 if (intbss == wdev->current_bss &&
5691 nla_put_u32(msg, NL80211_BSS_STATUS,
5692 NL80211_BSS_STATUS_ASSOCIATED))
5693 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005694 break;
5695 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005696 if (intbss == wdev->current_bss &&
5697 nla_put_u32(msg, NL80211_BSS_STATUS,
5698 NL80211_BSS_STATUS_IBSS_JOINED))
5699 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005700 break;
5701 default:
5702 break;
5703 }
5704
Johannes Berg2a519312009-02-10 21:25:55 +01005705 nla_nest_end(msg, bss);
5706
5707 return genlmsg_end(msg, hdr);
5708
Johannes Berg8cef2c92013-02-05 16:54:31 +01005709 fail_unlock_rcu:
5710 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005711 nla_put_failure:
5712 genlmsg_cancel(msg, hdr);
5713 return -EMSGSIZE;
5714}
5715
Johannes Berg97990a02013-04-19 01:02:55 +02005716static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005717{
Johannes Berg48ab9052009-07-10 18:42:31 +02005718 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005719 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005720 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005721 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005722 int err;
5723
Johannes Berg97990a02013-04-19 01:02:55 +02005724 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005725 if (err)
5726 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005727
Johannes Berg48ab9052009-07-10 18:42:31 +02005728 wdev_lock(wdev);
5729 spin_lock_bh(&rdev->bss_lock);
5730 cfg80211_bss_expire(rdev);
5731
Johannes Berg9720bb32011-06-21 09:45:33 +02005732 cb->seq = rdev->bss_generation;
5733
Johannes Berg48ab9052009-07-10 18:42:31 +02005734 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005735 if (++idx <= start)
5736 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005737 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005738 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005739 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005740 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005741 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005742 }
5743 }
5744
Johannes Berg48ab9052009-07-10 18:42:31 +02005745 spin_unlock_bh(&rdev->bss_lock);
5746 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005747
Johannes Berg97990a02013-04-19 01:02:55 +02005748 cb->args[2] = idx;
5749 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005750
Johannes Berg67748892010-10-04 21:14:06 +02005751 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005752}
5753
Eric W. Biederman15e47302012-09-07 20:12:54 +00005754static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005755 int flags, struct net_device *dev,
5756 struct survey_info *survey)
5757{
5758 void *hdr;
5759 struct nlattr *infoattr;
5760
Eric W. Biederman15e47302012-09-07 20:12:54 +00005761 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005762 NL80211_CMD_NEW_SURVEY_RESULTS);
5763 if (!hdr)
5764 return -ENOMEM;
5765
David S. Miller9360ffd2012-03-29 04:41:26 -04005766 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5767 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005768
5769 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5770 if (!infoattr)
5771 goto nla_put_failure;
5772
David S. Miller9360ffd2012-03-29 04:41:26 -04005773 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5774 survey->channel->center_freq))
5775 goto nla_put_failure;
5776
5777 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5778 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5779 goto nla_put_failure;
5780 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5781 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5782 goto nla_put_failure;
5783 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5784 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5785 survey->channel_time))
5786 goto nla_put_failure;
5787 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5788 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5789 survey->channel_time_busy))
5790 goto nla_put_failure;
5791 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5792 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5793 survey->channel_time_ext_busy))
5794 goto nla_put_failure;
5795 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5796 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5797 survey->channel_time_rx))
5798 goto nla_put_failure;
5799 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5800 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5801 survey->channel_time_tx))
5802 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005803
5804 nla_nest_end(msg, infoattr);
5805
5806 return genlmsg_end(msg, hdr);
5807
5808 nla_put_failure:
5809 genlmsg_cancel(msg, hdr);
5810 return -EMSGSIZE;
5811}
5812
5813static int nl80211_dump_survey(struct sk_buff *skb,
5814 struct netlink_callback *cb)
5815{
5816 struct survey_info survey;
5817 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005818 struct wireless_dev *wdev;
5819 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005820 int res;
5821
Johannes Berg97990a02013-04-19 01:02:55 +02005822 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005823 if (res)
5824 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005825
Johannes Berg97990a02013-04-19 01:02:55 +02005826 if (!wdev->netdev) {
5827 res = -EINVAL;
5828 goto out_err;
5829 }
5830
Holger Schurig61fa7132009-11-11 12:25:40 +01005831 if (!dev->ops->dump_survey) {
5832 res = -EOPNOTSUPP;
5833 goto out_err;
5834 }
5835
5836 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005837 struct ieee80211_channel *chan;
5838
Johannes Berg97990a02013-04-19 01:02:55 +02005839 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005840 if (res == -ENOENT)
5841 break;
5842 if (res)
5843 goto out_err;
5844
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005845 /* Survey without a channel doesn't make sense */
5846 if (!survey.channel) {
5847 res = -EINVAL;
5848 goto out;
5849 }
5850
5851 chan = ieee80211_get_channel(&dev->wiphy,
5852 survey.channel->center_freq);
5853 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5854 survey_idx++;
5855 continue;
5856 }
5857
Holger Schurig61fa7132009-11-11 12:25:40 +01005858 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005859 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005860 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005861 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005862 goto out;
5863 survey_idx++;
5864 }
5865
5866 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005867 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005868 res = skb->len;
5869 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005870 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005871 return res;
5872}
5873
Samuel Ortizb23aa672009-07-01 21:26:54 +02005874static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5875{
5876 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5877 NL80211_WPA_VERSION_2));
5878}
5879
Jouni Malinen636a5d32009-03-19 13:39:22 +02005880static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5881{
Johannes Berg4c476992010-10-04 21:36:35 +02005882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5883 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005884 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005885 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5886 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005887 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005888 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005889 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005890
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005891 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5892 return -EINVAL;
5893
5894 if (!info->attrs[NL80211_ATTR_MAC])
5895 return -EINVAL;
5896
Jouni Malinen17780922009-03-27 20:52:47 +02005897 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5898 return -EINVAL;
5899
Johannes Berg19957bb2009-07-02 17:20:43 +02005900 if (!info->attrs[NL80211_ATTR_SSID])
5901 return -EINVAL;
5902
5903 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5904 return -EINVAL;
5905
Johannes Bergfffd0932009-07-08 14:22:54 +02005906 err = nl80211_parse_key(info, &key);
5907 if (err)
5908 return err;
5909
5910 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005911 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5912 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005913 if (!key.p.key || !key.p.key_len)
5914 return -EINVAL;
5915 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5916 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5917 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5918 key.p.key_len != WLAN_KEY_LEN_WEP104))
5919 return -EINVAL;
5920 if (key.idx > 4)
5921 return -EINVAL;
5922 } else {
5923 key.p.key_len = 0;
5924 key.p.key = NULL;
5925 }
5926
Johannes Bergafea0b72010-08-10 09:46:42 +02005927 if (key.idx >= 0) {
5928 int i;
5929 bool ok = false;
5930 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5931 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5932 ok = true;
5933 break;
5934 }
5935 }
Johannes Berg4c476992010-10-04 21:36:35 +02005936 if (!ok)
5937 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005938 }
5939
Johannes Berg4c476992010-10-04 21:36:35 +02005940 if (!rdev->ops->auth)
5941 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005942
Johannes Berg074ac8d2010-09-16 14:58:22 +02005943 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005944 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5945 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005946
Johannes Berg19957bb2009-07-02 17:20:43 +02005947 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005948 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005949 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005950 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5951 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005952
Johannes Berg19957bb2009-07-02 17:20:43 +02005953 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5954 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5955
5956 if (info->attrs[NL80211_ATTR_IE]) {
5957 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5958 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5959 }
5960
5961 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005962 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005963 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005964
Jouni Malinene39e5b52012-09-30 19:29:39 +03005965 if (auth_type == NL80211_AUTHTYPE_SAE &&
5966 !info->attrs[NL80211_ATTR_SAE_DATA])
5967 return -EINVAL;
5968
5969 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5970 if (auth_type != NL80211_AUTHTYPE_SAE)
5971 return -EINVAL;
5972 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5973 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5974 /* need to include at least Auth Transaction and Status Code */
5975 if (sae_data_len < 4)
5976 return -EINVAL;
5977 }
5978
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005979 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5980
Johannes Berg95de8172012-01-20 13:55:25 +01005981 /*
5982 * Since we no longer track auth state, ignore
5983 * requests to only change local state.
5984 */
5985 if (local_state_change)
5986 return 0;
5987
Johannes Berg4c476992010-10-04 21:36:35 +02005988 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5989 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005990 key.p.key, key.p.key_len, key.idx,
5991 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005992}
5993
Johannes Bergc0692b82010-08-27 14:26:53 +03005994static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5995 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005996 struct cfg80211_crypto_settings *settings,
5997 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005998{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005999 memset(settings, 0, sizeof(*settings));
6000
Samuel Ortizb23aa672009-07-01 21:26:54 +02006001 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6002
Johannes Bergc0692b82010-08-27 14:26:53 +03006003 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6004 u16 proto;
6005 proto = nla_get_u16(
6006 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6007 settings->control_port_ethertype = cpu_to_be16(proto);
6008 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6009 proto != ETH_P_PAE)
6010 return -EINVAL;
6011 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6012 settings->control_port_no_encrypt = true;
6013 } else
6014 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6015
Samuel Ortizb23aa672009-07-01 21:26:54 +02006016 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6017 void *data;
6018 int len, i;
6019
6020 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6021 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6022 settings->n_ciphers_pairwise = len / sizeof(u32);
6023
6024 if (len % sizeof(u32))
6025 return -EINVAL;
6026
Johannes Berg3dc27d22009-07-02 21:36:37 +02006027 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006028 return -EINVAL;
6029
6030 memcpy(settings->ciphers_pairwise, data, len);
6031
6032 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006033 if (!cfg80211_supported_cipher_suite(
6034 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006035 settings->ciphers_pairwise[i]))
6036 return -EINVAL;
6037 }
6038
6039 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6040 settings->cipher_group =
6041 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006042 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6043 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006044 return -EINVAL;
6045 }
6046
6047 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6048 settings->wpa_versions =
6049 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6050 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6051 return -EINVAL;
6052 }
6053
6054 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6055 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006056 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006057
6058 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6059 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6060 settings->n_akm_suites = len / sizeof(u32);
6061
6062 if (len % sizeof(u32))
6063 return -EINVAL;
6064
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006065 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6066 return -EINVAL;
6067
Samuel Ortizb23aa672009-07-01 21:26:54 +02006068 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006069 }
6070
6071 return 0;
6072}
6073
Jouni Malinen636a5d32009-03-19 13:39:22 +02006074static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6075{
Johannes Berg4c476992010-10-04 21:36:35 +02006076 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6077 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006078 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006079 struct cfg80211_assoc_request req = {};
6080 const u8 *bssid, *ssid;
6081 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006082
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006083 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6084 return -EINVAL;
6085
6086 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006087 !info->attrs[NL80211_ATTR_SSID] ||
6088 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006089 return -EINVAL;
6090
Johannes Berg4c476992010-10-04 21:36:35 +02006091 if (!rdev->ops->assoc)
6092 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006093
Johannes Berg074ac8d2010-09-16 14:58:22 +02006094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006095 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6096 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006097
Johannes Berg19957bb2009-07-02 17:20:43 +02006098 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006099
Johannes Berg19957bb2009-07-02 17:20:43 +02006100 chan = ieee80211_get_channel(&rdev->wiphy,
6101 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006102 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6103 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006104
Johannes Berg19957bb2009-07-02 17:20:43 +02006105 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6106 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006107
6108 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006109 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6110 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006111 }
6112
Jouni Malinendc6382c2009-05-06 22:09:37 +03006113 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006114 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006115 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006116 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006117 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006118 else if (mfp != NL80211_MFP_NO)
6119 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006120 }
6121
Johannes Berg3e5d7642009-07-07 14:37:26 +02006122 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006123 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006124
Ben Greear7e7c8922011-11-18 11:31:59 -08006125 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006126 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006127
6128 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006129 memcpy(&req.ht_capa_mask,
6130 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6131 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006132
6133 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006134 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006135 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006136 memcpy(&req.ht_capa,
6137 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6138 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006139 }
6140
Johannes Bergee2aca32013-02-21 17:36:01 +01006141 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006142 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006143
6144 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006145 memcpy(&req.vht_capa_mask,
6146 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6147 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006148
6149 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006150 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006151 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006152 memcpy(&req.vht_capa,
6153 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6154 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006155 }
6156
Johannes Bergf62fab72013-02-21 20:09:09 +01006157 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006158 if (!err)
Johannes Bergf62fab72013-02-21 20:09:09 +01006159 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6160 ssid, ssid_len, &req);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006161
Jouni Malinen636a5d32009-03-19 13:39:22 +02006162 return err;
6163}
6164
6165static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6166{
Johannes Berg4c476992010-10-04 21:36:35 +02006167 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6168 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006169 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006170 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006171 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006172 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006173
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006174 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6175 return -EINVAL;
6176
6177 if (!info->attrs[NL80211_ATTR_MAC])
6178 return -EINVAL;
6179
6180 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6181 return -EINVAL;
6182
Johannes Berg4c476992010-10-04 21:36:35 +02006183 if (!rdev->ops->deauth)
6184 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006185
Johannes Berg074ac8d2010-09-16 14:58:22 +02006186 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006187 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6188 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006189
Johannes Berg19957bb2009-07-02 17:20:43 +02006190 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006191
Johannes Berg19957bb2009-07-02 17:20:43 +02006192 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6193 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006194 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006195 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006196 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006197
6198 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006199 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6200 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006201 }
6202
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006203 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6204
Johannes Berg4c476992010-10-04 21:36:35 +02006205 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6206 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006207}
6208
6209static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6210{
Johannes Berg4c476992010-10-04 21:36:35 +02006211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6212 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006213 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02006214 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006215 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006216 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006217
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006218 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6219 return -EINVAL;
6220
6221 if (!info->attrs[NL80211_ATTR_MAC])
6222 return -EINVAL;
6223
6224 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6225 return -EINVAL;
6226
Johannes Berg4c476992010-10-04 21:36:35 +02006227 if (!rdev->ops->disassoc)
6228 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006229
Johannes Berg074ac8d2010-09-16 14:58:22 +02006230 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006231 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6232 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006233
Johannes Berg19957bb2009-07-02 17:20:43 +02006234 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006235
Johannes Berg19957bb2009-07-02 17:20:43 +02006236 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6237 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006238 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006239 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006240 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006241
6242 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006243 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6244 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006245 }
6246
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006247 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6248
Johannes Berg4c476992010-10-04 21:36:35 +02006249 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6250 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006251}
6252
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006253static bool
6254nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6255 int mcast_rate[IEEE80211_NUM_BANDS],
6256 int rateval)
6257{
6258 struct wiphy *wiphy = &rdev->wiphy;
6259 bool found = false;
6260 int band, i;
6261
6262 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6263 struct ieee80211_supported_band *sband;
6264
6265 sband = wiphy->bands[band];
6266 if (!sband)
6267 continue;
6268
6269 for (i = 0; i < sband->n_bitrates; i++) {
6270 if (sband->bitrates[i].bitrate == rateval) {
6271 mcast_rate[band] = i + 1;
6272 found = true;
6273 break;
6274 }
6275 }
6276 }
6277
6278 return found;
6279}
6280
Johannes Berg04a773a2009-04-19 21:24:32 +02006281static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6282{
Johannes Berg4c476992010-10-04 21:36:35 +02006283 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6284 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006285 struct cfg80211_ibss_params ibss;
6286 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006287 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006288 int err;
6289
Johannes Berg8e30bc52009-04-22 17:45:38 +02006290 memset(&ibss, 0, sizeof(ibss));
6291
Johannes Berg04a773a2009-04-19 21:24:32 +02006292 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6293 return -EINVAL;
6294
Johannes Berg683b6d32012-11-08 21:25:48 +01006295 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006296 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6297 return -EINVAL;
6298
Johannes Berg8e30bc52009-04-22 17:45:38 +02006299 ibss.beacon_interval = 100;
6300
6301 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6302 ibss.beacon_interval =
6303 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6304 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6305 return -EINVAL;
6306 }
6307
Johannes Berg4c476992010-10-04 21:36:35 +02006308 if (!rdev->ops->join_ibss)
6309 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006310
Johannes Berg4c476992010-10-04 21:36:35 +02006311 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6312 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006313
Johannes Berg79c97e92009-07-07 03:56:12 +02006314 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006315
Johannes Berg39193492011-09-16 13:45:25 +02006316 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006317 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006318
6319 if (!is_valid_ether_addr(ibss.bssid))
6320 return -EINVAL;
6321 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006322 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6323 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6324
6325 if (info->attrs[NL80211_ATTR_IE]) {
6326 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6327 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6328 }
6329
Johannes Berg683b6d32012-11-08 21:25:48 +01006330 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6331 if (err)
6332 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006333
Johannes Berg683b6d32012-11-08 21:25:48 +01006334 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006335 return -EINVAL;
6336
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006337 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6338 return -EINVAL;
6339 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6340 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006341 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006342
Johannes Berg04a773a2009-04-19 21:24:32 +02006343 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006344 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006345
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006346 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6347 u8 *rates =
6348 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6349 int n_rates =
6350 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6351 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006352 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006353
Johannes Berg34850ab2011-07-18 18:08:35 +02006354 err = ieee80211_get_ratemask(sband, rates, n_rates,
6355 &ibss.basic_rates);
6356 if (err)
6357 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006358 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006359
6360 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6361 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6362 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6363 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006364
Johannes Berg4c476992010-10-04 21:36:35 +02006365 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306366 bool no_ht = false;
6367
Johannes Berg4c476992010-10-04 21:36:35 +02006368 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306369 info->attrs[NL80211_ATTR_KEYS],
6370 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006371 if (IS_ERR(connkeys))
6372 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306373
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006374 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6375 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306376 kfree(connkeys);
6377 return -EINVAL;
6378 }
Johannes Berg4c476992010-10-04 21:36:35 +02006379 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006380
Antonio Quartulli267335d2012-01-31 20:25:47 +01006381 ibss.control_port =
6382 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6383
Johannes Berg4c476992010-10-04 21:36:35 +02006384 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006385 if (err)
6386 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006387 return err;
6388}
6389
6390static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6391{
Johannes Berg4c476992010-10-04 21:36:35 +02006392 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6393 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006394
Johannes Berg4c476992010-10-04 21:36:35 +02006395 if (!rdev->ops->leave_ibss)
6396 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006397
Johannes Berg4c476992010-10-04 21:36:35 +02006398 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6399 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006400
Johannes Berg4c476992010-10-04 21:36:35 +02006401 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006402}
6403
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006404static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6405{
6406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6407 struct net_device *dev = info->user_ptr[1];
6408 int mcast_rate[IEEE80211_NUM_BANDS];
6409 u32 nla_rate;
6410 int err;
6411
6412 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6413 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6414 return -EOPNOTSUPP;
6415
6416 if (!rdev->ops->set_mcast_rate)
6417 return -EOPNOTSUPP;
6418
6419 memset(mcast_rate, 0, sizeof(mcast_rate));
6420
6421 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6422 return -EINVAL;
6423
6424 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6425 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6426 return -EINVAL;
6427
6428 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6429
6430 return err;
6431}
6432
6433
Johannes Bergaff89a92009-07-01 21:26:51 +02006434#ifdef CONFIG_NL80211_TESTMODE
6435static struct genl_multicast_group nl80211_testmode_mcgrp = {
6436 .name = "testmode",
6437};
6438
6439static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6440{
Johannes Berg4c476992010-10-04 21:36:35 +02006441 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006442 int err;
6443
6444 if (!info->attrs[NL80211_ATTR_TESTDATA])
6445 return -EINVAL;
6446
Johannes Bergaff89a92009-07-01 21:26:51 +02006447 err = -EOPNOTSUPP;
6448 if (rdev->ops->testmode_cmd) {
6449 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006450 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006451 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6452 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6453 rdev->testmode_info = NULL;
6454 }
6455
Johannes Bergaff89a92009-07-01 21:26:51 +02006456 return err;
6457}
6458
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006459static int nl80211_testmode_dump(struct sk_buff *skb,
6460 struct netlink_callback *cb)
6461{
Johannes Berg00918d32011-12-13 17:22:05 +01006462 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006463 int err;
6464 long phy_idx;
6465 void *data = NULL;
6466 int data_len = 0;
6467
6468 if (cb->args[0]) {
6469 /*
6470 * 0 is a valid index, but not valid for args[0],
6471 * so we need to offset by 1.
6472 */
6473 phy_idx = cb->args[0] - 1;
6474 } else {
6475 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6476 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6477 nl80211_policy);
6478 if (err)
6479 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01006480
Johannes Berg2bd7e352012-06-15 14:23:16 +02006481 mutex_lock(&cfg80211_mutex);
6482 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6483 nl80211_fam.attrbuf);
6484 if (IS_ERR(rdev)) {
6485 mutex_unlock(&cfg80211_mutex);
6486 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01006487 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006488 phy_idx = rdev->wiphy_idx;
6489 rdev = NULL;
6490 mutex_unlock(&cfg80211_mutex);
6491
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006492 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6493 cb->args[1] =
6494 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6495 }
6496
6497 if (cb->args[1]) {
6498 data = nla_data((void *)cb->args[1]);
6499 data_len = nla_len((void *)cb->args[1]);
6500 }
6501
6502 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01006503 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6504 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006505 mutex_unlock(&cfg80211_mutex);
6506 return -ENOENT;
6507 }
Johannes Berg00918d32011-12-13 17:22:05 +01006508 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006509 mutex_unlock(&cfg80211_mutex);
6510
Johannes Berg00918d32011-12-13 17:22:05 +01006511 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006512 err = -EOPNOTSUPP;
6513 goto out_err;
6514 }
6515
6516 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006517 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006518 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6519 NL80211_CMD_TESTMODE);
6520 struct nlattr *tmdata;
6521
David S. Miller9360ffd2012-03-29 04:41:26 -04006522 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006523 genlmsg_cancel(skb, hdr);
6524 break;
6525 }
6526
6527 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6528 if (!tmdata) {
6529 genlmsg_cancel(skb, hdr);
6530 break;
6531 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006532 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006533 nla_nest_end(skb, tmdata);
6534
6535 if (err == -ENOBUFS || err == -ENOENT) {
6536 genlmsg_cancel(skb, hdr);
6537 break;
6538 } else if (err) {
6539 genlmsg_cancel(skb, hdr);
6540 goto out_err;
6541 }
6542
6543 genlmsg_end(skb, hdr);
6544 }
6545
6546 err = skb->len;
6547 /* see above */
6548 cb->args[0] = phy_idx + 1;
6549 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01006550 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006551 return err;
6552}
6553
Johannes Bergaff89a92009-07-01 21:26:51 +02006554static struct sk_buff *
6555__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006556 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006557{
6558 struct sk_buff *skb;
6559 void *hdr;
6560 struct nlattr *data;
6561
6562 skb = nlmsg_new(approxlen + 100, gfp);
6563 if (!skb)
6564 return NULL;
6565
Eric W. Biederman15e47302012-09-07 20:12:54 +00006566 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006567 if (!hdr) {
6568 kfree_skb(skb);
6569 return NULL;
6570 }
6571
David S. Miller9360ffd2012-03-29 04:41:26 -04006572 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6573 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006574 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6575
6576 ((void **)skb->cb)[0] = rdev;
6577 ((void **)skb->cb)[1] = hdr;
6578 ((void **)skb->cb)[2] = data;
6579
6580 return skb;
6581
6582 nla_put_failure:
6583 kfree_skb(skb);
6584 return NULL;
6585}
6586
6587struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6588 int approxlen)
6589{
6590 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6591
6592 if (WARN_ON(!rdev->testmode_info))
6593 return NULL;
6594
6595 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006596 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006597 rdev->testmode_info->snd_seq,
6598 GFP_KERNEL);
6599}
6600EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6601
6602int cfg80211_testmode_reply(struct sk_buff *skb)
6603{
6604 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6605 void *hdr = ((void **)skb->cb)[1];
6606 struct nlattr *data = ((void **)skb->cb)[2];
6607
6608 if (WARN_ON(!rdev->testmode_info)) {
6609 kfree_skb(skb);
6610 return -EINVAL;
6611 }
6612
6613 nla_nest_end(skb, data);
6614 genlmsg_end(skb, hdr);
6615 return genlmsg_reply(skb, rdev->testmode_info);
6616}
6617EXPORT_SYMBOL(cfg80211_testmode_reply);
6618
6619struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6620 int approxlen, gfp_t gfp)
6621{
6622 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6623
6624 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6625}
6626EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6627
6628void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6629{
6630 void *hdr = ((void **)skb->cb)[1];
6631 struct nlattr *data = ((void **)skb->cb)[2];
6632
6633 nla_nest_end(skb, data);
6634 genlmsg_end(skb, hdr);
6635 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6636}
6637EXPORT_SYMBOL(cfg80211_testmode_event);
6638#endif
6639
Samuel Ortizb23aa672009-07-01 21:26:54 +02006640static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6641{
Johannes Berg4c476992010-10-04 21:36:35 +02006642 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6643 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006644 struct cfg80211_connect_params connect;
6645 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006646 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006647 int err;
6648
6649 memset(&connect, 0, sizeof(connect));
6650
6651 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6652 return -EINVAL;
6653
6654 if (!info->attrs[NL80211_ATTR_SSID] ||
6655 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6656 return -EINVAL;
6657
6658 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6659 connect.auth_type =
6660 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006661 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6662 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006663 return -EINVAL;
6664 } else
6665 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6666
6667 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6668
Johannes Bergc0692b82010-08-27 14:26:53 +03006669 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006670 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006671 if (err)
6672 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006673
Johannes Berg074ac8d2010-09-16 14:58:22 +02006674 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006675 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6676 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006677
Johannes Berg79c97e92009-07-07 03:56:12 +02006678 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006679
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306680 connect.bg_scan_period = -1;
6681 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6682 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6683 connect.bg_scan_period =
6684 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6685 }
6686
Samuel Ortizb23aa672009-07-01 21:26:54 +02006687 if (info->attrs[NL80211_ATTR_MAC])
6688 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6689 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6690 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6691
6692 if (info->attrs[NL80211_ATTR_IE]) {
6693 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6694 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6695 }
6696
Jouni Malinencee00a92013-01-15 17:15:57 +02006697 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6698 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6699 if (connect.mfp != NL80211_MFP_REQUIRED &&
6700 connect.mfp != NL80211_MFP_NO)
6701 return -EINVAL;
6702 } else {
6703 connect.mfp = NL80211_MFP_NO;
6704 }
6705
Samuel Ortizb23aa672009-07-01 21:26:54 +02006706 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6707 connect.channel =
6708 ieee80211_get_channel(wiphy,
6709 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6710 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006711 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6712 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006713 }
6714
Johannes Bergfffd0932009-07-08 14:22:54 +02006715 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6716 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306717 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006718 if (IS_ERR(connkeys))
6719 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006720 }
6721
Ben Greear7e7c8922011-11-18 11:31:59 -08006722 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6723 connect.flags |= ASSOC_REQ_DISABLE_HT;
6724
6725 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6726 memcpy(&connect.ht_capa_mask,
6727 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6728 sizeof(connect.ht_capa_mask));
6729
6730 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006731 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6732 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006733 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006734 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006735 memcpy(&connect.ht_capa,
6736 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6737 sizeof(connect.ht_capa));
6738 }
6739
Johannes Bergee2aca32013-02-21 17:36:01 +01006740 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6741 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6742
6743 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6744 memcpy(&connect.vht_capa_mask,
6745 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6746 sizeof(connect.vht_capa_mask));
6747
6748 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6749 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6750 kfree(connkeys);
6751 return -EINVAL;
6752 }
6753 memcpy(&connect.vht_capa,
6754 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6755 sizeof(connect.vht_capa));
6756 }
6757
Johannes Bergfffd0932009-07-08 14:22:54 +02006758 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006759 if (err)
6760 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006761 return err;
6762}
6763
6764static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6765{
Johannes Berg4c476992010-10-04 21:36:35 +02006766 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6767 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006768 u16 reason;
6769
6770 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6771 reason = WLAN_REASON_DEAUTH_LEAVING;
6772 else
6773 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6774
6775 if (reason == 0)
6776 return -EINVAL;
6777
Johannes Berg074ac8d2010-09-16 14:58:22 +02006778 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006779 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6780 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006781
Johannes Berg4c476992010-10-04 21:36:35 +02006782 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006783}
6784
Johannes Berg463d0182009-07-14 00:33:35 +02006785static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6786{
Johannes Berg4c476992010-10-04 21:36:35 +02006787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006788 struct net *net;
6789 int err;
6790 u32 pid;
6791
6792 if (!info->attrs[NL80211_ATTR_PID])
6793 return -EINVAL;
6794
6795 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6796
Johannes Berg463d0182009-07-14 00:33:35 +02006797 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006798 if (IS_ERR(net))
6799 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006800
6801 err = 0;
6802
6803 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006804 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6805 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006806
Johannes Berg463d0182009-07-14 00:33:35 +02006807 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006808 return err;
6809}
6810
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006811static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6812{
Johannes Berg4c476992010-10-04 21:36:35 +02006813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006814 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6815 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006816 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006817 struct cfg80211_pmksa pmksa;
6818
6819 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6820
6821 if (!info->attrs[NL80211_ATTR_MAC])
6822 return -EINVAL;
6823
6824 if (!info->attrs[NL80211_ATTR_PMKID])
6825 return -EINVAL;
6826
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006827 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6828 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6829
Johannes Berg074ac8d2010-09-16 14:58:22 +02006830 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006831 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6832 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006833
6834 switch (info->genlhdr->cmd) {
6835 case NL80211_CMD_SET_PMKSA:
6836 rdev_ops = rdev->ops->set_pmksa;
6837 break;
6838 case NL80211_CMD_DEL_PMKSA:
6839 rdev_ops = rdev->ops->del_pmksa;
6840 break;
6841 default:
6842 WARN_ON(1);
6843 break;
6844 }
6845
Johannes Berg4c476992010-10-04 21:36:35 +02006846 if (!rdev_ops)
6847 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006848
Johannes Berg4c476992010-10-04 21:36:35 +02006849 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006850}
6851
6852static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6853{
Johannes Berg4c476992010-10-04 21:36:35 +02006854 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6855 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006856
Johannes Berg074ac8d2010-09-16 14:58:22 +02006857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006858 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6859 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006860
Johannes Berg4c476992010-10-04 21:36:35 +02006861 if (!rdev->ops->flush_pmksa)
6862 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006863
Hila Gonene35e4d22012-06-27 17:19:42 +03006864 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006865}
6866
Arik Nemtsov109086c2011-09-28 14:12:50 +03006867static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6868{
6869 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6870 struct net_device *dev = info->user_ptr[1];
6871 u8 action_code, dialog_token;
6872 u16 status_code;
6873 u8 *peer;
6874
6875 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6876 !rdev->ops->tdls_mgmt)
6877 return -EOPNOTSUPP;
6878
6879 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6880 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6881 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6882 !info->attrs[NL80211_ATTR_IE] ||
6883 !info->attrs[NL80211_ATTR_MAC])
6884 return -EINVAL;
6885
6886 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6887 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6888 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6889 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6890
Hila Gonene35e4d22012-06-27 17:19:42 +03006891 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6892 dialog_token, status_code,
6893 nla_data(info->attrs[NL80211_ATTR_IE]),
6894 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006895}
6896
6897static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6898{
6899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6900 struct net_device *dev = info->user_ptr[1];
6901 enum nl80211_tdls_operation operation;
6902 u8 *peer;
6903
6904 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6905 !rdev->ops->tdls_oper)
6906 return -EOPNOTSUPP;
6907
6908 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6909 !info->attrs[NL80211_ATTR_MAC])
6910 return -EINVAL;
6911
6912 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6913 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6914
Hila Gonene35e4d22012-06-27 17:19:42 +03006915 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006916}
6917
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006918static int nl80211_remain_on_channel(struct sk_buff *skb,
6919 struct genl_info *info)
6920{
Johannes Berg4c476992010-10-04 21:36:35 +02006921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006922 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006923 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006924 struct sk_buff *msg;
6925 void *hdr;
6926 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006927 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006928 int err;
6929
6930 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6931 !info->attrs[NL80211_ATTR_DURATION])
6932 return -EINVAL;
6933
6934 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6935
Johannes Berg7c4ef712011-11-18 15:33:48 +01006936 if (!rdev->ops->remain_on_channel ||
6937 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006938 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006939
Johannes Bergebf348f2012-06-01 12:50:54 +02006940 /*
6941 * We should be on that channel for at least a minimum amount of
6942 * time (10ms) but no longer than the driver supports.
6943 */
6944 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6945 duration > rdev->wiphy.max_remain_on_channel_duration)
6946 return -EINVAL;
6947
Johannes Berg683b6d32012-11-08 21:25:48 +01006948 err = nl80211_parse_chandef(rdev, info, &chandef);
6949 if (err)
6950 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006951
6952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006953 if (!msg)
6954 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006955
Eric W. Biederman15e47302012-09-07 20:12:54 +00006956 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006957 NL80211_CMD_REMAIN_ON_CHANNEL);
6958
6959 if (IS_ERR(hdr)) {
6960 err = PTR_ERR(hdr);
6961 goto free_msg;
6962 }
6963
Johannes Berg683b6d32012-11-08 21:25:48 +01006964 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6965 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006966
6967 if (err)
6968 goto free_msg;
6969
David S. Miller9360ffd2012-03-29 04:41:26 -04006970 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6971 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006972
6973 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006974
6975 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006976
6977 nla_put_failure:
6978 err = -ENOBUFS;
6979 free_msg:
6980 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006981 return err;
6982}
6983
6984static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6985 struct genl_info *info)
6986{
Johannes Berg4c476992010-10-04 21:36:35 +02006987 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006988 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006989 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006990
6991 if (!info->attrs[NL80211_ATTR_COOKIE])
6992 return -EINVAL;
6993
Johannes Berg4c476992010-10-04 21:36:35 +02006994 if (!rdev->ops->cancel_remain_on_channel)
6995 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006996
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006997 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6998
Hila Gonene35e4d22012-06-27 17:19:42 +03006999 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007000}
7001
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007002static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7003 u8 *rates, u8 rates_len)
7004{
7005 u8 i;
7006 u32 mask = 0;
7007
7008 for (i = 0; i < rates_len; i++) {
7009 int rate = (rates[i] & 0x7f) * 5;
7010 int ridx;
7011 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7012 struct ieee80211_rate *srate =
7013 &sband->bitrates[ridx];
7014 if (rate == srate->bitrate) {
7015 mask |= 1 << ridx;
7016 break;
7017 }
7018 }
7019 if (ridx == sband->n_bitrates)
7020 return 0; /* rate not found */
7021 }
7022
7023 return mask;
7024}
7025
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007026static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7027 u8 *rates, u8 rates_len,
7028 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7029{
7030 u8 i;
7031
7032 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7033
7034 for (i = 0; i < rates_len; i++) {
7035 int ridx, rbit;
7036
7037 ridx = rates[i] / 8;
7038 rbit = BIT(rates[i] % 8);
7039
7040 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007041 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007042 return false;
7043
7044 /* check availability */
7045 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7046 mcs[ridx] |= rbit;
7047 else
7048 return false;
7049 }
7050
7051 return true;
7052}
7053
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007054static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007055 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7056 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007057 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7058 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007059};
7060
7061static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7062 struct genl_info *info)
7063{
7064 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007066 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007067 int rem, i;
7068 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007069 struct nlattr *tx_rates;
7070 struct ieee80211_supported_band *sband;
7071
7072 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7073 return -EINVAL;
7074
Johannes Berg4c476992010-10-04 21:36:35 +02007075 if (!rdev->ops->set_bitrate_mask)
7076 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007077
7078 memset(&mask, 0, sizeof(mask));
7079 /* Default to all rates enabled */
7080 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7081 sband = rdev->wiphy.bands[i];
7082 mask.control[i].legacy =
7083 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007084 if (sband)
7085 memcpy(mask.control[i].mcs,
7086 sband->ht_cap.mcs.rx_mask,
7087 sizeof(mask.control[i].mcs));
7088 else
7089 memset(mask.control[i].mcs, 0,
7090 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007091 }
7092
7093 /*
7094 * The nested attribute uses enum nl80211_band as the index. This maps
7095 * directly to the enum ieee80211_band values used in cfg80211.
7096 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007097 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007098 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7099 {
7100 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007101 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7102 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007103 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007104 if (sband == NULL)
7105 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007106 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7107 nla_len(tx_rates), nl80211_txattr_policy);
7108 if (tb[NL80211_TXRATE_LEGACY]) {
7109 mask.control[band].legacy = rateset_to_mask(
7110 sband,
7111 nla_data(tb[NL80211_TXRATE_LEGACY]),
7112 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307113 if ((mask.control[band].legacy == 0) &&
7114 nla_len(tb[NL80211_TXRATE_LEGACY]))
7115 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007116 }
7117 if (tb[NL80211_TXRATE_MCS]) {
7118 if (!ht_rateset_to_mask(
7119 sband,
7120 nla_data(tb[NL80211_TXRATE_MCS]),
7121 nla_len(tb[NL80211_TXRATE_MCS]),
7122 mask.control[band].mcs))
7123 return -EINVAL;
7124 }
7125
7126 if (mask.control[band].legacy == 0) {
7127 /* don't allow empty legacy rates if HT
7128 * is not even supported. */
7129 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7130 return -EINVAL;
7131
7132 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7133 if (mask.control[band].mcs[i])
7134 break;
7135
7136 /* legacy and mcs rates may not be both empty */
7137 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007138 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007139 }
7140 }
7141
Hila Gonene35e4d22012-06-27 17:19:42 +03007142 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007143}
7144
Johannes Berg2e161f72010-08-12 15:38:38 +02007145static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007146{
Johannes Berg4c476992010-10-04 21:36:35 +02007147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007148 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007149 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007150
7151 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7152 return -EINVAL;
7153
Johannes Berg2e161f72010-08-12 15:38:38 +02007154 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7155 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007156
Johannes Berg71bbc992012-06-15 15:30:18 +02007157 switch (wdev->iftype) {
7158 case NL80211_IFTYPE_STATION:
7159 case NL80211_IFTYPE_ADHOC:
7160 case NL80211_IFTYPE_P2P_CLIENT:
7161 case NL80211_IFTYPE_AP:
7162 case NL80211_IFTYPE_AP_VLAN:
7163 case NL80211_IFTYPE_MESH_POINT:
7164 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007165 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007166 break;
7167 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007168 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007169 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007170
7171 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007172 if (!rdev->ops->mgmt_tx)
7173 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007174
Eric W. Biederman15e47302012-09-07 20:12:54 +00007175 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007176 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7177 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007178}
7179
Johannes Berg2e161f72010-08-12 15:38:38 +02007180static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007181{
Johannes Berg4c476992010-10-04 21:36:35 +02007182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007183 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007184 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007185 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007186 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007187 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007188 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007189 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007190 bool offchan, no_cck, dont_wait_for_ack;
7191
7192 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007193
Johannes Berg683b6d32012-11-08 21:25:48 +01007194 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007195 return -EINVAL;
7196
Johannes Berg4c476992010-10-04 21:36:35 +02007197 if (!rdev->ops->mgmt_tx)
7198 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007199
Johannes Berg71bbc992012-06-15 15:30:18 +02007200 switch (wdev->iftype) {
7201 case NL80211_IFTYPE_STATION:
7202 case NL80211_IFTYPE_ADHOC:
7203 case NL80211_IFTYPE_P2P_CLIENT:
7204 case NL80211_IFTYPE_AP:
7205 case NL80211_IFTYPE_AP_VLAN:
7206 case NL80211_IFTYPE_MESH_POINT:
7207 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007208 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007209 break;
7210 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007211 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007212 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007213
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007214 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007215 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007216 return -EINVAL;
7217 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007218
7219 /*
7220 * We should wait on the channel for at least a minimum amount
7221 * of time (10ms) but no longer than the driver supports.
7222 */
7223 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7224 wait > rdev->wiphy.max_remain_on_channel_duration)
7225 return -EINVAL;
7226
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007227 }
7228
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007229 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7230
Johannes Berg7c4ef712011-11-18 15:33:48 +01007231 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7232 return -EINVAL;
7233
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307234 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7235
Johannes Berg683b6d32012-11-08 21:25:48 +01007236 err = nl80211_parse_chandef(rdev, info, &chandef);
7237 if (err)
7238 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007239
Johannes Berge247bd902011-11-04 11:18:21 +01007240 if (!dont_wait_for_ack) {
7241 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7242 if (!msg)
7243 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007244
Eric W. Biederman15e47302012-09-07 20:12:54 +00007245 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007246 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007247
Johannes Berge247bd902011-11-04 11:18:21 +01007248 if (IS_ERR(hdr)) {
7249 err = PTR_ERR(hdr);
7250 goto free_msg;
7251 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007252 }
Johannes Berge247bd902011-11-04 11:18:21 +01007253
Johannes Berg683b6d32012-11-08 21:25:48 +01007254 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007255 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7256 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007257 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007258 if (err)
7259 goto free_msg;
7260
Johannes Berge247bd902011-11-04 11:18:21 +01007261 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007262 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7263 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007264
Johannes Berge247bd902011-11-04 11:18:21 +01007265 genlmsg_end(msg, hdr);
7266 return genlmsg_reply(msg, info);
7267 }
7268
7269 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007270
7271 nla_put_failure:
7272 err = -ENOBUFS;
7273 free_msg:
7274 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007275 return err;
7276}
7277
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007278static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7279{
7280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007281 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007282 u64 cookie;
7283
7284 if (!info->attrs[NL80211_ATTR_COOKIE])
7285 return -EINVAL;
7286
7287 if (!rdev->ops->mgmt_tx_cancel_wait)
7288 return -EOPNOTSUPP;
7289
Johannes Berg71bbc992012-06-15 15:30:18 +02007290 switch (wdev->iftype) {
7291 case NL80211_IFTYPE_STATION:
7292 case NL80211_IFTYPE_ADHOC:
7293 case NL80211_IFTYPE_P2P_CLIENT:
7294 case NL80211_IFTYPE_AP:
7295 case NL80211_IFTYPE_AP_VLAN:
7296 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007297 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007298 break;
7299 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007300 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007301 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007302
7303 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7304
Hila Gonene35e4d22012-06-27 17:19:42 +03007305 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007306}
7307
Kalle Valoffb9eb32010-02-17 17:58:10 +02007308static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7309{
Johannes Berg4c476992010-10-04 21:36:35 +02007310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007311 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007312 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007313 u8 ps_state;
7314 bool state;
7315 int err;
7316
Johannes Berg4c476992010-10-04 21:36:35 +02007317 if (!info->attrs[NL80211_ATTR_PS_STATE])
7318 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007319
7320 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7321
Johannes Berg4c476992010-10-04 21:36:35 +02007322 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7323 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007324
7325 wdev = dev->ieee80211_ptr;
7326
Johannes Berg4c476992010-10-04 21:36:35 +02007327 if (!rdev->ops->set_power_mgmt)
7328 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007329
7330 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7331
7332 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007333 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007334
Hila Gonene35e4d22012-06-27 17:19:42 +03007335 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007336 if (!err)
7337 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007338 return err;
7339}
7340
7341static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7342{
Johannes Berg4c476992010-10-04 21:36:35 +02007343 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007344 enum nl80211_ps_state ps_state;
7345 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007346 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007347 struct sk_buff *msg;
7348 void *hdr;
7349 int err;
7350
Kalle Valoffb9eb32010-02-17 17:58:10 +02007351 wdev = dev->ieee80211_ptr;
7352
Johannes Berg4c476992010-10-04 21:36:35 +02007353 if (!rdev->ops->set_power_mgmt)
7354 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007355
7356 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007357 if (!msg)
7358 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007359
Eric W. Biederman15e47302012-09-07 20:12:54 +00007360 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007361 NL80211_CMD_GET_POWER_SAVE);
7362 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007363 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007364 goto free_msg;
7365 }
7366
7367 if (wdev->ps)
7368 ps_state = NL80211_PS_ENABLED;
7369 else
7370 ps_state = NL80211_PS_DISABLED;
7371
David S. Miller9360ffd2012-03-29 04:41:26 -04007372 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7373 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007374
7375 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007376 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007377
Johannes Berg4c476992010-10-04 21:36:35 +02007378 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007379 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007380 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007381 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007382 return err;
7383}
7384
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007385static struct nla_policy
7386nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7387 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7388 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7389 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007390 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7391 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7392 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007393};
7394
Thomas Pedersen84f10702012-07-12 16:17:33 -07007395static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007396 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007397{
7398 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7399 struct wireless_dev *wdev;
7400 struct net_device *dev = info->user_ptr[1];
7401
Johannes Bergd9d8b012012-11-26 12:51:52 +01007402 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007403 return -EINVAL;
7404
7405 wdev = dev->ieee80211_ptr;
7406
7407 if (!rdev->ops->set_cqm_txe_config)
7408 return -EOPNOTSUPP;
7409
7410 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7411 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7412 return -EOPNOTSUPP;
7413
Hila Gonene35e4d22012-06-27 17:19:42 +03007414 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007415}
7416
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007417static int nl80211_set_cqm_rssi(struct genl_info *info,
7418 s32 threshold, u32 hysteresis)
7419{
Johannes Berg4c476992010-10-04 21:36:35 +02007420 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007421 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007422 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007423
7424 if (threshold > 0)
7425 return -EINVAL;
7426
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007427 wdev = dev->ieee80211_ptr;
7428
Johannes Berg4c476992010-10-04 21:36:35 +02007429 if (!rdev->ops->set_cqm_rssi_config)
7430 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007431
Johannes Berg074ac8d2010-09-16 14:58:22 +02007432 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007433 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7434 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007435
Hila Gonene35e4d22012-06-27 17:19:42 +03007436 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007437}
7438
7439static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7440{
7441 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7442 struct nlattr *cqm;
7443 int err;
7444
7445 cqm = info->attrs[NL80211_ATTR_CQM];
7446 if (!cqm) {
7447 err = -EINVAL;
7448 goto out;
7449 }
7450
7451 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7452 nl80211_attr_cqm_policy);
7453 if (err)
7454 goto out;
7455
7456 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7457 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7458 s32 threshold;
7459 u32 hysteresis;
7460 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7461 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7462 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007463 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7464 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7465 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7466 u32 rate, pkts, intvl;
7467 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7468 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7469 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7470 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007471 } else
7472 err = -EINVAL;
7473
7474out:
7475 return err;
7476}
7477
Johannes Berg29cbe682010-12-03 09:20:44 +01007478static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7479{
7480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7481 struct net_device *dev = info->user_ptr[1];
7482 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007483 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007484 int err;
7485
7486 /* start with default */
7487 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007488 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007489
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007490 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007491 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007492 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007493 if (err)
7494 return err;
7495 }
7496
7497 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7498 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7499 return -EINVAL;
7500
Javier Cardonac80d5452010-12-16 17:37:49 -08007501 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7502 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7503
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007504 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7505 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7506 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7507 return -EINVAL;
7508
Marco Porsch9bdbf042013-01-07 16:04:51 +01007509 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7510 setup.beacon_interval =
7511 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7512 if (setup.beacon_interval < 10 ||
7513 setup.beacon_interval > 10000)
7514 return -EINVAL;
7515 }
7516
7517 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7518 setup.dtim_period =
7519 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7520 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7521 return -EINVAL;
7522 }
7523
Javier Cardonac80d5452010-12-16 17:37:49 -08007524 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7525 /* parse additional setup parameters if given */
7526 err = nl80211_parse_mesh_setup(info, &setup);
7527 if (err)
7528 return err;
7529 }
7530
Thomas Pedersend37bb182013-03-04 13:06:13 -08007531 if (setup.user_mpm)
7532 cfg.auto_open_plinks = false;
7533
Johannes Bergcc1d2802012-05-16 23:50:20 +02007534 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007535 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7536 if (err)
7537 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007538 } else {
7539 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007540 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007541 }
7542
Javier Cardonac80d5452010-12-16 17:37:49 -08007543 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007544}
7545
7546static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7547{
7548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7549 struct net_device *dev = info->user_ptr[1];
7550
7551 return cfg80211_leave_mesh(rdev, dev);
7552}
7553
Johannes Bergdfb89c52012-06-27 09:23:48 +02007554#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007555static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7556 struct cfg80211_registered_device *rdev)
7557{
7558 struct nlattr *nl_pats, *nl_pat;
7559 int i, pat_len;
7560
7561 if (!rdev->wowlan->n_patterns)
7562 return 0;
7563
7564 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7565 if (!nl_pats)
7566 return -ENOBUFS;
7567
7568 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7569 nl_pat = nla_nest_start(msg, i + 1);
7570 if (!nl_pat)
7571 return -ENOBUFS;
7572 pat_len = rdev->wowlan->patterns[i].pattern_len;
7573 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7574 DIV_ROUND_UP(pat_len, 8),
7575 rdev->wowlan->patterns[i].mask) ||
7576 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7577 pat_len, rdev->wowlan->patterns[i].pattern) ||
7578 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7579 rdev->wowlan->patterns[i].pkt_offset))
7580 return -ENOBUFS;
7581 nla_nest_end(msg, nl_pat);
7582 }
7583 nla_nest_end(msg, nl_pats);
7584
7585 return 0;
7586}
7587
Johannes Berg2a0e0472013-01-23 22:57:40 +01007588static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7589 struct cfg80211_wowlan_tcp *tcp)
7590{
7591 struct nlattr *nl_tcp;
7592
7593 if (!tcp)
7594 return 0;
7595
7596 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7597 if (!nl_tcp)
7598 return -ENOBUFS;
7599
7600 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7601 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7602 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7603 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7604 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7605 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7606 tcp->payload_len, tcp->payload) ||
7607 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7608 tcp->data_interval) ||
7609 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7610 tcp->wake_len, tcp->wake_data) ||
7611 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7612 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7613 return -ENOBUFS;
7614
7615 if (tcp->payload_seq.len &&
7616 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7617 sizeof(tcp->payload_seq), &tcp->payload_seq))
7618 return -ENOBUFS;
7619
7620 if (tcp->payload_tok.len &&
7621 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7622 sizeof(tcp->payload_tok) + tcp->tokens_size,
7623 &tcp->payload_tok))
7624 return -ENOBUFS;
7625
7626 return 0;
7627}
7628
Johannes Bergff1b6e62011-05-04 15:37:28 +02007629static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7630{
7631 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7632 struct sk_buff *msg;
7633 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007634 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007635
Johannes Berg2a0e0472013-01-23 22:57:40 +01007636 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7637 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007638 return -EOPNOTSUPP;
7639
Johannes Berg2a0e0472013-01-23 22:57:40 +01007640 if (rdev->wowlan && rdev->wowlan->tcp) {
7641 /* adjust size to have room for all the data */
7642 size += rdev->wowlan->tcp->tokens_size +
7643 rdev->wowlan->tcp->payload_len +
7644 rdev->wowlan->tcp->wake_len +
7645 rdev->wowlan->tcp->wake_len / 8;
7646 }
7647
7648 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007649 if (!msg)
7650 return -ENOMEM;
7651
Eric W. Biederman15e47302012-09-07 20:12:54 +00007652 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007653 NL80211_CMD_GET_WOWLAN);
7654 if (!hdr)
7655 goto nla_put_failure;
7656
7657 if (rdev->wowlan) {
7658 struct nlattr *nl_wowlan;
7659
7660 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7661 if (!nl_wowlan)
7662 goto nla_put_failure;
7663
David S. Miller9360ffd2012-03-29 04:41:26 -04007664 if ((rdev->wowlan->any &&
7665 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7666 (rdev->wowlan->disconnect &&
7667 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7668 (rdev->wowlan->magic_pkt &&
7669 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7670 (rdev->wowlan->gtk_rekey_failure &&
7671 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7672 (rdev->wowlan->eap_identity_req &&
7673 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7674 (rdev->wowlan->four_way_handshake &&
7675 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7676 (rdev->wowlan->rfkill_release &&
7677 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7678 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007679
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007680 if (nl80211_send_wowlan_patterns(msg, rdev))
7681 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007682
7683 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7684 goto nla_put_failure;
7685
Johannes Bergff1b6e62011-05-04 15:37:28 +02007686 nla_nest_end(msg, nl_wowlan);
7687 }
7688
7689 genlmsg_end(msg, hdr);
7690 return genlmsg_reply(msg, info);
7691
7692nla_put_failure:
7693 nlmsg_free(msg);
7694 return -ENOBUFS;
7695}
7696
Johannes Berg2a0e0472013-01-23 22:57:40 +01007697static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7698 struct nlattr *attr,
7699 struct cfg80211_wowlan *trig)
7700{
7701 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7702 struct cfg80211_wowlan_tcp *cfg;
7703 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7704 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7705 u32 size;
7706 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7707 int err, port;
7708
7709 if (!rdev->wiphy.wowlan.tcp)
7710 return -EINVAL;
7711
7712 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7713 nla_data(attr), nla_len(attr),
7714 nl80211_wowlan_tcp_policy);
7715 if (err)
7716 return err;
7717
7718 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7719 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7720 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7721 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7722 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7723 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7724 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7725 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7726 return -EINVAL;
7727
7728 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7729 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7730 return -EINVAL;
7731
7732 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg723d5682013-02-26 13:56:40 +01007733 rdev->wiphy.wowlan.tcp->data_interval_max ||
7734 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007735 return -EINVAL;
7736
7737 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7738 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7739 return -EINVAL;
7740
7741 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7742 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7743 return -EINVAL;
7744
7745 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7746 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7747
7748 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7749 tokens_size = tokln - sizeof(*tok);
7750
7751 if (!tok->len || tokens_size % tok->len)
7752 return -EINVAL;
7753 if (!rdev->wiphy.wowlan.tcp->tok)
7754 return -EINVAL;
7755 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7756 return -EINVAL;
7757 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7758 return -EINVAL;
7759 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7760 return -EINVAL;
7761 if (tok->offset + tok->len > data_size)
7762 return -EINVAL;
7763 }
7764
7765 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7766 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7767 if (!rdev->wiphy.wowlan.tcp->seq)
7768 return -EINVAL;
7769 if (seq->len == 0 || seq->len > 4)
7770 return -EINVAL;
7771 if (seq->len + seq->offset > data_size)
7772 return -EINVAL;
7773 }
7774
7775 size = sizeof(*cfg);
7776 size += data_size;
7777 size += wake_size + wake_mask_size;
7778 size += tokens_size;
7779
7780 cfg = kzalloc(size, GFP_KERNEL);
7781 if (!cfg)
7782 return -ENOMEM;
7783 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7784 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7785 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7786 ETH_ALEN);
7787 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7788 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7789 else
7790 port = 0;
7791#ifdef CONFIG_INET
7792 /* allocate a socket and port for it and use it */
7793 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7794 IPPROTO_TCP, &cfg->sock, 1);
7795 if (err) {
7796 kfree(cfg);
7797 return err;
7798 }
7799 if (inet_csk_get_port(cfg->sock->sk, port)) {
7800 sock_release(cfg->sock);
7801 kfree(cfg);
7802 return -EADDRINUSE;
7803 }
7804 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7805#else
7806 if (!port) {
7807 kfree(cfg);
7808 return -EINVAL;
7809 }
7810 cfg->src_port = port;
7811#endif
7812
7813 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7814 cfg->payload_len = data_size;
7815 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7816 memcpy((void *)cfg->payload,
7817 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7818 data_size);
7819 if (seq)
7820 cfg->payload_seq = *seq;
7821 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7822 cfg->wake_len = wake_size;
7823 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7824 memcpy((void *)cfg->wake_data,
7825 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7826 wake_size);
7827 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7828 data_size + wake_size;
7829 memcpy((void *)cfg->wake_mask,
7830 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7831 wake_mask_size);
7832 if (tok) {
7833 cfg->tokens_size = tokens_size;
7834 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7835 }
7836
7837 trig->tcp = cfg;
7838
7839 return 0;
7840}
7841
Johannes Bergff1b6e62011-05-04 15:37:28 +02007842static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7843{
7844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7845 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007846 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007847 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007848 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7849 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02007850 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007851
Johannes Berg2a0e0472013-01-23 22:57:40 +01007852 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7853 !rdev->wiphy.wowlan.tcp)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007854 return -EOPNOTSUPP;
7855
Johannes Bergae33bd82012-07-12 16:25:02 +02007856 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7857 cfg80211_rdev_free_wowlan(rdev);
7858 rdev->wowlan = NULL;
7859 goto set_wakeup;
7860 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007861
7862 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7863 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7864 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7865 nl80211_wowlan_policy);
7866 if (err)
7867 return err;
7868
7869 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7870 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7871 return -EINVAL;
7872 new_triggers.any = true;
7873 }
7874
7875 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7876 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7877 return -EINVAL;
7878 new_triggers.disconnect = true;
7879 }
7880
7881 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7882 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7883 return -EINVAL;
7884 new_triggers.magic_pkt = true;
7885 }
7886
Johannes Berg77dbbb12011-07-13 10:48:55 +02007887 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7888 return -EINVAL;
7889
7890 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7891 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7892 return -EINVAL;
7893 new_triggers.gtk_rekey_failure = true;
7894 }
7895
7896 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7897 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7898 return -EINVAL;
7899 new_triggers.eap_identity_req = true;
7900 }
7901
7902 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7903 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7904 return -EINVAL;
7905 new_triggers.four_way_handshake = true;
7906 }
7907
7908 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7909 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7910 return -EINVAL;
7911 new_triggers.rfkill_release = true;
7912 }
7913
Johannes Bergff1b6e62011-05-04 15:37:28 +02007914 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7915 struct nlattr *pat;
7916 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007917 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007918 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7919
7920 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7921 rem)
7922 n_patterns++;
7923 if (n_patterns > wowlan->n_patterns)
7924 return -EINVAL;
7925
7926 new_triggers.patterns = kcalloc(n_patterns,
7927 sizeof(new_triggers.patterns[0]),
7928 GFP_KERNEL);
7929 if (!new_triggers.patterns)
7930 return -ENOMEM;
7931
7932 new_triggers.n_patterns = n_patterns;
7933 i = 0;
7934
7935 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7936 rem) {
7937 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7938 nla_data(pat), nla_len(pat), NULL);
7939 err = -EINVAL;
7940 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7941 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7942 goto error;
7943 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7944 mask_len = DIV_ROUND_UP(pat_len, 8);
7945 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7946 mask_len)
7947 goto error;
7948 if (pat_len > wowlan->pattern_max_len ||
7949 pat_len < wowlan->pattern_min_len)
7950 goto error;
7951
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007952 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7953 pkt_offset = 0;
7954 else
7955 pkt_offset = nla_get_u32(
7956 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7957 if (pkt_offset > wowlan->max_pkt_offset)
7958 goto error;
7959 new_triggers.patterns[i].pkt_offset = pkt_offset;
7960
Johannes Bergff1b6e62011-05-04 15:37:28 +02007961 new_triggers.patterns[i].mask =
7962 kmalloc(mask_len + pat_len, GFP_KERNEL);
7963 if (!new_triggers.patterns[i].mask) {
7964 err = -ENOMEM;
7965 goto error;
7966 }
7967 new_triggers.patterns[i].pattern =
7968 new_triggers.patterns[i].mask + mask_len;
7969 memcpy(new_triggers.patterns[i].mask,
7970 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7971 mask_len);
7972 new_triggers.patterns[i].pattern_len = pat_len;
7973 memcpy(new_triggers.patterns[i].pattern,
7974 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7975 pat_len);
7976 i++;
7977 }
7978 }
7979
Johannes Berg2a0e0472013-01-23 22:57:40 +01007980 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7981 err = nl80211_parse_wowlan_tcp(
7982 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7983 &new_triggers);
7984 if (err)
7985 goto error;
7986 }
7987
Johannes Bergae33bd82012-07-12 16:25:02 +02007988 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7989 if (!ntrig) {
7990 err = -ENOMEM;
7991 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007992 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007993 cfg80211_rdev_free_wowlan(rdev);
7994 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007995
Johannes Bergae33bd82012-07-12 16:25:02 +02007996 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02007997 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03007998 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02007999
Johannes Bergff1b6e62011-05-04 15:37:28 +02008000 return 0;
8001 error:
8002 for (i = 0; i < new_triggers.n_patterns; i++)
8003 kfree(new_triggers.patterns[i].mask);
8004 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008005 if (new_triggers.tcp && new_triggers.tcp->sock)
8006 sock_release(new_triggers.tcp->sock);
8007 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008008 return err;
8009}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008010#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008011
Johannes Berge5497d72011-07-05 16:35:40 +02008012static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8013{
8014 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8015 struct net_device *dev = info->user_ptr[1];
8016 struct wireless_dev *wdev = dev->ieee80211_ptr;
8017 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8018 struct cfg80211_gtk_rekey_data rekey_data;
8019 int err;
8020
8021 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8022 return -EINVAL;
8023
8024 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8025 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8026 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8027 nl80211_rekey_policy);
8028 if (err)
8029 return err;
8030
8031 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8032 return -ERANGE;
8033 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8034 return -ERANGE;
8035 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8036 return -ERANGE;
8037
8038 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8039 NL80211_KEK_LEN);
8040 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8041 NL80211_KCK_LEN);
8042 memcpy(rekey_data.replay_ctr,
8043 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8044 NL80211_REPLAY_CTR_LEN);
8045
8046 wdev_lock(wdev);
8047 if (!wdev->current_bss) {
8048 err = -ENOTCONN;
8049 goto out;
8050 }
8051
8052 if (!rdev->ops->set_rekey_data) {
8053 err = -EOPNOTSUPP;
8054 goto out;
8055 }
8056
Hila Gonene35e4d22012-06-27 17:19:42 +03008057 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008058 out:
8059 wdev_unlock(wdev);
8060 return err;
8061}
8062
Johannes Berg28946da2011-11-04 11:18:12 +01008063static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8064 struct genl_info *info)
8065{
8066 struct net_device *dev = info->user_ptr[1];
8067 struct wireless_dev *wdev = dev->ieee80211_ptr;
8068
8069 if (wdev->iftype != NL80211_IFTYPE_AP &&
8070 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8071 return -EINVAL;
8072
Eric W. Biederman15e47302012-09-07 20:12:54 +00008073 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008074 return -EBUSY;
8075
Eric W. Biederman15e47302012-09-07 20:12:54 +00008076 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008077 return 0;
8078}
8079
Johannes Berg7f6cf312011-11-04 11:18:15 +01008080static int nl80211_probe_client(struct sk_buff *skb,
8081 struct genl_info *info)
8082{
8083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8084 struct net_device *dev = info->user_ptr[1];
8085 struct wireless_dev *wdev = dev->ieee80211_ptr;
8086 struct sk_buff *msg;
8087 void *hdr;
8088 const u8 *addr;
8089 u64 cookie;
8090 int err;
8091
8092 if (wdev->iftype != NL80211_IFTYPE_AP &&
8093 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8094 return -EOPNOTSUPP;
8095
8096 if (!info->attrs[NL80211_ATTR_MAC])
8097 return -EINVAL;
8098
8099 if (!rdev->ops->probe_client)
8100 return -EOPNOTSUPP;
8101
8102 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8103 if (!msg)
8104 return -ENOMEM;
8105
Eric W. Biederman15e47302012-09-07 20:12:54 +00008106 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008107 NL80211_CMD_PROBE_CLIENT);
8108
8109 if (IS_ERR(hdr)) {
8110 err = PTR_ERR(hdr);
8111 goto free_msg;
8112 }
8113
8114 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8115
Hila Gonene35e4d22012-06-27 17:19:42 +03008116 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008117 if (err)
8118 goto free_msg;
8119
David S. Miller9360ffd2012-03-29 04:41:26 -04008120 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8121 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008122
8123 genlmsg_end(msg, hdr);
8124
8125 return genlmsg_reply(msg, info);
8126
8127 nla_put_failure:
8128 err = -ENOBUFS;
8129 free_msg:
8130 nlmsg_free(msg);
8131 return err;
8132}
8133
Johannes Berg5e7602302011-11-04 11:18:17 +01008134static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8135{
8136 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008137 struct cfg80211_beacon_registration *reg, *nreg;
8138 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008139
8140 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8141 return -EOPNOTSUPP;
8142
Ben Greear37c73b52012-10-26 14:49:25 -07008143 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8144 if (!nreg)
8145 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008146
Ben Greear37c73b52012-10-26 14:49:25 -07008147 /* First, check if already registered. */
8148 spin_lock_bh(&rdev->beacon_registrations_lock);
8149 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8150 if (reg->nlportid == info->snd_portid) {
8151 rv = -EALREADY;
8152 goto out_err;
8153 }
8154 }
8155 /* Add it to the list */
8156 nreg->nlportid = info->snd_portid;
8157 list_add(&nreg->list, &rdev->beacon_registrations);
8158
8159 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008160
8161 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008162out_err:
8163 spin_unlock_bh(&rdev->beacon_registrations_lock);
8164 kfree(nreg);
8165 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008166}
8167
Johannes Berg98104fde2012-06-16 00:19:54 +02008168static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8169{
8170 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8171 struct wireless_dev *wdev = info->user_ptr[1];
8172 int err;
8173
8174 if (!rdev->ops->start_p2p_device)
8175 return -EOPNOTSUPP;
8176
8177 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8178 return -EOPNOTSUPP;
8179
8180 if (wdev->p2p_started)
8181 return 0;
8182
8183 mutex_lock(&rdev->devlist_mtx);
8184 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8185 mutex_unlock(&rdev->devlist_mtx);
8186 if (err)
8187 return err;
8188
Johannes Bergeeb126e2012-10-23 15:16:50 +02008189 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008190 if (err)
8191 return err;
8192
8193 wdev->p2p_started = true;
8194 mutex_lock(&rdev->devlist_mtx);
8195 rdev->opencount++;
8196 mutex_unlock(&rdev->devlist_mtx);
8197
8198 return 0;
8199}
8200
8201static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8202{
8203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8204 struct wireless_dev *wdev = info->user_ptr[1];
8205
8206 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8207 return -EOPNOTSUPP;
8208
8209 if (!rdev->ops->stop_p2p_device)
8210 return -EOPNOTSUPP;
8211
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008212 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +01008213 mutex_lock(&rdev->sched_scan_mtx);
8214 cfg80211_stop_p2p_device(rdev, wdev);
8215 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg65e8d5b2013-04-19 12:18:19 +02008216 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg98104fde2012-06-16 00:19:54 +02008217
8218 return 0;
8219}
8220
Johannes Berg3713b4e2013-02-14 16:19:38 +01008221static int nl80211_get_protocol_features(struct sk_buff *skb,
8222 struct genl_info *info)
8223{
8224 void *hdr;
8225 struct sk_buff *msg;
8226
8227 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8228 if (!msg)
8229 return -ENOMEM;
8230
8231 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8232 NL80211_CMD_GET_PROTOCOL_FEATURES);
8233 if (!hdr)
8234 goto nla_put_failure;
8235
8236 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8237 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8238 goto nla_put_failure;
8239
8240 genlmsg_end(msg, hdr);
8241 return genlmsg_reply(msg, info);
8242
8243 nla_put_failure:
8244 kfree_skb(msg);
8245 return -ENOBUFS;
8246}
8247
Jouni Malinen355199e2013-02-27 17:14:27 +02008248static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8249{
8250 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8251 struct cfg80211_update_ft_ies_params ft_params;
8252 struct net_device *dev = info->user_ptr[1];
8253
8254 if (!rdev->ops->update_ft_ies)
8255 return -EOPNOTSUPP;
8256
8257 if (!info->attrs[NL80211_ATTR_MDID] ||
8258 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8259 return -EINVAL;
8260
8261 memset(&ft_params, 0, sizeof(ft_params));
8262 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8263 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8264 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8265
8266 return rdev_update_ft_ies(rdev, dev, &ft_params);
8267}
8268
Arend van Spriel5de17982013-04-18 15:49:00 +02008269static int nl80211_crit_protocol_start(struct sk_buff *skb,
8270 struct genl_info *info)
8271{
8272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8273 struct wireless_dev *wdev = info->user_ptr[1];
8274 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8275 u16 duration;
8276 int ret;
8277
8278 if (!rdev->ops->crit_proto_start)
8279 return -EOPNOTSUPP;
8280
8281 if (WARN_ON(!rdev->ops->crit_proto_stop))
8282 return -EINVAL;
8283
8284 if (rdev->crit_proto_nlportid)
8285 return -EBUSY;
8286
8287 /* determine protocol if provided */
8288 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8289 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8290
8291 if (proto >= NUM_NL80211_CRIT_PROTO)
8292 return -EINVAL;
8293
8294 /* timeout must be provided */
8295 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8296 return -EINVAL;
8297
8298 duration =
8299 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8300
8301 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8302 return -ERANGE;
8303
8304 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8305 if (!ret)
8306 rdev->crit_proto_nlportid = info->snd_portid;
8307
8308 return ret;
8309}
8310
8311static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8312 struct genl_info *info)
8313{
8314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8315 struct wireless_dev *wdev = info->user_ptr[1];
8316
8317 if (!rdev->ops->crit_proto_stop)
8318 return -EOPNOTSUPP;
8319
8320 if (rdev->crit_proto_nlportid) {
8321 rdev->crit_proto_nlportid = 0;
8322 rdev_crit_proto_stop(rdev, wdev);
8323 }
8324 return 0;
8325}
8326
Johannes Berg4c476992010-10-04 21:36:35 +02008327#define NL80211_FLAG_NEED_WIPHY 0x01
8328#define NL80211_FLAG_NEED_NETDEV 0x02
8329#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008330#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8331#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8332 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008333#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008334/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008335#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8336 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008337
8338static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8339 struct genl_info *info)
8340{
8341 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008342 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008343 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008344 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8345
8346 if (rtnl)
8347 rtnl_lock();
8348
8349 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008350 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008351 if (IS_ERR(rdev)) {
8352 if (rtnl)
8353 rtnl_unlock();
8354 return PTR_ERR(rdev);
8355 }
8356 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008357 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8358 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008359 mutex_lock(&cfg80211_mutex);
8360 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8361 info->attrs);
8362 if (IS_ERR(wdev)) {
8363 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02008364 if (rtnl)
8365 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008366 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008367 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008368
Johannes Berg89a54e42012-06-15 14:33:17 +02008369 dev = wdev->netdev;
8370 rdev = wiphy_to_dev(wdev->wiphy);
8371
Johannes Berg1bf614e2012-06-15 15:23:36 +02008372 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8373 if (!dev) {
8374 mutex_unlock(&cfg80211_mutex);
8375 if (rtnl)
8376 rtnl_unlock();
8377 return -EINVAL;
8378 }
8379
8380 info->user_ptr[1] = dev;
8381 } else {
8382 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008383 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008384
Johannes Berg1bf614e2012-06-15 15:23:36 +02008385 if (dev) {
8386 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8387 !netif_running(dev)) {
8388 mutex_unlock(&cfg80211_mutex);
8389 if (rtnl)
8390 rtnl_unlock();
8391 return -ENETDOWN;
8392 }
8393
8394 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008395 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8396 if (!wdev->p2p_started) {
8397 mutex_unlock(&cfg80211_mutex);
8398 if (rtnl)
8399 rtnl_unlock();
8400 return -ENETDOWN;
8401 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008402 }
8403
Johannes Berg89a54e42012-06-15 14:33:17 +02008404 cfg80211_lock_rdev(rdev);
8405
8406 mutex_unlock(&cfg80211_mutex);
8407
Johannes Berg4c476992010-10-04 21:36:35 +02008408 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008409 }
8410
8411 return 0;
8412}
8413
8414static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8415 struct genl_info *info)
8416{
8417 if (info->user_ptr[0])
8418 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02008419 if (info->user_ptr[1]) {
8420 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8421 struct wireless_dev *wdev = info->user_ptr[1];
8422
8423 if (wdev->netdev)
8424 dev_put(wdev->netdev);
8425 } else {
8426 dev_put(info->user_ptr[1]);
8427 }
8428 }
Johannes Berg4c476992010-10-04 21:36:35 +02008429 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8430 rtnl_unlock();
8431}
8432
Johannes Berg55682962007-09-20 13:09:35 -04008433static struct genl_ops nl80211_ops[] = {
8434 {
8435 .cmd = NL80211_CMD_GET_WIPHY,
8436 .doit = nl80211_get_wiphy,
8437 .dumpit = nl80211_dump_wiphy,
8438 .policy = nl80211_policy,
8439 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008440 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04008441 },
8442 {
8443 .cmd = NL80211_CMD_SET_WIPHY,
8444 .doit = nl80211_set_wiphy,
8445 .policy = nl80211_policy,
8446 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008447 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008448 },
8449 {
8450 .cmd = NL80211_CMD_GET_INTERFACE,
8451 .doit = nl80211_get_interface,
8452 .dumpit = nl80211_dump_interface,
8453 .policy = nl80211_policy,
8454 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02008455 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04008456 },
8457 {
8458 .cmd = NL80211_CMD_SET_INTERFACE,
8459 .doit = nl80211_set_interface,
8460 .policy = nl80211_policy,
8461 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008462 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8463 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008464 },
8465 {
8466 .cmd = NL80211_CMD_NEW_INTERFACE,
8467 .doit = nl80211_new_interface,
8468 .policy = nl80211_policy,
8469 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008470 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8471 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008472 },
8473 {
8474 .cmd = NL80211_CMD_DEL_INTERFACE,
8475 .doit = nl80211_del_interface,
8476 .policy = nl80211_policy,
8477 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008478 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008479 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008480 },
Johannes Berg41ade002007-12-19 02:03:29 +01008481 {
8482 .cmd = NL80211_CMD_GET_KEY,
8483 .doit = nl80211_get_key,
8484 .policy = nl80211_policy,
8485 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008487 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008488 },
8489 {
8490 .cmd = NL80211_CMD_SET_KEY,
8491 .doit = nl80211_set_key,
8492 .policy = nl80211_policy,
8493 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008495 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008496 },
8497 {
8498 .cmd = NL80211_CMD_NEW_KEY,
8499 .doit = nl80211_new_key,
8500 .policy = nl80211_policy,
8501 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008503 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008504 },
8505 {
8506 .cmd = NL80211_CMD_DEL_KEY,
8507 .doit = nl80211_del_key,
8508 .policy = nl80211_policy,
8509 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008511 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008512 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008513 {
8514 .cmd = NL80211_CMD_SET_BEACON,
8515 .policy = nl80211_policy,
8516 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008517 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008519 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008520 },
8521 {
Johannes Berg88600202012-02-13 15:17:18 +01008522 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008523 .policy = nl80211_policy,
8524 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008525 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008527 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008528 },
8529 {
Johannes Berg88600202012-02-13 15:17:18 +01008530 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008531 .policy = nl80211_policy,
8532 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008533 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008535 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008536 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008537 {
8538 .cmd = NL80211_CMD_GET_STATION,
8539 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008540 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008541 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008542 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8543 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008544 },
8545 {
8546 .cmd = NL80211_CMD_SET_STATION,
8547 .doit = nl80211_set_station,
8548 .policy = nl80211_policy,
8549 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008551 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008552 },
8553 {
8554 .cmd = NL80211_CMD_NEW_STATION,
8555 .doit = nl80211_new_station,
8556 .policy = nl80211_policy,
8557 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008559 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008560 },
8561 {
8562 .cmd = NL80211_CMD_DEL_STATION,
8563 .doit = nl80211_del_station,
8564 .policy = nl80211_policy,
8565 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008567 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008568 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008569 {
8570 .cmd = NL80211_CMD_GET_MPATH,
8571 .doit = nl80211_get_mpath,
8572 .dumpit = nl80211_dump_mpath,
8573 .policy = nl80211_policy,
8574 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008575 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008576 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008577 },
8578 {
8579 .cmd = NL80211_CMD_SET_MPATH,
8580 .doit = nl80211_set_mpath,
8581 .policy = nl80211_policy,
8582 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008584 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008585 },
8586 {
8587 .cmd = NL80211_CMD_NEW_MPATH,
8588 .doit = nl80211_new_mpath,
8589 .policy = nl80211_policy,
8590 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008592 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008593 },
8594 {
8595 .cmd = NL80211_CMD_DEL_MPATH,
8596 .doit = nl80211_del_mpath,
8597 .policy = nl80211_policy,
8598 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008600 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008601 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008602 {
8603 .cmd = NL80211_CMD_SET_BSS,
8604 .doit = nl80211_set_bss,
8605 .policy = nl80211_policy,
8606 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008607 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008608 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008609 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008610 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008611 .cmd = NL80211_CMD_GET_REG,
8612 .doit = nl80211_get_reg,
8613 .policy = nl80211_policy,
8614 /* can be retrieved by unprivileged users */
8615 },
8616 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008617 .cmd = NL80211_CMD_SET_REG,
8618 .doit = nl80211_set_reg,
8619 .policy = nl80211_policy,
8620 .flags = GENL_ADMIN_PERM,
8621 },
8622 {
8623 .cmd = NL80211_CMD_REQ_SET_REG,
8624 .doit = nl80211_req_set_reg,
8625 .policy = nl80211_policy,
8626 .flags = GENL_ADMIN_PERM,
8627 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008628 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008629 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8630 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008631 .policy = nl80211_policy,
8632 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008634 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008635 },
8636 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008637 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8638 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008639 .policy = nl80211_policy,
8640 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008641 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008642 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008643 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008644 {
Johannes Berg2a519312009-02-10 21:25:55 +01008645 .cmd = NL80211_CMD_TRIGGER_SCAN,
8646 .doit = nl80211_trigger_scan,
8647 .policy = nl80211_policy,
8648 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008649 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008650 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008651 },
8652 {
8653 .cmd = NL80211_CMD_GET_SCAN,
8654 .policy = nl80211_policy,
8655 .dumpit = nl80211_dump_scan,
8656 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008657 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008658 .cmd = NL80211_CMD_START_SCHED_SCAN,
8659 .doit = nl80211_start_sched_scan,
8660 .policy = nl80211_policy,
8661 .flags = GENL_ADMIN_PERM,
8662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8663 NL80211_FLAG_NEED_RTNL,
8664 },
8665 {
8666 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8667 .doit = nl80211_stop_sched_scan,
8668 .policy = nl80211_policy,
8669 .flags = GENL_ADMIN_PERM,
8670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8671 NL80211_FLAG_NEED_RTNL,
8672 },
8673 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008674 .cmd = NL80211_CMD_AUTHENTICATE,
8675 .doit = nl80211_authenticate,
8676 .policy = nl80211_policy,
8677 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008678 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008679 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008680 },
8681 {
8682 .cmd = NL80211_CMD_ASSOCIATE,
8683 .doit = nl80211_associate,
8684 .policy = nl80211_policy,
8685 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008687 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008688 },
8689 {
8690 .cmd = NL80211_CMD_DEAUTHENTICATE,
8691 .doit = nl80211_deauthenticate,
8692 .policy = nl80211_policy,
8693 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008695 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008696 },
8697 {
8698 .cmd = NL80211_CMD_DISASSOCIATE,
8699 .doit = nl80211_disassociate,
8700 .policy = nl80211_policy,
8701 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008703 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008704 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008705 {
8706 .cmd = NL80211_CMD_JOIN_IBSS,
8707 .doit = nl80211_join_ibss,
8708 .policy = nl80211_policy,
8709 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008711 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008712 },
8713 {
8714 .cmd = NL80211_CMD_LEAVE_IBSS,
8715 .doit = nl80211_leave_ibss,
8716 .policy = nl80211_policy,
8717 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008718 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008719 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008720 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008721#ifdef CONFIG_NL80211_TESTMODE
8722 {
8723 .cmd = NL80211_CMD_TESTMODE,
8724 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008725 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008726 .policy = nl80211_policy,
8727 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008728 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8729 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008730 },
8731#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008732 {
8733 .cmd = NL80211_CMD_CONNECT,
8734 .doit = nl80211_connect,
8735 .policy = nl80211_policy,
8736 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008737 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008738 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008739 },
8740 {
8741 .cmd = NL80211_CMD_DISCONNECT,
8742 .doit = nl80211_disconnect,
8743 .policy = nl80211_policy,
8744 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008745 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008746 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008747 },
Johannes Berg463d0182009-07-14 00:33:35 +02008748 {
8749 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8750 .doit = nl80211_wiphy_netns,
8751 .policy = nl80211_policy,
8752 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008753 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8754 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008755 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008756 {
8757 .cmd = NL80211_CMD_GET_SURVEY,
8758 .policy = nl80211_policy,
8759 .dumpit = nl80211_dump_survey,
8760 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008761 {
8762 .cmd = NL80211_CMD_SET_PMKSA,
8763 .doit = nl80211_setdel_pmksa,
8764 .policy = nl80211_policy,
8765 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008766 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008767 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008768 },
8769 {
8770 .cmd = NL80211_CMD_DEL_PMKSA,
8771 .doit = nl80211_setdel_pmksa,
8772 .policy = nl80211_policy,
8773 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008775 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008776 },
8777 {
8778 .cmd = NL80211_CMD_FLUSH_PMKSA,
8779 .doit = nl80211_flush_pmksa,
8780 .policy = nl80211_policy,
8781 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008782 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008783 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008784 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008785 {
8786 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8787 .doit = nl80211_remain_on_channel,
8788 .policy = nl80211_policy,
8789 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008790 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008791 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008792 },
8793 {
8794 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8795 .doit = nl80211_cancel_remain_on_channel,
8796 .policy = nl80211_policy,
8797 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008798 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008799 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008800 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008801 {
8802 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8803 .doit = nl80211_set_tx_bitrate_mask,
8804 .policy = nl80211_policy,
8805 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008806 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8807 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008808 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008809 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008810 .cmd = NL80211_CMD_REGISTER_FRAME,
8811 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008812 .policy = nl80211_policy,
8813 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008814 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008815 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008816 },
8817 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008818 .cmd = NL80211_CMD_FRAME,
8819 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008820 .policy = nl80211_policy,
8821 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008822 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008823 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008824 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008825 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008826 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8827 .doit = nl80211_tx_mgmt_cancel_wait,
8828 .policy = nl80211_policy,
8829 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008830 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008831 NL80211_FLAG_NEED_RTNL,
8832 },
8833 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008834 .cmd = NL80211_CMD_SET_POWER_SAVE,
8835 .doit = nl80211_set_power_save,
8836 .policy = nl80211_policy,
8837 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008838 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8839 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008840 },
8841 {
8842 .cmd = NL80211_CMD_GET_POWER_SAVE,
8843 .doit = nl80211_get_power_save,
8844 .policy = nl80211_policy,
8845 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008846 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8847 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008848 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008849 {
8850 .cmd = NL80211_CMD_SET_CQM,
8851 .doit = nl80211_set_cqm,
8852 .policy = nl80211_policy,
8853 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008854 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8855 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008856 },
Johannes Bergf444de02010-05-05 15:25:02 +02008857 {
8858 .cmd = NL80211_CMD_SET_CHANNEL,
8859 .doit = nl80211_set_channel,
8860 .policy = nl80211_policy,
8861 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008862 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8863 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008864 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008865 {
8866 .cmd = NL80211_CMD_SET_WDS_PEER,
8867 .doit = nl80211_set_wds_peer,
8868 .policy = nl80211_policy,
8869 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008870 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8871 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008872 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008873 {
8874 .cmd = NL80211_CMD_JOIN_MESH,
8875 .doit = nl80211_join_mesh,
8876 .policy = nl80211_policy,
8877 .flags = GENL_ADMIN_PERM,
8878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8879 NL80211_FLAG_NEED_RTNL,
8880 },
8881 {
8882 .cmd = NL80211_CMD_LEAVE_MESH,
8883 .doit = nl80211_leave_mesh,
8884 .policy = nl80211_policy,
8885 .flags = GENL_ADMIN_PERM,
8886 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8887 NL80211_FLAG_NEED_RTNL,
8888 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008889#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008890 {
8891 .cmd = NL80211_CMD_GET_WOWLAN,
8892 .doit = nl80211_get_wowlan,
8893 .policy = nl80211_policy,
8894 /* can be retrieved by unprivileged users */
8895 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8896 NL80211_FLAG_NEED_RTNL,
8897 },
8898 {
8899 .cmd = NL80211_CMD_SET_WOWLAN,
8900 .doit = nl80211_set_wowlan,
8901 .policy = nl80211_policy,
8902 .flags = GENL_ADMIN_PERM,
8903 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8904 NL80211_FLAG_NEED_RTNL,
8905 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008906#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008907 {
8908 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8909 .doit = nl80211_set_rekey_data,
8910 .policy = nl80211_policy,
8911 .flags = GENL_ADMIN_PERM,
8912 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8913 NL80211_FLAG_NEED_RTNL,
8914 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008915 {
8916 .cmd = NL80211_CMD_TDLS_MGMT,
8917 .doit = nl80211_tdls_mgmt,
8918 .policy = nl80211_policy,
8919 .flags = GENL_ADMIN_PERM,
8920 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8921 NL80211_FLAG_NEED_RTNL,
8922 },
8923 {
8924 .cmd = NL80211_CMD_TDLS_OPER,
8925 .doit = nl80211_tdls_oper,
8926 .policy = nl80211_policy,
8927 .flags = GENL_ADMIN_PERM,
8928 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8929 NL80211_FLAG_NEED_RTNL,
8930 },
Johannes Berg28946da2011-11-04 11:18:12 +01008931 {
8932 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8933 .doit = nl80211_register_unexpected_frame,
8934 .policy = nl80211_policy,
8935 .flags = GENL_ADMIN_PERM,
8936 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8937 NL80211_FLAG_NEED_RTNL,
8938 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008939 {
8940 .cmd = NL80211_CMD_PROBE_CLIENT,
8941 .doit = nl80211_probe_client,
8942 .policy = nl80211_policy,
8943 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008944 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008945 NL80211_FLAG_NEED_RTNL,
8946 },
Johannes Berg5e7602302011-11-04 11:18:17 +01008947 {
8948 .cmd = NL80211_CMD_REGISTER_BEACONS,
8949 .doit = nl80211_register_beacons,
8950 .policy = nl80211_policy,
8951 .flags = GENL_ADMIN_PERM,
8952 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8953 NL80211_FLAG_NEED_RTNL,
8954 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008955 {
8956 .cmd = NL80211_CMD_SET_NOACK_MAP,
8957 .doit = nl80211_set_noack_map,
8958 .policy = nl80211_policy,
8959 .flags = GENL_ADMIN_PERM,
8960 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8961 NL80211_FLAG_NEED_RTNL,
8962 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008963 {
8964 .cmd = NL80211_CMD_START_P2P_DEVICE,
8965 .doit = nl80211_start_p2p_device,
8966 .policy = nl80211_policy,
8967 .flags = GENL_ADMIN_PERM,
8968 .internal_flags = NL80211_FLAG_NEED_WDEV |
8969 NL80211_FLAG_NEED_RTNL,
8970 },
8971 {
8972 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8973 .doit = nl80211_stop_p2p_device,
8974 .policy = nl80211_policy,
8975 .flags = GENL_ADMIN_PERM,
8976 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8977 NL80211_FLAG_NEED_RTNL,
8978 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008979 {
8980 .cmd = NL80211_CMD_SET_MCAST_RATE,
8981 .doit = nl80211_set_mcast_rate,
8982 .policy = nl80211_policy,
8983 .flags = GENL_ADMIN_PERM,
8984 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8985 NL80211_FLAG_NEED_RTNL,
8986 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308987 {
8988 .cmd = NL80211_CMD_SET_MAC_ACL,
8989 .doit = nl80211_set_mac_acl,
8990 .policy = nl80211_policy,
8991 .flags = GENL_ADMIN_PERM,
8992 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8993 NL80211_FLAG_NEED_RTNL,
8994 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008995 {
8996 .cmd = NL80211_CMD_RADAR_DETECT,
8997 .doit = nl80211_start_radar_detection,
8998 .policy = nl80211_policy,
8999 .flags = GENL_ADMIN_PERM,
9000 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9001 NL80211_FLAG_NEED_RTNL,
9002 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009003 {
9004 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9005 .doit = nl80211_get_protocol_features,
9006 .policy = nl80211_policy,
9007 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009008 {
9009 .cmd = NL80211_CMD_UPDATE_FT_IES,
9010 .doit = nl80211_update_ft_ies,
9011 .policy = nl80211_policy,
9012 .flags = GENL_ADMIN_PERM,
9013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9014 NL80211_FLAG_NEED_RTNL,
9015 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009016 {
9017 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9018 .doit = nl80211_crit_protocol_start,
9019 .policy = nl80211_policy,
9020 .flags = GENL_ADMIN_PERM,
9021 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9022 NL80211_FLAG_NEED_RTNL,
9023 },
9024 {
9025 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9026 .doit = nl80211_crit_protocol_stop,
9027 .policy = nl80211_policy,
9028 .flags = GENL_ADMIN_PERM,
9029 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9030 NL80211_FLAG_NEED_RTNL,
9031 }
Johannes Berg55682962007-09-20 13:09:35 -04009032};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009033
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009034static struct genl_multicast_group nl80211_mlme_mcgrp = {
9035 .name = "mlme",
9036};
Johannes Berg55682962007-09-20 13:09:35 -04009037
9038/* multicast groups */
9039static struct genl_multicast_group nl80211_config_mcgrp = {
9040 .name = "config",
9041};
Johannes Berg2a519312009-02-10 21:25:55 +01009042static struct genl_multicast_group nl80211_scan_mcgrp = {
9043 .name = "scan",
9044};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009045static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9046 .name = "regulatory",
9047};
Johannes Berg55682962007-09-20 13:09:35 -04009048
9049/* notification functions */
9050
9051void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9052{
9053 struct sk_buff *msg;
9054
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009055 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009056 if (!msg)
9057 return;
9058
Johannes Berg3713b4e2013-02-14 16:19:38 +01009059 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9060 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009061 nlmsg_free(msg);
9062 return;
9063 }
9064
Johannes Berg463d0182009-07-14 00:33:35 +02009065 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9066 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009067}
9068
Johannes Berg362a4152009-05-24 16:43:15 +02009069static int nl80211_add_scan_req(struct sk_buff *msg,
9070 struct cfg80211_registered_device *rdev)
9071{
9072 struct cfg80211_scan_request *req = rdev->scan_req;
9073 struct nlattr *nest;
9074 int i;
9075
Johannes Bergf9f47522013-03-19 15:04:07 +01009076 lockdep_assert_held(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +02009077
Johannes Berg362a4152009-05-24 16:43:15 +02009078 if (WARN_ON(!req))
9079 return 0;
9080
9081 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9082 if (!nest)
9083 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009084 for (i = 0; i < req->n_ssids; i++) {
9085 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9086 goto nla_put_failure;
9087 }
Johannes Berg362a4152009-05-24 16:43:15 +02009088 nla_nest_end(msg, nest);
9089
9090 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9091 if (!nest)
9092 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009093 for (i = 0; i < req->n_channels; i++) {
9094 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9095 goto nla_put_failure;
9096 }
Johannes Berg362a4152009-05-24 16:43:15 +02009097 nla_nest_end(msg, nest);
9098
David S. Miller9360ffd2012-03-29 04:41:26 -04009099 if (req->ie &&
9100 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9101 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009102
Sam Lefflered4737712012-10-11 21:03:31 -07009103 if (req->flags)
9104 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9105
Johannes Berg362a4152009-05-24 16:43:15 +02009106 return 0;
9107 nla_put_failure:
9108 return -ENOBUFS;
9109}
9110
Johannes Berga538e2d2009-06-16 19:56:42 +02009111static int nl80211_send_scan_msg(struct sk_buff *msg,
9112 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009113 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009114 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009115 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009116{
9117 void *hdr;
9118
Eric W. Biederman15e47302012-09-07 20:12:54 +00009119 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009120 if (!hdr)
9121 return -1;
9122
David S. Miller9360ffd2012-03-29 04:41:26 -04009123 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009124 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9125 wdev->netdev->ifindex)) ||
9126 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009127 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009128
Johannes Berg362a4152009-05-24 16:43:15 +02009129 /* ignore errors and send incomplete event anyway */
9130 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009131
9132 return genlmsg_end(msg, hdr);
9133
9134 nla_put_failure:
9135 genlmsg_cancel(msg, hdr);
9136 return -EMSGSIZE;
9137}
9138
Luciano Coelho807f8a82011-05-11 17:09:35 +03009139static int
9140nl80211_send_sched_scan_msg(struct sk_buff *msg,
9141 struct cfg80211_registered_device *rdev,
9142 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009143 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009144{
9145 void *hdr;
9146
Eric W. Biederman15e47302012-09-07 20:12:54 +00009147 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009148 if (!hdr)
9149 return -1;
9150
David S. Miller9360ffd2012-03-29 04:41:26 -04009151 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9152 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9153 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009154
9155 return genlmsg_end(msg, hdr);
9156
9157 nla_put_failure:
9158 genlmsg_cancel(msg, hdr);
9159 return -EMSGSIZE;
9160}
9161
Johannes Berga538e2d2009-06-16 19:56:42 +02009162void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009163 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009164{
9165 struct sk_buff *msg;
9166
Thomas Graf58050fc2012-06-28 03:57:45 +00009167 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009168 if (!msg)
9169 return;
9170
Johannes Bergfd014282012-06-18 19:17:03 +02009171 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009172 NL80211_CMD_TRIGGER_SCAN) < 0) {
9173 nlmsg_free(msg);
9174 return;
9175 }
9176
Johannes Berg463d0182009-07-14 00:33:35 +02009177 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9178 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009179}
9180
Johannes Berg2a519312009-02-10 21:25:55 +01009181void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009182 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009183{
9184 struct sk_buff *msg;
9185
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009186 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009187 if (!msg)
9188 return;
9189
Johannes Bergfd014282012-06-18 19:17:03 +02009190 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009191 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009192 nlmsg_free(msg);
9193 return;
9194 }
9195
Johannes Berg463d0182009-07-14 00:33:35 +02009196 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9197 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009198}
9199
9200void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009201 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009202{
9203 struct sk_buff *msg;
9204
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009205 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009206 if (!msg)
9207 return;
9208
Johannes Bergfd014282012-06-18 19:17:03 +02009209 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009210 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009211 nlmsg_free(msg);
9212 return;
9213 }
9214
Johannes Berg463d0182009-07-14 00:33:35 +02009215 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9216 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009217}
9218
Luciano Coelho807f8a82011-05-11 17:09:35 +03009219void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9220 struct net_device *netdev)
9221{
9222 struct sk_buff *msg;
9223
9224 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9225 if (!msg)
9226 return;
9227
9228 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9229 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9230 nlmsg_free(msg);
9231 return;
9232 }
9233
9234 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9235 nl80211_scan_mcgrp.id, GFP_KERNEL);
9236}
9237
9238void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9239 struct net_device *netdev, u32 cmd)
9240{
9241 struct sk_buff *msg;
9242
Thomas Graf58050fc2012-06-28 03:57:45 +00009243 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009244 if (!msg)
9245 return;
9246
9247 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9248 nlmsg_free(msg);
9249 return;
9250 }
9251
9252 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9253 nl80211_scan_mcgrp.id, GFP_KERNEL);
9254}
9255
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009256/*
9257 * This can happen on global regulatory changes or device specific settings
9258 * based on custom world regulatory domains.
9259 */
9260void nl80211_send_reg_change_event(struct regulatory_request *request)
9261{
9262 struct sk_buff *msg;
9263 void *hdr;
9264
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009266 if (!msg)
9267 return;
9268
9269 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9270 if (!hdr) {
9271 nlmsg_free(msg);
9272 return;
9273 }
9274
9275 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009276 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9277 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009278
David S. Miller9360ffd2012-03-29 04:41:26 -04009279 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9280 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9281 NL80211_REGDOM_TYPE_WORLD))
9282 goto nla_put_failure;
9283 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9284 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9285 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9286 goto nla_put_failure;
9287 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9288 request->intersect) {
9289 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9290 NL80211_REGDOM_TYPE_INTERSECTION))
9291 goto nla_put_failure;
9292 } else {
9293 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9294 NL80211_REGDOM_TYPE_COUNTRY) ||
9295 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9296 request->alpha2))
9297 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009298 }
9299
Johannes Bergf4173762012-12-03 18:23:37 +01009300 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009301 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9302 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009303
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009304 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009305
Johannes Bergbc43b282009-07-25 10:54:13 +02009306 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009307 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009308 GFP_ATOMIC);
9309 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009310
9311 return;
9312
9313nla_put_failure:
9314 genlmsg_cancel(msg, hdr);
9315 nlmsg_free(msg);
9316}
9317
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009318static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9319 struct net_device *netdev,
9320 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009321 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009322{
9323 struct sk_buff *msg;
9324 void *hdr;
9325
Johannes Berge6d6e342009-07-01 21:26:47 +02009326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009327 if (!msg)
9328 return;
9329
9330 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9331 if (!hdr) {
9332 nlmsg_free(msg);
9333 return;
9334 }
9335
David S. Miller9360ffd2012-03-29 04:41:26 -04009336 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9337 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9338 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9339 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009340
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009341 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009342
Johannes Berg463d0182009-07-14 00:33:35 +02009343 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9344 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009345 return;
9346
9347 nla_put_failure:
9348 genlmsg_cancel(msg, hdr);
9349 nlmsg_free(msg);
9350}
9351
9352void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009353 struct net_device *netdev, const u8 *buf,
9354 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009355{
9356 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009357 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009358}
9359
9360void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9361 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009362 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009363{
Johannes Berge6d6e342009-07-01 21:26:47 +02009364 nl80211_send_mlme_event(rdev, netdev, buf, len,
9365 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009366}
9367
Jouni Malinen53b46b82009-03-27 20:53:56 +02009368void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009369 struct net_device *netdev, const u8 *buf,
9370 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009371{
9372 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009373 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009374}
9375
Jouni Malinen53b46b82009-03-27 20:53:56 +02009376void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9377 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009378 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009379{
9380 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009381 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009382}
9383
Johannes Berg947add32013-02-22 22:05:20 +01009384void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9385 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009386{
Johannes Berg947add32013-02-22 22:05:20 +01009387 struct wireless_dev *wdev = dev->ieee80211_ptr;
9388 struct wiphy *wiphy = wdev->wiphy;
9389 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009390
Johannes Berg947add32013-02-22 22:05:20 +01009391 trace_cfg80211_send_unprot_deauth(dev);
9392 nl80211_send_mlme_event(rdev, dev, buf, len,
9393 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009394}
Johannes Berg947add32013-02-22 22:05:20 +01009395EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9396
9397void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9398 size_t len)
9399{
9400 struct wireless_dev *wdev = dev->ieee80211_ptr;
9401 struct wiphy *wiphy = wdev->wiphy;
9402 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9403
9404 trace_cfg80211_send_unprot_disassoc(dev);
9405 nl80211_send_mlme_event(rdev, dev, buf, len,
9406 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9407}
9408EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009409
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009410static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9411 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009412 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009413{
9414 struct sk_buff *msg;
9415 void *hdr;
9416
Johannes Berge6d6e342009-07-01 21:26:47 +02009417 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009418 if (!msg)
9419 return;
9420
9421 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9422 if (!hdr) {
9423 nlmsg_free(msg);
9424 return;
9425 }
9426
David S. Miller9360ffd2012-03-29 04:41:26 -04009427 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9428 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9429 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9430 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9431 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009432
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009433 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009434
Johannes Berg463d0182009-07-14 00:33:35 +02009435 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9436 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009437 return;
9438
9439 nla_put_failure:
9440 genlmsg_cancel(msg, hdr);
9441 nlmsg_free(msg);
9442}
9443
9444void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009445 struct net_device *netdev, const u8 *addr,
9446 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009447{
9448 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009449 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009450}
9451
9452void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009453 struct net_device *netdev, const u8 *addr,
9454 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009455{
Johannes Berge6d6e342009-07-01 21:26:47 +02009456 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9457 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009458}
9459
Samuel Ortizb23aa672009-07-01 21:26:54 +02009460void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9461 struct net_device *netdev, const u8 *bssid,
9462 const u8 *req_ie, size_t req_ie_len,
9463 const u8 *resp_ie, size_t resp_ie_len,
9464 u16 status, gfp_t gfp)
9465{
9466 struct sk_buff *msg;
9467 void *hdr;
9468
Thomas Graf58050fc2012-06-28 03:57:45 +00009469 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009470 if (!msg)
9471 return;
9472
9473 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9474 if (!hdr) {
9475 nlmsg_free(msg);
9476 return;
9477 }
9478
David S. Miller9360ffd2012-03-29 04:41:26 -04009479 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9480 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9481 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9482 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9483 (req_ie &&
9484 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9485 (resp_ie &&
9486 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9487 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009488
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009489 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009490
Johannes Berg463d0182009-07-14 00:33:35 +02009491 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9492 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009493 return;
9494
9495 nla_put_failure:
9496 genlmsg_cancel(msg, hdr);
9497 nlmsg_free(msg);
9498
9499}
9500
9501void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9502 struct net_device *netdev, const u8 *bssid,
9503 const u8 *req_ie, size_t req_ie_len,
9504 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9505{
9506 struct sk_buff *msg;
9507 void *hdr;
9508
Thomas Graf58050fc2012-06-28 03:57:45 +00009509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009510 if (!msg)
9511 return;
9512
9513 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9514 if (!hdr) {
9515 nlmsg_free(msg);
9516 return;
9517 }
9518
David S. Miller9360ffd2012-03-29 04:41:26 -04009519 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9520 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9521 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9522 (req_ie &&
9523 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9524 (resp_ie &&
9525 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9526 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009527
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009528 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009529
Johannes Berg463d0182009-07-14 00:33:35 +02009530 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9531 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009532 return;
9533
9534 nla_put_failure:
9535 genlmsg_cancel(msg, hdr);
9536 nlmsg_free(msg);
9537
9538}
9539
9540void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9541 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02009542 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009543{
9544 struct sk_buff *msg;
9545 void *hdr;
9546
Thomas Graf58050fc2012-06-28 03:57:45 +00009547 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009548 if (!msg)
9549 return;
9550
9551 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9552 if (!hdr) {
9553 nlmsg_free(msg);
9554 return;
9555 }
9556
David S. Miller9360ffd2012-03-29 04:41:26 -04009557 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9558 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9559 (from_ap && reason &&
9560 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9561 (from_ap &&
9562 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9563 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9564 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009565
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009566 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009567
Johannes Berg463d0182009-07-14 00:33:35 +02009568 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9569 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009570 return;
9571
9572 nla_put_failure:
9573 genlmsg_cancel(msg, hdr);
9574 nlmsg_free(msg);
9575
9576}
9577
Johannes Berg04a773a2009-04-19 21:24:32 +02009578void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9579 struct net_device *netdev, const u8 *bssid,
9580 gfp_t gfp)
9581{
9582 struct sk_buff *msg;
9583 void *hdr;
9584
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009585 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009586 if (!msg)
9587 return;
9588
9589 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9590 if (!hdr) {
9591 nlmsg_free(msg);
9592 return;
9593 }
9594
David S. Miller9360ffd2012-03-29 04:41:26 -04009595 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9596 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9597 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9598 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009599
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009600 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009601
Johannes Berg463d0182009-07-14 00:33:35 +02009602 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9603 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009604 return;
9605
9606 nla_put_failure:
9607 genlmsg_cancel(msg, hdr);
9608 nlmsg_free(msg);
9609}
9610
Johannes Berg947add32013-02-22 22:05:20 +01009611void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9612 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009613{
Johannes Berg947add32013-02-22 22:05:20 +01009614 struct wireless_dev *wdev = dev->ieee80211_ptr;
9615 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009616 struct sk_buff *msg;
9617 void *hdr;
9618
Johannes Berg947add32013-02-22 22:05:20 +01009619 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9620 return;
9621
9622 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9623
Javier Cardonac93b5e72011-04-07 15:08:34 -07009624 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9625 if (!msg)
9626 return;
9627
9628 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9629 if (!hdr) {
9630 nlmsg_free(msg);
9631 return;
9632 }
9633
David S. Miller9360ffd2012-03-29 04:41:26 -04009634 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009635 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9636 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009637 (ie_len && ie &&
9638 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9639 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009640
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009641 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009642
9643 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9644 nl80211_mlme_mcgrp.id, gfp);
9645 return;
9646
9647 nla_put_failure:
9648 genlmsg_cancel(msg, hdr);
9649 nlmsg_free(msg);
9650}
Johannes Berg947add32013-02-22 22:05:20 +01009651EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009652
Jouni Malinena3b8b052009-03-27 21:59:49 +02009653void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9654 struct net_device *netdev, const u8 *addr,
9655 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009656 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009657{
9658 struct sk_buff *msg;
9659 void *hdr;
9660
Johannes Berge6d6e342009-07-01 21:26:47 +02009661 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009662 if (!msg)
9663 return;
9664
9665 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9666 if (!hdr) {
9667 nlmsg_free(msg);
9668 return;
9669 }
9670
David S. Miller9360ffd2012-03-29 04:41:26 -04009671 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9672 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9673 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9674 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9675 (key_id != -1 &&
9676 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9677 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9678 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009679
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009680 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009681
Johannes Berg463d0182009-07-14 00:33:35 +02009682 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9683 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009684 return;
9685
9686 nla_put_failure:
9687 genlmsg_cancel(msg, hdr);
9688 nlmsg_free(msg);
9689}
9690
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009691void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9692 struct ieee80211_channel *channel_before,
9693 struct ieee80211_channel *channel_after)
9694{
9695 struct sk_buff *msg;
9696 void *hdr;
9697 struct nlattr *nl_freq;
9698
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009699 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009700 if (!msg)
9701 return;
9702
9703 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9704 if (!hdr) {
9705 nlmsg_free(msg);
9706 return;
9707 }
9708
9709 /*
9710 * Since we are applying the beacon hint to a wiphy we know its
9711 * wiphy_idx is valid
9712 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009713 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9714 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009715
9716 /* Before */
9717 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9718 if (!nl_freq)
9719 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009720 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009721 goto nla_put_failure;
9722 nla_nest_end(msg, nl_freq);
9723
9724 /* After */
9725 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9726 if (!nl_freq)
9727 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009728 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009729 goto nla_put_failure;
9730 nla_nest_end(msg, nl_freq);
9731
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009732 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009733
Johannes Berg463d0182009-07-14 00:33:35 +02009734 rcu_read_lock();
9735 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9736 GFP_ATOMIC);
9737 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009738
9739 return;
9740
9741nla_put_failure:
9742 genlmsg_cancel(msg, hdr);
9743 nlmsg_free(msg);
9744}
9745
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009746static void nl80211_send_remain_on_chan_event(
9747 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009748 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009749 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009750 unsigned int duration, gfp_t gfp)
9751{
9752 struct sk_buff *msg;
9753 void *hdr;
9754
9755 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9756 if (!msg)
9757 return;
9758
9759 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9760 if (!hdr) {
9761 nlmsg_free(msg);
9762 return;
9763 }
9764
David S. Miller9360ffd2012-03-29 04:41:26 -04009765 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009766 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9767 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009768 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009769 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009770 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9771 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009772 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9773 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009774
David S. Miller9360ffd2012-03-29 04:41:26 -04009775 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9776 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9777 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009778
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009779 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009780
9781 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9782 nl80211_mlme_mcgrp.id, gfp);
9783 return;
9784
9785 nla_put_failure:
9786 genlmsg_cancel(msg, hdr);
9787 nlmsg_free(msg);
9788}
9789
Johannes Berg947add32013-02-22 22:05:20 +01009790void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9791 struct ieee80211_channel *chan,
9792 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009793{
Johannes Berg947add32013-02-22 22:05:20 +01009794 struct wiphy *wiphy = wdev->wiphy;
9795 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9796
9797 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009798 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009799 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009800 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009801}
Johannes Berg947add32013-02-22 22:05:20 +01009802EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009803
Johannes Berg947add32013-02-22 22:05:20 +01009804void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9805 struct ieee80211_channel *chan,
9806 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009807{
Johannes Berg947add32013-02-22 22:05:20 +01009808 struct wiphy *wiphy = wdev->wiphy;
9809 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9810
9811 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009812 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009813 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009814}
Johannes Berg947add32013-02-22 22:05:20 +01009815EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009816
Johannes Berg947add32013-02-22 22:05:20 +01009817void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9818 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009819{
Johannes Berg947add32013-02-22 22:05:20 +01009820 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9821 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009822 struct sk_buff *msg;
9823
Johannes Berg947add32013-02-22 22:05:20 +01009824 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9825
Thomas Graf58050fc2012-06-28 03:57:45 +00009826 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009827 if (!msg)
9828 return;
9829
John W. Linville66266b32012-03-15 13:25:41 -04009830 if (nl80211_send_station(msg, 0, 0, 0,
9831 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009832 nlmsg_free(msg);
9833 return;
9834 }
9835
9836 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9837 nl80211_mlme_mcgrp.id, gfp);
9838}
Johannes Berg947add32013-02-22 22:05:20 +01009839EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009840
Johannes Berg947add32013-02-22 22:05:20 +01009841void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009842{
Johannes Berg947add32013-02-22 22:05:20 +01009843 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9844 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009845 struct sk_buff *msg;
9846 void *hdr;
9847
Johannes Berg947add32013-02-22 22:05:20 +01009848 trace_cfg80211_del_sta(dev, mac_addr);
9849
Thomas Graf58050fc2012-06-28 03:57:45 +00009850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009851 if (!msg)
9852 return;
9853
9854 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9855 if (!hdr) {
9856 nlmsg_free(msg);
9857 return;
9858 }
9859
David S. Miller9360ffd2012-03-29 04:41:26 -04009860 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9861 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9862 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009863
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009864 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009865
9866 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9867 nl80211_mlme_mcgrp.id, gfp);
9868 return;
9869
9870 nla_put_failure:
9871 genlmsg_cancel(msg, hdr);
9872 nlmsg_free(msg);
9873}
Johannes Berg947add32013-02-22 22:05:20 +01009874EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009875
Johannes Berg947add32013-02-22 22:05:20 +01009876void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9877 enum nl80211_connect_failed_reason reason,
9878 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309879{
Johannes Berg947add32013-02-22 22:05:20 +01009880 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9881 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309882 struct sk_buff *msg;
9883 void *hdr;
9884
9885 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9886 if (!msg)
9887 return;
9888
9889 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9890 if (!hdr) {
9891 nlmsg_free(msg);
9892 return;
9893 }
9894
9895 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9896 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9897 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9898 goto nla_put_failure;
9899
9900 genlmsg_end(msg, hdr);
9901
9902 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9903 nl80211_mlme_mcgrp.id, gfp);
9904 return;
9905
9906 nla_put_failure:
9907 genlmsg_cancel(msg, hdr);
9908 nlmsg_free(msg);
9909}
Johannes Berg947add32013-02-22 22:05:20 +01009910EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309911
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009912static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9913 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009914{
9915 struct wireless_dev *wdev = dev->ieee80211_ptr;
9916 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9917 struct sk_buff *msg;
9918 void *hdr;
9919 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009920 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009921
Eric W. Biederman15e47302012-09-07 20:12:54 +00009922 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009923 return false;
9924
9925 msg = nlmsg_new(100, gfp);
9926 if (!msg)
9927 return true;
9928
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009929 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009930 if (!hdr) {
9931 nlmsg_free(msg);
9932 return true;
9933 }
9934
David S. Miller9360ffd2012-03-29 04:41:26 -04009935 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9936 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9937 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9938 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009939
9940 err = genlmsg_end(msg, hdr);
9941 if (err < 0) {
9942 nlmsg_free(msg);
9943 return true;
9944 }
9945
Eric W. Biederman15e47302012-09-07 20:12:54 +00009946 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009947 return true;
9948
9949 nla_put_failure:
9950 genlmsg_cancel(msg, hdr);
9951 nlmsg_free(msg);
9952 return true;
9953}
9954
Johannes Berg947add32013-02-22 22:05:20 +01009955bool cfg80211_rx_spurious_frame(struct net_device *dev,
9956 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009957{
Johannes Berg947add32013-02-22 22:05:20 +01009958 struct wireless_dev *wdev = dev->ieee80211_ptr;
9959 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009960
Johannes Berg947add32013-02-22 22:05:20 +01009961 trace_cfg80211_rx_spurious_frame(dev, addr);
9962
9963 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9964 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9965 trace_cfg80211_return_bool(false);
9966 return false;
9967 }
9968 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9969 addr, gfp);
9970 trace_cfg80211_return_bool(ret);
9971 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009972}
Johannes Berg947add32013-02-22 22:05:20 +01009973EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9974
9975bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9976 const u8 *addr, gfp_t gfp)
9977{
9978 struct wireless_dev *wdev = dev->ieee80211_ptr;
9979 bool ret;
9980
9981 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9982
9983 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9984 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9985 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9986 trace_cfg80211_return_bool(false);
9987 return false;
9988 }
9989 ret = __nl80211_unexpected_frame(dev,
9990 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9991 addr, gfp);
9992 trace_cfg80211_return_bool(ret);
9993 return ret;
9994}
9995EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009996
Johannes Berg2e161f72010-08-12 15:38:38 +02009997int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009998 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009999 int freq, int sig_dbm,
10000 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010001{
Johannes Berg71bbc992012-06-15 15:30:18 +020010002 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010003 struct sk_buff *msg;
10004 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010005
10006 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10007 if (!msg)
10008 return -ENOMEM;
10009
Johannes Berg2e161f72010-08-12 15:38:38 +020010010 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010011 if (!hdr) {
10012 nlmsg_free(msg);
10013 return -ENOMEM;
10014 }
10015
David S. Miller9360ffd2012-03-29 04:41:26 -040010016 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010017 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10018 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010019 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10020 (sig_dbm &&
10021 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10022 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10023 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010024
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010025 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010026
Eric W. Biederman15e47302012-09-07 20:12:54 +000010027 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010028
10029 nla_put_failure:
10030 genlmsg_cancel(msg, hdr);
10031 nlmsg_free(msg);
10032 return -ENOBUFS;
10033}
10034
Johannes Berg947add32013-02-22 22:05:20 +010010035void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10036 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010037{
Johannes Berg947add32013-02-22 22:05:20 +010010038 struct wiphy *wiphy = wdev->wiphy;
10039 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010040 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010041 struct sk_buff *msg;
10042 void *hdr;
10043
Johannes Berg947add32013-02-22 22:05:20 +010010044 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10045
Jouni Malinen026331c2010-02-15 12:53:10 +020010046 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10047 if (!msg)
10048 return;
10049
Johannes Berg2e161f72010-08-12 15:38:38 +020010050 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010051 if (!hdr) {
10052 nlmsg_free(msg);
10053 return;
10054 }
10055
David S. Miller9360ffd2012-03-29 04:41:26 -040010056 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010057 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10058 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010059 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10060 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10061 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10062 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010063
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010064 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010065
10066 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
10067 return;
10068
10069 nla_put_failure:
10070 genlmsg_cancel(msg, hdr);
10071 nlmsg_free(msg);
10072}
Johannes Berg947add32013-02-22 22:05:20 +010010073EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010074
Johannes Berg947add32013-02-22 22:05:20 +010010075void cfg80211_cqm_rssi_notify(struct net_device *dev,
10076 enum nl80211_cqm_rssi_threshold_event rssi_event,
10077 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010078{
Johannes Berg947add32013-02-22 22:05:20 +010010079 struct wireless_dev *wdev = dev->ieee80211_ptr;
10080 struct wiphy *wiphy = wdev->wiphy;
10081 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010082 struct sk_buff *msg;
10083 struct nlattr *pinfoattr;
10084 void *hdr;
10085
Johannes Berg947add32013-02-22 22:05:20 +010010086 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10087
Thomas Graf58050fc2012-06-28 03:57:45 +000010088 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010089 if (!msg)
10090 return;
10091
10092 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10093 if (!hdr) {
10094 nlmsg_free(msg);
10095 return;
10096 }
10097
David S. Miller9360ffd2012-03-29 04:41:26 -040010098 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010099 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010100 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010101
10102 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10103 if (!pinfoattr)
10104 goto nla_put_failure;
10105
David S. Miller9360ffd2012-03-29 04:41:26 -040010106 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10107 rssi_event))
10108 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010109
10110 nla_nest_end(msg, pinfoattr);
10111
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010112 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010113
10114 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10115 nl80211_mlme_mcgrp.id, gfp);
10116 return;
10117
10118 nla_put_failure:
10119 genlmsg_cancel(msg, hdr);
10120 nlmsg_free(msg);
10121}
Johannes Berg947add32013-02-22 22:05:20 +010010122EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010123
Johannes Berg947add32013-02-22 22:05:20 +010010124static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10125 struct net_device *netdev, const u8 *bssid,
10126 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010127{
10128 struct sk_buff *msg;
10129 struct nlattr *rekey_attr;
10130 void *hdr;
10131
Thomas Graf58050fc2012-06-28 03:57:45 +000010132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010133 if (!msg)
10134 return;
10135
10136 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10137 if (!hdr) {
10138 nlmsg_free(msg);
10139 return;
10140 }
10141
David S. Miller9360ffd2012-03-29 04:41:26 -040010142 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10143 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10144 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10145 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010146
10147 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10148 if (!rekey_attr)
10149 goto nla_put_failure;
10150
David S. Miller9360ffd2012-03-29 04:41:26 -040010151 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10152 NL80211_REPLAY_CTR_LEN, replay_ctr))
10153 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010154
10155 nla_nest_end(msg, rekey_attr);
10156
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010157 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010158
10159 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10160 nl80211_mlme_mcgrp.id, gfp);
10161 return;
10162
10163 nla_put_failure:
10164 genlmsg_cancel(msg, hdr);
10165 nlmsg_free(msg);
10166}
10167
Johannes Berg947add32013-02-22 22:05:20 +010010168void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10169 const u8 *replay_ctr, gfp_t gfp)
10170{
10171 struct wireless_dev *wdev = dev->ieee80211_ptr;
10172 struct wiphy *wiphy = wdev->wiphy;
10173 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10174
10175 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10176 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10177}
10178EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10179
10180static void
10181nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10182 struct net_device *netdev, int index,
10183 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010184{
10185 struct sk_buff *msg;
10186 struct nlattr *attr;
10187 void *hdr;
10188
Thomas Graf58050fc2012-06-28 03:57:45 +000010189 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010190 if (!msg)
10191 return;
10192
10193 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10194 if (!hdr) {
10195 nlmsg_free(msg);
10196 return;
10197 }
10198
David S. Miller9360ffd2012-03-29 04:41:26 -040010199 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10200 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10201 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010202
10203 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10204 if (!attr)
10205 goto nla_put_failure;
10206
David S. Miller9360ffd2012-03-29 04:41:26 -040010207 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10208 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10209 (preauth &&
10210 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10211 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010212
10213 nla_nest_end(msg, attr);
10214
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010215 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010216
10217 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10218 nl80211_mlme_mcgrp.id, gfp);
10219 return;
10220
10221 nla_put_failure:
10222 genlmsg_cancel(msg, hdr);
10223 nlmsg_free(msg);
10224}
10225
Johannes Berg947add32013-02-22 22:05:20 +010010226void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10227 const u8 *bssid, bool preauth, gfp_t gfp)
10228{
10229 struct wireless_dev *wdev = dev->ieee80211_ptr;
10230 struct wiphy *wiphy = wdev->wiphy;
10231 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10232
10233 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10234 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10235}
10236EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10237
10238static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10239 struct net_device *netdev,
10240 struct cfg80211_chan_def *chandef,
10241 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010242{
10243 struct sk_buff *msg;
10244 void *hdr;
10245
Thomas Graf58050fc2012-06-28 03:57:45 +000010246 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010247 if (!msg)
10248 return;
10249
10250 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10251 if (!hdr) {
10252 nlmsg_free(msg);
10253 return;
10254 }
10255
Johannes Berg683b6d32012-11-08 21:25:48 +010010256 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10257 goto nla_put_failure;
10258
10259 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010260 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010261
10262 genlmsg_end(msg, hdr);
10263
10264 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10265 nl80211_mlme_mcgrp.id, gfp);
10266 return;
10267
10268 nla_put_failure:
10269 genlmsg_cancel(msg, hdr);
10270 nlmsg_free(msg);
10271}
10272
Johannes Berg947add32013-02-22 22:05:20 +010010273void cfg80211_ch_switch_notify(struct net_device *dev,
10274 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010275{
Johannes Berg947add32013-02-22 22:05:20 +010010276 struct wireless_dev *wdev = dev->ieee80211_ptr;
10277 struct wiphy *wiphy = wdev->wiphy;
10278 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10279
10280 trace_cfg80211_ch_switch_notify(dev, chandef);
10281
10282 wdev_lock(wdev);
10283
10284 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10285 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10286 goto out;
10287
10288 wdev->channel = chandef->chan;
10289 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10290out:
10291 wdev_unlock(wdev);
10292 return;
10293}
10294EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10295
10296void cfg80211_cqm_txe_notify(struct net_device *dev,
10297 const u8 *peer, u32 num_packets,
10298 u32 rate, u32 intvl, gfp_t gfp)
10299{
10300 struct wireless_dev *wdev = dev->ieee80211_ptr;
10301 struct wiphy *wiphy = wdev->wiphy;
10302 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010303 struct sk_buff *msg;
10304 struct nlattr *pinfoattr;
10305 void *hdr;
10306
10307 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10308 if (!msg)
10309 return;
10310
10311 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10312 if (!hdr) {
10313 nlmsg_free(msg);
10314 return;
10315 }
10316
10317 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010318 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010319 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10320 goto nla_put_failure;
10321
10322 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10323 if (!pinfoattr)
10324 goto nla_put_failure;
10325
10326 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10327 goto nla_put_failure;
10328
10329 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10330 goto nla_put_failure;
10331
10332 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10333 goto nla_put_failure;
10334
10335 nla_nest_end(msg, pinfoattr);
10336
10337 genlmsg_end(msg, hdr);
10338
10339 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10340 nl80211_mlme_mcgrp.id, gfp);
10341 return;
10342
10343 nla_put_failure:
10344 genlmsg_cancel(msg, hdr);
10345 nlmsg_free(msg);
10346}
Johannes Berg947add32013-02-22 22:05:20 +010010347EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010348
10349void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010350nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10351 struct cfg80211_chan_def *chandef,
10352 enum nl80211_radar_event event,
10353 struct net_device *netdev, gfp_t gfp)
10354{
10355 struct sk_buff *msg;
10356 void *hdr;
10357
10358 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10359 if (!msg)
10360 return;
10361
10362 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10363 if (!hdr) {
10364 nlmsg_free(msg);
10365 return;
10366 }
10367
10368 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10369 goto nla_put_failure;
10370
10371 /* NOP and radar events don't need a netdev parameter */
10372 if (netdev) {
10373 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10374
10375 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10376 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10377 goto nla_put_failure;
10378 }
10379
10380 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10381 goto nla_put_failure;
10382
10383 if (nl80211_send_chandef(msg, chandef))
10384 goto nla_put_failure;
10385
10386 if (genlmsg_end(msg, hdr) < 0) {
10387 nlmsg_free(msg);
10388 return;
10389 }
10390
10391 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10392 nl80211_mlme_mcgrp.id, gfp);
10393 return;
10394
10395 nla_put_failure:
10396 genlmsg_cancel(msg, hdr);
10397 nlmsg_free(msg);
10398}
10399
Johannes Berg947add32013-02-22 22:05:20 +010010400void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10401 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010402{
Johannes Berg947add32013-02-22 22:05:20 +010010403 struct wireless_dev *wdev = dev->ieee80211_ptr;
10404 struct wiphy *wiphy = wdev->wiphy;
10405 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010406 struct sk_buff *msg;
10407 struct nlattr *pinfoattr;
10408 void *hdr;
10409
Johannes Berg947add32013-02-22 22:05:20 +010010410 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10411
Thomas Graf58050fc2012-06-28 03:57:45 +000010412 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010413 if (!msg)
10414 return;
10415
10416 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10417 if (!hdr) {
10418 nlmsg_free(msg);
10419 return;
10420 }
10421
David S. Miller9360ffd2012-03-29 04:41:26 -040010422 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010423 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010424 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10425 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010426
10427 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10428 if (!pinfoattr)
10429 goto nla_put_failure;
10430
David S. Miller9360ffd2012-03-29 04:41:26 -040010431 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10432 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010433
10434 nla_nest_end(msg, pinfoattr);
10435
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010436 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010437
10438 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10439 nl80211_mlme_mcgrp.id, gfp);
10440 return;
10441
10442 nla_put_failure:
10443 genlmsg_cancel(msg, hdr);
10444 nlmsg_free(msg);
10445}
Johannes Berg947add32013-02-22 22:05:20 +010010446EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010447
Johannes Berg7f6cf312011-11-04 11:18:15 +010010448void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10449 u64 cookie, bool acked, gfp_t gfp)
10450{
10451 struct wireless_dev *wdev = dev->ieee80211_ptr;
10452 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10453 struct sk_buff *msg;
10454 void *hdr;
10455 int err;
10456
Beni Lev4ee3e062012-08-27 12:49:39 +030010457 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10458
Thomas Graf58050fc2012-06-28 03:57:45 +000010459 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010460
Johannes Berg7f6cf312011-11-04 11:18:15 +010010461 if (!msg)
10462 return;
10463
10464 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10465 if (!hdr) {
10466 nlmsg_free(msg);
10467 return;
10468 }
10469
David S. Miller9360ffd2012-03-29 04:41:26 -040010470 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10471 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10472 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10473 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10474 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10475 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010476
10477 err = genlmsg_end(msg, hdr);
10478 if (err < 0) {
10479 nlmsg_free(msg);
10480 return;
10481 }
10482
10483 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10484 nl80211_mlme_mcgrp.id, gfp);
10485 return;
10486
10487 nla_put_failure:
10488 genlmsg_cancel(msg, hdr);
10489 nlmsg_free(msg);
10490}
10491EXPORT_SYMBOL(cfg80211_probe_status);
10492
Johannes Berg5e7602302011-11-04 11:18:17 +010010493void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10494 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010495 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010010496{
10497 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10498 struct sk_buff *msg;
10499 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010500 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010010501
Beni Lev4ee3e062012-08-27 12:49:39 +030010502 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10503
Ben Greear37c73b52012-10-26 14:49:25 -070010504 spin_lock_bh(&rdev->beacon_registrations_lock);
10505 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10506 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10507 if (!msg) {
10508 spin_unlock_bh(&rdev->beacon_registrations_lock);
10509 return;
10510 }
Johannes Berg5e7602302011-11-04 11:18:17 +010010511
Ben Greear37c73b52012-10-26 14:49:25 -070010512 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10513 if (!hdr)
10514 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010010515
Ben Greear37c73b52012-10-26 14:49:25 -070010516 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10517 (freq &&
10518 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10519 (sig_dbm &&
10520 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10521 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10522 goto nla_put_failure;
10523
10524 genlmsg_end(msg, hdr);
10525
10526 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010010527 }
Ben Greear37c73b52012-10-26 14:49:25 -070010528 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010529 return;
10530
10531 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010532 spin_unlock_bh(&rdev->beacon_registrations_lock);
10533 if (hdr)
10534 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010010535 nlmsg_free(msg);
10536}
10537EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10538
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010539#ifdef CONFIG_PM
10540void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10541 struct cfg80211_wowlan_wakeup *wakeup,
10542 gfp_t gfp)
10543{
10544 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10545 struct sk_buff *msg;
10546 void *hdr;
10547 int err, size = 200;
10548
10549 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10550
10551 if (wakeup)
10552 size += wakeup->packet_present_len;
10553
10554 msg = nlmsg_new(size, gfp);
10555 if (!msg)
10556 return;
10557
10558 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10559 if (!hdr)
10560 goto free_msg;
10561
10562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10563 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10564 goto free_msg;
10565
10566 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10567 wdev->netdev->ifindex))
10568 goto free_msg;
10569
10570 if (wakeup) {
10571 struct nlattr *reasons;
10572
10573 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10574
10575 if (wakeup->disconnect &&
10576 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10577 goto free_msg;
10578 if (wakeup->magic_pkt &&
10579 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10580 goto free_msg;
10581 if (wakeup->gtk_rekey_failure &&
10582 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10583 goto free_msg;
10584 if (wakeup->eap_identity_req &&
10585 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10586 goto free_msg;
10587 if (wakeup->four_way_handshake &&
10588 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10589 goto free_msg;
10590 if (wakeup->rfkill_release &&
10591 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10592 goto free_msg;
10593
10594 if (wakeup->pattern_idx >= 0 &&
10595 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10596 wakeup->pattern_idx))
10597 goto free_msg;
10598
Johannes Berg2a0e0472013-01-23 22:57:40 +010010599 if (wakeup->tcp_match)
10600 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10601
10602 if (wakeup->tcp_connlost)
10603 nla_put_flag(msg,
10604 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10605
10606 if (wakeup->tcp_nomoretokens)
10607 nla_put_flag(msg,
10608 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10609
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010610 if (wakeup->packet) {
10611 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10612 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10613
10614 if (!wakeup->packet_80211) {
10615 pkt_attr =
10616 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10617 len_attr =
10618 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10619 }
10620
10621 if (wakeup->packet_len &&
10622 nla_put_u32(msg, len_attr, wakeup->packet_len))
10623 goto free_msg;
10624
10625 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10626 wakeup->packet))
10627 goto free_msg;
10628 }
10629
10630 nla_nest_end(msg, reasons);
10631 }
10632
10633 err = genlmsg_end(msg, hdr);
10634 if (err < 0)
10635 goto free_msg;
10636
10637 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10638 nl80211_mlme_mcgrp.id, gfp);
10639 return;
10640
10641 free_msg:
10642 nlmsg_free(msg);
10643}
10644EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10645#endif
10646
Jouni Malinen3475b092012-11-16 22:49:57 +020010647void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10648 enum nl80211_tdls_operation oper,
10649 u16 reason_code, gfp_t gfp)
10650{
10651 struct wireless_dev *wdev = dev->ieee80211_ptr;
10652 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10653 struct sk_buff *msg;
10654 void *hdr;
10655 int err;
10656
10657 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10658 reason_code);
10659
10660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10661 if (!msg)
10662 return;
10663
10664 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10665 if (!hdr) {
10666 nlmsg_free(msg);
10667 return;
10668 }
10669
10670 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10671 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10672 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10673 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10674 (reason_code > 0 &&
10675 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10676 goto nla_put_failure;
10677
10678 err = genlmsg_end(msg, hdr);
10679 if (err < 0) {
10680 nlmsg_free(msg);
10681 return;
10682 }
10683
10684 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10685 nl80211_mlme_mcgrp.id, gfp);
10686 return;
10687
10688 nla_put_failure:
10689 genlmsg_cancel(msg, hdr);
10690 nlmsg_free(msg);
10691}
10692EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10693
Jouni Malinen026331c2010-02-15 12:53:10 +020010694static int nl80211_netlink_notify(struct notifier_block * nb,
10695 unsigned long state,
10696 void *_notify)
10697{
10698 struct netlink_notify *notify = _notify;
10699 struct cfg80211_registered_device *rdev;
10700 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010701 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010702
10703 if (state != NETLINK_URELEASE)
10704 return NOTIFY_DONE;
10705
10706 rcu_read_lock();
10707
Johannes Berg5e7602302011-11-04 11:18:17 +010010708 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010709 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010710 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010711
10712 spin_lock_bh(&rdev->beacon_registrations_lock);
10713 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10714 list) {
10715 if (reg->nlportid == notify->portid) {
10716 list_del(&reg->list);
10717 kfree(reg);
10718 break;
10719 }
10720 }
10721 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010010722 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010723
10724 rcu_read_unlock();
10725
10726 return NOTIFY_DONE;
10727}
10728
10729static struct notifier_block nl80211_netlink_notifier = {
10730 .notifier_call = nl80211_netlink_notify,
10731};
10732
Jouni Malinen355199e2013-02-27 17:14:27 +020010733void cfg80211_ft_event(struct net_device *netdev,
10734 struct cfg80211_ft_event_params *ft_event)
10735{
10736 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10737 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10738 struct sk_buff *msg;
10739 void *hdr;
10740 int err;
10741
10742 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10743
10744 if (!ft_event->target_ap)
10745 return;
10746
10747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10748 if (!msg)
10749 return;
10750
10751 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10752 if (!hdr) {
10753 nlmsg_free(msg);
10754 return;
10755 }
10756
10757 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10758 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10759 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10760 if (ft_event->ies)
10761 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10762 if (ft_event->ric_ies)
10763 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10764 ft_event->ric_ies);
10765
10766 err = genlmsg_end(msg, hdr);
10767 if (err < 0) {
10768 nlmsg_free(msg);
10769 return;
10770 }
10771
10772 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10773 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10774}
10775EXPORT_SYMBOL(cfg80211_ft_event);
10776
Arend van Spriel5de17982013-04-18 15:49:00 +020010777void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10778{
10779 struct cfg80211_registered_device *rdev;
10780 struct sk_buff *msg;
10781 void *hdr;
10782 u32 nlportid;
10783
10784 rdev = wiphy_to_dev(wdev->wiphy);
10785 if (!rdev->crit_proto_nlportid)
10786 return;
10787
10788 nlportid = rdev->crit_proto_nlportid;
10789 rdev->crit_proto_nlportid = 0;
10790
10791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10792 if (!msg)
10793 return;
10794
10795 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10796 if (!hdr)
10797 goto nla_put_failure;
10798
10799 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10800 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10801 goto nla_put_failure;
10802
10803 genlmsg_end(msg, hdr);
10804
10805 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10806 return;
10807
10808 nla_put_failure:
10809 if (hdr)
10810 genlmsg_cancel(msg, hdr);
10811 nlmsg_free(msg);
10812
10813}
10814EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10815
Johannes Berg55682962007-09-20 13:09:35 -040010816/* initialisation/exit functions */
10817
10818int nl80211_init(void)
10819{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010820 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010821
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010822 err = genl_register_family_with_ops(&nl80211_fam,
10823 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010824 if (err)
10825 return err;
10826
Johannes Berg55682962007-09-20 13:09:35 -040010827 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10828 if (err)
10829 goto err_out;
10830
Johannes Berg2a519312009-02-10 21:25:55 +010010831 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10832 if (err)
10833 goto err_out;
10834
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010835 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10836 if (err)
10837 goto err_out;
10838
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010839 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10840 if (err)
10841 goto err_out;
10842
Johannes Bergaff89a92009-07-01 21:26:51 +020010843#ifdef CONFIG_NL80211_TESTMODE
10844 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10845 if (err)
10846 goto err_out;
10847#endif
10848
Jouni Malinen026331c2010-02-15 12:53:10 +020010849 err = netlink_register_notifier(&nl80211_netlink_notifier);
10850 if (err)
10851 goto err_out;
10852
Johannes Berg55682962007-09-20 13:09:35 -040010853 return 0;
10854 err_out:
10855 genl_unregister_family(&nl80211_fam);
10856 return err;
10857}
10858
10859void nl80211_exit(void)
10860{
Jouni Malinen026331c2010-02-15 12:53:10 +020010861 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010862 genl_unregister_family(&nl80211_fam);
10863}